Completed
Pull Request — master (#1385)
by Naveen
03:21
created
src/shortcodes/wordlift_shortcode_faceted_search.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -262,6 +262,9 @@
 block discarded – undo
262 262
 
263 263
 }
264 264
 
265
+/**
266
+ * @return string
267
+ */
265 268
 function wl_shortcode_faceted_search_get_the_title( $post_id ) {
266 269
 
267 270
 	$title = get_the_title( $post_id );
Please login to merge, or discard this patch.
Indentation   +208 added lines, -208 removed lines patch added patch discarded remove patch
@@ -20,33 +20,33 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_shortcode_faceted_search( $request ) {
22 22
 
23
-	// Create the cache key.
24
-	$cache_key = array(
25
-		'request_params' => $_GET,
26
-	);
23
+    // Create the cache key.
24
+    $cache_key = array(
25
+        'request_params' => $_GET,
26
+    );
27 27
 
28
-	// Create the TTL cache and try to get the results.
29
-	$cache         = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours.
30
-	$cache_results = $cache->get( $cache_key );
28
+    // Create the TTL cache and try to get the results.
29
+    $cache         = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours.
30
+    $cache_results = $cache->get( $cache_key );
31 31
 
32
-	// So that the endpoint can be used remotely
33
-	header( 'Access-Control-Allow-Origin: *' );
32
+    // So that the endpoint can be used remotely
33
+    header( 'Access-Control-Allow-Origin: *' );
34 34
 
35
-	if ( isset( $cache_results ) ) {
36
-		header( 'X-WordLift-Cache: HIT' );
37
-		wl_core_send_json( $cache_results );
35
+    if ( isset( $cache_results ) ) {
36
+        header( 'X-WordLift-Cache: HIT' );
37
+        wl_core_send_json( $cache_results );
38 38
 
39
-		return;
40
-	}
39
+        return;
40
+    }
41 41
 
42
-	header( 'X-WordLift-Cache: MISS' );
42
+    header( 'X-WordLift-Cache: MISS' );
43 43
 
44
-	$results = wl_shortcode_faceted_search_origin( $request );
44
+    $results = wl_shortcode_faceted_search_origin( $request );
45 45
 
46
-	// Put the result before sending the json to the client, since sending the json will terminate us.
47
-	$cache->put( $cache_key, $results );
46
+    // Put the result before sending the json to the client, since sending the json will terminate us.
47
+    $cache->put( $cache_key, $results );
48 48
 
49
-	wl_core_send_json( $results );
49
+    wl_core_send_json( $results );
50 50
 
51 51
 }
52 52
 
@@ -57,151 +57,151 @@  discard block
 block discarded – undo
57 57
  * @since        3.26.0
58 58
  */
59 59
 function wl_shortcode_faceted_search_origin( $request ) {
60
-	// Post ID must be defined.
61
-	if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
62
-		wp_die( 'No post_id given' );
63
-
64
-		return;
65
-	}
66
-
67
-	$current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
68
-	$current_post    = get_post( $current_post_id );
69
-	$faceted_id      = $_GET['uniqid'];
70
-
71
-	$post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
72
-	$post_types          = explode( ',', $post_types );
73
-	$existing_post_types = get_post_types();
74
-	$post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
75
-
76
-
77
-	// Post ID has to match an existing item.
78
-	if ( null === $current_post ) {
79
-		wp_die( 'No valid post_id given' );
80
-
81
-		return;
82
-	}
83
-
84
-	// If the current post is an entity,
85
-	// the current post is used as main entity.
86
-	// Otherwise, current post related entities are used.
87
-	$entity_service = Wordlift_Entity_Service::get_instance();
88
-
89
-	$entity_ids = $entity_service->is_entity( $current_post->ID ) ?
90
-		array( $current_post->ID ) :
91
-		$entity_service->get_related_entities( $current_post->ID );
92
-
93
-	// If there are no entities we cannot render the widget.
94
-	if ( 0 === count( $entity_ids ) ) {
95
-		/**
96
-		 * If this function is not called from ajax
97
-		 * then this should not throw an error.
98
-		 * Note: Used in scripbox longtail project on json endpoint.
99
-		 */
100
-		if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
101
-			wp_die( 'No entities available' );
102
-		}
103
-
104
-	}
105
-
106
-	$limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4;  // WPCS: input var ok; CSRF ok.
107
-	$amp   = ( isset( $_GET['amp'] ) ) ? true : false;
108
-
109
-
110
-	/**
111
-	 * see https://github.com/insideout10/wordlift-plugin/issues/1181
112
-	 * The ordering should be descending by date on default.
113
-	 */
114
-	$order_by = 'DESC';
115
-	if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) {
116
-		$order_by = (string) $_GET['sort'];
117
-	}
118
-
119
-
120
-	$referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
121
-		$entity_ids,
122
-		'*',
123
-		null,
124
-		'publish',
125
-		array( $current_post_id ),
126
-		$limit,
127
-		null,
128
-		$order_by,
129
-		$post_types
130
-	);
131
-
132
-
133
-	$referencing_post_ids = array_map( function ( $p ) {
134
-		return $p->ID;
135
-	}, $referencing_posts );
136
-
137
-	$post_results   = array();
138
-	$entity_results = array();
139
-
140
-	// Populate $post_results
141
-
142
-	$filtered_posts = ( empty( $filtering_entity_uris ) ) ?
143
-		$referencing_posts :
144
-		Wordlift_Relation_Service::get_instance()->get_article_subjects(
145
-			wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
146
-			'*',
147
-			null,
148
-			null,
149
-			array(),
150
-			null,
151
-			$referencing_post_ids
152
-		);
153
-
154
-	if ( $filtered_posts ) {
155
-		foreach ( $filtered_posts as $post_obj ) {
156
-
157
-			/**
158
-			 * Use the thumbnail.
159
-			 *
160
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
161
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/837
162
-			 *
163
-			 * @since 3.19.3 We're using the medium size image.
164
-			 */
165
-			$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
166
-			$post_obj->thumbnail = ( $thumbnail ) ?
167
-				$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
168
-			$post_obj->permalink = get_permalink( $post_obj->ID );
169
-			$post_obj->srcset    = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
170
-
171
-			$result         = $post_obj;
172
-			$post_results[] = $result;
173
-		}
174
-	}
175
-
176
-	// Add filler posts if needed
177
-
178
-	$filler_count = $limit - count( $post_results );
179
-	if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) {
180
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id );
181
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
182
-		$filler_posts            = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
183
-
184
-		$post_results = array_merge( $post_results, $filler_posts );
185
-	}
186
-	$referencing_post_ids = array_map( function ( $post ) {
187
-		return $post->ID;
188
-	}, $post_results );
189
-
190
-	// Populate $entity_results
191
-
192
-	global $wpdb;
193
-
194
-	// Retrieve Wordlift relation instances table name.
195
-	$table_name = wl_core_get_relation_instances_table_name();
196
-
197
-	/*
60
+    // Post ID must be defined.
61
+    if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
62
+        wp_die( 'No post_id given' );
63
+
64
+        return;
65
+    }
66
+
67
+    $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
68
+    $current_post    = get_post( $current_post_id );
69
+    $faceted_id      = $_GET['uniqid'];
70
+
71
+    $post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
72
+    $post_types          = explode( ',', $post_types );
73
+    $existing_post_types = get_post_types();
74
+    $post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
75
+
76
+
77
+    // Post ID has to match an existing item.
78
+    if ( null === $current_post ) {
79
+        wp_die( 'No valid post_id given' );
80
+
81
+        return;
82
+    }
83
+
84
+    // If the current post is an entity,
85
+    // the current post is used as main entity.
86
+    // Otherwise, current post related entities are used.
87
+    $entity_service = Wordlift_Entity_Service::get_instance();
88
+
89
+    $entity_ids = $entity_service->is_entity( $current_post->ID ) ?
90
+        array( $current_post->ID ) :
91
+        $entity_service->get_related_entities( $current_post->ID );
92
+
93
+    // If there are no entities we cannot render the widget.
94
+    if ( 0 === count( $entity_ids ) ) {
95
+        /**
96
+         * If this function is not called from ajax
97
+         * then this should not throw an error.
98
+         * Note: Used in scripbox longtail project on json endpoint.
99
+         */
100
+        if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
101
+            wp_die( 'No entities available' );
102
+        }
103
+
104
+    }
105
+
106
+    $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4;  // WPCS: input var ok; CSRF ok.
107
+    $amp   = ( isset( $_GET['amp'] ) ) ? true : false;
108
+
109
+
110
+    /**
111
+     * see https://github.com/insideout10/wordlift-plugin/issues/1181
112
+     * The ordering should be descending by date on default.
113
+     */
114
+    $order_by = 'DESC';
115
+    if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) {
116
+        $order_by = (string) $_GET['sort'];
117
+    }
118
+
119
+
120
+    $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
121
+        $entity_ids,
122
+        '*',
123
+        null,
124
+        'publish',
125
+        array( $current_post_id ),
126
+        $limit,
127
+        null,
128
+        $order_by,
129
+        $post_types
130
+    );
131
+
132
+
133
+    $referencing_post_ids = array_map( function ( $p ) {
134
+        return $p->ID;
135
+    }, $referencing_posts );
136
+
137
+    $post_results   = array();
138
+    $entity_results = array();
139
+
140
+    // Populate $post_results
141
+
142
+    $filtered_posts = ( empty( $filtering_entity_uris ) ) ?
143
+        $referencing_posts :
144
+        Wordlift_Relation_Service::get_instance()->get_article_subjects(
145
+            wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
146
+            '*',
147
+            null,
148
+            null,
149
+            array(),
150
+            null,
151
+            $referencing_post_ids
152
+        );
153
+
154
+    if ( $filtered_posts ) {
155
+        foreach ( $filtered_posts as $post_obj ) {
156
+
157
+            /**
158
+             * Use the thumbnail.
159
+             *
160
+             * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
161
+             * @see https://github.com/insideout10/wordlift-plugin/issues/837
162
+             *
163
+             * @since 3.19.3 We're using the medium size image.
164
+             */
165
+            $thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
166
+            $post_obj->thumbnail = ( $thumbnail ) ?
167
+                $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
168
+            $post_obj->permalink = get_permalink( $post_obj->ID );
169
+            $post_obj->srcset    = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
170
+
171
+            $result         = $post_obj;
172
+            $post_results[] = $result;
173
+        }
174
+    }
175
+
176
+    // Add filler posts if needed
177
+
178
+    $filler_count = $limit - count( $post_results );
179
+    if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) {
180
+        $filler_posts_util       = new Filler_Posts_Util( $current_post_id );
181
+        $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
182
+        $filler_posts            = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
183
+
184
+        $post_results = array_merge( $post_results, $filler_posts );
185
+    }
186
+    $referencing_post_ids = array_map( function ( $post ) {
187
+        return $post->ID;
188
+    }, $post_results );
189
+
190
+    // Populate $entity_results
191
+
192
+    global $wpdb;
193
+
194
+    // Retrieve Wordlift relation instances table name.
195
+    $table_name = wl_core_get_relation_instances_table_name();
196
+
197
+    /*
198 198
 	 * Make sure we have some referenced post, otherwise the IN parts of
199 199
 	 * the SQL will produce an SQL error.
200 200
 	 */
201
-	if ( ! empty( $referencing_post_ids ) ) {
202
-		$subject_ids = implode( ',', $referencing_post_ids );
201
+    if ( ! empty( $referencing_post_ids ) ) {
202
+        $subject_ids = implode( ',', $referencing_post_ids );
203 203
 
204
-		$query = "
204
+        $query = "
205 205
 				SELECT
206 206
 					object_id AS ID,
207 207
 					count( object_id ) AS counter
@@ -213,68 +213,68 @@  discard block
 block discarded – undo
213 213
 				LIMIT $limit;
214 214
 			";
215 215
 
216
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
216
+        wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
217 217
 
218
-		$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
218
+        $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
219 219
 
220
-		wl_write_log( 'Entities found ' . count( $entities ) );
220
+        wl_write_log( 'Entities found ' . count( $entities ) );
221 221
 
222
-		foreach ( $entities as $obj ) {
222
+        foreach ( $entities as $obj ) {
223 223
 
224
-			$entity = get_post( $obj->ID );
224
+            $entity = get_post( $obj->ID );
225 225
 
226
-			// Ensure only valid and published entities are returned.
227
-			if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
226
+            // Ensure only valid and published entities are returned.
227
+            if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
228 228
 
229
-				$serialized_entity                    = wl_serialize_entity( $entity );
230
-				$serialized_entity['label']           = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' );
231
-				$serialized_entity['counter']         = $obj->counter;
232
-				$serialized_entity['createdAt']       = $entity->post_date;
233
-				$serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects(
234
-					$obj->ID,
235
-					'ids',
236
-					null,
237
-					null,
238
-					array(),
239
-					null,
240
-					$referencing_post_ids
241
-				);
242
-				$entity_results[]                     = $serialized_entity;
243
-			}
244
-		}
245
-	}
229
+                $serialized_entity                    = wl_serialize_entity( $entity );
230
+                $serialized_entity['label']           = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' );
231
+                $serialized_entity['counter']         = $obj->counter;
232
+                $serialized_entity['createdAt']       = $entity->post_date;
233
+                $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects(
234
+                    $obj->ID,
235
+                    'ids',
236
+                    null,
237
+                    null,
238
+                    array(),
239
+                    null,
240
+                    $referencing_post_ids
241
+                );
242
+                $entity_results[]                     = $serialized_entity;
243
+            }
244
+        }
245
+    }
246 246
 
247
-	$post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id );
247
+    $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id );
248 248
 
249
-	// Add srcset attribute.
250
-	$post_results = array_map( function ( $post ) {
251
-		$post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
249
+    // Add srcset attribute.
250
+    $post_results = array_map( function ( $post ) {
251
+        $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
252 252
 
253
-		return $post;
254
-	}, $post_results );
253
+        return $post;
254
+    }, $post_results );
255 255
 
256
-	$entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id );
256
+    $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id );
257 257
 
258
-	return array(
259
-		'posts'    => $amp ? array( array( 'values' => $post_results ) ) : $post_results,
260
-		'entities' => $entity_results
261
-	);
258
+    return array(
259
+        'posts'    => $amp ? array( array( 'values' => $post_results ) ) : $post_results,
260
+        'entities' => $entity_results
261
+    );
262 262
 
263 263
 }
264 264
 
265 265
 function wl_shortcode_faceted_search_get_the_title( $post_id ) {
266 266
 
267
-	$title = get_the_title( $post_id );
267
+    $title = get_the_title( $post_id );
268 268
 
269
-	if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) {
270
-		$alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id );
269
+    if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) {
270
+        $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id );
271 271
 
272
-		if ( count( $alternative_labels ) > 0 ) {
273
-			$title = $alternative_labels[0];
274
-		}
275
-	}
272
+        if ( count( $alternative_labels ) > 0 ) {
273
+            $title = $alternative_labels[0];
274
+        }
275
+    }
276 276
 
277
-	return remove_accents( $title );
277
+    return remove_accents( $title );
278 278
 
279 279
 }
280 280
 
@@ -282,9 +282,9 @@  discard block
 block discarded – undo
282 282
  * Adding `rest_api_init` action for network faceted-search
283 283
  */
284 284
 add_action( 'rest_api_init', function () {
285
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
286
-		'methods'             => 'GET',
287
-		'callback'            => 'wl_shortcode_faceted_search',
288
-		'permission_callback' => '__return_true',
289
-	) );
285
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
286
+        'methods'             => 'GET',
287
+        'callback'            => 'wl_shortcode_faceted_search',
288
+        'permission_callback' => '__return_true',
289
+    ) );
290 290
 } );
Please login to merge, or discard this patch.
Spacing   +74 added lines, -76 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
  * @since 3.21.4 fix the cache key by also using the request body.
19 19
  * @since 3.21.2 add a caching layer.
20 20
  */
21
-function wl_shortcode_faceted_search( $request ) {
21
+function wl_shortcode_faceted_search($request) {
22 22
 
23 23
 	// Create the cache key.
24 24
 	$cache_key = array(
@@ -26,27 +26,27 @@  discard block
 block discarded – undo
26 26
 	);
27 27
 
28 28
 	// Create the TTL cache and try to get the results.
29
-	$cache         = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours.
30
-	$cache_results = $cache->get( $cache_key );
29
+	$cache         = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours.
30
+	$cache_results = $cache->get($cache_key);
31 31
 
32 32
 	// So that the endpoint can be used remotely
33
-	header( 'Access-Control-Allow-Origin: *' );
33
+	header('Access-Control-Allow-Origin: *');
34 34
 
35
-	if ( isset( $cache_results ) ) {
36
-		header( 'X-WordLift-Cache: HIT' );
37
-		wl_core_send_json( $cache_results );
35
+	if (isset($cache_results)) {
36
+		header('X-WordLift-Cache: HIT');
37
+		wl_core_send_json($cache_results);
38 38
 
39 39
 		return;
40 40
 	}
41 41
 
42
-	header( 'X-WordLift-Cache: MISS' );
42
+	header('X-WordLift-Cache: MISS');
43 43
 
44
-	$results = wl_shortcode_faceted_search_origin( $request );
44
+	$results = wl_shortcode_faceted_search_origin($request);
45 45
 
46 46
 	// Put the result before sending the json to the client, since sending the json will terminate us.
47
-	$cache->put( $cache_key, $results );
47
+	$cache->put($cache_key, $results);
48 48
 
49
-	wl_core_send_json( $results );
49
+	wl_core_send_json($results);
50 50
 
51 51
 }
52 52
 
@@ -56,27 +56,27 @@  discard block
 block discarded – undo
56 56
  * @return array $results
57 57
  * @since        3.26.0
58 58
  */
59
-function wl_shortcode_faceted_search_origin( $request ) {
59
+function wl_shortcode_faceted_search_origin($request) {
60 60
 	// Post ID must be defined.
61
-	if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
62
-		wp_die( 'No post_id given' );
61
+	if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok.
62
+		wp_die('No post_id given');
63 63
 
64 64
 		return;
65 65
 	}
66 66
 
67 67
 	$current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
68
-	$current_post    = get_post( $current_post_id );
68
+	$current_post    = get_post($current_post_id);
69 69
 	$faceted_id      = $_GET['uniqid'];
70 70
 
71
-	$post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
72
-	$post_types          = explode( ',', $post_types );
71
+	$post_types          = isset($_GET['post_types']) ? (string) $_GET['post_types'] : '';
72
+	$post_types          = explode(',', $post_types);
73 73
 	$existing_post_types = get_post_types();
74
-	$post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
74
+	$post_types          = array_values(array_intersect($existing_post_types, $post_types));
75 75
 
76 76
 
77 77
 	// Post ID has to match an existing item.
78
-	if ( null === $current_post ) {
79
-		wp_die( 'No valid post_id given' );
78
+	if (null === $current_post) {
79
+		wp_die('No valid post_id given');
80 80
 
81 81
 		return;
82 82
 	}
@@ -86,25 +86,24 @@  discard block
 block discarded – undo
86 86
 	// Otherwise, current post related entities are used.
87 87
 	$entity_service = Wordlift_Entity_Service::get_instance();
88 88
 
89
-	$entity_ids = $entity_service->is_entity( $current_post->ID ) ?
90
-		array( $current_post->ID ) :
91
-		$entity_service->get_related_entities( $current_post->ID );
89
+	$entity_ids = $entity_service->is_entity($current_post->ID) ?
90
+		array($current_post->ID) : $entity_service->get_related_entities($current_post->ID);
92 91
 
93 92
 	// If there are no entities we cannot render the widget.
94
-	if ( 0 === count( $entity_ids ) ) {
93
+	if (0 === count($entity_ids)) {
95 94
 		/**
96 95
 		 * If this function is not called from ajax
97 96
 		 * then this should not throw an error.
98 97
 		 * Note: Used in scripbox longtail project on json endpoint.
99 98
 		 */
100
-		if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
101
-			wp_die( 'No entities available' );
99
+		if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) {
100
+			wp_die('No entities available');
102 101
 		}
103 102
 
104 103
 	}
105 104
 
106
-	$limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4;  // WPCS: input var ok; CSRF ok.
107
-	$amp   = ( isset( $_GET['amp'] ) ) ? true : false;
105
+	$limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok.
106
+	$amp   = (isset($_GET['amp'])) ? true : false;
108 107
 
109 108
 
110 109
 	/**
@@ -112,7 +111,7 @@  discard block
 block discarded – undo
112 111
 	 * The ordering should be descending by date on default.
113 112
 	 */
114 113
 	$order_by = 'DESC';
115
-	if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) {
114
+	if (isset($_GET['sort']) && is_string($_GET['sort'])) {
116 115
 		$order_by = (string) $_GET['sort'];
117 116
 	}
118 117
 
@@ -122,7 +121,7 @@  discard block
 block discarded – undo
122 121
 		'*',
123 122
 		null,
124 123
 		'publish',
125
-		array( $current_post_id ),
124
+		array($current_post_id),
126 125
 		$limit,
127 126
 		null,
128 127
 		$order_by,
@@ -130,19 +129,18 @@  discard block
 block discarded – undo
130 129
 	);
131 130
 
132 131
 
133
-	$referencing_post_ids = array_map( function ( $p ) {
132
+	$referencing_post_ids = array_map(function($p) {
134 133
 		return $p->ID;
135
-	}, $referencing_posts );
134
+	}, $referencing_posts);
136 135
 
137 136
 	$post_results   = array();
138 137
 	$entity_results = array();
139 138
 
140 139
 	// Populate $post_results
141 140
 
142
-	$filtered_posts = ( empty( $filtering_entity_uris ) ) ?
143
-		$referencing_posts :
144
-		Wordlift_Relation_Service::get_instance()->get_article_subjects(
145
-			wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
141
+	$filtered_posts = (empty($filtering_entity_uris)) ?
142
+		$referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects(
143
+			wl_get_entity_post_ids_by_uris($filtering_entity_uris),
146 144
 			'*',
147 145
 			null,
148 146
 			null,
@@ -151,8 +149,8 @@  discard block
 block discarded – undo
151 149
 			$referencing_post_ids
152 150
 		);
153 151
 
154
-	if ( $filtered_posts ) {
155
-		foreach ( $filtered_posts as $post_obj ) {
152
+	if ($filtered_posts) {
153
+		foreach ($filtered_posts as $post_obj) {
156 154
 
157 155
 			/**
158 156
 			 * Use the thumbnail.
@@ -162,11 +160,11 @@  discard block
 block discarded – undo
162 160
 			 *
163 161
 			 * @since 3.19.3 We're using the medium size image.
164 162
 			 */
165
-			$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
166
-			$post_obj->thumbnail = ( $thumbnail ) ?
163
+			$thumbnail           = get_the_post_thumbnail_url($post_obj, 'medium');
164
+			$post_obj->thumbnail = ($thumbnail) ?
167 165
 				$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
168
-			$post_obj->permalink = get_permalink( $post_obj->ID );
169
-			$post_obj->srcset    = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
166
+			$post_obj->permalink = get_permalink($post_obj->ID);
167
+			$post_obj->srcset    = Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET);
170 168
 
171 169
 			$result         = $post_obj;
172 170
 			$post_results[] = $result;
@@ -175,17 +173,17 @@  discard block
 block discarded – undo
175 173
 
176 174
 	// Add filler posts if needed
177 175
 
178
-	$filler_count = $limit - count( $post_results );
179
-	if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) {
180
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id );
181
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
182
-		$filler_posts            = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
176
+	$filler_count = $limit - count($post_results);
177
+	if (0 < apply_filters('wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids)) {
178
+		$filler_posts_util       = new Filler_Posts_Util($current_post_id);
179
+		$post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids);
180
+		$filler_posts            = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded);
183 181
 
184
-		$post_results = array_merge( $post_results, $filler_posts );
182
+		$post_results = array_merge($post_results, $filler_posts);
185 183
 	}
186
-	$referencing_post_ids = array_map( function ( $post ) {
184
+	$referencing_post_ids = array_map(function($post) {
187 185
 		return $post->ID;
188
-	}, $post_results );
186
+	}, $post_results);
189 187
 
190 188
 	// Populate $entity_results
191 189
 
@@ -198,8 +196,8 @@  discard block
 block discarded – undo
198 196
 	 * Make sure we have some referenced post, otherwise the IN parts of
199 197
 	 * the SQL will produce an SQL error.
200 198
 	 */
201
-	if ( ! empty( $referencing_post_ids ) ) {
202
-		$subject_ids = implode( ',', $referencing_post_ids );
199
+	if ( ! empty($referencing_post_ids)) {
200
+		$subject_ids = implode(',', $referencing_post_ids);
203 201
 
204 202
 		$query = "
205 203
 				SELECT
@@ -213,21 +211,21 @@  discard block
 block discarded – undo
213 211
 				LIMIT $limit;
214 212
 			";
215 213
 
216
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
214
+		wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]");
217 215
 
218
-		$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
216
+		$entities = $wpdb->get_results($query, OBJECT); // No cache ok.
219 217
 
220
-		wl_write_log( 'Entities found ' . count( $entities ) );
218
+		wl_write_log('Entities found '.count($entities));
221 219
 
222
-		foreach ( $entities as $obj ) {
220
+		foreach ($entities as $obj) {
223 221
 
224
-			$entity = get_post( $obj->ID );
222
+			$entity = get_post($obj->ID);
225 223
 
226 224
 			// Ensure only valid and published entities are returned.
227
-			if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
225
+			if ((null !== $entity) && ('publish' === $entity->post_status)) {
228 226
 
229
-				$serialized_entity                    = wl_serialize_entity( $entity );
230
-				$serialized_entity['label']           = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' );
227
+				$serialized_entity                    = wl_serialize_entity($entity);
228
+				$serialized_entity['label']           = html_entity_decode(wl_shortcode_faceted_search_get_the_title($obj->ID), ENT_QUOTES, 'UTF-8');
231 229
 				$serialized_entity['counter']         = $obj->counter;
232 230
 				$serialized_entity['createdAt']       = $entity->post_date;
233 231
 				$serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects(
@@ -239,52 +237,52 @@  discard block
 block discarded – undo
239 237
 					null,
240 238
 					$referencing_post_ids
241 239
 				);
242
-				$entity_results[]                     = $serialized_entity;
240
+				$entity_results[] = $serialized_entity;
243 241
 			}
244 242
 		}
245 243
 	}
246 244
 
247
-	$post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id );
245
+	$post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id);
248 246
 
249 247
 	// Add srcset attribute.
250
-	$post_results = array_map( function ( $post ) {
251
-		$post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
248
+	$post_results = array_map(function($post) {
249
+		$post->srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET);
252 250
 
253 251
 		return $post;
254
-	}, $post_results );
252
+	}, $post_results);
255 253
 
256
-	$entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id );
254
+	$entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id);
257 255
 
258 256
 	return array(
259
-		'posts'    => $amp ? array( array( 'values' => $post_results ) ) : $post_results,
257
+		'posts'    => $amp ? array(array('values' => $post_results)) : $post_results,
260 258
 		'entities' => $entity_results
261 259
 	);
262 260
 
263 261
 }
264 262
 
265
-function wl_shortcode_faceted_search_get_the_title( $post_id ) {
263
+function wl_shortcode_faceted_search_get_the_title($post_id) {
266 264
 
267
-	$title = get_the_title( $post_id );
265
+	$title = get_the_title($post_id);
268 266
 
269
-	if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) {
270
-		$alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id );
267
+	if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) {
268
+		$alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id);
271 269
 
272
-		if ( count( $alternative_labels ) > 0 ) {
270
+		if (count($alternative_labels) > 0) {
273 271
 			$title = $alternative_labels[0];
274 272
 		}
275 273
 	}
276 274
 
277
-	return remove_accents( $title );
275
+	return remove_accents($title);
278 276
 
279 277
 }
280 278
 
281 279
 /**
282 280
  * Adding `rest_api_init` action for network faceted-search
283 281
  */
284
-add_action( 'rest_api_init', function () {
285
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
282
+add_action('rest_api_init', function() {
283
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
286 284
 		'methods'             => 'GET',
287 285
 		'callback'            => 'wl_shortcode_faceted_search',
288 286
 		'permission_callback' => '__return_true',
289
-	) );
287
+	));
290 288
 } );
Please login to merge, or discard this patch.