@@ -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_attr( $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_attr( $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_attr( $rest_url ), |
|
| 147 | - esc_attr( $title ), |
|
| 148 | - esc_attr( $template_id ), |
|
| 149 | - esc_url( $template_url ) |
|
| 145 | + esc_attr($faceted_id), |
|
| 146 | + esc_attr($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, |