@@ -17,116 +17,116 @@ 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( 'init', function () use ( $scope ) { |
|
| 44 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | - // Gutenberg is not active. |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - register_block_type( 'wordlift/faceted-search', array( |
|
| 50 | - 'editor_script' => 'wl-block-editor', |
|
| 51 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | - $attr_code = ''; |
|
| 53 | - foreach ( $attributes as $key => $value ) { |
|
| 54 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | - }, |
|
| 59 | - |
|
| 60 | - 'attributes' => $scope->get_block_attributes(), |
|
| 61 | - ) ); |
|
| 62 | - } ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | - * |
|
| 69 | - * @param array $atts Shortcode attributes. |
|
| 70 | - * |
|
| 71 | - * @return array $shortcode_atts |
|
| 72 | - * @since 3.20.0 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 76 | - |
|
| 77 | - // Extract attributes and set default values. |
|
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 81 | - 'post_id' => '', |
|
| 82 | - 'template_id' => '', |
|
| 83 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 84 | - 'post_types' => '', |
|
| 85 | - ), $atts ); |
|
| 86 | - |
|
| 87 | - return $shortcode_atts; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 92 | - * |
|
| 93 | - * @param array $atts Shortcode attributes. |
|
| 94 | - * |
|
| 95 | - * @return string Shortcode HTML for web |
|
| 96 | - * @since 3.20.0 |
|
| 97 | - * |
|
| 98 | - */ |
|
| 99 | - private function web_shortcode( $atts ) { |
|
| 100 | - |
|
| 101 | - // attributes extraction and boolean filtering |
|
| 102 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 103 | - |
|
| 104 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 105 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 110 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 111 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 112 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 113 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 114 | - |
|
| 115 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 116 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 117 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 118 | - $rest_url = esc_attr( $rest_url ); |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - // avoid building the widget when no valid $rest_url |
|
| 122 | - if ( ! $rest_url ) { |
|
| 123 | - return; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 127 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 128 | - |
|
| 129 | - return <<<HTML |
|
| 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( 'init', function () use ( $scope ) { |
|
| 44 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | + // Gutenberg is not active. |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + register_block_type( 'wordlift/faceted-search', array( |
|
| 50 | + 'editor_script' => 'wl-block-editor', |
|
| 51 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | + $attr_code = ''; |
|
| 53 | + foreach ( $attributes as $key => $value ) { |
|
| 54 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | + }, |
|
| 59 | + |
|
| 60 | + 'attributes' => $scope->get_block_attributes(), |
|
| 61 | + ) ); |
|
| 62 | + } ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | + * |
|
| 69 | + * @param array $atts Shortcode attributes. |
|
| 70 | + * |
|
| 71 | + * @return array $shortcode_atts |
|
| 72 | + * @since 3.20.0 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + private function make_shortcode_atts( $atts ) { |
|
| 76 | + |
|
| 77 | + // Extract attributes and set default values. |
|
| 78 | + $shortcode_atts = shortcode_atts( array( |
|
| 79 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | + 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 81 | + 'post_id' => '', |
|
| 82 | + 'template_id' => '', |
|
| 83 | + 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 84 | + 'post_types' => '', |
|
| 85 | + ), $atts ); |
|
| 86 | + |
|
| 87 | + return $shortcode_atts; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 92 | + * |
|
| 93 | + * @param array $atts Shortcode attributes. |
|
| 94 | + * |
|
| 95 | + * @return string Shortcode HTML for web |
|
| 96 | + * @since 3.20.0 |
|
| 97 | + * |
|
| 98 | + */ |
|
| 99 | + private function web_shortcode( $atts ) { |
|
| 100 | + |
|
| 101 | + // attributes extraction and boolean filtering |
|
| 102 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 103 | + |
|
| 104 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 105 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 110 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 111 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 112 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 113 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 114 | + |
|
| 115 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 116 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 117 | + $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 118 | + $rest_url = esc_attr( $rest_url ); |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + // avoid building the widget when no valid $rest_url |
|
| 122 | + if ( ! $rest_url ) { |
|
| 123 | + return; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 127 | + $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 128 | + |
|
| 129 | + return <<<HTML |
|
| 130 | 130 | <!-- Faceted {$faceted_id} --> |
| 131 | 131 | <div id="{$faceted_id}" |
| 132 | 132 | class="wl-faceted" |
@@ -136,56 +136,56 @@ discard block |
||
| 136 | 136 | data-template-url="{$template_url}"></div> |
| 137 | 137 | <!-- /Faceted {$faceted_id} --> |
| 138 | 138 | HTML; |
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 143 | - * |
|
| 144 | - * @param array $atts Shortcode attributes. |
|
| 145 | - * |
|
| 146 | - * @return string Shortcode HTML for amp |
|
| 147 | - * @since 3.20.0 |
|
| 148 | - * |
|
| 149 | - */ |
|
| 150 | - private function amp_shortcode( $atts ) { |
|
| 151 | - |
|
| 152 | - // attributes extraction and boolean filtering |
|
| 153 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 154 | - |
|
| 155 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 156 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 157 | - return; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 161 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 162 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 163 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 164 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 165 | - |
|
| 166 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 167 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 168 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 169 | - 'amp' => 1, |
|
| 170 | - 'post_id' => $post->ID, |
|
| 171 | - 'limit' => $limit |
|
| 172 | - ) ) ) : false; |
|
| 173 | - |
|
| 174 | - // avoid building the widget when no valid $rest_url |
|
| 175 | - if ( ! $rest_url ) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 180 | - // This is a hackish way, but this works for http and https URLs |
|
| 181 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 182 | - |
|
| 183 | - if ( empty( $template_id ) ) { |
|
| 184 | - $template_id = "template-" . $faceted_id; |
|
| 185 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return <<<HTML |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 143 | + * |
|
| 144 | + * @param array $atts Shortcode attributes. |
|
| 145 | + * |
|
| 146 | + * @return string Shortcode HTML for amp |
|
| 147 | + * @since 3.20.0 |
|
| 148 | + * |
|
| 149 | + */ |
|
| 150 | + private function amp_shortcode( $atts ) { |
|
| 151 | + |
|
| 152 | + // attributes extraction and boolean filtering |
|
| 153 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 154 | + |
|
| 155 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 156 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 161 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 162 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 163 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 164 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 165 | + |
|
| 166 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 167 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 168 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 169 | + 'amp' => 1, |
|
| 170 | + 'post_id' => $post->ID, |
|
| 171 | + 'limit' => $limit |
|
| 172 | + ) ) ) : false; |
|
| 173 | + |
|
| 174 | + // avoid building the widget when no valid $rest_url |
|
| 175 | + if ( ! $rest_url ) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 180 | + // This is a hackish way, but this works for http and https URLs |
|
| 181 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 182 | + |
|
| 183 | + if ( empty( $template_id ) ) { |
|
| 184 | + $template_id = "template-" . $faceted_id; |
|
| 185 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return <<<HTML |
|
| 189 | 189 | <div id="{$faceted_id}" class="wl-amp-faceted"> |
| 190 | 190 | <h2 class="wl-headline">{$title}</h2> |
| 191 | 191 | <amp-state id="referencedPosts"> |
@@ -268,62 +268,62 @@ discard block |
||
| 268 | 268 | </section> |
| 269 | 269 | </div> |
| 270 | 270 | HTML; |
| 271 | - } |
|
| 272 | - |
|
| 273 | - public function get_block_attributes() { |
|
| 274 | - return array( |
|
| 275 | - 'title' => array( |
|
| 276 | - 'type' => 'string', |
|
| 277 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 278 | - ), |
|
| 279 | - 'template_id' => array( |
|
| 280 | - 'type' => 'string', |
|
| 281 | - 'default' => '', |
|
| 282 | - ), |
|
| 283 | - 'post_id' => array( |
|
| 284 | - 'type' => 'number', |
|
| 285 | - 'default' => '', |
|
| 286 | - ), |
|
| 287 | - 'uniqid' => array( |
|
| 288 | - 'type' => 'string', |
|
| 289 | - 'default' => '', |
|
| 290 | - ), |
|
| 291 | - 'limit' => array( |
|
| 292 | - 'type' => 'number', |
|
| 293 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 294 | - ), |
|
| 295 | - 'preview' => array( |
|
| 296 | - 'type' => 'boolean', |
|
| 297 | - 'default' => false, |
|
| 298 | - ), |
|
| 299 | - 'preview_src' => array( |
|
| 300 | - 'type' => 'string', |
|
| 301 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 302 | - ), |
|
| 303 | - 'post_types' => array( |
|
| 304 | - 'type' => 'string', |
|
| 305 | - 'default' => '', |
|
| 306 | - ) |
|
| 307 | - ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param $post |
|
| 312 | - * @param $delimiter |
|
| 313 | - * @param $limit |
|
| 314 | - * |
|
| 315 | - * @param $post_types |
|
| 316 | - * |
|
| 317 | - * @return bool|string |
|
| 318 | - */ |
|
| 319 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 320 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 321 | - 'post_id' => $post->ID, |
|
| 322 | - 'limit' => $limit, |
|
| 323 | - 'post_types' => $post_types |
|
| 324 | - ) ) ) : false; |
|
| 325 | - |
|
| 326 | - return $rest_url; |
|
| 327 | - } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + public function get_block_attributes() { |
|
| 274 | + return array( |
|
| 275 | + 'title' => array( |
|
| 276 | + 'type' => 'string', |
|
| 277 | + 'default' => __( 'Related articles', 'wordlift' ), |
|
| 278 | + ), |
|
| 279 | + 'template_id' => array( |
|
| 280 | + 'type' => 'string', |
|
| 281 | + 'default' => '', |
|
| 282 | + ), |
|
| 283 | + 'post_id' => array( |
|
| 284 | + 'type' => 'number', |
|
| 285 | + 'default' => '', |
|
| 286 | + ), |
|
| 287 | + 'uniqid' => array( |
|
| 288 | + 'type' => 'string', |
|
| 289 | + 'default' => '', |
|
| 290 | + ), |
|
| 291 | + 'limit' => array( |
|
| 292 | + 'type' => 'number', |
|
| 293 | + 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 294 | + ), |
|
| 295 | + 'preview' => array( |
|
| 296 | + 'type' => 'boolean', |
|
| 297 | + 'default' => false, |
|
| 298 | + ), |
|
| 299 | + 'preview_src' => array( |
|
| 300 | + 'type' => 'string', |
|
| 301 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 302 | + ), |
|
| 303 | + 'post_types' => array( |
|
| 304 | + 'type' => 'string', |
|
| 305 | + 'default' => '', |
|
| 306 | + ) |
|
| 307 | + ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param $post |
|
| 312 | + * @param $delimiter |
|
| 313 | + * @param $limit |
|
| 314 | + * |
|
| 315 | + * @param $post_types |
|
| 316 | + * |
|
| 317 | + * @return bool|string |
|
| 318 | + */ |
|
| 319 | + public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 320 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 321 | + 'post_id' => $post->ID, |
|
| 322 | + 'limit' => $limit, |
|
| 323 | + 'post_types' => $post_types |
|
| 324 | + ) ) ) : false; |
|
| 325 | + |
|
| 326 | + return $rest_url; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | 329 | } |
@@ -30,35 +30,35 @@ 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() { |
| 40 | 40 | |
| 41 | 41 | $scope = $this; |
| 42 | 42 | |
| 43 | - add_action( 'init', function () use ( $scope ) { |
|
| 44 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 43 | + add_action('init', function() use ($scope) { |
|
| 44 | + if ( ! function_exists('register_block_type')) { |
|
| 45 | 45 | // Gutenberg is not active. |
| 46 | 46 | return; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - register_block_type( 'wordlift/faceted-search', array( |
|
| 49 | + register_block_type('wordlift/faceted-search', array( |
|
| 50 | 50 | 'editor_script' => 'wl-block-editor', |
| 51 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 51 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 52 | 52 | $attr_code = ''; |
| 53 | - foreach ( $attributes as $key => $value ) { |
|
| 54 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 53 | + foreach ($attributes as $key => $value) { |
|
| 54 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 57 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 58 | 58 | }, |
| 59 | 59 | |
| 60 | 60 | 'attributes' => $scope->get_block_attributes(), |
| 61 | - ) ); |
|
| 61 | + )); |
|
| 62 | 62 | } ); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -72,17 +72,17 @@ discard block |
||
| 72 | 72 | * @since 3.20.0 |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 75 | + private function make_shortcode_atts($atts) { |
|
| 76 | 76 | |
| 77 | 77 | // Extract attributes and set default values. |
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 78 | + $shortcode_atts = shortcode_atts(array( |
|
| 79 | + 'title' => __('Related articles', 'wordlift'), |
|
| 80 | + 'limit' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 81 | 81 | 'post_id' => '', |
| 82 | 82 | 'template_id' => '', |
| 83 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 83 | + 'uniqid' => uniqid('wl-faceted-widget-'), |
|
| 84 | 84 | 'post_types' => '', |
| 85 | - ), $atts ); |
|
| 85 | + ), $atts); |
|
| 86 | 86 | |
| 87 | 87 | return $shortcode_atts; |
| 88 | 88 | } |
@@ -96,35 +96,35 @@ discard block |
||
| 96 | 96 | * @since 3.20.0 |
| 97 | 97 | * |
| 98 | 98 | */ |
| 99 | - private function web_shortcode( $atts ) { |
|
| 99 | + private function web_shortcode($atts) { |
|
| 100 | 100 | |
| 101 | 101 | // attributes extraction and boolean filtering |
| 102 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 102 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 103 | 103 | |
| 104 | 104 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 105 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 105 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 110 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 111 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 112 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 113 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 109 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 110 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 111 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 112 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 113 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 114 | 114 | |
| 115 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 116 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 117 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 118 | - $rest_url = esc_attr( $rest_url ); |
|
| 115 | + $permalink_structure = get_option('permalink_structure'); |
|
| 116 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 117 | + $rest_url = $this->get_rest_url($post, $delimiter, $limit, $shortcode_atts['post_types']); |
|
| 118 | + $rest_url = esc_attr($rest_url); |
|
| 119 | 119 | |
| 120 | 120 | |
| 121 | 121 | // avoid building the widget when no valid $rest_url |
| 122 | - if ( ! $rest_url ) { |
|
| 122 | + if ( ! $rest_url) { |
|
| 123 | 123 | return; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 127 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 126 | + wp_enqueue_script('wordlift-cloud'); |
|
| 127 | + $template_url = get_rest_url(null, '/wordlift/v1/faceted-search/template'); |
|
| 128 | 128 | |
| 129 | 129 | return <<<HTML |
| 130 | 130 | <!-- Faceted {$faceted_id} --> |
@@ -147,42 +147,42 @@ discard block |
||
| 147 | 147 | * @since 3.20.0 |
| 148 | 148 | * |
| 149 | 149 | */ |
| 150 | - private function amp_shortcode( $atts ) { |
|
| 150 | + private function amp_shortcode($atts) { |
|
| 151 | 151 | |
| 152 | 152 | // attributes extraction and boolean filtering |
| 153 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 153 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 154 | 154 | |
| 155 | 155 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 156 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 156 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 157 | 157 | return; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 161 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 162 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 163 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 164 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 160 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 161 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 162 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 163 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 164 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 165 | 165 | |
| 166 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 167 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 168 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 166 | + $permalink_structure = get_option('permalink_structure'); |
|
| 167 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 168 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array( |
|
| 169 | 169 | 'amp' => 1, |
| 170 | 170 | 'post_id' => $post->ID, |
| 171 | 171 | 'limit' => $limit |
| 172 | - ) ) ) : false; |
|
| 172 | + ))) : false; |
|
| 173 | 173 | |
| 174 | 174 | // avoid building the widget when no valid $rest_url |
| 175 | - if ( ! $rest_url ) { |
|
| 175 | + if ( ! $rest_url) { |
|
| 176 | 176 | return; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 180 | 180 | // This is a hackish way, but this works for http and https URLs |
| 181 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 181 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 182 | 182 | |
| 183 | - if ( empty( $template_id ) ) { |
|
| 184 | - $template_id = "template-" . $faceted_id; |
|
| 185 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 183 | + if (empty($template_id)) { |
|
| 184 | + $template_id = "template-".$faceted_id; |
|
| 185 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | return <<<HTML |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | return array( |
| 275 | 275 | 'title' => array( |
| 276 | 276 | 'type' => 'string', |
| 277 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 277 | + 'default' => __('Related articles', 'wordlift'), |
|
| 278 | 278 | ), |
| 279 | 279 | 'template_id' => array( |
| 280 | 280 | 'type' => 'string', |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | ), |
| 291 | 291 | 'limit' => array( |
| 292 | 292 | 'type' => 'number', |
| 293 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 293 | + 'default' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 294 | 294 | ), |
| 295 | 295 | 'preview' => array( |
| 296 | 296 | 'type' => 'boolean', |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | ), |
| 299 | 299 | 'preview_src' => array( |
| 300 | 300 | 'type' => 'string', |
| 301 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 301 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 302 | 302 | ), |
| 303 | 303 | 'post_types' => array( |
| 304 | 304 | 'type' => 'string', |
@@ -316,12 +316,12 @@ discard block |
||
| 316 | 316 | * |
| 317 | 317 | * @return bool|string |
| 318 | 318 | */ |
| 319 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 320 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 319 | + public function get_rest_url($post, $delimiter, $limit, $post_types) { |
|
| 320 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array( |
|
| 321 | 321 | 'post_id' => $post->ID, |
| 322 | 322 | 'limit' => $limit, |
| 323 | 323 | 'post_types' => $post_types |
| 324 | - ) ) ) : false; |
|
| 324 | + ))) : false; |
|
| 325 | 325 | |
| 326 | 326 | return $rest_url; |
| 327 | 327 | } |