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