Completed
Push — master ( 9f5aa8...08a9ce )
by Naveen
01:09
created
src/shortcodes/wordlift_shortcode_faceted_search.php 2 patches
Indentation   +196 added lines, -196 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,139 +57,139 @@  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 = (int) $_GET['post_id']; // WPCS: input var ok; CSRF ok.
68
-	$current_post    = get_post( $current_post_id );
69
-	$faceted_id      = isset( $_GET['uniqid'] ) ? sanitize_text_field( (string) $_GET['uniqid'] ) : '';
70
-
71
-	$post_types          = isset( $_GET['post_types'] ) ? sanitize_text_field( (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
-	if ( $referencing_posts ) {
143
-		foreach ( $referencing_posts as $post_obj ) {
144
-
145
-			/**
146
-			 * Use the thumbnail.
147
-			 *
148
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
149
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/837
150
-			 *
151
-			 * @since 3.19.3 We're using the medium size image.
152
-			 */
153
-			$thumbnail            = get_the_post_thumbnail_url( $post_obj, 'medium' );
154
-			$post_obj->thumbnail  = ( $thumbnail ) ?
155
-				$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
156
-			$post_obj->permalink  = get_permalink( $post_obj->ID );
157
-			$post_obj->srcset     = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
158
-			$post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) );
159
-			$result               = $post_obj;
160
-			$post_results[]       = $result;
161
-		}
162
-	}
163
-
164
-	// Add filler posts if needed
165
-
166
-	$filler_count = $limit - count( $post_results );
167
-	if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) {
168
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id );
169
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
170
-		$filler_posts            = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
171
-
172
-		$post_results = array_merge( $post_results, $filler_posts );
173
-	}
174
-	$referencing_post_ids = array_map( function ( $post ) {
175
-		return $post->ID;
176
-	}, $post_results );
177
-
178
-	// Populate $entity_results
179
-
180
-	global $wpdb;
181
-
182
-	// Retrieve Wordlift relation instances table name.
183
-	$table_name = wl_core_get_relation_instances_table_name();
184
-
185
-	/*
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 = (int) $_GET['post_id']; // WPCS: input var ok; CSRF ok.
68
+    $current_post    = get_post( $current_post_id );
69
+    $faceted_id      = isset( $_GET['uniqid'] ) ? sanitize_text_field( (string) $_GET['uniqid'] ) : '';
70
+
71
+    $post_types          = isset( $_GET['post_types'] ) ? sanitize_text_field( (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
+    if ( $referencing_posts ) {
143
+        foreach ( $referencing_posts as $post_obj ) {
144
+
145
+            /**
146
+             * Use the thumbnail.
147
+             *
148
+             * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
149
+             * @see https://github.com/insideout10/wordlift-plugin/issues/837
150
+             *
151
+             * @since 3.19.3 We're using the medium size image.
152
+             */
153
+            $thumbnail            = get_the_post_thumbnail_url( $post_obj, 'medium' );
154
+            $post_obj->thumbnail  = ( $thumbnail ) ?
155
+                $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
156
+            $post_obj->permalink  = get_permalink( $post_obj->ID );
157
+            $post_obj->srcset     = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
158
+            $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) );
159
+            $result               = $post_obj;
160
+            $post_results[]       = $result;
161
+        }
162
+    }
163
+
164
+    // Add filler posts if needed
165
+
166
+    $filler_count = $limit - count( $post_results );
167
+    if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) {
168
+        $filler_posts_util       = new Filler_Posts_Util( $current_post_id );
169
+        $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
170
+        $filler_posts            = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
171
+
172
+        $post_results = array_merge( $post_results, $filler_posts );
173
+    }
174
+    $referencing_post_ids = array_map( function ( $post ) {
175
+        return $post->ID;
176
+    }, $post_results );
177
+
178
+    // Populate $entity_results
179
+
180
+    global $wpdb;
181
+
182
+    // Retrieve Wordlift relation instances table name.
183
+    $table_name = wl_core_get_relation_instances_table_name();
184
+
185
+    /*
186 186
 	 * Make sure we have some referenced post, otherwise the IN parts of
187 187
 	 * the SQL will produce an SQL error.
188 188
 	 */
189
-	if ( ! empty( $referencing_post_ids ) ) {
190
-		$subject_ids = implode( ',', $referencing_post_ids );
189
+    if ( ! empty( $referencing_post_ids ) ) {
190
+        $subject_ids = implode( ',', $referencing_post_ids );
191 191
 
192
-		$query = "
192
+        $query = "
193 193
 				SELECT
194 194
 					object_id AS ID,
195 195
 					count( object_id ) AS counter
@@ -201,68 +201,68 @@  discard block
 block discarded – undo
201 201
 				LIMIT $limit;
202 202
 			";
203 203
 
204
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
204
+        wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
205 205
 
206
-		$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
206
+        $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
207 207
 
208
-		wl_write_log( 'Entities found ' . count( $entities ) );
208
+        wl_write_log( 'Entities found ' . count( $entities ) );
209 209
 
210
-		foreach ( $entities as $obj ) {
210
+        foreach ( $entities as $obj ) {
211 211
 
212
-			$entity = get_post( $obj->ID );
212
+            $entity = get_post( $obj->ID );
213 213
 
214
-			// Ensure only valid and published entities are returned.
215
-			if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
214
+            // Ensure only valid and published entities are returned.
215
+            if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
216 216
 
217
-				$serialized_entity                    = wl_serialize_entity( $entity );
218
-				$serialized_entity['label']           = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' );
219
-				$serialized_entity['counter']         = $obj->counter;
220
-				$serialized_entity['createdAt']       = $entity->post_date;
221
-				$serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects(
222
-					$obj->ID,
223
-					'ids',
224
-					null,
225
-					null,
226
-					array(),
227
-					null,
228
-					$referencing_post_ids
229
-				);
230
-				$entity_results[]                     = $serialized_entity;
231
-			}
232
-		}
233
-	}
217
+                $serialized_entity                    = wl_serialize_entity( $entity );
218
+                $serialized_entity['label']           = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' );
219
+                $serialized_entity['counter']         = $obj->counter;
220
+                $serialized_entity['createdAt']       = $entity->post_date;
221
+                $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects(
222
+                    $obj->ID,
223
+                    'ids',
224
+                    null,
225
+                    null,
226
+                    array(),
227
+                    null,
228
+                    $referencing_post_ids
229
+                );
230
+                $entity_results[]                     = $serialized_entity;
231
+            }
232
+        }
233
+    }
234 234
 
235
-	$post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id );
235
+    $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id );
236 236
 
237
-	// Add srcset attribute.
238
-	$post_results = array_map( function ( $post ) {
239
-		$post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
237
+    // Add srcset attribute.
238
+    $post_results = array_map( function ( $post ) {
239
+        $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
240 240
 
241
-		return $post;
242
-	}, $post_results );
241
+        return $post;
242
+    }, $post_results );
243 243
 
244
-	$entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id );
244
+    $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id );
245 245
 
246
-	return array(
247
-		'posts'    => $amp ? array( array( 'values' => $post_results ) ) : $post_results,
248
-		'entities' => $entity_results
249
-	);
246
+    return array(
247
+        'posts'    => $amp ? array( array( 'values' => $post_results ) ) : $post_results,
248
+        'entities' => $entity_results
249
+    );
250 250
 
251 251
 }
252 252
 
253 253
 function wl_shortcode_faceted_search_get_the_title( $post_id ) {
254 254
 
255
-	$title = wp_strip_all_tags( get_the_title( $post_id ) );
255
+    $title = wp_strip_all_tags( get_the_title( $post_id ) );
256 256
 
257
-	if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) {
258
-		$alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id );
257
+    if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) {
258
+        $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id );
259 259
 
260
-		if ( count( $alternative_labels ) > 0 ) {
261
-			$title = $alternative_labels[0];
262
-		}
263
-	}
260
+        if ( count( $alternative_labels ) > 0 ) {
261
+            $title = $alternative_labels[0];
262
+        }
263
+    }
264 264
 
265
-	return remove_accents( $title );
265
+    return remove_accents( $title );
266 266
 
267 267
 }
268 268
 
@@ -270,9 +270,9 @@  discard block
 block discarded – undo
270 270
  * Adding `rest_api_init` action for network faceted-search
271 271
  */
272 272
 add_action( 'rest_api_init', function () {
273
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
274
-		'methods'             => 'GET',
275
-		'callback'            => 'wl_shortcode_faceted_search',
276
-		'permission_callback' => '__return_true',
277
-	) );
273
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
274
+        'methods'             => 'GET',
275
+        'callback'            => 'wl_shortcode_faceted_search',
276
+        'permission_callback' => '__return_true',
277
+    ) );
278 278
 } );
Please login to merge, or discard this patch.
Spacing   +73 added lines, -74 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 = (int) $_GET['post_id']; // WPCS: input var ok; CSRF ok.
68
-	$current_post    = get_post( $current_post_id );
69
-	$faceted_id      = isset( $_GET['uniqid'] ) ? sanitize_text_field( (string) $_GET['uniqid'] ) : '';
68
+	$current_post    = get_post($current_post_id);
69
+	$faceted_id      = isset($_GET['uniqid']) ? sanitize_text_field((string) $_GET['uniqid']) : '';
70 70
 
71
-	$post_types          = isset( $_GET['post_types'] ) ? sanitize_text_field( (string) $_GET['post_types'] ) : '';
72
-	$post_types          = explode( ',', $post_types );
71
+	$post_types          = isset($_GET['post_types']) ? sanitize_text_field((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,17 +129,17 @@  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
-	if ( $referencing_posts ) {
143
-		foreach ( $referencing_posts as $post_obj ) {
141
+	if ($referencing_posts) {
142
+		foreach ($referencing_posts as $post_obj) {
144 143
 
145 144
 			/**
146 145
 			 * Use the thumbnail.
@@ -150,12 +149,12 @@  discard block
 block discarded – undo
150 149
 			 *
151 150
 			 * @since 3.19.3 We're using the medium size image.
152 151
 			 */
153
-			$thumbnail            = get_the_post_thumbnail_url( $post_obj, 'medium' );
154
-			$post_obj->thumbnail  = ( $thumbnail ) ?
152
+			$thumbnail            = get_the_post_thumbnail_url($post_obj, 'medium');
153
+			$post_obj->thumbnail  = ($thumbnail) ?
155 154
 				$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
156
-			$post_obj->permalink  = get_permalink( $post_obj->ID );
157
-			$post_obj->srcset     = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
158
-			$post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) );
155
+			$post_obj->permalink  = get_permalink($post_obj->ID);
156
+			$post_obj->srcset     = Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET);
157
+			$post_obj->post_title = wp_strip_all_tags(html_entity_decode($post_obj->post_title, ENT_QUOTES, 'UTF-8'));
159 158
 			$result               = $post_obj;
160 159
 			$post_results[]       = $result;
161 160
 		}
@@ -163,17 +162,17 @@  discard block
 block discarded – undo
163 162
 
164 163
 	// Add filler posts if needed
165 164
 
166
-	$filler_count = $limit - count( $post_results );
167
-	if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) {
168
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id );
169
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
170
-		$filler_posts            = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
165
+	$filler_count = $limit - count($post_results);
166
+	if (0 < apply_filters('wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids)) {
167
+		$filler_posts_util       = new Filler_Posts_Util($current_post_id);
168
+		$post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids);
169
+		$filler_posts            = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded);
171 170
 
172
-		$post_results = array_merge( $post_results, $filler_posts );
171
+		$post_results = array_merge($post_results, $filler_posts);
173 172
 	}
174
-	$referencing_post_ids = array_map( function ( $post ) {
173
+	$referencing_post_ids = array_map(function($post) {
175 174
 		return $post->ID;
176
-	}, $post_results );
175
+	}, $post_results);
177 176
 
178 177
 	// Populate $entity_results
179 178
 
@@ -186,8 +185,8 @@  discard block
 block discarded – undo
186 185
 	 * Make sure we have some referenced post, otherwise the IN parts of
187 186
 	 * the SQL will produce an SQL error.
188 187
 	 */
189
-	if ( ! empty( $referencing_post_ids ) ) {
190
-		$subject_ids = implode( ',', $referencing_post_ids );
188
+	if ( ! empty($referencing_post_ids)) {
189
+		$subject_ids = implode(',', $referencing_post_ids);
191 190
 
192 191
 		$query = "
193 192
 				SELECT
@@ -201,21 +200,21 @@  discard block
 block discarded – undo
201 200
 				LIMIT $limit;
202 201
 			";
203 202
 
204
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
203
+		wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]");
205 204
 
206
-		$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
205
+		$entities = $wpdb->get_results($query, OBJECT); // No cache ok.
207 206
 
208
-		wl_write_log( 'Entities found ' . count( $entities ) );
207
+		wl_write_log('Entities found '.count($entities));
209 208
 
210
-		foreach ( $entities as $obj ) {
209
+		foreach ($entities as $obj) {
211 210
 
212
-			$entity = get_post( $obj->ID );
211
+			$entity = get_post($obj->ID);
213 212
 
214 213
 			// Ensure only valid and published entities are returned.
215
-			if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
214
+			if ((null !== $entity) && ('publish' === $entity->post_status)) {
216 215
 
217
-				$serialized_entity                    = wl_serialize_entity( $entity );
218
-				$serialized_entity['label']           = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' );
216
+				$serialized_entity                    = wl_serialize_entity($entity);
217
+				$serialized_entity['label']           = html_entity_decode(wl_shortcode_faceted_search_get_the_title($obj->ID), ENT_QUOTES, 'UTF-8');
219 218
 				$serialized_entity['counter']         = $obj->counter;
220 219
 				$serialized_entity['createdAt']       = $entity->post_date;
221 220
 				$serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects(
@@ -227,52 +226,52 @@  discard block
 block discarded – undo
227 226
 					null,
228 227
 					$referencing_post_ids
229 228
 				);
230
-				$entity_results[]                     = $serialized_entity;
229
+				$entity_results[] = $serialized_entity;
231 230
 			}
232 231
 		}
233 232
 	}
234 233
 
235
-	$post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id );
234
+	$post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id);
236 235
 
237 236
 	// Add srcset attribute.
238
-	$post_results = array_map( function ( $post ) {
239
-		$post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET );
237
+	$post_results = array_map(function($post) {
238
+		$post->srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET);
240 239
 
241 240
 		return $post;
242
-	}, $post_results );
241
+	}, $post_results);
243 242
 
244
-	$entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id );
243
+	$entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id);
245 244
 
246 245
 	return array(
247
-		'posts'    => $amp ? array( array( 'values' => $post_results ) ) : $post_results,
246
+		'posts'    => $amp ? array(array('values' => $post_results)) : $post_results,
248 247
 		'entities' => $entity_results
249 248
 	);
250 249
 
251 250
 }
252 251
 
253
-function wl_shortcode_faceted_search_get_the_title( $post_id ) {
252
+function wl_shortcode_faceted_search_get_the_title($post_id) {
254 253
 
255
-	$title = wp_strip_all_tags( get_the_title( $post_id ) );
254
+	$title = wp_strip_all_tags(get_the_title($post_id));
256 255
 
257
-	if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) {
258
-		$alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id );
256
+	if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) {
257
+		$alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id);
259 258
 
260
-		if ( count( $alternative_labels ) > 0 ) {
259
+		if (count($alternative_labels) > 0) {
261 260
 			$title = $alternative_labels[0];
262 261
 		}
263 262
 	}
264 263
 
265
-	return remove_accents( $title );
264
+	return remove_accents($title);
266 265
 
267 266
 }
268 267
 
269 268
 /**
270 269
  * Adding `rest_api_init` action for network faceted-search
271 270
  */
272
-add_action( 'rest_api_init', function () {
273
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
271
+add_action('rest_api_init', function() {
272
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
274 273
 		'methods'             => 'GET',
275 274
 		'callback'            => 'wl_shortcode_faceted_search',
276 275
 		'permission_callback' => '__return_true',
277
-	) );
276
+	));
278 277
 } );
Please login to merge, or discard this patch.
src/shortcodes/wordlift_shortcode_navigator.php 2 patches
Indentation   +268 added lines, -268 removed lines patch added patch discarded remove patch
@@ -20,32 +20,32 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_shortcode_navigator_data() {
22 22
 
23
-	// Create the cache key.
24
-	$cache_key_params = $_REQUEST;
25
-	unset( $cache_key_params['uniqid'] );
26
-	$cache_key = array( 'request_params' => $cache_key_params );
23
+    // Create the cache key.
24
+    $cache_key_params = $_REQUEST;
25
+    unset( $cache_key_params['uniqid'] );
26
+    $cache_key = array( 'request_params' => $cache_key_params );
27 27
 
28
-	// Create the TTL cache and try to get the results.
29
-	$cache         = new Ttl_Cache( "navigator", 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( "navigator", 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' );
35
+    if ( isset( $cache_results ) ) {
36
+        header( 'X-WordLift-Cache: HIT' );
37 37
 
38
-		return $cache_results;
39
-	}
38
+        return $cache_results;
39
+    }
40 40
 
41
-	header( 'X-WordLift-Cache: MISS' );
41
+    header( 'X-WordLift-Cache: MISS' );
42 42
 
43
-	$results = _wl_navigator_get_data();
43
+    $results = _wl_navigator_get_data();
44 44
 
45
-	// Put the result before sending the json to the client, since sending the json will terminate us.
46
-	$cache->put( $cache_key, $results );
45
+    // Put the result before sending the json to the client, since sending the json will terminate us.
46
+    $cache->put( $cache_key, $results );
47 47
 
48
-	return $results;
48
+    return $results;
49 49
 }
50 50
 
51 51
 /**
@@ -60,240 +60,240 @@  discard block
 block discarded – undo
60 60
  */
61 61
 function wl_network_navigator_wp_json( $request ) {
62 62
 
63
-	// Create the cache key.
64
-	$cache_key_params = $_REQUEST;
65
-	unset( $cache_key_params['uniqid'] );
66
-	$cache_key = array( 'request_params' => $cache_key_params );
63
+    // Create the cache key.
64
+    $cache_key_params = $_REQUEST;
65
+    unset( $cache_key_params['uniqid'] );
66
+    $cache_key = array( 'request_params' => $cache_key_params );
67 67
 
68
-	// Create the TTL cache and try to get the results.
69
-	$cache         = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours.
70
-	$cache_results = $cache->get( $cache_key );
68
+    // Create the TTL cache and try to get the results.
69
+    $cache         = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours.
70
+    $cache_results = $cache->get( $cache_key );
71 71
 
72
-	if ( isset( $cache_results ) ) {
73
-		header( 'X-WordLift-Cache: HIT' );
72
+    if ( isset( $cache_results ) ) {
73
+        header( 'X-WordLift-Cache: HIT' );
74 74
 
75
-		return $cache_results;
76
-	}
75
+        return $cache_results;
76
+    }
77 77
 
78
-	header( 'X-WordLift-Cache: MISS' );
78
+    header( 'X-WordLift-Cache: MISS' );
79 79
 
80
-	$results = _wl_network_navigator_get_data( $request );
80
+    $results = _wl_network_navigator_get_data( $request );
81 81
 
82
-	// Put the result before sending the json to the client, since sending the json will terminate us.
83
-	$cache->put( $cache_key, $results );
82
+    // Put the result before sending the json to the client, since sending the json will terminate us.
83
+    $cache->put( $cache_key, $results );
84 84
 
85
-	return $results;
85
+    return $results;
86 86
 
87 87
 }
88 88
 
89 89
 function _wl_navigator_get_data() {
90 90
 
91
-	// Post ID must be defined
92
-	if ( ! isset( $_GET['post_id'] ) ) {
93
-		wp_send_json_error( 'No post_id given' );
94
-
95
-		return array();
96
-	}
97
-
98
-	// Post ID must be defined
99
-	if ( ! isset( $_GET['uniqid'] ) ) {
100
-		wp_send_json_error( 'No uniqid given' );
101
-
102
-		return array();
103
-	}
104
-
105
-	// Limit the results (defaults to 4)
106
-	$navigator_length    = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4;
107
-	$navigator_offset    = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
108
-	$order_by            = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
109
-	$post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
110
-	$post_types          = explode( ',', $post_types );
111
-	$existing_post_types = get_post_types();
112
-	$post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
113
-	$current_post_id     = (int) $_GET['post_id'];
114
-	$current_post        = get_post( $current_post_id );
115
-
116
-	$navigator_id = (string) $_GET['uniqid'];
117
-
118
-	// Post ID has to match an existing item
119
-	if ( null === $current_post ) {
120
-		wp_send_json_error( 'No valid post_id given' );
121
-
122
-		return array();
123
-	}
124
-
125
-	// Determine navigator type and call respective _get_results
126
-	if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
127
-
128
-		$referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array(
129
-			'ID',
130
-			'post_title',
131
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
132
-	} else {
133
-		$referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array(
134
-			'ID',
135
-			'post_title',
136
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
137
-
138
-	}
139
-
140
-	// loop over them and take the first one which is not already in the $related_posts
141
-	$results = array();
142
-	foreach ( $referencing_posts as $referencing_post ) {
143
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
144
-
145
-		/**
146
-		 * Use the thumbnail.
147
-		 *
148
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
149
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/837
150
-		 *
151
-		 * @since 3.19.3 We're using the medium size image.
152
-		 */
153
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
154
-
155
-		$result = array(
156
-			'post'   => array(
157
-				'id'        => $referencing_post->ID,
158
-				'permalink' => get_permalink( $referencing_post->ID ),
159
-				'title'     => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ),
160
-				'thumbnail' => $thumbnail,
161
-				'srcset'    => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET )
162
-			),
163
-			'entity' => array(
164
-				'id'        => $referencing_post->entity_id,
165
-				'label'     => $serialized_entity['label'],
166
-				'mainType'  => $serialized_entity['mainType'],
167
-				'permalink' => get_permalink( $referencing_post->entity_id ),
168
-			),
169
-		);
170
-
171
-		$results[] = $result;
172
-	}
173
-
174
-
175
-	if ( count( $results ) < $navigator_length ) {
176
-		$results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
177
-	}
178
-
179
-	// Add filler posts if needed
180
-	$filler_count = $navigator_length - count( $results );
181
-	if ( $filler_count > 0 ) {
182
-		$referencing_post_ids = array_map( function ( $p ) {
183
-			return $p->ID;
184
-		}, $referencing_posts );
185
-		/**
186
-		 * @since 3.27.8
187
-		 * Filler posts are fetched using this util.
188
-		 */
189
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id, $post_types );
190
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
191
-		$filler_posts            = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded );
192
-		$results                 = array_merge( $results, $filler_posts );
193
-	}
194
-
195
-	// Apply filters after fillers are added
196
-	foreach ( $results as $result_index => $result ) {
197
-		$results[ $result_index ]['post']   = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id );
198
-		$results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
199
-	}
200
-
201
-	$results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id );
202
-
203
-	return $results;
91
+    // Post ID must be defined
92
+    if ( ! isset( $_GET['post_id'] ) ) {
93
+        wp_send_json_error( 'No post_id given' );
94
+
95
+        return array();
96
+    }
97
+
98
+    // Post ID must be defined
99
+    if ( ! isset( $_GET['uniqid'] ) ) {
100
+        wp_send_json_error( 'No uniqid given' );
101
+
102
+        return array();
103
+    }
104
+
105
+    // Limit the results (defaults to 4)
106
+    $navigator_length    = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4;
107
+    $navigator_offset    = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
108
+    $order_by            = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
109
+    $post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
110
+    $post_types          = explode( ',', $post_types );
111
+    $existing_post_types = get_post_types();
112
+    $post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
113
+    $current_post_id     = (int) $_GET['post_id'];
114
+    $current_post        = get_post( $current_post_id );
115
+
116
+    $navigator_id = (string) $_GET['uniqid'];
117
+
118
+    // Post ID has to match an existing item
119
+    if ( null === $current_post ) {
120
+        wp_send_json_error( 'No valid post_id given' );
121
+
122
+        return array();
123
+    }
124
+
125
+    // Determine navigator type and call respective _get_results
126
+    if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
127
+
128
+        $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array(
129
+            'ID',
130
+            'post_title',
131
+        ), $order_by, $navigator_length, $navigator_offset, $post_types );
132
+    } else {
133
+        $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array(
134
+            'ID',
135
+            'post_title',
136
+        ), $order_by, $navigator_length, $navigator_offset, $post_types );
137
+
138
+    }
139
+
140
+    // loop over them and take the first one which is not already in the $related_posts
141
+    $results = array();
142
+    foreach ( $referencing_posts as $referencing_post ) {
143
+        $serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
144
+
145
+        /**
146
+         * Use the thumbnail.
147
+         *
148
+         * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
149
+         * @see https://github.com/insideout10/wordlift-plugin/issues/837
150
+         *
151
+         * @since 3.19.3 We're using the medium size image.
152
+         */
153
+        $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
154
+
155
+        $result = array(
156
+            'post'   => array(
157
+                'id'        => $referencing_post->ID,
158
+                'permalink' => get_permalink( $referencing_post->ID ),
159
+                'title'     => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ),
160
+                'thumbnail' => $thumbnail,
161
+                'srcset'    => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET )
162
+            ),
163
+            'entity' => array(
164
+                'id'        => $referencing_post->entity_id,
165
+                'label'     => $serialized_entity['label'],
166
+                'mainType'  => $serialized_entity['mainType'],
167
+                'permalink' => get_permalink( $referencing_post->entity_id ),
168
+            ),
169
+        );
170
+
171
+        $results[] = $result;
172
+    }
173
+
174
+
175
+    if ( count( $results ) < $navigator_length ) {
176
+        $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
177
+    }
178
+
179
+    // Add filler posts if needed
180
+    $filler_count = $navigator_length - count( $results );
181
+    if ( $filler_count > 0 ) {
182
+        $referencing_post_ids = array_map( function ( $p ) {
183
+            return $p->ID;
184
+        }, $referencing_posts );
185
+        /**
186
+         * @since 3.27.8
187
+         * Filler posts are fetched using this util.
188
+         */
189
+        $filler_posts_util       = new Filler_Posts_Util( $current_post_id, $post_types );
190
+        $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
191
+        $filler_posts            = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded );
192
+        $results                 = array_merge( $results, $filler_posts );
193
+    }
194
+
195
+    // Apply filters after fillers are added
196
+    foreach ( $results as $result_index => $result ) {
197
+        $results[ $result_index ]['post']   = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id );
198
+        $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
199
+    }
200
+
201
+    $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id );
202
+
203
+    return $results;
204 204
 }
205 205
 
206 206
 function _wl_network_navigator_get_data( $request ) {
207 207
 
208
-	// Limit the results (defaults to 4)
209
-	$navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4;
210
-	$navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0;
211
-	$navigator_id     = $request['uniqid'];
212
-	$order_by         = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
213
-
214
-	$entities = $request['entities'];
215
-
216
-	// Post ID has to match an existing item
217
-	if ( ! isset( $entities ) || empty( $entities ) ) {
218
-		wp_send_json_error( 'No valid entities provided' );
219
-	}
220
-
221
-	$referencing_posts = _wl_network_navigator_get_results( $entities, array(
222
-		'ID',
223
-		'post_title',
224
-	), $order_by, $navigator_length, $navigator_offset );
225
-
226
-	// loop over them and take the first one which is not already in the $related_posts
227
-	$results = array();
228
-	foreach ( $referencing_posts as $referencing_post ) {
229
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
230
-
231
-		/**
232
-		 * Use the thumbnail.
233
-		 *
234
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
235
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/837
236
-		 *
237
-		 * @since 3.19.3 We're using the medium size image.
238
-		 */
239
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
240
-
241
-		$result = array(
242
-			'post'   => array(
243
-				'permalink' => get_permalink( $referencing_post->ID ),
244
-				'title'     => $referencing_post->post_title,
245
-				'thumbnail' => $thumbnail,
246
-			),
247
-			'entity' => array(
248
-				'label'     => $serialized_entity['label'],
249
-				'mainType'  => $serialized_entity['mainType'],
250
-				'permalink' => get_permalink( $referencing_post->entity_id ),
251
-			),
252
-		);
253
-
254
-		$result['post']   = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id );
255
-		$result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id );
256
-
257
-		$results[] = $result;
258
-
259
-	}
260
-
261
-	if ( count( $results ) < $navigator_length ) {
262
-		$results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
263
-	}
264
-
265
-	$results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id );
266
-
267
-	return $results;
208
+    // Limit the results (defaults to 4)
209
+    $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4;
210
+    $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0;
211
+    $navigator_id     = $request['uniqid'];
212
+    $order_by         = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
213
+
214
+    $entities = $request['entities'];
215
+
216
+    // Post ID has to match an existing item
217
+    if ( ! isset( $entities ) || empty( $entities ) ) {
218
+        wp_send_json_error( 'No valid entities provided' );
219
+    }
220
+
221
+    $referencing_posts = _wl_network_navigator_get_results( $entities, array(
222
+        'ID',
223
+        'post_title',
224
+    ), $order_by, $navigator_length, $navigator_offset );
225
+
226
+    // loop over them and take the first one which is not already in the $related_posts
227
+    $results = array();
228
+    foreach ( $referencing_posts as $referencing_post ) {
229
+        $serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
230
+
231
+        /**
232
+         * Use the thumbnail.
233
+         *
234
+         * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
235
+         * @see https://github.com/insideout10/wordlift-plugin/issues/837
236
+         *
237
+         * @since 3.19.3 We're using the medium size image.
238
+         */
239
+        $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
240
+
241
+        $result = array(
242
+            'post'   => array(
243
+                'permalink' => get_permalink( $referencing_post->ID ),
244
+                'title'     => $referencing_post->post_title,
245
+                'thumbnail' => $thumbnail,
246
+            ),
247
+            'entity' => array(
248
+                'label'     => $serialized_entity['label'],
249
+                'mainType'  => $serialized_entity['mainType'],
250
+                'permalink' => get_permalink( $referencing_post->entity_id ),
251
+            ),
252
+        );
253
+
254
+        $result['post']   = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id );
255
+        $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id );
256
+
257
+        $results[] = $result;
258
+
259
+    }
260
+
261
+    if ( count( $results ) < $navigator_length ) {
262
+        $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
263
+    }
264
+
265
+    $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id );
266
+
267
+    return $results;
268 268
 
269 269
 }
270 270
 
271 271
 function _wl_network_navigator_get_results(
272
-	$entities, $fields = array(
273
-	'ID',
274
-	'post_title',
272
+    $entities, $fields = array(
273
+    'ID',
274
+    'post_title',
275 275
 ), $order_by = 'ID DESC', $limit = 10, $offset = 0
276 276
 ) {
277
-	global $wpdb;
278
-
279
-	$select = implode( ', ', array_map( function ( $item ) {
280
-		return "p.$item AS $item";
281
-	}, (array) $fields ) );
282
-
283
-	$order_by = implode( ', ', array_map( function ( $item ) {
284
-		return "p.$item";
285
-	}, (array) $order_by ) );
286
-
287
-	$entities_in = implode( ',', array_map( function ( $item ) {
288
-		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) );
289
-		if ( isset( $entity ) ) {
290
-			return $entity->ID;
291
-		}
292
-	}, $entities ) );
293
-
294
-	/** @noinspection SqlNoDataSourceInspection */
295
-	return $wpdb->get_results(
296
-		$wpdb->prepare( "
277
+    global $wpdb;
278
+
279
+    $select = implode( ', ', array_map( function ( $item ) {
280
+        return "p.$item AS $item";
281
+    }, (array) $fields ) );
282
+
283
+    $order_by = implode( ', ', array_map( function ( $item ) {
284
+        return "p.$item";
285
+    }, (array) $order_by ) );
286
+
287
+    $entities_in = implode( ',', array_map( function ( $item ) {
288
+        $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) );
289
+        if ( isset( $entity ) ) {
290
+            return $entity->ID;
291
+        }
292
+    }, $entities ) );
293
+
294
+    /** @noinspection SqlNoDataSourceInspection */
295
+    return $wpdb->get_results(
296
+        $wpdb->prepare( "
297 297
 SELECT %3\$s, p2.ID as entity_id
298 298
  FROM {$wpdb->prefix}wl_relation_instances r1
299 299
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -319,8 +319,8 @@  discard block
 block discarded – undo
319 319
  LIMIT %1\$d
320 320
  OFFSET %2\$d
321 321
 "
322
-			, $limit, $offset, $select, $order_by )
323
-	);
322
+            , $limit, $offset, $select, $order_by )
323
+    );
324 324
 
325 325
 }
326 326
 
@@ -332,9 +332,9 @@  discard block
 block discarded – undo
332 332
  */
333 333
 function wl_shortcode_navigator_ajax() {
334 334
 
335
-	// Temporary blocking the Navigator.
336
-	$results = wl_shortcode_navigator_data();
337
-	wl_core_send_json( $results );
335
+    // Temporary blocking the Navigator.
336
+    $results = wl_shortcode_navigator_data();
337
+    wl_core_send_json( $results );
338 338
 
339 339
 }
340 340
 
@@ -346,16 +346,16 @@  discard block
 block discarded – undo
346 346
  */
347 347
 function wl_shortcode_navigator_wp_json() {
348 348
 
349
-	$results = wl_shortcode_navigator_data();
350
-	if ( ob_get_contents() ) {
351
-		ob_clean();
352
-	}
349
+    $results = wl_shortcode_navigator_data();
350
+    if ( ob_get_contents() ) {
351
+        ob_clean();
352
+    }
353 353
 
354
-	return array(
355
-		'items' => array(
356
-			array( 'values' => $results ),
357
-		),
358
-	);
354
+    return array(
355
+        'items' => array(
356
+            array( 'values' => $results ),
357
+        ),
358
+    );
359 359
 
360 360
 }
361 361
 
@@ -363,22 +363,22 @@  discard block
 block discarded – undo
363 363
  * Adding `rest_api_init` action for amp backend of navigator
364 364
  */
365 365
 add_action( 'rest_api_init', function () {
366
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
367
-		'methods'             => 'GET',
368
-		'permission_callback' => '__return_true',
369
-		'callback'            => 'wl_shortcode_navigator_wp_json'
370
-	) );
366
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
367
+        'methods'             => 'GET',
368
+        'permission_callback' => '__return_true',
369
+        'callback'            => 'wl_shortcode_navigator_wp_json'
370
+    ) );
371 371
 } );
372 372
 
373 373
 /**
374 374
  * Adding `rest_api_init` action for backend of network navigator
375 375
  */
376 376
 add_action( 'rest_api_init', function () {
377
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
378
-		'methods'             => 'GET',
379
-		'callback'            => 'wl_network_navigator_wp_json',
380
-		'permission_callback' => '__return_true',
381
-	) );
377
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
378
+        'methods'             => 'GET',
379
+        'callback'            => 'wl_network_navigator_wp_json',
380
+        'permission_callback' => '__return_true',
381
+    ) );
382 382
 } );
383 383
 
384 384
 /**
@@ -387,23 +387,23 @@  discard block
 block discarded – undo
387 387
  * @since 2.2.0
388 388
  */
389 389
 add_action( 'plugins_loaded', function () {
390
-	$action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( (string) $_REQUEST['action'] ) : '';
391
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
392
-		return;
393
-	}
394
-
395
-	remove_action( 'plugins_loaded', 'rocket_init' );
396
-	remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
397
-	remove_action( 'plugins_loaded', 'wpseo_init', 14 );
390
+    $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( (string) $_REQUEST['action'] ) : '';
391
+    if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
392
+        return;
393
+    }
394
+
395
+    remove_action( 'plugins_loaded', 'rocket_init' );
396
+    remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
397
+    remove_action( 'plugins_loaded', 'wpseo_init', 14 );
398 398
 }, 0 );
399 399
 
400 400
 add_action( 'init', function () {
401
-	$action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( (string) $_REQUEST['action'] ) : '';
402
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
403
-		return;
404
-	}
401
+    $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( (string) $_REQUEST['action'] ) : '';
402
+    if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
403
+        return;
404
+    }
405 405
 
406
-	remove_action( 'init', 'wp_widgets_init', 1 );
407
-	remove_action( 'init', 'gglcptch_init' );
406
+    remove_action( 'init', 'wp_widgets_init', 1 );
407
+    remove_action( 'init', 'gglcptch_init' );
408 408
 }, 0 );
409 409
 
Please login to merge, or discard this patch.
Spacing   +110 added lines, -110 removed lines patch added patch discarded remove patch
@@ -22,28 +22,28 @@  discard block
 block discarded – undo
22 22
 
23 23
 	// Create the cache key.
24 24
 	$cache_key_params = $_REQUEST;
25
-	unset( $cache_key_params['uniqid'] );
26
-	$cache_key = array( 'request_params' => $cache_key_params );
25
+	unset($cache_key_params['uniqid']);
26
+	$cache_key = array('request_params' => $cache_key_params);
27 27
 
28 28
 	// Create the TTL cache and try to get the results.
29
-	$cache         = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours.
30
-	$cache_results = $cache->get( $cache_key );
29
+	$cache         = new Ttl_Cache("navigator", 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' );
35
+	if (isset($cache_results)) {
36
+		header('X-WordLift-Cache: HIT');
37 37
 
38 38
 		return $cache_results;
39 39
 	}
40 40
 
41
-	header( 'X-WordLift-Cache: MISS' );
41
+	header('X-WordLift-Cache: MISS');
42 42
 
43 43
 	$results = _wl_navigator_get_data();
44 44
 
45 45
 	// Put the result before sending the json to the client, since sending the json will terminate us.
46
-	$cache->put( $cache_key, $results );
46
+	$cache->put($cache_key, $results);
47 47
 
48 48
 	return $results;
49 49
 }
@@ -58,29 +58,29 @@  discard block
 block discarded – undo
58 58
  * @since 3.22.6
59 59
  *
60 60
  */
61
-function wl_network_navigator_wp_json( $request ) {
61
+function wl_network_navigator_wp_json($request) {
62 62
 
63 63
 	// Create the cache key.
64 64
 	$cache_key_params = $_REQUEST;
65
-	unset( $cache_key_params['uniqid'] );
66
-	$cache_key = array( 'request_params' => $cache_key_params );
65
+	unset($cache_key_params['uniqid']);
66
+	$cache_key = array('request_params' => $cache_key_params);
67 67
 
68 68
 	// Create the TTL cache and try to get the results.
69
-	$cache         = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours.
70
-	$cache_results = $cache->get( $cache_key );
69
+	$cache         = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours.
70
+	$cache_results = $cache->get($cache_key);
71 71
 
72
-	if ( isset( $cache_results ) ) {
73
-		header( 'X-WordLift-Cache: HIT' );
72
+	if (isset($cache_results)) {
73
+		header('X-WordLift-Cache: HIT');
74 74
 
75 75
 		return $cache_results;
76 76
 	}
77 77
 
78
-	header( 'X-WordLift-Cache: MISS' );
78
+	header('X-WordLift-Cache: MISS');
79 79
 
80
-	$results = _wl_network_navigator_get_data( $request );
80
+	$results = _wl_network_navigator_get_data($request);
81 81
 
82 82
 	// Put the result before sending the json to the client, since sending the json will terminate us.
83
-	$cache->put( $cache_key, $results );
83
+	$cache->put($cache_key, $results);
84 84
 
85 85
 	return $results;
86 86
 
@@ -89,58 +89,58 @@  discard block
 block discarded – undo
89 89
 function _wl_navigator_get_data() {
90 90
 
91 91
 	// Post ID must be defined
92
-	if ( ! isset( $_GET['post_id'] ) ) {
93
-		wp_send_json_error( 'No post_id given' );
92
+	if ( ! isset($_GET['post_id'])) {
93
+		wp_send_json_error('No post_id given');
94 94
 
95 95
 		return array();
96 96
 	}
97 97
 
98 98
 	// Post ID must be defined
99
-	if ( ! isset( $_GET['uniqid'] ) ) {
100
-		wp_send_json_error( 'No uniqid given' );
99
+	if ( ! isset($_GET['uniqid'])) {
100
+		wp_send_json_error('No uniqid given');
101 101
 
102 102
 		return array();
103 103
 	}
104 104
 
105 105
 	// Limit the results (defaults to 4)
106
-	$navigator_length    = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4;
107
-	$navigator_offset    = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
108
-	$order_by            = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
109
-	$post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
110
-	$post_types          = explode( ',', $post_types );
106
+	$navigator_length    = isset($_GET['limit']) ? intval($_GET['limit']) : 4;
107
+	$navigator_offset    = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
108
+	$order_by            = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC';
109
+	$post_types          = isset($_GET['post_types']) ? (string) $_GET['post_types'] : '';
110
+	$post_types          = explode(',', $post_types);
111 111
 	$existing_post_types = get_post_types();
112
-	$post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
112
+	$post_types          = array_values(array_intersect($existing_post_types, $post_types));
113 113
 	$current_post_id     = (int) $_GET['post_id'];
114
-	$current_post        = get_post( $current_post_id );
114
+	$current_post        = get_post($current_post_id);
115 115
 
116 116
 	$navigator_id = (string) $_GET['uniqid'];
117 117
 
118 118
 	// Post ID has to match an existing item
119
-	if ( null === $current_post ) {
120
-		wp_send_json_error( 'No valid post_id given' );
119
+	if (null === $current_post) {
120
+		wp_send_json_error('No valid post_id given');
121 121
 
122 122
 		return array();
123 123
 	}
124 124
 
125 125
 	// Determine navigator type and call respective _get_results
126
-	if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
126
+	if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) {
127 127
 
128
-		$referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array(
128
+		$referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array(
129 129
 			'ID',
130 130
 			'post_title',
131
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
131
+		), $order_by, $navigator_length, $navigator_offset, $post_types);
132 132
 	} else {
133
-		$referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array(
133
+		$referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array(
134 134
 			'ID',
135 135
 			'post_title',
136
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
136
+		), $order_by, $navigator_length, $navigator_offset, $post_types);
137 137
 
138 138
 	}
139 139
 
140 140
 	// loop over them and take the first one which is not already in the $related_posts
141 141
 	$results = array();
142
-	foreach ( $referencing_posts as $referencing_post ) {
143
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
142
+	foreach ($referencing_posts as $referencing_post) {
143
+		$serialized_entity = wl_serialize_entity($referencing_post->entity_id);
144 144
 
145 145
 		/**
146 146
 		 * Use the thumbnail.
@@ -150,21 +150,21 @@  discard block
 block discarded – undo
150 150
 		 *
151 151
 		 * @since 3.19.3 We're using the medium size image.
152 152
 		 */
153
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
153
+		$thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium');
154 154
 
155 155
 		$result = array(
156 156
 			'post'   => array(
157 157
 				'id'        => $referencing_post->ID,
158
-				'permalink' => get_permalink( $referencing_post->ID ),
159
-				'title'     => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ),
158
+				'permalink' => get_permalink($referencing_post->ID),
159
+				'title'     => html_entity_decode($referencing_post->post_title, ENT_QUOTES, 'UTF-8'),
160 160
 				'thumbnail' => $thumbnail,
161
-				'srcset'    => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET )
161
+				'srcset'    => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET)
162 162
 			),
163 163
 			'entity' => array(
164 164
 				'id'        => $referencing_post->entity_id,
165 165
 				'label'     => $serialized_entity['label'],
166 166
 				'mainType'  => $serialized_entity['mainType'],
167
-				'permalink' => get_permalink( $referencing_post->entity_id ),
167
+				'permalink' => get_permalink($referencing_post->entity_id),
168 168
 			),
169 169
 		);
170 170
 
@@ -172,61 +172,61 @@  discard block
 block discarded – undo
172 172
 	}
173 173
 
174 174
 
175
-	if ( count( $results ) < $navigator_length ) {
176
-		$results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
175
+	if (count($results) < $navigator_length) {
176
+		$results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length);
177 177
 	}
178 178
 
179 179
 	// Add filler posts if needed
180
-	$filler_count = $navigator_length - count( $results );
181
-	if ( $filler_count > 0 ) {
182
-		$referencing_post_ids = array_map( function ( $p ) {
180
+	$filler_count = $navigator_length - count($results);
181
+	if ($filler_count > 0) {
182
+		$referencing_post_ids = array_map(function($p) {
183 183
 			return $p->ID;
184
-		}, $referencing_posts );
184
+		}, $referencing_posts);
185 185
 		/**
186 186
 		 * @since 3.27.8
187 187
 		 * Filler posts are fetched using this util.
188 188
 		 */
189
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id, $post_types );
190
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
191
-		$filler_posts            = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded );
192
-		$results                 = array_merge( $results, $filler_posts );
189
+		$filler_posts_util       = new Filler_Posts_Util($current_post_id, $post_types);
190
+		$post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids);
191
+		$filler_posts            = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded);
192
+		$results                 = array_merge($results, $filler_posts);
193 193
 	}
194 194
 
195 195
 	// Apply filters after fillers are added
196
-	foreach ( $results as $result_index => $result ) {
197
-		$results[ $result_index ]['post']   = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id );
198
-		$results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
196
+	foreach ($results as $result_index => $result) {
197
+		$results[$result_index]['post']   = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id);
198
+		$results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id);
199 199
 	}
200 200
 
201
-	$results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id );
201
+	$results = apply_filters('wl_navigator_results', $results, $navigator_id, $current_post_id);
202 202
 
203 203
 	return $results;
204 204
 }
205 205
 
206
-function _wl_network_navigator_get_data( $request ) {
206
+function _wl_network_navigator_get_data($request) {
207 207
 
208 208
 	// Limit the results (defaults to 4)
209
-	$navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4;
210
-	$navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0;
209
+	$navigator_length = isset($request['limit']) ? intval($request['limit']) : 4;
210
+	$navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0;
211 211
 	$navigator_id     = $request['uniqid'];
212
-	$order_by         = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
212
+	$order_by         = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC';
213 213
 
214 214
 	$entities = $request['entities'];
215 215
 
216 216
 	// Post ID has to match an existing item
217
-	if ( ! isset( $entities ) || empty( $entities ) ) {
218
-		wp_send_json_error( 'No valid entities provided' );
217
+	if ( ! isset($entities) || empty($entities)) {
218
+		wp_send_json_error('No valid entities provided');
219 219
 	}
220 220
 
221
-	$referencing_posts = _wl_network_navigator_get_results( $entities, array(
221
+	$referencing_posts = _wl_network_navigator_get_results($entities, array(
222 222
 		'ID',
223 223
 		'post_title',
224
-	), $order_by, $navigator_length, $navigator_offset );
224
+	), $order_by, $navigator_length, $navigator_offset);
225 225
 
226 226
 	// loop over them and take the first one which is not already in the $related_posts
227 227
 	$results = array();
228
-	foreach ( $referencing_posts as $referencing_post ) {
229
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
228
+	foreach ($referencing_posts as $referencing_post) {
229
+		$serialized_entity = wl_serialize_entity($referencing_post->entity_id);
230 230
 
231 231
 		/**
232 232
 		 * Use the thumbnail.
@@ -236,33 +236,33 @@  discard block
 block discarded – undo
236 236
 		 *
237 237
 		 * @since 3.19.3 We're using the medium size image.
238 238
 		 */
239
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
239
+		$thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium');
240 240
 
241 241
 		$result = array(
242 242
 			'post'   => array(
243
-				'permalink' => get_permalink( $referencing_post->ID ),
243
+				'permalink' => get_permalink($referencing_post->ID),
244 244
 				'title'     => $referencing_post->post_title,
245 245
 				'thumbnail' => $thumbnail,
246 246
 			),
247 247
 			'entity' => array(
248 248
 				'label'     => $serialized_entity['label'],
249 249
 				'mainType'  => $serialized_entity['mainType'],
250
-				'permalink' => get_permalink( $referencing_post->entity_id ),
250
+				'permalink' => get_permalink($referencing_post->entity_id),
251 251
 			),
252 252
 		);
253 253
 
254
-		$result['post']   = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id );
255
-		$result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id );
254
+		$result['post']   = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id);
255
+		$result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id);
256 256
 
257 257
 		$results[] = $result;
258 258
 
259 259
 	}
260 260
 
261
-	if ( count( $results ) < $navigator_length ) {
262
-		$results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
261
+	if (count($results) < $navigator_length) {
262
+		$results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length);
263 263
 	}
264 264
 
265
-	$results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id );
265
+	$results = apply_filters('wl_network_navigator_results', $results, $navigator_id);
266 266
 
267 267
 	return $results;
268 268
 
@@ -276,24 +276,24 @@  discard block
 block discarded – undo
276 276
 ) {
277 277
 	global $wpdb;
278 278
 
279
-	$select = implode( ', ', array_map( function ( $item ) {
279
+	$select = implode(', ', array_map(function($item) {
280 280
 		return "p.$item AS $item";
281
-	}, (array) $fields ) );
281
+	}, (array) $fields));
282 282
 
283
-	$order_by = implode( ', ', array_map( function ( $item ) {
283
+	$order_by = implode(', ', array_map(function($item) {
284 284
 		return "p.$item";
285
-	}, (array) $order_by ) );
285
+	}, (array) $order_by));
286 286
 
287
-	$entities_in = implode( ',', array_map( function ( $item ) {
288
-		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) );
289
-		if ( isset( $entity ) ) {
287
+	$entities_in = implode(',', array_map(function($item) {
288
+		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item));
289
+		if (isset($entity)) {
290 290
 			return $entity->ID;
291 291
 		}
292
-	}, $entities ) );
292
+	}, $entities));
293 293
 
294 294
 	/** @noinspection SqlNoDataSourceInspection */
295 295
 	return $wpdb->get_results(
296
-		$wpdb->prepare( "
296
+		$wpdb->prepare("
297 297
 SELECT %3\$s, p2.ID as entity_id
298 298
  FROM {$wpdb->prefix}wl_relation_instances r1
299 299
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -319,7 +319,7 @@  discard block
 block discarded – undo
319 319
  LIMIT %1\$d
320 320
  OFFSET %2\$d
321 321
 "
322
-			, $limit, $offset, $select, $order_by )
322
+			, $limit, $offset, $select, $order_by)
323 323
 	);
324 324
 
325 325
 }
@@ -334,12 +334,12 @@  discard block
 block discarded – undo
334 334
 
335 335
 	// Temporary blocking the Navigator.
336 336
 	$results = wl_shortcode_navigator_data();
337
-	wl_core_send_json( $results );
337
+	wl_core_send_json($results);
338 338
 
339 339
 }
340 340
 
341
-add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' );
342
-add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' );
341
+add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax');
342
+add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax');
343 343
 
344 344
 /**
345 345
  * wp-json call for the navigator widget
@@ -347,13 +347,13 @@  discard block
 block discarded – undo
347 347
 function wl_shortcode_navigator_wp_json() {
348 348
 
349 349
 	$results = wl_shortcode_navigator_data();
350
-	if ( ob_get_contents() ) {
350
+	if (ob_get_contents()) {
351 351
 		ob_clean();
352 352
 	}
353 353
 
354 354
 	return array(
355 355
 		'items' => array(
356
-			array( 'values' => $results ),
356
+			array('values' => $results),
357 357
 		),
358 358
 	);
359 359
 
@@ -362,23 +362,23 @@  discard block
 block discarded – undo
362 362
 /**
363 363
  * Adding `rest_api_init` action for amp backend of navigator
364 364
  */
365
-add_action( 'rest_api_init', function () {
366
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
365
+add_action('rest_api_init', function() {
366
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
367 367
 		'methods'             => 'GET',
368 368
 		'permission_callback' => '__return_true',
369 369
 		'callback'            => 'wl_shortcode_navigator_wp_json'
370
-	) );
370
+	));
371 371
 } );
372 372
 
373 373
 /**
374 374
  * Adding `rest_api_init` action for backend of network navigator
375 375
  */
376
-add_action( 'rest_api_init', function () {
377
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
376
+add_action('rest_api_init', function() {
377
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
378 378
 		'methods'             => 'GET',
379 379
 		'callback'            => 'wl_network_navigator_wp_json',
380 380
 		'permission_callback' => '__return_true',
381
-	) );
381
+	));
382 382
 } );
383 383
 
384 384
 /**
@@ -386,24 +386,24 @@  discard block
 block discarded – undo
386 386
  *
387 387
  * @since 2.2.0
388 388
  */
389
-add_action( 'plugins_loaded', function () {
390
-	$action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( (string) $_REQUEST['action'] ) : '';
391
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
389
+add_action('plugins_loaded', function() {
390
+	$action = array_key_exists('action', $_REQUEST) ? sanitize_text_field((string) $_REQUEST['action']) : '';
391
+	if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) {
392 392
 		return;
393 393
 	}
394 394
 
395
-	remove_action( 'plugins_loaded', 'rocket_init' );
396
-	remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
397
-	remove_action( 'plugins_loaded', 'wpseo_init', 14 );
398
-}, 0 );
395
+	remove_action('plugins_loaded', 'rocket_init');
396
+	remove_action('plugins_loaded', 'wpseo_premium_init', 14);
397
+	remove_action('plugins_loaded', 'wpseo_init', 14);
398
+}, 0);
399 399
 
400
-add_action( 'init', function () {
401
-	$action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( (string) $_REQUEST['action'] ) : '';
402
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
400
+add_action('init', function() {
401
+	$action = array_key_exists('action', $_REQUEST) ? sanitize_text_field((string) $_REQUEST['action']) : '';
402
+	if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) {
403 403
 		return;
404 404
 	}
405 405
 
406
-	remove_action( 'init', 'wp_widgets_init', 1 );
407
-	remove_action( 'init', 'gglcptch_init' );
408
-}, 0 );
406
+	remove_action('init', 'wp_widgets_init', 1);
407
+	remove_action('init', 'gglcptch_init');
408
+}, 0);
409 409
 
Please login to merge, or discard this patch.