Completed
Push — develop ( 47dc27...bfd9be )
by Naveen
03:20
created
src/public/class-wordlift-faceted-search-shortcode.php 2 patches
Indentation   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -17,117 +17,117 @@  discard block
 block discarded – undo
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
-		$json_faceted_id = wp_json_encode( $faceted_id );
128
-		$template_url    = get_rest_url( null, '/wordlift/v1/faceted-search/template' );
129
-
130
-		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
+        $json_faceted_id = wp_json_encode( $faceted_id );
128
+        $template_url    = get_rest_url( null, '/wordlift/v1/faceted-search/template' );
129
+
130
+        return <<<HTML
131 131
 			<!-- Faceted {$faceted_id} -->
132 132
 			<script type="application/javascript">
133 133
 				window.wlFaceteds = window.wlFaceteds || []; wlFaceteds.push({$json_faceted_id});
@@ -140,58 +140,58 @@  discard block
 block discarded – undo
140 140
 				 data-template-url="{$template_url}"></div>
141 141
 			<!-- /Faceted {$faceted_id} -->
142 142
 HTML;
143
-	}
144
-
145
-	/**
146
-	 * Function in charge of diplaying the [wl-faceted-search] in amp mode.
147
-	 *
148
-	 * @param array $atts Shortcode attributes.
149
-	 *
150
-	 * @return string Shortcode HTML for amp
151
-	 * @since 3.20.0
152
-	 *
153
-	 */
154
-	private function amp_shortcode( $atts ) {
155
-
156
-		// attributes extraction and boolean filtering
157
-		$shortcode_atts = $this->make_shortcode_atts( $atts );
158
-
159
-		// avoid building the widget when no post_id is specified and there is a list of posts.
160
-		if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
161
-			return;
162
-		}
163
-
164
-		$post        = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post();
165
-		$title       = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
166
-		$template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
167
-		$limit       = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
168
-		$faceted_id  = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' );
169
-
170
-		$permalink_structure = get_option( 'permalink_structure' );
171
-		$delimiter           = empty( $permalink_structure ) ? '&' : '?';
172
-		$rest_url            = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array(
173
-				'amp',
174
-				'post_id' => $post->ID,
175
-				'limit'   => $limit
176
-			) ) ) : false;
177
-
178
-		$rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false;
179
-
180
-		// avoid building the widget when no valid $rest_url
181
-		if ( ! $rest_url ) {
182
-			return;
183
-		}
184
-
185
-		// Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS.
186
-		// This is a hackish way, but this works for http and https URLs
187
-		$rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url );
188
-
189
-		if ( empty( $template_id ) ) {
190
-			$template_id = "template-" . $faceted_id;
191
-			wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' );
192
-		}
193
-
194
-		return <<<HTML
143
+    }
144
+
145
+    /**
146
+     * Function in charge of diplaying the [wl-faceted-search] in amp mode.
147
+     *
148
+     * @param array $atts Shortcode attributes.
149
+     *
150
+     * @return string Shortcode HTML for amp
151
+     * @since 3.20.0
152
+     *
153
+     */
154
+    private function amp_shortcode( $atts ) {
155
+
156
+        // attributes extraction and boolean filtering
157
+        $shortcode_atts = $this->make_shortcode_atts( $atts );
158
+
159
+        // avoid building the widget when no post_id is specified and there is a list of posts.
160
+        if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
161
+            return;
162
+        }
163
+
164
+        $post        = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post();
165
+        $title       = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
166
+        $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
167
+        $limit       = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
168
+        $faceted_id  = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' );
169
+
170
+        $permalink_structure = get_option( 'permalink_structure' );
171
+        $delimiter           = empty( $permalink_structure ) ? '&' : '?';
172
+        $rest_url            = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array(
173
+                'amp',
174
+                'post_id' => $post->ID,
175
+                'limit'   => $limit
176
+            ) ) ) : false;
177
+
178
+        $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false;
179
+
180
+        // avoid building the widget when no valid $rest_url
181
+        if ( ! $rest_url ) {
182
+            return;
183
+        }
184
+
185
+        // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS.
186
+        // This is a hackish way, but this works for http and https URLs
187
+        $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url );
188
+
189
+        if ( empty( $template_id ) ) {
190
+            $template_id = "template-" . $faceted_id;
191
+            wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' );
192
+        }
193
+
194
+        return <<<HTML
195 195
 		<div id="{$faceted_id}" class="wl-amp-faceted">
196 196
 			<h2 class="wl-headline">{$title}</h2>
197 197
 			<amp-state id="referencedPosts">
@@ -274,62 +274,62 @@  discard block
 block discarded – undo
274 274
 			</section>
275 275
 		</div>
276 276
 HTML;
277
-	}
278
-
279
-	public function get_block_attributes() {
280
-		return array(
281
-			'title'       => array(
282
-				'type'    => 'string',
283
-				'default' => __( 'Related articles', 'wordlift' ),
284
-			),
285
-			'template_id' => array(
286
-				'type'    => 'string',
287
-				'default' => '',
288
-			),
289
-			'post_id'     => array(
290
-				'type'    => 'number',
291
-				'default' => '',
292
-			),
293
-			'uniqid'      => array(
294
-				'type'    => 'string',
295
-				'default' => '',
296
-			),
297
-			'limit'       => array(
298
-				'type'    => 'number',
299
-				'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ),
300
-			),
301
-			'preview'     => array(
302
-				'type'    => 'boolean',
303
-				'default' => false,
304
-			),
305
-			'preview_src' => array(
306
-				'type'    => 'string',
307
-				'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png',
308
-			),
309
-			'post_types'  => array(
310
-				'type'    => 'string',
311
-				'default' => '',
312
-			)
313
-		);
314
-	}
315
-
316
-	/**
317
-	 * @param $post
318
-	 * @param $delimiter
319
-	 * @param $limit
320
-	 *
321
-	 * @param $post_types
322
-	 *
323
-	 * @return bool|string
324
-	 */
325
-	public function get_rest_url( $post, $delimiter, $limit, $post_types ) {
326
-		$rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array(
327
-				'post_id'    => $post->ID,
328
-				'limit'      => $limit,
329
-				'post_types' => $post_types
330
-			) ) ) : false;
331
-
332
-		return $rest_url;
333
-	}
277
+    }
278
+
279
+    public function get_block_attributes() {
280
+        return array(
281
+            'title'       => array(
282
+                'type'    => 'string',
283
+                'default' => __( 'Related articles', 'wordlift' ),
284
+            ),
285
+            'template_id' => array(
286
+                'type'    => 'string',
287
+                'default' => '',
288
+            ),
289
+            'post_id'     => array(
290
+                'type'    => 'number',
291
+                'default' => '',
292
+            ),
293
+            'uniqid'      => array(
294
+                'type'    => 'string',
295
+                'default' => '',
296
+            ),
297
+            'limit'       => array(
298
+                'type'    => 'number',
299
+                'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ),
300
+            ),
301
+            'preview'     => array(
302
+                'type'    => 'boolean',
303
+                'default' => false,
304
+            ),
305
+            'preview_src' => array(
306
+                'type'    => 'string',
307
+                'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png',
308
+            ),
309
+            'post_types'  => array(
310
+                'type'    => 'string',
311
+                'default' => '',
312
+            )
313
+        );
314
+    }
315
+
316
+    /**
317
+     * @param $post
318
+     * @param $delimiter
319
+     * @param $limit
320
+     *
321
+     * @param $post_types
322
+     *
323
+     * @return bool|string
324
+     */
325
+    public function get_rest_url( $post, $delimiter, $limit, $post_types ) {
326
+        $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array(
327
+                'post_id'    => $post->ID,
328
+                'limit'      => $limit,
329
+                'post_types' => $post_types
330
+            ) ) ) : false;
331
+
332
+        return $rest_url;
333
+    }
334 334
 
335 335
 }
Please login to merge, or discard this patch.
Spacing   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -30,35 +30,35 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,36 +96,36 @@  discard block
 block discarded – undo
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
-		$json_faceted_id = wp_json_encode( $faceted_id );
128
-		$template_url    = get_rest_url( null, '/wordlift/v1/faceted-search/template' );
126
+		wp_enqueue_script('wordlift-cloud');
127
+		$json_faceted_id = wp_json_encode($faceted_id);
128
+		$template_url    = get_rest_url(null, '/wordlift/v1/faceted-search/template');
129 129
 
130 130
 		return <<<HTML
131 131
 			<!-- Faceted {$faceted_id} -->
@@ -151,44 +151,44 @@  discard block
 block discarded – undo
151 151
 	 * @since 3.20.0
152 152
 	 *
153 153
 	 */
154
-	private function amp_shortcode( $atts ) {
154
+	private function amp_shortcode($atts) {
155 155
 
156 156
 		// attributes extraction and boolean filtering
157
-		$shortcode_atts = $this->make_shortcode_atts( $atts );
157
+		$shortcode_atts = $this->make_shortcode_atts($atts);
158 158
 
159 159
 		// avoid building the widget when no post_id is specified and there is a list of posts.
160
-		if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
160
+		if (empty($shortcode_atts['post_id']) && ! is_singular()) {
161 161
 			return;
162 162
 		}
163 163
 
164
-		$post        = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post();
165
-		$title       = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
166
-		$template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
167
-		$limit       = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
168
-		$faceted_id  = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' );
164
+		$post        = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post();
165
+		$title       = esc_attr(sanitize_text_field($shortcode_atts['title']));
166
+		$template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id']));
167
+		$limit       = esc_attr(sanitize_text_field($shortcode_atts['limit']));
168
+		$faceted_id  = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-');
169 169
 
170
-		$permalink_structure = get_option( 'permalink_structure' );
171
-		$delimiter           = empty( $permalink_structure ) ? '&' : '?';
172
-		$rest_url            = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array(
170
+		$permalink_structure = get_option('permalink_structure');
171
+		$delimiter           = empty($permalink_structure) ? '&' : '?';
172
+		$rest_url            = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array(
173 173
 				'amp',
174 174
 				'post_id' => $post->ID,
175 175
 				'limit'   => $limit
176
-			) ) ) : false;
176
+			))) : false;
177 177
 
178
-		$rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false;
178
+		$rest_url = $post ? rest_url(sprintf("wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit)) : false;
179 179
 
180 180
 		// avoid building the widget when no valid $rest_url
181
-		if ( ! $rest_url ) {
181
+		if ( ! $rest_url) {
182 182
 			return;
183 183
 		}
184 184
 
185 185
 		// Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS.
186 186
 		// This is a hackish way, but this works for http and https URLs
187
-		$rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url );
187
+		$rest_url = str_replace(array('http:', 'https:'), '', $rest_url);
188 188
 
189
-		if ( empty( $template_id ) ) {
190
-			$template_id = "template-" . $faceted_id;
191
-			wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' );
189
+		if (empty($template_id)) {
190
+			$template_id = "template-".$faceted_id;
191
+			wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css');
192 192
 		}
193 193
 
194 194
 		return <<<HTML
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
 		return array(
281 281
 			'title'       => array(
282 282
 				'type'    => 'string',
283
-				'default' => __( 'Related articles', 'wordlift' ),
283
+				'default' => __('Related articles', 'wordlift'),
284 284
 			),
285 285
 			'template_id' => array(
286 286
 				'type'    => 'string',
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
 			),
297 297
 			'limit'       => array(
298 298
 				'type'    => 'number',
299
-				'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ),
299
+				'default' => apply_filters('wl_faceted_search_default_limit', 10),
300 300
 			),
301 301
 			'preview'     => array(
302 302
 				'type'    => 'boolean',
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
 			),
305 305
 			'preview_src' => array(
306 306
 				'type'    => 'string',
307
-				'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png',
307
+				'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/faceted-search.png',
308 308
 			),
309 309
 			'post_types'  => array(
310 310
 				'type'    => 'string',
@@ -322,12 +322,12 @@  discard block
 block discarded – undo
322 322
 	 *
323 323
 	 * @return bool|string
324 324
 	 */
325
-	public function get_rest_url( $post, $delimiter, $limit, $post_types ) {
326
-		$rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array(
325
+	public function get_rest_url($post, $delimiter, $limit, $post_types) {
326
+		$rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array(
327 327
 				'post_id'    => $post->ID,
328 328
 				'limit'      => $limit,
329 329
 				'post_types' => $post_types
330
-			) ) ) : false;
330
+			))) : false;
331 331
 
332 332
 		return $rest_url;
333 333
 	}
Please login to merge, or discard this patch.
src/public/class-wordlift-navigator-shortcode.php 2 patches
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -17,124 +17,124 @@  discard block
 block discarded – undo
17 17
  */
18 18
 class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode {
19 19
 
20
-	/**
21
-	 * {@inheritdoc}
22
-	 */
23
-	const SHORTCODE = 'wl_navigator';
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/navigator', 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
-				'attributes'      => $scope->get_navigator_block_attributes(),
60
-			) );
61
-		} );
62
-	}
63
-
64
-	/**
65
-	 * Shared function used by web_shortcode and amp_shortcode
66
-	 * Bootstrap logic for attributes extraction and boolean filtering
67
-	 *
68
-	 * @param array $atts Shortcode attributes.
69
-	 *
70
-	 * @return array $shortcode_atts
71
-	 * @since      3.20.0
72
-	 *
73
-	 */
74
-	private function make_shortcode_atts( $atts ) {
75
-
76
-		// Extract attributes and set default values.
77
-		$shortcode_atts = shortcode_atts( array(
78
-			'title'       => __( 'Related articles', 'wordlift' ),
79
-			'limit'       => 4,
80
-			'offset'      => 0,
81
-			'template_id' => '',
82
-			'post_id'     => '',
83
-			'uniqid'      => uniqid( 'wl-navigator-widget-' ),
84
-			'order_by'    => 'ID DESC',
85
-			'post_types'  => '',
86
-		), $atts );
87
-
88
-		return $shortcode_atts;
89
-	}
90
-
91
-	/**
92
-	 * Function in charge of displaying the [wl-navigator] in web mode.
93
-	 *
94
-	 * @param array $atts Shortcode attributes.
95
-	 *
96
-	 * @return string Shortcode HTML for web
97
-	 * @since 3.20.0
98
-	 *
99
-	 */
100
-	private function web_shortcode( $atts ) {
101
-
102
-		// attributes extraction and boolean filtering
103
-		$shortcode_atts = $this->make_shortcode_atts( $atts );
104
-
105
-		// avoid building the widget when no post_id is specified and there is a list of posts.
106
-		if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
107
-			return;
108
-		}
109
-
110
-		$post         = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post();
111
-		$title        = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
112
-		$template_id  = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
113
-		$limit        = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
114
-		$offset       = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) );
115
-		$sort         = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) );
116
-		$navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' );
117
-
118
-		$rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array(
119
-				'action'  => 'wl_navigator',
120
-				'uniqid'  => $navigator_id,
121
-				'post_id' => $post->ID,
122
-				'limit'   => $limit,
123
-				'offset'  => $offset,
124
-				'sort'    => $sort,
125
-				'post_types' => $shortcode_atts['post_types']
126
-			) ) ) : false;
127
-
128
-		// avoid building the widget when no valid $rest_url
129
-		if ( ! $rest_url ) {
130
-			return;
131
-		}
132
-
133
-		wp_enqueue_script( 'wordlift-cloud' );
134
-		$json_navigator_id = wp_json_encode( $navigator_id );
135
-		$template_url      = get_rest_url( null, '/wordlift/v1/navigator/template' );
136
-
137
-		return <<<HTML
20
+    /**
21
+     * {@inheritdoc}
22
+     */
23
+    const SHORTCODE = 'wl_navigator';
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/navigator', 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
+                'attributes'      => $scope->get_navigator_block_attributes(),
60
+            ) );
61
+        } );
62
+    }
63
+
64
+    /**
65
+     * Shared function used by web_shortcode and amp_shortcode
66
+     * Bootstrap logic for attributes extraction and boolean filtering
67
+     *
68
+     * @param array $atts Shortcode attributes.
69
+     *
70
+     * @return array $shortcode_atts
71
+     * @since      3.20.0
72
+     *
73
+     */
74
+    private function make_shortcode_atts( $atts ) {
75
+
76
+        // Extract attributes and set default values.
77
+        $shortcode_atts = shortcode_atts( array(
78
+            'title'       => __( 'Related articles', 'wordlift' ),
79
+            'limit'       => 4,
80
+            'offset'      => 0,
81
+            'template_id' => '',
82
+            'post_id'     => '',
83
+            'uniqid'      => uniqid( 'wl-navigator-widget-' ),
84
+            'order_by'    => 'ID DESC',
85
+            'post_types'  => '',
86
+        ), $atts );
87
+
88
+        return $shortcode_atts;
89
+    }
90
+
91
+    /**
92
+     * Function in charge of displaying the [wl-navigator] in web mode.
93
+     *
94
+     * @param array $atts Shortcode attributes.
95
+     *
96
+     * @return string Shortcode HTML for web
97
+     * @since 3.20.0
98
+     *
99
+     */
100
+    private function web_shortcode( $atts ) {
101
+
102
+        // attributes extraction and boolean filtering
103
+        $shortcode_atts = $this->make_shortcode_atts( $atts );
104
+
105
+        // avoid building the widget when no post_id is specified and there is a list of posts.
106
+        if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
107
+            return;
108
+        }
109
+
110
+        $post         = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post();
111
+        $title        = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
112
+        $template_id  = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
113
+        $limit        = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
114
+        $offset       = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) );
115
+        $sort         = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) );
116
+        $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' );
117
+
118
+        $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array(
119
+                'action'  => 'wl_navigator',
120
+                'uniqid'  => $navigator_id,
121
+                'post_id' => $post->ID,
122
+                'limit'   => $limit,
123
+                'offset'  => $offset,
124
+                'sort'    => $sort,
125
+                'post_types' => $shortcode_atts['post_types']
126
+            ) ) ) : false;
127
+
128
+        // avoid building the widget when no valid $rest_url
129
+        if ( ! $rest_url ) {
130
+            return;
131
+        }
132
+
133
+        wp_enqueue_script( 'wordlift-cloud' );
134
+        $json_navigator_id = wp_json_encode( $navigator_id );
135
+        $template_url      = get_rest_url( null, '/wordlift/v1/navigator/template' );
136
+
137
+        return <<<HTML
138 138
 			<!-- Navigator {$navigator_id} -->
139 139
 			<script type="application/javascript">
140 140
 				window.wlNavigators = window.wlNavigators || []; wlNavigators.push({$json_navigator_id});
@@ -148,60 +148,60 @@  discard block
 block discarded – undo
148 148
 				 data-limit="{$limit}"></div>
149 149
 			<!-- /Navigator {$navigator_id} -->
150 150
 HTML;
151
-	}
152
-
153
-	/**
154
-	 * Function in charge of diplaying the [wl-faceted-search] in amp mode.
155
-	 *
156
-	 * @param array $atts Shortcode attributes.
157
-	 *
158
-	 * @return string Shortcode HTML for amp
159
-	 * @since 3.20.0
160
-	 *
161
-	 */
162
-	private function amp_shortcode( $atts ) {
163
-
164
-		// attributes extraction and boolean filtering
165
-		$shortcode_atts = $this->make_shortcode_atts( $atts );
166
-
167
-		// avoid building the widget when no post_id is specified and there is a list of posts.
168
-		if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
169
-			return;
170
-		}
171
-
172
-		$post         = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post();
173
-		$title        = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
174
-		$template_id  = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
175
-		$limit        = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
176
-		$offset       = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) );
177
-		$sort         = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) );
178
-		$navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' );
179
-
180
-		$permalink_structure = get_option( 'permalink_structure' );
181
-		$delimiter           = empty( $permalink_structure ) ? '&' : '?';
182
-		$rest_url            = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array(
183
-				'uniqid'  => $navigator_id,
184
-				'post_id' => $post->ID,
185
-				'limit'   => $limit,
186
-				'offset'  => $offset,
187
-				'sort'    => $sort
188
-			) ) ) : false;
189
-
190
-		// avoid building the widget when no valid $rest_url
191
-		if ( ! $rest_url ) {
192
-			return;
193
-		}
194
-
195
-		// Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS.
196
-		// This is a hackish way, but this works for http and https URLs
197
-		$rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url );
198
-
199
-		if ( empty( $template_id ) ) {
200
-			$template_id = "template-" . $navigator_id;
201
-			wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' );
202
-		}
203
-
204
-		return <<<HTML
151
+    }
152
+
153
+    /**
154
+     * Function in charge of diplaying the [wl-faceted-search] in amp mode.
155
+     *
156
+     * @param array $atts Shortcode attributes.
157
+     *
158
+     * @return string Shortcode HTML for amp
159
+     * @since 3.20.0
160
+     *
161
+     */
162
+    private function amp_shortcode( $atts ) {
163
+
164
+        // attributes extraction and boolean filtering
165
+        $shortcode_atts = $this->make_shortcode_atts( $atts );
166
+
167
+        // avoid building the widget when no post_id is specified and there is a list of posts.
168
+        if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
169
+            return;
170
+        }
171
+
172
+        $post         = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post();
173
+        $title        = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
174
+        $template_id  = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
175
+        $limit        = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
176
+        $offset       = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) );
177
+        $sort         = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) );
178
+        $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' );
179
+
180
+        $permalink_structure = get_option( 'permalink_structure' );
181
+        $delimiter           = empty( $permalink_structure ) ? '&' : '?';
182
+        $rest_url            = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array(
183
+                'uniqid'  => $navigator_id,
184
+                'post_id' => $post->ID,
185
+                'limit'   => $limit,
186
+                'offset'  => $offset,
187
+                'sort'    => $sort
188
+            ) ) ) : false;
189
+
190
+        // avoid building the widget when no valid $rest_url
191
+        if ( ! $rest_url ) {
192
+            return;
193
+        }
194
+
195
+        // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS.
196
+        // This is a hackish way, but this works for http and https URLs
197
+        $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url );
198
+
199
+        if ( empty( $template_id ) ) {
200
+            $template_id = "template-" . $navigator_id;
201
+            wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' );
202
+        }
203
+
204
+        return <<<HTML
205 205
 		<div id="{$navigator_id}" class="wl-amp-navigator" style="width: 100%">
206 206
 			<h3 class="wl-headline">{$title}</h3>
207 207
 			<amp-list 
@@ -233,54 +233,54 @@  discard block
 block discarded – undo
233 233
 			</div>
234 234
 		</template>
235 235
 HTML;
236
-	}
237
-
238
-	/**
239
-	 * @return array
240
-	 */
241
-	public function get_navigator_block_attributes() {
242
-		return array(
243
-			'title'       => array(
244
-				'type'    => 'string',
245
-				'default' => __( 'Related articles', 'wordlift' ),
246
-			),
247
-			'limit'       => array(
248
-				'type'    => 'number',
249
-				'default' => 4,
250
-			),
251
-			'template_id' => array(
252
-				'type'    => 'string',
253
-				'default' => '',
254
-			),
255
-			'post_id'     => array(
256
-				'type'    => 'number',
257
-				'default' => '',
258
-			),
259
-			'offset'      => array(
260
-				'type'    => 'number',
261
-				'default' => 0,
262
-			),
263
-			'uniqid'      => array(
264
-				'type'    => 'string',
265
-				'default' => '',
266
-			),
267
-			'order_by'    => array(
268
-				'type'    => 'string',
269
-				'default' => 'ID DESC',
270
-			),
271
-			'preview'     => array(
272
-				'type'    => 'boolean',
273
-				'default' => false,
274
-			),
275
-			'preview_src' => array(
276
-				'type'    => 'string',
277
-				'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png',
278
-			),
279
-			'post_types'  => array(
280
-				'type'    => 'string',
281
-				'default' => '',
282
-			)
283
-		);
284
-	}
236
+    }
237
+
238
+    /**
239
+     * @return array
240
+     */
241
+    public function get_navigator_block_attributes() {
242
+        return array(
243
+            'title'       => array(
244
+                'type'    => 'string',
245
+                'default' => __( 'Related articles', 'wordlift' ),
246
+            ),
247
+            'limit'       => array(
248
+                'type'    => 'number',
249
+                'default' => 4,
250
+            ),
251
+            'template_id' => array(
252
+                'type'    => 'string',
253
+                'default' => '',
254
+            ),
255
+            'post_id'     => array(
256
+                'type'    => 'number',
257
+                'default' => '',
258
+            ),
259
+            'offset'      => array(
260
+                'type'    => 'number',
261
+                'default' => 0,
262
+            ),
263
+            'uniqid'      => array(
264
+                'type'    => 'string',
265
+                'default' => '',
266
+            ),
267
+            'order_by'    => array(
268
+                'type'    => 'string',
269
+                'default' => 'ID DESC',
270
+            ),
271
+            'preview'     => array(
272
+                'type'    => 'boolean',
273
+                'default' => false,
274
+            ),
275
+            'preview_src' => array(
276
+                'type'    => 'string',
277
+                'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png',
278
+            ),
279
+            'post_types'  => array(
280
+                'type'    => 'string',
281
+                'default' => '',
282
+            )
283
+        );
284
+    }
285 285
 
286 286
 }
Please login to merge, or discard this patch.
Spacing   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -30,34 +30,34 @@  discard block
 block discarded – undo
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/navigator', array(
49
+			register_block_type('wordlift/navigator', 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
 				'attributes'      => $scope->get_navigator_block_attributes(),
60
-			) );
60
+			));
61 61
 		} );
62 62
 	}
63 63
 
@@ -71,19 +71,19 @@  discard block
 block discarded – undo
71 71
 	 * @since      3.20.0
72 72
 	 *
73 73
 	 */
74
-	private function make_shortcode_atts( $atts ) {
74
+	private function make_shortcode_atts($atts) {
75 75
 
76 76
 		// Extract attributes and set default values.
77
-		$shortcode_atts = shortcode_atts( array(
78
-			'title'       => __( 'Related articles', 'wordlift' ),
77
+		$shortcode_atts = shortcode_atts(array(
78
+			'title'       => __('Related articles', 'wordlift'),
79 79
 			'limit'       => 4,
80 80
 			'offset'      => 0,
81 81
 			'template_id' => '',
82 82
 			'post_id'     => '',
83
-			'uniqid'      => uniqid( 'wl-navigator-widget-' ),
83
+			'uniqid'      => uniqid('wl-navigator-widget-'),
84 84
 			'order_by'    => 'ID DESC',
85 85
 			'post_types'  => '',
86
-		), $atts );
86
+		), $atts);
87 87
 
88 88
 		return $shortcode_atts;
89 89
 	}
@@ -97,25 +97,25 @@  discard block
 block discarded – undo
97 97
 	 * @since 3.20.0
98 98
 	 *
99 99
 	 */
100
-	private function web_shortcode( $atts ) {
100
+	private function web_shortcode($atts) {
101 101
 
102 102
 		// attributes extraction and boolean filtering
103
-		$shortcode_atts = $this->make_shortcode_atts( $atts );
103
+		$shortcode_atts = $this->make_shortcode_atts($atts);
104 104
 
105 105
 		// avoid building the widget when no post_id is specified and there is a list of posts.
106
-		if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
106
+		if (empty($shortcode_atts['post_id']) && ! is_singular()) {
107 107
 			return;
108 108
 		}
109 109
 
110
-		$post         = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post();
111
-		$title        = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
112
-		$template_id  = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
113
-		$limit        = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
114
-		$offset       = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) );
115
-		$sort         = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) );
116
-		$navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' );
110
+		$post         = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post();
111
+		$title        = esc_attr(sanitize_text_field($shortcode_atts['title']));
112
+		$template_id  = esc_attr(sanitize_text_field($shortcode_atts['template_id']));
113
+		$limit        = esc_attr(sanitize_text_field($shortcode_atts['limit']));
114
+		$offset       = esc_attr(sanitize_text_field($shortcode_atts['offset']));
115
+		$sort         = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by'])));
116
+		$navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-navigator-widget-');
117 117
 
118
-		$rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array(
118
+		$rest_url = $post ? admin_url('admin-ajax.php?'.build_query(array(
119 119
 				'action'  => 'wl_navigator',
120 120
 				'uniqid'  => $navigator_id,
121 121
 				'post_id' => $post->ID,
@@ -123,16 +123,16 @@  discard block
 block discarded – undo
123 123
 				'offset'  => $offset,
124 124
 				'sort'    => $sort,
125 125
 				'post_types' => $shortcode_atts['post_types']
126
-			) ) ) : false;
126
+			))) : false;
127 127
 
128 128
 		// avoid building the widget when no valid $rest_url
129
-		if ( ! $rest_url ) {
129
+		if ( ! $rest_url) {
130 130
 			return;
131 131
 		}
132 132
 
133
-		wp_enqueue_script( 'wordlift-cloud' );
134
-		$json_navigator_id = wp_json_encode( $navigator_id );
135
-		$template_url      = get_rest_url( null, '/wordlift/v1/navigator/template' );
133
+		wp_enqueue_script('wordlift-cloud');
134
+		$json_navigator_id = wp_json_encode($navigator_id);
135
+		$template_url      = get_rest_url(null, '/wordlift/v1/navigator/template');
136 136
 
137 137
 		return <<<HTML
138 138
 			<!-- Navigator {$navigator_id} -->
@@ -159,46 +159,46 @@  discard block
 block discarded – undo
159 159
 	 * @since 3.20.0
160 160
 	 *
161 161
 	 */
162
-	private function amp_shortcode( $atts ) {
162
+	private function amp_shortcode($atts) {
163 163
 
164 164
 		// attributes extraction and boolean filtering
165
-		$shortcode_atts = $this->make_shortcode_atts( $atts );
165
+		$shortcode_atts = $this->make_shortcode_atts($atts);
166 166
 
167 167
 		// avoid building the widget when no post_id is specified and there is a list of posts.
168
-		if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) {
168
+		if (empty($shortcode_atts['post_id']) && ! is_singular()) {
169 169
 			return;
170 170
 		}
171 171
 
172
-		$post         = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post();
173
-		$title        = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) );
174
-		$template_id  = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) );
175
-		$limit        = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) );
176
-		$offset       = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) );
177
-		$sort         = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) );
178
-		$navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' );
179
-
180
-		$permalink_structure = get_option( 'permalink_structure' );
181
-		$delimiter           = empty( $permalink_structure ) ? '&' : '?';
182
-		$rest_url            = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array(
172
+		$post         = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post();
173
+		$title        = esc_attr(sanitize_text_field($shortcode_atts['title']));
174
+		$template_id  = esc_attr(sanitize_text_field($shortcode_atts['template_id']));
175
+		$limit        = esc_attr(sanitize_text_field($shortcode_atts['limit']));
176
+		$offset       = esc_attr(sanitize_text_field($shortcode_atts['offset']));
177
+		$sort         = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by'])));
178
+		$navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-navigator-widget-');
179
+
180
+		$permalink_structure = get_option('permalink_structure');
181
+		$delimiter           = empty($permalink_structure) ? '&' : '?';
182
+		$rest_url            = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/navigator'.$delimiter.build_query(array(
183 183
 				'uniqid'  => $navigator_id,
184 184
 				'post_id' => $post->ID,
185 185
 				'limit'   => $limit,
186 186
 				'offset'  => $offset,
187 187
 				'sort'    => $sort
188
-			) ) ) : false;
188
+			))) : false;
189 189
 
190 190
 		// avoid building the widget when no valid $rest_url
191
-		if ( ! $rest_url ) {
191
+		if ( ! $rest_url) {
192 192
 			return;
193 193
 		}
194 194
 
195 195
 		// Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS.
196 196
 		// This is a hackish way, but this works for http and https URLs
197
-		$rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url );
197
+		$rest_url = str_replace(array('http:', 'https:'), '', $rest_url);
198 198
 
199
-		if ( empty( $template_id ) ) {
200
-			$template_id = "template-" . $navigator_id;
201
-			wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' );
199
+		if (empty($template_id)) {
200
+			$template_id = "template-".$navigator_id;
201
+			wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css');
202 202
 		}
203 203
 
204 204
 		return <<<HTML
@@ -242,7 +242,7 @@  discard block
 block discarded – undo
242 242
 		return array(
243 243
 			'title'       => array(
244 244
 				'type'    => 'string',
245
-				'default' => __( 'Related articles', 'wordlift' ),
245
+				'default' => __('Related articles', 'wordlift'),
246 246
 			),
247 247
 			'limit'       => array(
248 248
 				'type'    => 'number',
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
 			),
275 275
 			'preview_src' => array(
276 276
 				'type'    => 'string',
277
-				'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png',
277
+				'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/navigator.png',
278 278
 			),
279 279
 			'post_types'  => array(
280 280
 				'type'    => 'string',
Please login to merge, or discard this patch.