@@ -20,80 +20,80 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_WpRocket_Adapter { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Create a {@link Wordlift_WpRocket_Adapter} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.19.4 |
|
| 27 | - */ |
|
| 28 | - public function __construct() { |
|
| 23 | + /** |
|
| 24 | + * Create a {@link Wordlift_WpRocket_Adapter} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.19.4 |
|
| 27 | + */ |
|
| 28 | + public function __construct() { |
|
| 29 | 29 | |
| 30 | - add_filter( 'rocket_exclude_js', array( $this, 'exclude_js' ) ); |
|
| 31 | - add_filter( 'rocket_excluded_inline_js_content', array( $this, 'excluded_inline_js_content' ) ); |
|
| 30 | + add_filter( 'rocket_exclude_js', array( $this, 'exclude_js' ) ); |
|
| 31 | + add_filter( 'rocket_excluded_inline_js_content', array( $this, 'excluded_inline_js_content' ) ); |
|
| 32 | 32 | |
| 33 | - } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the absolute path for the specified URL. |
|
| 37 | - * |
|
| 38 | - * @param string $url The full URL. |
|
| 39 | - * |
|
| 40 | - * @return string The absolute path. |
|
| 41 | - */ |
|
| 42 | - private function get_absolute_path( $url ) { |
|
| 35 | + /** |
|
| 36 | + * Get the absolute path for the specified URL. |
|
| 37 | + * |
|
| 38 | + * @param string $url The full URL. |
|
| 39 | + * |
|
| 40 | + * @return string The absolute path. |
|
| 41 | + */ |
|
| 42 | + private function get_absolute_path( $url ) { |
|
| 43 | 43 | |
| 44 | - if ( 1 !== preg_match( '|https?://[^/]+(/.*)$|', $url, $matches ) ) { |
|
| 45 | - return $url; |
|
| 46 | - } |
|
| 44 | + if ( 1 !== preg_match( '|https?://[^/]+(/.*)$|', $url, $matches ) ) { |
|
| 45 | + return $url; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return $matches[1]; |
|
| 49 | - } |
|
| 48 | + return $matches[1]; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Hook to `rocket_exclude_defer_js` filter. |
|
| 53 | - * |
|
| 54 | - * @param array $excluded_js The preset excluded files. |
|
| 55 | - * |
|
| 56 | - * @return array The updated excluded files array. |
|
| 57 | - * |
|
| 58 | - * @since 3.25.0 We realized that WP Rocket has an issue with these path: while it seems to expect a path relative |
|
| 59 | - * to home_url, it is actually looking for a path relative to the root. We leave both exclusions so |
|
| 60 | - * that we'll be compatible in case WP Rocket fixes its implementation. |
|
| 61 | - * @since 3.23.0 add the Cloud js. |
|
| 62 | - * @since 3.20.0 hook to `rocket_exclude_js`. |
|
| 63 | - * @since 3.19.4 |
|
| 64 | - */ |
|
| 65 | - public function exclude_js( $excluded_js = array() ) { |
|
| 51 | + /** |
|
| 52 | + * Hook to `rocket_exclude_defer_js` filter. |
|
| 53 | + * |
|
| 54 | + * @param array $excluded_js The preset excluded files. |
|
| 55 | + * |
|
| 56 | + * @return array The updated excluded files array. |
|
| 57 | + * |
|
| 58 | + * @since 3.25.0 We realized that WP Rocket has an issue with these path: while it seems to expect a path relative |
|
| 59 | + * to home_url, it is actually looking for a path relative to the root. We leave both exclusions so |
|
| 60 | + * that we'll be compatible in case WP Rocket fixes its implementation. |
|
| 61 | + * @since 3.23.0 add the Cloud js. |
|
| 62 | + * @since 3.20.0 hook to `rocket_exclude_js`. |
|
| 63 | + * @since 3.19.4 |
|
| 64 | + */ |
|
| 65 | + public function exclude_js( $excluded_js = array() ) { |
|
| 66 | 66 | |
| 67 | - // Exclude our own public JS. |
|
| 68 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_public_js_url() ); |
|
| 69 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_cloud_js_url() ); |
|
| 67 | + // Exclude our own public JS. |
|
| 68 | + $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_public_js_url() ); |
|
| 69 | + $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_cloud_js_url() ); |
|
| 70 | 70 | |
| 71 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/bundle.js' ); |
|
| 72 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/wordlift-cloud.js' ); |
|
| 71 | + $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/bundle.js' ); |
|
| 72 | + $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/wordlift-cloud.js' ); |
|
| 73 | 73 | |
| 74 | - return $excluded_js; |
|
| 75 | - } |
|
| 74 | + return $excluded_js; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Filters inline JS excluded from being combined |
|
| 79 | - * |
|
| 80 | - * @link https://github.com/insideout10/wordlift-plugin/issues/868 |
|
| 81 | - * |
|
| 82 | - * @since 3.20.0 |
|
| 83 | - * |
|
| 84 | - * @param array $pattern Patterns to match. |
|
| 85 | - * |
|
| 86 | - * @return array Patterns to match. |
|
| 87 | - */ |
|
| 88 | - public function excluded_inline_js_content( $pattern = array() ) { |
|
| 77 | + /** |
|
| 78 | + * Filters inline JS excluded from being combined |
|
| 79 | + * |
|
| 80 | + * @link https://github.com/insideout10/wordlift-plugin/issues/868 |
|
| 81 | + * |
|
| 82 | + * @since 3.20.0 |
|
| 83 | + * |
|
| 84 | + * @param array $pattern Patterns to match. |
|
| 85 | + * |
|
| 86 | + * @return array Patterns to match. |
|
| 87 | + */ |
|
| 88 | + public function excluded_inline_js_content( $pattern = array() ) { |
|
| 89 | 89 | |
| 90 | - $pattern[] = 'wlSettings'; |
|
| 91 | - $pattern[] = 'wlNavigators'; |
|
| 92 | - $pattern[] = '_wlCloudSettings'; |
|
| 93 | - $pattern[] = 'wlProductsNavigators'; |
|
| 94 | - $pattern[] = 'wlFaceteds'; |
|
| 90 | + $pattern[] = 'wlSettings'; |
|
| 91 | + $pattern[] = 'wlNavigators'; |
|
| 92 | + $pattern[] = '_wlCloudSettings'; |
|
| 93 | + $pattern[] = 'wlProductsNavigators'; |
|
| 94 | + $pattern[] = 'wlFaceteds'; |
|
| 95 | 95 | |
| 96 | - return $pattern; |
|
| 97 | - } |
|
| 96 | + return $pattern; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | 99 | } |
@@ -27,8 +27,8 @@ discard block |
||
| 27 | 27 | */ |
| 28 | 28 | public function __construct() { |
| 29 | 29 | |
| 30 | - add_filter( 'rocket_exclude_js', array( $this, 'exclude_js' ) ); |
|
| 31 | - add_filter( 'rocket_excluded_inline_js_content', array( $this, 'excluded_inline_js_content' ) ); |
|
| 30 | + add_filter('rocket_exclude_js', array($this, 'exclude_js')); |
|
| 31 | + add_filter('rocket_excluded_inline_js_content', array($this, 'excluded_inline_js_content')); |
|
| 32 | 32 | |
| 33 | 33 | } |
| 34 | 34 | |
@@ -39,9 +39,9 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @return string The absolute path. |
| 41 | 41 | */ |
| 42 | - private function get_absolute_path( $url ) { |
|
| 42 | + private function get_absolute_path($url) { |
|
| 43 | 43 | |
| 44 | - if ( 1 !== preg_match( '|https?://[^/]+(/.*)$|', $url, $matches ) ) { |
|
| 44 | + if (1 !== preg_match('|https?://[^/]+(/.*)$|', $url, $matches)) { |
|
| 45 | 45 | return $url; |
| 46 | 46 | } |
| 47 | 47 | |
@@ -62,14 +62,14 @@ discard block |
||
| 62 | 62 | * @since 3.20.0 hook to `rocket_exclude_js`. |
| 63 | 63 | * @since 3.19.4 |
| 64 | 64 | */ |
| 65 | - public function exclude_js( $excluded_js = array() ) { |
|
| 65 | + public function exclude_js($excluded_js = array()) { |
|
| 66 | 66 | |
| 67 | 67 | // Exclude our own public JS. |
| 68 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_public_js_url() ); |
|
| 69 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_cloud_js_url() ); |
|
| 68 | + $excluded_js[] = $this->get_absolute_path(Wordlift_Public::get_public_js_url()); |
|
| 69 | + $excluded_js[] = $this->get_absolute_path(Wordlift_Public::get_cloud_js_url()); |
|
| 70 | 70 | |
| 71 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/bundle.js' ); |
|
| 72 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/wordlift-cloud.js' ); |
|
| 71 | + $excluded_js[] = str_replace(home_url(), '', plugin_dir_url(__DIR__).'/js/dist/bundle.js'); |
|
| 72 | + $excluded_js[] = str_replace(home_url(), '', plugin_dir_url(__DIR__).'/js/dist/wordlift-cloud.js'); |
|
| 73 | 73 | |
| 74 | 74 | return $excluded_js; |
| 75 | 75 | } |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return array Patterns to match. |
| 87 | 87 | */ |
| 88 | - public function excluded_inline_js_content( $pattern = array() ) { |
|
| 88 | + public function excluded_inline_js_content($pattern = array()) { |
|
| 89 | 89 | |
| 90 | 90 | $pattern[] = 'wlSettings'; |
| 91 | 91 | $pattern[] = 'wlNavigators'; |
@@ -17,124 +17,124 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Faceted_Search_Shortcode extends Wordlift_Shortcode { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - const SHORTCODE = 'wl_faceted_search'; |
|
| 24 | - |
|
| 25 | - public function __construct() { |
|
| 26 | - parent::__construct(); |
|
| 27 | - $this->register_block_type(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - public function render( $atts ) { |
|
| 34 | - |
|
| 35 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | - : $this->web_shortcode( $atts ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - private function register_block_type() { |
|
| 40 | - |
|
| 41 | - $scope = $this; |
|
| 42 | - |
|
| 43 | - add_action( |
|
| 44 | - 'init', |
|
| 45 | - function () use ( $scope ) { |
|
| 46 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 47 | - // Gutenberg is not active. |
|
| 48 | - return; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - register_block_type( |
|
| 52 | - 'wordlift/faceted-search', |
|
| 53 | - array( |
|
| 54 | - 'editor_script' => 'wl-block-editor', |
|
| 55 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 56 | - $attr_code = ''; |
|
| 57 | - foreach ( $attributes as $key => $value ) { |
|
| 58 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 62 | - }, |
|
| 63 | - |
|
| 64 | - 'attributes' => $scope->get_block_attributes(), |
|
| 65 | - ) |
|
| 66 | - ); |
|
| 67 | - } |
|
| 68 | - ); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 73 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 74 | - * |
|
| 75 | - * @param array $atts Shortcode attributes. |
|
| 76 | - * |
|
| 77 | - * @return array $shortcode_atts |
|
| 78 | - * @since 3.20.0 |
|
| 79 | - */ |
|
| 80 | - private function make_shortcode_atts( $atts ) { |
|
| 81 | - |
|
| 82 | - // Extract attributes and set default values. |
|
| 83 | - $shortcode_atts = shortcode_atts( |
|
| 84 | - array( |
|
| 85 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 86 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 87 | - 'post_id' => '', |
|
| 88 | - 'template_id' => '', |
|
| 89 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 90 | - 'post_types' => '', |
|
| 91 | - ), |
|
| 92 | - $atts |
|
| 93 | - ); |
|
| 94 | - |
|
| 95 | - return $shortcode_atts; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 100 | - * |
|
| 101 | - * @param array $atts Shortcode attributes. |
|
| 102 | - * |
|
| 103 | - * @return string Shortcode HTML for web |
|
| 104 | - * @since 3.20.0 |
|
| 105 | - */ |
|
| 106 | - private function web_shortcode( $atts ) { |
|
| 107 | - |
|
| 108 | - // attributes extraction and boolean filtering |
|
| 109 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 110 | - |
|
| 111 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 112 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 113 | - return; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 117 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 118 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 119 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 120 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 121 | - |
|
| 122 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 123 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 124 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 125 | - $rest_url = esc_attr( $rest_url ); |
|
| 126 | - |
|
| 127 | - // avoid building the widget when no valid $rest_url |
|
| 128 | - if ( ! $rest_url ) { |
|
| 129 | - return; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 133 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 134 | - |
|
| 135 | - return wp_kses( |
|
| 136 | - sprintf( |
|
| 137 | - ' |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + const SHORTCODE = 'wl_faceted_search'; |
|
| 24 | + |
|
| 25 | + public function __construct() { |
|
| 26 | + parent::__construct(); |
|
| 27 | + $this->register_block_type(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + public function render( $atts ) { |
|
| 34 | + |
|
| 35 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | + : $this->web_shortcode( $atts ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + private function register_block_type() { |
|
| 40 | + |
|
| 41 | + $scope = $this; |
|
| 42 | + |
|
| 43 | + add_action( |
|
| 44 | + 'init', |
|
| 45 | + function () use ( $scope ) { |
|
| 46 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 47 | + // Gutenberg is not active. |
|
| 48 | + return; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + register_block_type( |
|
| 52 | + 'wordlift/faceted-search', |
|
| 53 | + array( |
|
| 54 | + 'editor_script' => 'wl-block-editor', |
|
| 55 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 56 | + $attr_code = ''; |
|
| 57 | + foreach ( $attributes as $key => $value ) { |
|
| 58 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 62 | + }, |
|
| 63 | + |
|
| 64 | + 'attributes' => $scope->get_block_attributes(), |
|
| 65 | + ) |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | + ); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 73 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 74 | + * |
|
| 75 | + * @param array $atts Shortcode attributes. |
|
| 76 | + * |
|
| 77 | + * @return array $shortcode_atts |
|
| 78 | + * @since 3.20.0 |
|
| 79 | + */ |
|
| 80 | + private function make_shortcode_atts( $atts ) { |
|
| 81 | + |
|
| 82 | + // Extract attributes and set default values. |
|
| 83 | + $shortcode_atts = shortcode_atts( |
|
| 84 | + array( |
|
| 85 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 86 | + 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 87 | + 'post_id' => '', |
|
| 88 | + 'template_id' => '', |
|
| 89 | + 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 90 | + 'post_types' => '', |
|
| 91 | + ), |
|
| 92 | + $atts |
|
| 93 | + ); |
|
| 94 | + |
|
| 95 | + return $shortcode_atts; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 100 | + * |
|
| 101 | + * @param array $atts Shortcode attributes. |
|
| 102 | + * |
|
| 103 | + * @return string Shortcode HTML for web |
|
| 104 | + * @since 3.20.0 |
|
| 105 | + */ |
|
| 106 | + private function web_shortcode( $atts ) { |
|
| 107 | + |
|
| 108 | + // attributes extraction and boolean filtering |
|
| 109 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 110 | + |
|
| 111 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 112 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 113 | + return; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 117 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 118 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 119 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 120 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 121 | + |
|
| 122 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 123 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 124 | + $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 125 | + $rest_url = esc_attr( $rest_url ); |
|
| 126 | + |
|
| 127 | + // avoid building the widget when no valid $rest_url |
|
| 128 | + if ( ! $rest_url ) { |
|
| 129 | + return; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 133 | + $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 134 | + |
|
| 135 | + return wp_kses( |
|
| 136 | + sprintf( |
|
| 137 | + ' |
|
| 138 | 138 | <div id="%s" |
| 139 | 139 | class="wl-faceted" |
| 140 | 140 | data-rest-url="%s" |
@@ -142,77 +142,77 @@ discard block |
||
| 142 | 142 | data-template-id="%s" |
| 143 | 143 | data-template-url="%s"></div> |
| 144 | 144 | ', |
| 145 | - esc_attr( $faceted_id ), |
|
| 146 | - esc_url( $rest_url ), |
|
| 147 | - esc_attr( $title ), |
|
| 148 | - esc_attr( $template_id ), |
|
| 149 | - esc_url( $template_url ) |
|
| 150 | - ), |
|
| 151 | - array( |
|
| 152 | - 'div' => array( |
|
| 153 | - 'id' => array(), |
|
| 154 | - 'class' => array(), |
|
| 155 | - 'data-rest-url' => array(), |
|
| 156 | - 'data-title' => array(), |
|
| 157 | - 'data-template-id' => array(), |
|
| 158 | - 'data-template-url' => array(), |
|
| 159 | - ), |
|
| 160 | - ) |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 167 | - * |
|
| 168 | - * @param array $atts Shortcode attributes. |
|
| 169 | - * |
|
| 170 | - * @return string Shortcode HTML for amp |
|
| 171 | - * @since 3.20.0 |
|
| 172 | - */ |
|
| 173 | - private function amp_shortcode( $atts ) { |
|
| 174 | - |
|
| 175 | - // attributes extraction and boolean filtering |
|
| 176 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 177 | - |
|
| 178 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 179 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 180 | - return; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 184 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 185 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 186 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 187 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 188 | - |
|
| 189 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 190 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 191 | - $rest_url = $post ? rest_url( |
|
| 192 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( |
|
| 193 | - array( |
|
| 194 | - 'amp' => 1, |
|
| 195 | - 'post_id' => $post->ID, |
|
| 196 | - 'limit' => $limit, |
|
| 197 | - ) |
|
| 198 | - ) |
|
| 199 | - ) : false; |
|
| 200 | - |
|
| 201 | - // avoid building the widget when no valid $rest_url |
|
| 202 | - if ( ! $rest_url ) { |
|
| 203 | - return; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 207 | - // This is a hackish way, but this works for http and https URLs |
|
| 208 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 209 | - |
|
| 210 | - if ( empty( $template_id ) ) { |
|
| 211 | - $template_id = 'template-' . $faceted_id; |
|
| 212 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( __DIR__ ) . '/css/wordlift-amp-custom.min.css', array(), WORDLIFT_VERSION ); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return <<<HTML |
|
| 145 | + esc_attr( $faceted_id ), |
|
| 146 | + esc_url( $rest_url ), |
|
| 147 | + esc_attr( $title ), |
|
| 148 | + esc_attr( $template_id ), |
|
| 149 | + esc_url( $template_url ) |
|
| 150 | + ), |
|
| 151 | + array( |
|
| 152 | + 'div' => array( |
|
| 153 | + 'id' => array(), |
|
| 154 | + 'class' => array(), |
|
| 155 | + 'data-rest-url' => array(), |
|
| 156 | + 'data-title' => array(), |
|
| 157 | + 'data-template-id' => array(), |
|
| 158 | + 'data-template-url' => array(), |
|
| 159 | + ), |
|
| 160 | + ) |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 167 | + * |
|
| 168 | + * @param array $atts Shortcode attributes. |
|
| 169 | + * |
|
| 170 | + * @return string Shortcode HTML for amp |
|
| 171 | + * @since 3.20.0 |
|
| 172 | + */ |
|
| 173 | + private function amp_shortcode( $atts ) { |
|
| 174 | + |
|
| 175 | + // attributes extraction and boolean filtering |
|
| 176 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 177 | + |
|
| 178 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 179 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 184 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 185 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 186 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 187 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 188 | + |
|
| 189 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 190 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 191 | + $rest_url = $post ? rest_url( |
|
| 192 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( |
|
| 193 | + array( |
|
| 194 | + 'amp' => 1, |
|
| 195 | + 'post_id' => $post->ID, |
|
| 196 | + 'limit' => $limit, |
|
| 197 | + ) |
|
| 198 | + ) |
|
| 199 | + ) : false; |
|
| 200 | + |
|
| 201 | + // avoid building the widget when no valid $rest_url |
|
| 202 | + if ( ! $rest_url ) { |
|
| 203 | + return; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 207 | + // This is a hackish way, but this works for http and https URLs |
|
| 208 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 209 | + |
|
| 210 | + if ( empty( $template_id ) ) { |
|
| 211 | + $template_id = 'template-' . $faceted_id; |
|
| 212 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( __DIR__ ) . '/css/wordlift-amp-custom.min.css', array(), WORDLIFT_VERSION ); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return <<<HTML |
|
| 216 | 216 | <div id="{$faceted_id}" class="wl-amp-faceted"> |
| 217 | 217 | <h2 class="wl-headline">{$title}</h2> |
| 218 | 218 | <amp-state id="referencedPosts"> |
@@ -278,66 +278,66 @@ discard block |
||
| 278 | 278 | </section> |
| 279 | 279 | </div> |
| 280 | 280 | HTML; |
| 281 | - } |
|
| 282 | - |
|
| 283 | - public function get_block_attributes() { |
|
| 284 | - return array( |
|
| 285 | - 'title' => array( |
|
| 286 | - 'type' => 'string', |
|
| 287 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 288 | - ), |
|
| 289 | - 'template_id' => array( |
|
| 290 | - 'type' => 'string', |
|
| 291 | - 'default' => '', |
|
| 292 | - ), |
|
| 293 | - 'post_id' => array( |
|
| 294 | - 'type' => 'number', |
|
| 295 | - 'default' => '', |
|
| 296 | - ), |
|
| 297 | - 'uniqid' => array( |
|
| 298 | - 'type' => 'string', |
|
| 299 | - 'default' => '', |
|
| 300 | - ), |
|
| 301 | - 'limit' => array( |
|
| 302 | - 'type' => 'number', |
|
| 303 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 304 | - ), |
|
| 305 | - 'preview' => array( |
|
| 306 | - 'type' => 'boolean', |
|
| 307 | - 'default' => false, |
|
| 308 | - ), |
|
| 309 | - 'preview_src' => array( |
|
| 310 | - 'type' => 'string', |
|
| 311 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 312 | - ), |
|
| 313 | - 'post_types' => array( |
|
| 314 | - 'type' => 'string', |
|
| 315 | - 'default' => '', |
|
| 316 | - ), |
|
| 317 | - ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * @param $post |
|
| 322 | - * @param $delimiter |
|
| 323 | - * @param $limit |
|
| 324 | - * |
|
| 325 | - * @param $post_types |
|
| 326 | - * |
|
| 327 | - * @return bool|string |
|
| 328 | - */ |
|
| 329 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 330 | - $rest_url = $post ? rest_url( |
|
| 331 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( |
|
| 332 | - array( |
|
| 333 | - 'post_id' => $post->ID, |
|
| 334 | - 'limit' => $limit, |
|
| 335 | - 'post_types' => $post_types, |
|
| 336 | - ) |
|
| 337 | - ) |
|
| 338 | - ) : false; |
|
| 339 | - |
|
| 340 | - return $rest_url; |
|
| 341 | - } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + public function get_block_attributes() { |
|
| 284 | + return array( |
|
| 285 | + 'title' => array( |
|
| 286 | + 'type' => 'string', |
|
| 287 | + 'default' => __( 'Related articles', 'wordlift' ), |
|
| 288 | + ), |
|
| 289 | + 'template_id' => array( |
|
| 290 | + 'type' => 'string', |
|
| 291 | + 'default' => '', |
|
| 292 | + ), |
|
| 293 | + 'post_id' => array( |
|
| 294 | + 'type' => 'number', |
|
| 295 | + 'default' => '', |
|
| 296 | + ), |
|
| 297 | + 'uniqid' => array( |
|
| 298 | + 'type' => 'string', |
|
| 299 | + 'default' => '', |
|
| 300 | + ), |
|
| 301 | + 'limit' => array( |
|
| 302 | + 'type' => 'number', |
|
| 303 | + 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 304 | + ), |
|
| 305 | + 'preview' => array( |
|
| 306 | + 'type' => 'boolean', |
|
| 307 | + 'default' => false, |
|
| 308 | + ), |
|
| 309 | + 'preview_src' => array( |
|
| 310 | + 'type' => 'string', |
|
| 311 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 312 | + ), |
|
| 313 | + 'post_types' => array( |
|
| 314 | + 'type' => 'string', |
|
| 315 | + 'default' => '', |
|
| 316 | + ), |
|
| 317 | + ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * @param $post |
|
| 322 | + * @param $delimiter |
|
| 323 | + * @param $limit |
|
| 324 | + * |
|
| 325 | + * @param $post_types |
|
| 326 | + * |
|
| 327 | + * @return bool|string |
|
| 328 | + */ |
|
| 329 | + public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 330 | + $rest_url = $post ? rest_url( |
|
| 331 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( |
|
| 332 | + array( |
|
| 333 | + 'post_id' => $post->ID, |
|
| 334 | + 'limit' => $limit, |
|
| 335 | + 'post_types' => $post_types, |
|
| 336 | + ) |
|
| 337 | + ) |
|
| 338 | + ) : false; |
|
| 339 | + |
|
| 340 | + return $rest_url; |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | 343 | } |
@@ -30,10 +30,10 @@ discard block |
||
| 30 | 30 | /** |
| 31 | 31 | * {@inheritdoc} |
| 32 | 32 | */ |
| 33 | - public function render( $atts ) { |
|
| 33 | + public function render($atts) { |
|
| 34 | 34 | |
| 35 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | - : $this->web_shortcode( $atts ); |
|
| 35 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 36 | + : $this->web_shortcode($atts); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | private function register_block_type() { |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | add_action( |
| 44 | 44 | 'init', |
| 45 | - function () use ( $scope ) { |
|
| 46 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | + function() use ($scope) { |
|
| 46 | + if ( ! function_exists('register_block_type')) { |
|
| 47 | 47 | // Gutenberg is not active. |
| 48 | 48 | return; |
| 49 | 49 | } |
@@ -52,13 +52,13 @@ discard block |
||
| 52 | 52 | 'wordlift/faceted-search', |
| 53 | 53 | array( |
| 54 | 54 | 'editor_script' => 'wl-block-editor', |
| 55 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 55 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 56 | 56 | $attr_code = ''; |
| 57 | - foreach ( $attributes as $key => $value ) { |
|
| 58 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 57 | + foreach ($attributes as $key => $value) { |
|
| 58 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 61 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 62 | 62 | }, |
| 63 | 63 | |
| 64 | 64 | 'attributes' => $scope->get_block_attributes(), |
@@ -77,16 +77,16 @@ discard block |
||
| 77 | 77 | * @return array $shortcode_atts |
| 78 | 78 | * @since 3.20.0 |
| 79 | 79 | */ |
| 80 | - private function make_shortcode_atts( $atts ) { |
|
| 80 | + private function make_shortcode_atts($atts) { |
|
| 81 | 81 | |
| 82 | 82 | // Extract attributes and set default values. |
| 83 | 83 | $shortcode_atts = shortcode_atts( |
| 84 | 84 | array( |
| 85 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 86 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 85 | + 'title' => __('Related articles', 'wordlift'), |
|
| 86 | + 'limit' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 87 | 87 | 'post_id' => '', |
| 88 | 88 | 'template_id' => '', |
| 89 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 89 | + 'uniqid' => uniqid('wl-faceted-widget-'), |
|
| 90 | 90 | 'post_types' => '', |
| 91 | 91 | ), |
| 92 | 92 | $atts |
@@ -103,34 +103,34 @@ discard block |
||
| 103 | 103 | * @return string Shortcode HTML for web |
| 104 | 104 | * @since 3.20.0 |
| 105 | 105 | */ |
| 106 | - private function web_shortcode( $atts ) { |
|
| 106 | + private function web_shortcode($atts) { |
|
| 107 | 107 | |
| 108 | 108 | // attributes extraction and boolean filtering |
| 109 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 109 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 110 | 110 | |
| 111 | 111 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 112 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 112 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 113 | 113 | return; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 117 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 118 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 119 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 120 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 116 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 117 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 118 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 119 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 120 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 121 | 121 | |
| 122 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 123 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 124 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 125 | - $rest_url = esc_attr( $rest_url ); |
|
| 122 | + $permalink_structure = get_option('permalink_structure'); |
|
| 123 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 124 | + $rest_url = $this->get_rest_url($post, $delimiter, $limit, $shortcode_atts['post_types']); |
|
| 125 | + $rest_url = esc_attr($rest_url); |
|
| 126 | 126 | |
| 127 | 127 | // avoid building the widget when no valid $rest_url |
| 128 | - if ( ! $rest_url ) { |
|
| 128 | + if ( ! $rest_url) { |
|
| 129 | 129 | return; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 133 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 132 | + wp_enqueue_script('wordlift-cloud'); |
|
| 133 | + $template_url = get_rest_url(null, '/wordlift/v1/faceted-search/template'); |
|
| 134 | 134 | |
| 135 | 135 | return wp_kses( |
| 136 | 136 | sprintf( |
@@ -142,11 +142,11 @@ discard block |
||
| 142 | 142 | data-template-id="%s" |
| 143 | 143 | data-template-url="%s"></div> |
| 144 | 144 | ', |
| 145 | - esc_attr( $faceted_id ), |
|
| 146 | - esc_url( $rest_url ), |
|
| 147 | - esc_attr( $title ), |
|
| 148 | - esc_attr( $template_id ), |
|
| 149 | - esc_url( $template_url ) |
|
| 145 | + esc_attr($faceted_id), |
|
| 146 | + esc_url($rest_url), |
|
| 147 | + esc_attr($title), |
|
| 148 | + esc_attr($template_id), |
|
| 149 | + esc_url($template_url) |
|
| 150 | 150 | ), |
| 151 | 151 | array( |
| 152 | 152 | 'div' => array( |
@@ -170,26 +170,26 @@ discard block |
||
| 170 | 170 | * @return string Shortcode HTML for amp |
| 171 | 171 | * @since 3.20.0 |
| 172 | 172 | */ |
| 173 | - private function amp_shortcode( $atts ) { |
|
| 173 | + private function amp_shortcode($atts) { |
|
| 174 | 174 | |
| 175 | 175 | // attributes extraction and boolean filtering |
| 176 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 176 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 177 | 177 | |
| 178 | 178 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 179 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 179 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 180 | 180 | return; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 184 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 185 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 186 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 187 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 183 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 184 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 185 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 186 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 187 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 188 | 188 | |
| 189 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 190 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 189 | + $permalink_structure = get_option('permalink_structure'); |
|
| 190 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 191 | 191 | $rest_url = $post ? rest_url( |
| 192 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( |
|
| 192 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query( |
|
| 193 | 193 | array( |
| 194 | 194 | 'amp' => 1, |
| 195 | 195 | 'post_id' => $post->ID, |
@@ -199,17 +199,17 @@ discard block |
||
| 199 | 199 | ) : false; |
| 200 | 200 | |
| 201 | 201 | // avoid building the widget when no valid $rest_url |
| 202 | - if ( ! $rest_url ) { |
|
| 202 | + if ( ! $rest_url) { |
|
| 203 | 203 | return; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 207 | 207 | // This is a hackish way, but this works for http and https URLs |
| 208 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 208 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 209 | 209 | |
| 210 | - if ( empty( $template_id ) ) { |
|
| 211 | - $template_id = 'template-' . $faceted_id; |
|
| 212 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( __DIR__ ) . '/css/wordlift-amp-custom.min.css', array(), WORDLIFT_VERSION ); |
|
| 210 | + if (empty($template_id)) { |
|
| 211 | + $template_id = 'template-'.$faceted_id; |
|
| 212 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(__DIR__).'/css/wordlift-amp-custom.min.css', array(), WORDLIFT_VERSION); |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | return <<<HTML |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | return array( |
| 285 | 285 | 'title' => array( |
| 286 | 286 | 'type' => 'string', |
| 287 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 287 | + 'default' => __('Related articles', 'wordlift'), |
|
| 288 | 288 | ), |
| 289 | 289 | 'template_id' => array( |
| 290 | 290 | 'type' => 'string', |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | ), |
| 301 | 301 | 'limit' => array( |
| 302 | 302 | 'type' => 'number', |
| 303 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 303 | + 'default' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 304 | 304 | ), |
| 305 | 305 | 'preview' => array( |
| 306 | 306 | 'type' => 'boolean', |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | ), |
| 309 | 309 | 'preview_src' => array( |
| 310 | 310 | 'type' => 'string', |
| 311 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 311 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 312 | 312 | ), |
| 313 | 313 | 'post_types' => array( |
| 314 | 314 | 'type' => 'string', |
@@ -326,9 +326,9 @@ discard block |
||
| 326 | 326 | * |
| 327 | 327 | * @return bool|string |
| 328 | 328 | */ |
| 329 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 329 | + public function get_rest_url($post, $delimiter, $limit, $post_types) { |
|
| 330 | 330 | $rest_url = $post ? rest_url( |
| 331 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( |
|
| 331 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query( |
|
| 332 | 332 | array( |
| 333 | 333 | 'post_id' => $post->ID, |
| 334 | 334 | 'limit' => $limit, |
@@ -16,35 +16,35 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Related_Entities_Cloud_Widget extends Wordlift_Widget { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Create an {@link Wordlift_Related_Entities_Cloud_Widget} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.11.0 |
|
| 23 | - */ |
|
| 24 | - public function __construct() { |
|
| 25 | - parent::__construct( |
|
| 26 | - 'wl_related_entities_cloud', |
|
| 27 | - __( 'WordLift Entities Cloud', 'wordlift' ), |
|
| 28 | - array( |
|
| 29 | - 'classname' => 'wl_related_entities_cloud', |
|
| 30 | - 'description' => __( 'Display entities related to the current post/entity in a tag cloud.', 'wordlift' ), |
|
| 31 | - ) |
|
| 32 | - ); |
|
| 33 | - |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @inheritdoc |
|
| 38 | - */ |
|
| 39 | - public function form( $instance ) { |
|
| 40 | - $title_id = $this->get_field_id( 'title' ); |
|
| 41 | - $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
|
| 42 | - ?> |
|
| 19 | + /** |
|
| 20 | + * Create an {@link Wordlift_Related_Entities_Cloud_Widget} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.11.0 |
|
| 23 | + */ |
|
| 24 | + public function __construct() { |
|
| 25 | + parent::__construct( |
|
| 26 | + 'wl_related_entities_cloud', |
|
| 27 | + __( 'WordLift Entities Cloud', 'wordlift' ), |
|
| 28 | + array( |
|
| 29 | + 'classname' => 'wl_related_entities_cloud', |
|
| 30 | + 'description' => __( 'Display entities related to the current post/entity in a tag cloud.', 'wordlift' ), |
|
| 31 | + ) |
|
| 32 | + ); |
|
| 33 | + |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @inheritdoc |
|
| 38 | + */ |
|
| 39 | + public function form( $instance ) { |
|
| 40 | + $title_id = $this->get_field_id( 'title' ); |
|
| 41 | + $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
|
| 42 | + ?> |
|
| 43 | 43 | |
| 44 | 44 | <p><label for="<?php echo esc_attr( $title_id ); ?>"> |
| 45 | 45 | <?php |
| 46 | - esc_html_e( 'Title:', 'wordlift' ); |
|
| 47 | - ?> |
|
| 46 | + esc_html_e( 'Title:', 'wordlift' ); |
|
| 47 | + ?> |
|
| 48 | 48 | </label> |
| 49 | 49 | <input type="text" class="widefat" |
| 50 | 50 | id="<?php echo esc_attr( $title_id ); ?>" |
@@ -54,65 +54,65 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | <?php |
| 56 | 56 | |
| 57 | - return 'wl_related_entities_cloud_form'; |
|
| 58 | - } |
|
| 57 | + return 'wl_related_entities_cloud_form'; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @inheritdoc |
|
| 62 | - */ |
|
| 63 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 64 | - public function update( $new_instance, $old_instance ) { |
|
| 60 | + /** |
|
| 61 | + * @inheritdoc |
|
| 62 | + */ |
|
| 63 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 64 | + public function update( $new_instance, $old_instance ) { |
|
| 65 | 65 | |
| 66 | - return array( 'title' => sanitize_text_field( $new_instance['title'] ) ); |
|
| 67 | - } |
|
| 66 | + return array( 'title' => sanitize_text_field( $new_instance['title'] ) ); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @inheritdoc |
|
| 71 | - */ |
|
| 72 | - public function widget( $args, $instance ) { |
|
| 69 | + /** |
|
| 70 | + * @inheritdoc |
|
| 71 | + */ |
|
| 72 | + public function widget( $args, $instance ) { |
|
| 73 | 73 | |
| 74 | - /* |
|
| 74 | + /* |
|
| 75 | 75 | * Use the shortcode to calculate the HTML required to show the cloud |
| 76 | 76 | * if there is no such html do not render the widget at all. |
| 77 | 77 | */ |
| 78 | - $cloud_html = do_shortcode( '[wl_cloud]' ); |
|
| 79 | - if ( empty( $cloud_html ) ) { |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // The widget title. |
|
| 84 | - $title = empty( $instance['title'] ) ? __( 'Related Entities', 'wordlift' ) : $instance['title']; |
|
| 85 | - |
|
| 86 | - // Standard filter all widgets should apply |
|
| 87 | - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
|
| 88 | - |
|
| 89 | - echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 90 | - |
|
| 91 | - if ( $title ) { |
|
| 92 | - echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - echo wp_kses( |
|
| 96 | - $cloud_html, |
|
| 97 | - array( |
|
| 98 | - 'div' => array( 'class' => array() ), |
|
| 99 | - 'span' => array( 'class' => array() ), |
|
| 100 | - 'a' => array( |
|
| 101 | - 'href' => array(), |
|
| 102 | - 'class' => array(), |
|
| 103 | - 'style' => array(), |
|
| 104 | - 'aria-label' => array(), |
|
| 105 | - ), |
|
| 106 | - 'ul' => array( |
|
| 107 | - 'class' => array(), |
|
| 108 | - 'role' => array(), |
|
| 109 | - ), |
|
| 110 | - 'li' => array(), |
|
| 111 | - ) |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 115 | - |
|
| 116 | - } |
|
| 78 | + $cloud_html = do_shortcode( '[wl_cloud]' ); |
|
| 79 | + if ( empty( $cloud_html ) ) { |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // The widget title. |
|
| 84 | + $title = empty( $instance['title'] ) ? __( 'Related Entities', 'wordlift' ) : $instance['title']; |
|
| 85 | + |
|
| 86 | + // Standard filter all widgets should apply |
|
| 87 | + $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
|
| 88 | + |
|
| 89 | + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 90 | + |
|
| 91 | + if ( $title ) { |
|
| 92 | + echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + echo wp_kses( |
|
| 96 | + $cloud_html, |
|
| 97 | + array( |
|
| 98 | + 'div' => array( 'class' => array() ), |
|
| 99 | + 'span' => array( 'class' => array() ), |
|
| 100 | + 'a' => array( |
|
| 101 | + 'href' => array(), |
|
| 102 | + 'class' => array(), |
|
| 103 | + 'style' => array(), |
|
| 104 | + 'aria-label' => array(), |
|
| 105 | + ), |
|
| 106 | + 'ul' => array( |
|
| 107 | + 'class' => array(), |
|
| 108 | + 'role' => array(), |
|
| 109 | + ), |
|
| 110 | + 'li' => array(), |
|
| 111 | + ) |
|
| 112 | + ); |
|
| 113 | + |
|
| 114 | + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 115 | + |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | 118 | } |
@@ -24,10 +24,10 @@ discard block |
||
| 24 | 24 | public function __construct() { |
| 25 | 25 | parent::__construct( |
| 26 | 26 | 'wl_related_entities_cloud', |
| 27 | - __( 'WordLift Entities Cloud', 'wordlift' ), |
|
| 27 | + __('WordLift Entities Cloud', 'wordlift'), |
|
| 28 | 28 | array( |
| 29 | 29 | 'classname' => 'wl_related_entities_cloud', |
| 30 | - 'description' => __( 'Display entities related to the current post/entity in a tag cloud.', 'wordlift' ), |
|
| 30 | + 'description' => __('Display entities related to the current post/entity in a tag cloud.', 'wordlift'), |
|
| 31 | 31 | ) |
| 32 | 32 | ); |
| 33 | 33 | |
@@ -36,20 +36,20 @@ discard block |
||
| 36 | 36 | /** |
| 37 | 37 | * @inheritdoc |
| 38 | 38 | */ |
| 39 | - public function form( $instance ) { |
|
| 40 | - $title_id = $this->get_field_id( 'title' ); |
|
| 41 | - $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
|
| 39 | + public function form($instance) { |
|
| 40 | + $title_id = $this->get_field_id('title'); |
|
| 41 | + $instance['title'] = ! empty($instance['title']) ? esc_attr($instance['title']) : ''; |
|
| 42 | 42 | ?> |
| 43 | 43 | |
| 44 | - <p><label for="<?php echo esc_attr( $title_id ); ?>"> |
|
| 44 | + <p><label for="<?php echo esc_attr($title_id); ?>"> |
|
| 45 | 45 | <?php |
| 46 | - esc_html_e( 'Title:', 'wordlift' ); |
|
| 46 | + esc_html_e('Title:', 'wordlift'); |
|
| 47 | 47 | ?> |
| 48 | 48 | </label> |
| 49 | 49 | <input type="text" class="widefat" |
| 50 | - id="<?php echo esc_attr( $title_id ); ?>" |
|
| 51 | - name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" |
|
| 52 | - value="<?php echo esc_attr( $instance['title'] ); ?>"/> |
|
| 50 | + id="<?php echo esc_attr($title_id); ?>" |
|
| 51 | + name="<?php echo esc_attr($this->get_field_name('title')); ?>" |
|
| 52 | + value="<?php echo esc_attr($instance['title']); ?>"/> |
|
| 53 | 53 | </p> |
| 54 | 54 | |
| 55 | 55 | <?php |
@@ -61,42 +61,42 @@ discard block |
||
| 61 | 61 | * @inheritdoc |
| 62 | 62 | */ |
| 63 | 63 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 64 | - public function update( $new_instance, $old_instance ) { |
|
| 64 | + public function update($new_instance, $old_instance) { |
|
| 65 | 65 | |
| 66 | - return array( 'title' => sanitize_text_field( $new_instance['title'] ) ); |
|
| 66 | + return array('title' => sanitize_text_field($new_instance['title'])); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * @inheritdoc |
| 71 | 71 | */ |
| 72 | - public function widget( $args, $instance ) { |
|
| 72 | + public function widget($args, $instance) { |
|
| 73 | 73 | |
| 74 | 74 | /* |
| 75 | 75 | * Use the shortcode to calculate the HTML required to show the cloud |
| 76 | 76 | * if there is no such html do not render the widget at all. |
| 77 | 77 | */ |
| 78 | - $cloud_html = do_shortcode( '[wl_cloud]' ); |
|
| 79 | - if ( empty( $cloud_html ) ) { |
|
| 78 | + $cloud_html = do_shortcode('[wl_cloud]'); |
|
| 79 | + if (empty($cloud_html)) { |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | // The widget title. |
| 84 | - $title = empty( $instance['title'] ) ? __( 'Related Entities', 'wordlift' ) : $instance['title']; |
|
| 84 | + $title = empty($instance['title']) ? __('Related Entities', 'wordlift') : $instance['title']; |
|
| 85 | 85 | |
| 86 | 86 | // Standard filter all widgets should apply |
| 87 | - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
|
| 87 | + $title = apply_filters('widget_title', $title, $instance, $this->id_base); |
|
| 88 | 88 | |
| 89 | 89 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 90 | 90 | |
| 91 | - if ( $title ) { |
|
| 92 | - echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 91 | + if ($title) { |
|
| 92 | + echo $args['before_title'].esc_html($title).$args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | echo wp_kses( |
| 96 | 96 | $cloud_html, |
| 97 | 97 | array( |
| 98 | - 'div' => array( 'class' => array() ), |
|
| 99 | - 'span' => array( 'class' => array() ), |
|
| 98 | + 'div' => array('class' => array()), |
|
| 99 | + 'span' => array('class' => array()), |
|
| 100 | 100 | 'a' => array( |
| 101 | 101 | 'href' => array(), |
| 102 | 102 | 'class' => array(), |
@@ -18,267 +18,267 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Vocabulary_Shortcode extends Wordlift_Shortcode { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The shortcode. |
|
| 23 | - * |
|
| 24 | - * @since 3.17.0 |
|
| 25 | - */ |
|
| 26 | - const SHORTCODE = 'wl_vocabulary'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Log_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.17.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $log; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The vocabulary id |
|
| 39 | - * |
|
| 40 | - * @since 3.18.3 |
|
| 41 | - * @access private |
|
| 42 | - * @var int $vocabulary_id The vocabulary unique id. |
|
| 43 | - */ |
|
| 44 | - private static $vocabulary_id = 0; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.16.0 |
|
| 50 | - */ |
|
| 51 | - public function __construct() { |
|
| 52 | - parent::__construct(); |
|
| 53 | - |
|
| 54 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | - |
|
| 56 | - $this->register_block_type(); |
|
| 57 | - |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Check whether the requirements for this shortcode to work are available. |
|
| 62 | - * |
|
| 63 | - * @return bool True if the requirements are satisfied otherwise false. |
|
| 64 | - * @since 3.17.0 |
|
| 65 | - */ |
|
| 66 | - private static function are_requirements_satisfied() { |
|
| 67 | - |
|
| 68 | - return function_exists( 'mb_strlen' ) && |
|
| 69 | - function_exists( 'mb_substr' ) && |
|
| 70 | - function_exists( 'mb_strtolower' ) && |
|
| 71 | - function_exists( 'mb_strtoupper' ) && |
|
| 72 | - function_exists( 'mb_convert_case' ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Render the shortcode. |
|
| 77 | - * |
|
| 78 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 79 | - * |
|
| 80 | - * @return string The output html code. |
|
| 81 | - * @since 3.16.0 |
|
| 82 | - */ |
|
| 83 | - public function render( $atts ) { |
|
| 84 | - |
|
| 85 | - // Bail out if the requirements aren't satisfied: we need mbstring for |
|
| 86 | - // the vocabulary widget to work. |
|
| 87 | - if ( ! self::are_requirements_satisfied() ) { |
|
| 88 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 89 | - |
|
| 90 | - return ''; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION ); |
|
| 94 | - |
|
| 95 | - // Extract attributes and set default values. |
|
| 96 | - $atts = shortcode_atts( |
|
| 97 | - array( |
|
| 98 | - // The entity type, such as `person`, `organization`, ... |
|
| 99 | - 'type' => 'all', |
|
| 100 | - // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
| 101 | - 'limit' => 100, |
|
| 102 | - // Sort by title. |
|
| 103 | - 'orderby' => 'post_date', |
|
| 104 | - // Sort DESC. |
|
| 105 | - 'order' => 'DESC', |
|
| 106 | - // Allow to specify the category ID. |
|
| 107 | - 'cat' => '', |
|
| 108 | - ), |
|
| 109 | - $atts |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - // Get the posts. Note that if a `type` is specified before, then the |
|
| 113 | - // `tax_query` from the `add_criterias` call isn't added. |
|
| 114 | - $posts = $this->get_posts( $atts ); |
|
| 115 | - |
|
| 116 | - // Get the alphabet. |
|
| 117 | - $language_code = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
| 118 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 119 | - |
|
| 120 | - // Add posts to the alphabet. |
|
| 121 | - foreach ( $posts as $post ) { |
|
| 122 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $header = ''; |
|
| 126 | - $sections = ''; |
|
| 127 | - |
|
| 128 | - // Get unique id for each vocabulary shortcode. |
|
| 129 | - $vocabulary_id = self::get_and_increment_vocabulary_id(); |
|
| 130 | - |
|
| 131 | - // Generate the header. |
|
| 132 | - foreach ( $alphabet as $item => $translations ) { |
|
| 133 | - $template = ( empty( $translations ) |
|
| 134 | - ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
| 135 | - : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
| 136 | - |
|
| 137 | - $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // Generate the sections. |
|
| 141 | - foreach ( $alphabet as $item => $translations ) { |
|
| 142 | - // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
|
| 143 | - // since we had `AB` being placed before `Aa` with `asort`. |
|
| 144 | - // |
|
| 145 | - // Order the translations alphabetically. |
|
| 146 | - // asort( $translations ); |
|
| 147 | - uasort( |
|
| 148 | - $translations, |
|
| 149 | - function ( $a, $b ) { |
|
| 150 | - if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
| 151 | - || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
| 152 | - return 0; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
| 156 | - } |
|
| 157 | - ); |
|
| 158 | - $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Return HTML template. |
|
| 162 | - ob_start(); |
|
| 163 | - ?> |
|
| 21 | + /** |
|
| 22 | + * The shortcode. |
|
| 23 | + * |
|
| 24 | + * @since 3.17.0 |
|
| 25 | + */ |
|
| 26 | + const SHORTCODE = 'wl_vocabulary'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Log_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.17.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $log; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The vocabulary id |
|
| 39 | + * |
|
| 40 | + * @since 3.18.3 |
|
| 41 | + * @access private |
|
| 42 | + * @var int $vocabulary_id The vocabulary unique id. |
|
| 43 | + */ |
|
| 44 | + private static $vocabulary_id = 0; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.16.0 |
|
| 50 | + */ |
|
| 51 | + public function __construct() { |
|
| 52 | + parent::__construct(); |
|
| 53 | + |
|
| 54 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | + |
|
| 56 | + $this->register_block_type(); |
|
| 57 | + |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Check whether the requirements for this shortcode to work are available. |
|
| 62 | + * |
|
| 63 | + * @return bool True if the requirements are satisfied otherwise false. |
|
| 64 | + * @since 3.17.0 |
|
| 65 | + */ |
|
| 66 | + private static function are_requirements_satisfied() { |
|
| 67 | + |
|
| 68 | + return function_exists( 'mb_strlen' ) && |
|
| 69 | + function_exists( 'mb_substr' ) && |
|
| 70 | + function_exists( 'mb_strtolower' ) && |
|
| 71 | + function_exists( 'mb_strtoupper' ) && |
|
| 72 | + function_exists( 'mb_convert_case' ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Render the shortcode. |
|
| 77 | + * |
|
| 78 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 79 | + * |
|
| 80 | + * @return string The output html code. |
|
| 81 | + * @since 3.16.0 |
|
| 82 | + */ |
|
| 83 | + public function render( $atts ) { |
|
| 84 | + |
|
| 85 | + // Bail out if the requirements aren't satisfied: we need mbstring for |
|
| 86 | + // the vocabulary widget to work. |
|
| 87 | + if ( ! self::are_requirements_satisfied() ) { |
|
| 88 | + $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 89 | + |
|
| 90 | + return ''; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION ); |
|
| 94 | + |
|
| 95 | + // Extract attributes and set default values. |
|
| 96 | + $atts = shortcode_atts( |
|
| 97 | + array( |
|
| 98 | + // The entity type, such as `person`, `organization`, ... |
|
| 99 | + 'type' => 'all', |
|
| 100 | + // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
| 101 | + 'limit' => 100, |
|
| 102 | + // Sort by title. |
|
| 103 | + 'orderby' => 'post_date', |
|
| 104 | + // Sort DESC. |
|
| 105 | + 'order' => 'DESC', |
|
| 106 | + // Allow to specify the category ID. |
|
| 107 | + 'cat' => '', |
|
| 108 | + ), |
|
| 109 | + $atts |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + // Get the posts. Note that if a `type` is specified before, then the |
|
| 113 | + // `tax_query` from the `add_criterias` call isn't added. |
|
| 114 | + $posts = $this->get_posts( $atts ); |
|
| 115 | + |
|
| 116 | + // Get the alphabet. |
|
| 117 | + $language_code = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
| 118 | + $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 119 | + |
|
| 120 | + // Add posts to the alphabet. |
|
| 121 | + foreach ( $posts as $post ) { |
|
| 122 | + $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $header = ''; |
|
| 126 | + $sections = ''; |
|
| 127 | + |
|
| 128 | + // Get unique id for each vocabulary shortcode. |
|
| 129 | + $vocabulary_id = self::get_and_increment_vocabulary_id(); |
|
| 130 | + |
|
| 131 | + // Generate the header. |
|
| 132 | + foreach ( $alphabet as $item => $translations ) { |
|
| 133 | + $template = ( empty( $translations ) |
|
| 134 | + ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
| 135 | + : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
| 136 | + |
|
| 137 | + $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // Generate the sections. |
|
| 141 | + foreach ( $alphabet as $item => $translations ) { |
|
| 142 | + // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
|
| 143 | + // since we had `AB` being placed before `Aa` with `asort`. |
|
| 144 | + // |
|
| 145 | + // Order the translations alphabetically. |
|
| 146 | + // asort( $translations ); |
|
| 147 | + uasort( |
|
| 148 | + $translations, |
|
| 149 | + function ( $a, $b ) { |
|
| 150 | + if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
| 151 | + || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
| 152 | + return 0; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
| 156 | + } |
|
| 157 | + ); |
|
| 158 | + $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Return HTML template. |
|
| 162 | + ob_start(); |
|
| 163 | + ?> |
|
| 164 | 164 | <div class='wl-vocabulary'> |
| 165 | 165 | <nav class='wl-vocabulary-alphabet-nav'> |
| 166 | 166 | <?php |
| 167 | - echo wp_kses( |
|
| 168 | - $header, |
|
| 169 | - array( |
|
| 170 | - 'span' => array( 'class' => array() ), |
|
| 171 | - 'a' => array( 'href' => array() ), |
|
| 172 | - ) |
|
| 173 | - ); |
|
| 174 | - ?> |
|
| 167 | + echo wp_kses( |
|
| 168 | + $header, |
|
| 169 | + array( |
|
| 170 | + 'span' => array( 'class' => array() ), |
|
| 171 | + 'a' => array( 'href' => array() ), |
|
| 172 | + ) |
|
| 173 | + ); |
|
| 174 | + ?> |
|
| 175 | 175 | </nav> |
| 176 | 176 | <div class='wl-vocabulary-grid'> |
| 177 | 177 | <?php |
| 178 | - echo wp_kses( |
|
| 179 | - $sections, |
|
| 180 | - array( |
|
| 181 | - 'div' => array( |
|
| 182 | - 'class' => array(), |
|
| 183 | - 'id' => array(), |
|
| 184 | - ), |
|
| 185 | - 'aside' => array( 'class' => array() ), |
|
| 186 | - 'ul' => array( 'class' => array() ), |
|
| 187 | - 'li' => array(), |
|
| 188 | - 'a' => array( 'href' => array() ), |
|
| 189 | - ) |
|
| 190 | - ); |
|
| 191 | - ?> |
|
| 178 | + echo wp_kses( |
|
| 179 | + $sections, |
|
| 180 | + array( |
|
| 181 | + 'div' => array( |
|
| 182 | + 'class' => array(), |
|
| 183 | + 'id' => array(), |
|
| 184 | + ), |
|
| 185 | + 'aside' => array( 'class' => array() ), |
|
| 186 | + 'ul' => array( 'class' => array() ), |
|
| 187 | + 'li' => array(), |
|
| 188 | + 'a' => array( 'href' => array() ), |
|
| 189 | + ) |
|
| 190 | + ); |
|
| 191 | + ?> |
|
| 192 | 192 | </div> |
| 193 | 193 | </div> |
| 194 | 194 | <?php |
| 195 | - $html = ob_get_clean(); |
|
| 196 | - |
|
| 197 | - return $html; |
|
| 198 | - |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - private function register_block_type() { |
|
| 202 | - |
|
| 203 | - $scope = $this; |
|
| 204 | - |
|
| 205 | - add_action( |
|
| 206 | - 'init', |
|
| 207 | - function () use ( $scope ) { |
|
| 208 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 209 | - // Gutenberg is not active. |
|
| 210 | - return; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - register_block_type( |
|
| 214 | - 'wordlift/vocabulary', |
|
| 215 | - array( |
|
| 216 | - 'editor_script' => 'wl-block-editor', |
|
| 217 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 218 | - $attr_code = ''; |
|
| 219 | - foreach ( $attributes as $key => $value ) { |
|
| 220 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 224 | - }, |
|
| 225 | - 'attributes' => array( |
|
| 226 | - 'type' => array( |
|
| 227 | - 'type' => 'string', |
|
| 228 | - 'default' => 'all', |
|
| 229 | - ), |
|
| 230 | - 'limit' => array( |
|
| 231 | - 'type' => 'number', |
|
| 232 | - 'default' => 100, |
|
| 233 | - ), |
|
| 234 | - 'orderby' => array( |
|
| 235 | - 'type' => 'string', |
|
| 236 | - 'default' => 'post_date', |
|
| 237 | - ), |
|
| 238 | - 'order' => array( |
|
| 239 | - 'type' => 'string', |
|
| 240 | - 'default' => 'DESC', |
|
| 241 | - ), |
|
| 242 | - 'cat' => array( |
|
| 243 | - 'type' => 'string', |
|
| 244 | - 'default' => '', |
|
| 245 | - ), |
|
| 246 | - 'preview' => array( |
|
| 247 | - 'type' => 'boolean', |
|
| 248 | - 'default' => false, |
|
| 249 | - ), |
|
| 250 | - 'preview_src' => array( |
|
| 251 | - 'type' => 'string', |
|
| 252 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/vocabulary.png', |
|
| 253 | - ), |
|
| 254 | - ), |
|
| 255 | - ) |
|
| 256 | - ); |
|
| 257 | - } |
|
| 258 | - ); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Generate the html code for the section. |
|
| 263 | - * |
|
| 264 | - * @param string $letter The section's letter. |
|
| 265 | - * @param array $posts An array of `$post_id => $post_title` associated with |
|
| 266 | - * the section. |
|
| 267 | - * @param int $vocabulary_id Unique vocabulary id. |
|
| 268 | - * |
|
| 269 | - * @return string The section html code (or an empty string if the section has |
|
| 270 | - * no posts). |
|
| 271 | - * @since 3.17.0 |
|
| 272 | - */ |
|
| 273 | - private function get_section( $letter, $posts, $vocabulary_id ) { |
|
| 274 | - |
|
| 275 | - // Return an empty string if there are no posts. |
|
| 276 | - if ( 0 === count( $posts ) ) { |
|
| 277 | - return ''; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return sprintf( |
|
| 281 | - ' |
|
| 195 | + $html = ob_get_clean(); |
|
| 196 | + |
|
| 197 | + return $html; |
|
| 198 | + |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + private function register_block_type() { |
|
| 202 | + |
|
| 203 | + $scope = $this; |
|
| 204 | + |
|
| 205 | + add_action( |
|
| 206 | + 'init', |
|
| 207 | + function () use ( $scope ) { |
|
| 208 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 209 | + // Gutenberg is not active. |
|
| 210 | + return; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + register_block_type( |
|
| 214 | + 'wordlift/vocabulary', |
|
| 215 | + array( |
|
| 216 | + 'editor_script' => 'wl-block-editor', |
|
| 217 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 218 | + $attr_code = ''; |
|
| 219 | + foreach ( $attributes as $key => $value ) { |
|
| 220 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 224 | + }, |
|
| 225 | + 'attributes' => array( |
|
| 226 | + 'type' => array( |
|
| 227 | + 'type' => 'string', |
|
| 228 | + 'default' => 'all', |
|
| 229 | + ), |
|
| 230 | + 'limit' => array( |
|
| 231 | + 'type' => 'number', |
|
| 232 | + 'default' => 100, |
|
| 233 | + ), |
|
| 234 | + 'orderby' => array( |
|
| 235 | + 'type' => 'string', |
|
| 236 | + 'default' => 'post_date', |
|
| 237 | + ), |
|
| 238 | + 'order' => array( |
|
| 239 | + 'type' => 'string', |
|
| 240 | + 'default' => 'DESC', |
|
| 241 | + ), |
|
| 242 | + 'cat' => array( |
|
| 243 | + 'type' => 'string', |
|
| 244 | + 'default' => '', |
|
| 245 | + ), |
|
| 246 | + 'preview' => array( |
|
| 247 | + 'type' => 'boolean', |
|
| 248 | + 'default' => false, |
|
| 249 | + ), |
|
| 250 | + 'preview_src' => array( |
|
| 251 | + 'type' => 'string', |
|
| 252 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/vocabulary.png', |
|
| 253 | + ), |
|
| 254 | + ), |
|
| 255 | + ) |
|
| 256 | + ); |
|
| 257 | + } |
|
| 258 | + ); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Generate the html code for the section. |
|
| 263 | + * |
|
| 264 | + * @param string $letter The section's letter. |
|
| 265 | + * @param array $posts An array of `$post_id => $post_title` associated with |
|
| 266 | + * the section. |
|
| 267 | + * @param int $vocabulary_id Unique vocabulary id. |
|
| 268 | + * |
|
| 269 | + * @return string The section html code (or an empty string if the section has |
|
| 270 | + * no posts). |
|
| 271 | + * @since 3.17.0 |
|
| 272 | + */ |
|
| 273 | + private function get_section( $letter, $posts, $vocabulary_id ) { |
|
| 274 | + |
|
| 275 | + // Return an empty string if there are no posts. |
|
| 276 | + if ( 0 === count( $posts ) ) { |
|
| 277 | + return ''; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return sprintf( |
|
| 281 | + ' |
|
| 282 | 282 | <div class="wl-vocabulary-letter-block" id="wl-vocabulary-%d-%s"> |
| 283 | 283 | <aside class="wl-vocabulary-left-column">%s</aside> |
| 284 | 284 | <div class="wl-vocabulary-right-column"> |
@@ -288,157 +288,157 @@ discard block |
||
| 288 | 288 | </div> |
| 289 | 289 | </div> |
| 290 | 290 | ', |
| 291 | - $vocabulary_id, |
|
| 292 | - esc_attr( $letter ), |
|
| 293 | - esc_html( $letter ), |
|
| 294 | - $this->format_posts_as_list( $posts ) |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Format an array post `$post_id => $post_title` as a list. |
|
| 300 | - * |
|
| 301 | - * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
| 302 | - * |
|
| 303 | - * @return string A list. |
|
| 304 | - * @since 3.17.0 |
|
| 305 | - */ |
|
| 306 | - private function format_posts_as_list( $posts ) { |
|
| 307 | - |
|
| 308 | - return array_reduce( |
|
| 309 | - array_keys( $posts ), |
|
| 310 | - function ( $carry, $item ) use ( $posts ) { |
|
| 311 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 312 | - }, |
|
| 313 | - '' |
|
| 314 | - ); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Get the posts from WordPress using the provided attributes. |
|
| 319 | - * |
|
| 320 | - * @param array $atts The shortcode attributes. |
|
| 321 | - * |
|
| 322 | - * @return array An array of {@link WP_Post}s. |
|
| 323 | - * @since 3.17.0 |
|
| 324 | - */ |
|
| 325 | - private function get_posts( $atts ) { |
|
| 326 | - // The default arguments for the query. |
|
| 327 | - $args = array( |
|
| 328 | - // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
|
| 329 | - 'posts_per_page' => intval( $atts['limit'] ), |
|
| 330 | - 'update_post_meta_cache' => false, |
|
| 331 | - 'update_post_term_cache' => false, |
|
| 332 | - 'orderby' => $atts['orderby'], |
|
| 333 | - 'order' => $atts['order'], |
|
| 334 | - // Exclude the publisher. |
|
| 335 | - 'post__not_in' => array( Wordlift_Configuration_Service::get_instance()->get_publisher_id() ), |
|
| 336 | - ); |
|
| 337 | - |
|
| 338 | - // Limit the based entity type if needed. |
|
| 339 | - if ( 'all' !== $atts['type'] ) { |
|
| 340 | - $args['tax_query'] = array( |
|
| 341 | - array( |
|
| 342 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 343 | - 'field' => 'slug', |
|
| 344 | - 'terms' => $atts['type'], |
|
| 345 | - ), |
|
| 346 | - ); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - if ( ! empty( $atts['cat'] ) ) { |
|
| 350 | - $args['cat'] = $atts['cat']; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - // Get the posts. Note that if a `type` is specified before, then the |
|
| 354 | - // `tax_query` from the `add_criterias` call isn't added. |
|
| 355 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 356 | - |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Populate the alphabet with posts. |
|
| 361 | - * |
|
| 362 | - * @param array $alphabet An array of letters. |
|
| 363 | - * @param int $post_id The {@link WP_Post} id. |
|
| 364 | - * |
|
| 365 | - * @since 3.17.0 |
|
| 366 | - */ |
|
| 367 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 368 | - |
|
| 369 | - // Get the title without accents. |
|
| 370 | - $title = $this->get_the_title( $post_id ); |
|
| 371 | - |
|
| 372 | - // Get the initial letter. |
|
| 373 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 374 | - |
|
| 375 | - // Add the post. |
|
| 376 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
| 377 | - |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Get the title without accents. |
|
| 382 | - * If the post is not of type `entity`, use first synonym if synonyms exists. |
|
| 383 | - * |
|
| 384 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1096 |
|
| 385 | - * |
|
| 386 | - * @since 3.27.0 |
|
| 387 | - * |
|
| 388 | - * @param int $post_id The {@link WP_Post} id. |
|
| 389 | - * |
|
| 390 | - * @return string |
|
| 391 | - */ |
|
| 392 | - private function get_the_title( $post_id ) { |
|
| 393 | - |
|
| 394 | - $title = get_the_title( $post_id ); |
|
| 395 | - |
|
| 396 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 397 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 398 | - |
|
| 399 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 400 | - $title = $alternative_labels[0]; |
|
| 401 | - } |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return remove_accents( $title ); |
|
| 405 | - |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Find the first letter in the alphabet. |
|
| 410 | - * |
|
| 411 | - * In some alphabets a letter is a compound of letters, therefore this function |
|
| 412 | - * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
| 413 | - * single letter. In case the letter is not found a # (hash) key is returned. |
|
| 414 | - * |
|
| 415 | - * @param array $alphabet An array of alphabet letters. |
|
| 416 | - * @param string $title The title to match. |
|
| 417 | - * |
|
| 418 | - * @return string The initial letter or a `#` key. |
|
| 419 | - * @since 3.17.0 |
|
| 420 | - */ |
|
| 421 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 422 | - |
|
| 423 | - // Need to handle letters which consist of 3 and 2 characters. |
|
| 424 | - for ( $i = 3; $i > 0; $i -- ) { |
|
| 425 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 426 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
| 427 | - return $letter; |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - return '#'; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Get and increment the `$vocabulary_id`. |
|
| 436 | - * |
|
| 437 | - * @return int The incremented vocabulary id. |
|
| 438 | - * @since 3.18.3 |
|
| 439 | - */ |
|
| 440 | - private static function get_and_increment_vocabulary_id() { |
|
| 441 | - return self::$vocabulary_id ++; |
|
| 442 | - } |
|
| 291 | + $vocabulary_id, |
|
| 292 | + esc_attr( $letter ), |
|
| 293 | + esc_html( $letter ), |
|
| 294 | + $this->format_posts_as_list( $posts ) |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Format an array post `$post_id => $post_title` as a list. |
|
| 300 | + * |
|
| 301 | + * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
| 302 | + * |
|
| 303 | + * @return string A list. |
|
| 304 | + * @since 3.17.0 |
|
| 305 | + */ |
|
| 306 | + private function format_posts_as_list( $posts ) { |
|
| 307 | + |
|
| 308 | + return array_reduce( |
|
| 309 | + array_keys( $posts ), |
|
| 310 | + function ( $carry, $item ) use ( $posts ) { |
|
| 311 | + return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 312 | + }, |
|
| 313 | + '' |
|
| 314 | + ); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Get the posts from WordPress using the provided attributes. |
|
| 319 | + * |
|
| 320 | + * @param array $atts The shortcode attributes. |
|
| 321 | + * |
|
| 322 | + * @return array An array of {@link WP_Post}s. |
|
| 323 | + * @since 3.17.0 |
|
| 324 | + */ |
|
| 325 | + private function get_posts( $atts ) { |
|
| 326 | + // The default arguments for the query. |
|
| 327 | + $args = array( |
|
| 328 | + // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
|
| 329 | + 'posts_per_page' => intval( $atts['limit'] ), |
|
| 330 | + 'update_post_meta_cache' => false, |
|
| 331 | + 'update_post_term_cache' => false, |
|
| 332 | + 'orderby' => $atts['orderby'], |
|
| 333 | + 'order' => $atts['order'], |
|
| 334 | + // Exclude the publisher. |
|
| 335 | + 'post__not_in' => array( Wordlift_Configuration_Service::get_instance()->get_publisher_id() ), |
|
| 336 | + ); |
|
| 337 | + |
|
| 338 | + // Limit the based entity type if needed. |
|
| 339 | + if ( 'all' !== $atts['type'] ) { |
|
| 340 | + $args['tax_query'] = array( |
|
| 341 | + array( |
|
| 342 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 343 | + 'field' => 'slug', |
|
| 344 | + 'terms' => $atts['type'], |
|
| 345 | + ), |
|
| 346 | + ); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + if ( ! empty( $atts['cat'] ) ) { |
|
| 350 | + $args['cat'] = $atts['cat']; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + // Get the posts. Note that if a `type` is specified before, then the |
|
| 354 | + // `tax_query` from the `add_criterias` call isn't added. |
|
| 355 | + return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 356 | + |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Populate the alphabet with posts. |
|
| 361 | + * |
|
| 362 | + * @param array $alphabet An array of letters. |
|
| 363 | + * @param int $post_id The {@link WP_Post} id. |
|
| 364 | + * |
|
| 365 | + * @since 3.17.0 |
|
| 366 | + */ |
|
| 367 | + private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 368 | + |
|
| 369 | + // Get the title without accents. |
|
| 370 | + $title = $this->get_the_title( $post_id ); |
|
| 371 | + |
|
| 372 | + // Get the initial letter. |
|
| 373 | + $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 374 | + |
|
| 375 | + // Add the post. |
|
| 376 | + $alphabet[ $letter ][ $post_id ] = $title; |
|
| 377 | + |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Get the title without accents. |
|
| 382 | + * If the post is not of type `entity`, use first synonym if synonyms exists. |
|
| 383 | + * |
|
| 384 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1096 |
|
| 385 | + * |
|
| 386 | + * @since 3.27.0 |
|
| 387 | + * |
|
| 388 | + * @param int $post_id The {@link WP_Post} id. |
|
| 389 | + * |
|
| 390 | + * @return string |
|
| 391 | + */ |
|
| 392 | + private function get_the_title( $post_id ) { |
|
| 393 | + |
|
| 394 | + $title = get_the_title( $post_id ); |
|
| 395 | + |
|
| 396 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 397 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 398 | + |
|
| 399 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 400 | + $title = $alternative_labels[0]; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return remove_accents( $title ); |
|
| 405 | + |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Find the first letter in the alphabet. |
|
| 410 | + * |
|
| 411 | + * In some alphabets a letter is a compound of letters, therefore this function |
|
| 412 | + * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
| 413 | + * single letter. In case the letter is not found a # (hash) key is returned. |
|
| 414 | + * |
|
| 415 | + * @param array $alphabet An array of alphabet letters. |
|
| 416 | + * @param string $title The title to match. |
|
| 417 | + * |
|
| 418 | + * @return string The initial letter or a `#` key. |
|
| 419 | + * @since 3.17.0 |
|
| 420 | + */ |
|
| 421 | + private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 422 | + |
|
| 423 | + // Need to handle letters which consist of 3 and 2 characters. |
|
| 424 | + for ( $i = 3; $i > 0; $i -- ) { |
|
| 425 | + $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 426 | + if ( isset( $alphabet[ $letter ] ) ) { |
|
| 427 | + return $letter; |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + return '#'; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Get and increment the `$vocabulary_id`. |
|
| 436 | + * |
|
| 437 | + * @return int The incremented vocabulary id. |
|
| 438 | + * @since 3.18.3 |
|
| 439 | + */ |
|
| 440 | + private static function get_and_increment_vocabulary_id() { |
|
| 441 | + return self::$vocabulary_id ++; |
|
| 442 | + } |
|
| 443 | 443 | |
| 444 | 444 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | public function __construct() { |
| 52 | 52 | parent::__construct(); |
| 53 | 53 | |
| 54 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 54 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 55 | 55 | |
| 56 | 56 | $this->register_block_type(); |
| 57 | 57 | |
@@ -65,11 +65,11 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | private static function are_requirements_satisfied() { |
| 67 | 67 | |
| 68 | - return function_exists( 'mb_strlen' ) && |
|
| 69 | - function_exists( 'mb_substr' ) && |
|
| 70 | - function_exists( 'mb_strtolower' ) && |
|
| 71 | - function_exists( 'mb_strtoupper' ) && |
|
| 72 | - function_exists( 'mb_convert_case' ); |
|
| 68 | + return function_exists('mb_strlen') && |
|
| 69 | + function_exists('mb_substr') && |
|
| 70 | + function_exists('mb_strtolower') && |
|
| 71 | + function_exists('mb_strtoupper') && |
|
| 72 | + function_exists('mb_convert_case'); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -80,17 +80,17 @@ discard block |
||
| 80 | 80 | * @return string The output html code. |
| 81 | 81 | * @since 3.16.0 |
| 82 | 82 | */ |
| 83 | - public function render( $atts ) { |
|
| 83 | + public function render($atts) { |
|
| 84 | 84 | |
| 85 | 85 | // Bail out if the requirements aren't satisfied: we need mbstring for |
| 86 | 86 | // the vocabulary widget to work. |
| 87 | - if ( ! self::are_requirements_satisfied() ) { |
|
| 88 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 87 | + if ( ! self::are_requirements_satisfied()) { |
|
| 88 | + $this->log->warn("The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements."); |
|
| 89 | 89 | |
| 90 | 90 | return ''; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION ); |
|
| 93 | + wp_enqueue_style('wl-vocabulary-shortcode', dirname(plugin_dir_url(__FILE__)).'/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION); |
|
| 94 | 94 | |
| 95 | 95 | // Extract attributes and set default values. |
| 96 | 96 | $atts = shortcode_atts( |
@@ -111,15 +111,15 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | // Get the posts. Note that if a `type` is specified before, then the |
| 113 | 113 | // `tax_query` from the `add_criterias` call isn't added. |
| 114 | - $posts = $this->get_posts( $atts ); |
|
| 114 | + $posts = $this->get_posts($atts); |
|
| 115 | 115 | |
| 116 | 116 | // Get the alphabet. |
| 117 | 117 | $language_code = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
| 118 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 118 | + $alphabet = Wordlift_Alphabet_Service::get($language_code); |
|
| 119 | 119 | |
| 120 | 120 | // Add posts to the alphabet. |
| 121 | - foreach ( $posts as $post ) { |
|
| 122 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 121 | + foreach ($posts as $post) { |
|
| 122 | + $this->add_to_alphabet($alphabet, $post->ID); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | $header = ''; |
@@ -129,16 +129,16 @@ discard block |
||
| 129 | 129 | $vocabulary_id = self::get_and_increment_vocabulary_id(); |
| 130 | 130 | |
| 131 | 131 | // Generate the header. |
| 132 | - foreach ( $alphabet as $item => $translations ) { |
|
| 133 | - $template = ( empty( $translations ) |
|
| 132 | + foreach ($alphabet as $item => $translations) { |
|
| 133 | + $template = (empty($translations) |
|
| 134 | 134 | ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
| 135 | - : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
| 135 | + : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>'); |
|
| 136 | 136 | |
| 137 | - $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
| 137 | + $header .= sprintf($template, esc_html($item), esc_attr($item), $vocabulary_id); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // Generate the sections. |
| 141 | - foreach ( $alphabet as $item => $translations ) { |
|
| 141 | + foreach ($alphabet as $item => $translations) { |
|
| 142 | 142 | // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
| 143 | 143 | // since we had `AB` being placed before `Aa` with `asort`. |
| 144 | 144 | // |
@@ -146,16 +146,16 @@ discard block |
||
| 146 | 146 | // asort( $translations ); |
| 147 | 147 | uasort( |
| 148 | 148 | $translations, |
| 149 | - function ( $a, $b ) { |
|
| 150 | - if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
| 151 | - || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
| 149 | + function($a, $b) { |
|
| 150 | + if (mb_strtolower($a) === mb_strtolower($b) |
|
| 151 | + || mb_strtoupper($a) === mb_strtoupper($b)) { |
|
| 152 | 152 | return 0; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
| 155 | + return (mb_strtolower($a) < mb_strtolower($b)) ? -1 : 1; |
|
| 156 | 156 | } |
| 157 | 157 | ); |
| 158 | - $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
| 158 | + $sections .= $this->get_section($item, $translations, $vocabulary_id); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // Return HTML template. |
@@ -167,8 +167,8 @@ discard block |
||
| 167 | 167 | echo wp_kses( |
| 168 | 168 | $header, |
| 169 | 169 | array( |
| 170 | - 'span' => array( 'class' => array() ), |
|
| 171 | - 'a' => array( 'href' => array() ), |
|
| 170 | + 'span' => array('class' => array()), |
|
| 171 | + 'a' => array('href' => array()), |
|
| 172 | 172 | ) |
| 173 | 173 | ); |
| 174 | 174 | ?> |
@@ -182,10 +182,10 @@ discard block |
||
| 182 | 182 | 'class' => array(), |
| 183 | 183 | 'id' => array(), |
| 184 | 184 | ), |
| 185 | - 'aside' => array( 'class' => array() ), |
|
| 186 | - 'ul' => array( 'class' => array() ), |
|
| 185 | + 'aside' => array('class' => array()), |
|
| 186 | + 'ul' => array('class' => array()), |
|
| 187 | 187 | 'li' => array(), |
| 188 | - 'a' => array( 'href' => array() ), |
|
| 188 | + 'a' => array('href' => array()), |
|
| 189 | 189 | ) |
| 190 | 190 | ); |
| 191 | 191 | ?> |
@@ -204,8 +204,8 @@ discard block |
||
| 204 | 204 | |
| 205 | 205 | add_action( |
| 206 | 206 | 'init', |
| 207 | - function () use ( $scope ) { |
|
| 208 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 207 | + function() use ($scope) { |
|
| 208 | + if ( ! function_exists('register_block_type')) { |
|
| 209 | 209 | // Gutenberg is not active. |
| 210 | 210 | return; |
| 211 | 211 | } |
@@ -214,13 +214,13 @@ discard block |
||
| 214 | 214 | 'wordlift/vocabulary', |
| 215 | 215 | array( |
| 216 | 216 | 'editor_script' => 'wl-block-editor', |
| 217 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 217 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 218 | 218 | $attr_code = ''; |
| 219 | - foreach ( $attributes as $key => $value ) { |
|
| 220 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 219 | + foreach ($attributes as $key => $value) { |
|
| 220 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 223 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 224 | 224 | }, |
| 225 | 225 | 'attributes' => array( |
| 226 | 226 | 'type' => array( |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | ), |
| 250 | 250 | 'preview_src' => array( |
| 251 | 251 | 'type' => 'string', |
| 252 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/vocabulary.png', |
|
| 252 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/vocabulary.png', |
|
| 253 | 253 | ), |
| 254 | 254 | ), |
| 255 | 255 | ) |
@@ -270,10 +270,10 @@ discard block |
||
| 270 | 270 | * no posts). |
| 271 | 271 | * @since 3.17.0 |
| 272 | 272 | */ |
| 273 | - private function get_section( $letter, $posts, $vocabulary_id ) { |
|
| 273 | + private function get_section($letter, $posts, $vocabulary_id) { |
|
| 274 | 274 | |
| 275 | 275 | // Return an empty string if there are no posts. |
| 276 | - if ( 0 === count( $posts ) ) { |
|
| 276 | + if (0 === count($posts)) { |
|
| 277 | 277 | return ''; |
| 278 | 278 | } |
| 279 | 279 | |
@@ -289,9 +289,9 @@ discard block |
||
| 289 | 289 | </div> |
| 290 | 290 | ', |
| 291 | 291 | $vocabulary_id, |
| 292 | - esc_attr( $letter ), |
|
| 293 | - esc_html( $letter ), |
|
| 294 | - $this->format_posts_as_list( $posts ) |
|
| 292 | + esc_attr($letter), |
|
| 293 | + esc_html($letter), |
|
| 294 | + $this->format_posts_as_list($posts) |
|
| 295 | 295 | ); |
| 296 | 296 | } |
| 297 | 297 | |
@@ -303,12 +303,12 @@ discard block |
||
| 303 | 303 | * @return string A list. |
| 304 | 304 | * @since 3.17.0 |
| 305 | 305 | */ |
| 306 | - private function format_posts_as_list( $posts ) { |
|
| 306 | + private function format_posts_as_list($posts) { |
|
| 307 | 307 | |
| 308 | 308 | return array_reduce( |
| 309 | - array_keys( $posts ), |
|
| 310 | - function ( $carry, $item ) use ( $posts ) { |
|
| 311 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 309 | + array_keys($posts), |
|
| 310 | + function($carry, $item) use ($posts) { |
|
| 311 | + return $carry.sprintf('<li><a href="%s">%s</a></li>', esc_attr(get_permalink($item)), esc_html($posts[$item])); |
|
| 312 | 312 | }, |
| 313 | 313 | '' |
| 314 | 314 | ); |
@@ -322,21 +322,21 @@ discard block |
||
| 322 | 322 | * @return array An array of {@link WP_Post}s. |
| 323 | 323 | * @since 3.17.0 |
| 324 | 324 | */ |
| 325 | - private function get_posts( $atts ) { |
|
| 325 | + private function get_posts($atts) { |
|
| 326 | 326 | // The default arguments for the query. |
| 327 | 327 | $args = array( |
| 328 | 328 | // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
| 329 | - 'posts_per_page' => intval( $atts['limit'] ), |
|
| 329 | + 'posts_per_page' => intval($atts['limit']), |
|
| 330 | 330 | 'update_post_meta_cache' => false, |
| 331 | 331 | 'update_post_term_cache' => false, |
| 332 | 332 | 'orderby' => $atts['orderby'], |
| 333 | 333 | 'order' => $atts['order'], |
| 334 | 334 | // Exclude the publisher. |
| 335 | - 'post__not_in' => array( Wordlift_Configuration_Service::get_instance()->get_publisher_id() ), |
|
| 335 | + 'post__not_in' => array(Wordlift_Configuration_Service::get_instance()->get_publisher_id()), |
|
| 336 | 336 | ); |
| 337 | 337 | |
| 338 | 338 | // Limit the based entity type if needed. |
| 339 | - if ( 'all' !== $atts['type'] ) { |
|
| 339 | + if ('all' !== $atts['type']) { |
|
| 340 | 340 | $args['tax_query'] = array( |
| 341 | 341 | array( |
| 342 | 342 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
@@ -346,13 +346,13 @@ discard block |
||
| 346 | 346 | ); |
| 347 | 347 | } |
| 348 | 348 | |
| 349 | - if ( ! empty( $atts['cat'] ) ) { |
|
| 349 | + if ( ! empty($atts['cat'])) { |
|
| 350 | 350 | $args['cat'] = $atts['cat']; |
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | // Get the posts. Note that if a `type` is specified before, then the |
| 354 | 354 | // `tax_query` from the `add_criterias` call isn't added. |
| 355 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 355 | + return get_posts(Wordlift_Entity_Service::add_criterias($args)); |
|
| 356 | 356 | |
| 357 | 357 | } |
| 358 | 358 | |
@@ -364,16 +364,16 @@ discard block |
||
| 364 | 364 | * |
| 365 | 365 | * @since 3.17.0 |
| 366 | 366 | */ |
| 367 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 367 | + private function add_to_alphabet(&$alphabet, $post_id) { |
|
| 368 | 368 | |
| 369 | 369 | // Get the title without accents. |
| 370 | - $title = $this->get_the_title( $post_id ); |
|
| 370 | + $title = $this->get_the_title($post_id); |
|
| 371 | 371 | |
| 372 | 372 | // Get the initial letter. |
| 373 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 373 | + $letter = $this->get_first_letter_in_alphabet_or_hash($alphabet, $title); |
|
| 374 | 374 | |
| 375 | 375 | // Add the post. |
| 376 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
| 376 | + $alphabet[$letter][$post_id] = $title; |
|
| 377 | 377 | |
| 378 | 378 | } |
| 379 | 379 | |
@@ -389,19 +389,19 @@ discard block |
||
| 389 | 389 | * |
| 390 | 390 | * @return string |
| 391 | 391 | */ |
| 392 | - private function get_the_title( $post_id ) { |
|
| 392 | + private function get_the_title($post_id) { |
|
| 393 | 393 | |
| 394 | - $title = get_the_title( $post_id ); |
|
| 394 | + $title = get_the_title($post_id); |
|
| 395 | 395 | |
| 396 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 397 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 396 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 397 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 398 | 398 | |
| 399 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 399 | + if (count($alternative_labels) > 0) { |
|
| 400 | 400 | $title = $alternative_labels[0]; |
| 401 | 401 | } |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | - return remove_accents( $title ); |
|
| 404 | + return remove_accents($title); |
|
| 405 | 405 | |
| 406 | 406 | } |
| 407 | 407 | |
@@ -418,12 +418,12 @@ discard block |
||
| 418 | 418 | * @return string The initial letter or a `#` key. |
| 419 | 419 | * @since 3.17.0 |
| 420 | 420 | */ |
| 421 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 421 | + private function get_first_letter_in_alphabet_or_hash($alphabet, $title) { |
|
| 422 | 422 | |
| 423 | 423 | // Need to handle letters which consist of 3 and 2 characters. |
| 424 | - for ( $i = 3; $i > 0; $i -- ) { |
|
| 425 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 426 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
| 424 | + for ($i = 3; $i > 0; $i--) { |
|
| 425 | + $letter = mb_convert_case(mb_substr($title, 0, $i), MB_CASE_UPPER); |
|
| 426 | + if (isset($alphabet[$letter])) { |
|
| 427 | 427 | return $letter; |
| 428 | 428 | } |
| 429 | 429 | } |
@@ -438,7 +438,7 @@ discard block |
||
| 438 | 438 | * @since 3.18.3 |
| 439 | 439 | */ |
| 440 | 440 | private static function get_and_increment_vocabulary_id() { |
| 441 | - return self::$vocabulary_id ++; |
|
| 441 | + return self::$vocabulary_id++; |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | } |