Completed
Push — develop ( f38351...c66f47 )
by David
02:21 queued 15s
created
src/public/class-wordlift-term-jsonld-adapter.php 2 patches
Indentation   +304 added lines, -304 removed lines patch added patch discarded remove patch
@@ -21,309 +21,309 @@
 block discarded – undo
21 21
  */
22 22
 class Wordlift_Term_JsonLd_Adapter {
23 23
 
24
-	/**
25
-	 * The {@link Wordlift_Entity_Uri_Service} instance.
26
-	 *
27
-	 * @since 3.20.0
28
-	 * @access private
29
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
30
-	 */
31
-	private $entity_uri_service;
32
-
33
-	/**
34
-	 * Instance.
35
-	 *
36
-	 * @var Wordlift_Term_JsonLd_Adapter
37
-	 */
38
-	private static $instance;
39
-
40
-	/**
41
-	 * @var Wordlift_Post_Converter
42
-	 */
43
-	private $post_id_to_jsonld_converter;
44
-
45
-	/**
46
-	 * Wordlift_Term_JsonLd_Adapter constructor.
47
-	 *
48
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
49
-	 * @param \Wordlift_Post_Converter     $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance.
50
-	 *
51
-	 * @since 3.20.0
52
-	 */
53
-	public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) {
54
-
55
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
56
-
57
-		$this->entity_uri_service          = $entity_uri_service;
58
-		$this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter;
59
-
60
-		self::$instance = $this;
61
-	}
62
-
63
-	/**
64
-	 * Get instance.
65
-	 *
66
-	 * @return Wordlift_Term_JsonLd_Adapter|static
67
-	 */
68
-	public static function get_instance() {
69
-		return self::$instance;
70
-	}
71
-
72
-	/**
73
-	 * Adds carousel json ld data to term page if the conditions match
74
-	 *
75
-	 * @return array|boolean
76
-	 */
77
-	public function get_carousel_jsonld( $id = null ) {
78
-		$posts       = $this->get_posts( $id );
79
-		$post_jsonld = array();
80
-		if ( ! is_array( $posts ) || count( $posts ) < 2 ) {
81
-			// Bail out if no posts are present.
82
-			return false;
83
-		}
84
-
85
-		if ( $id !== null ) {
86
-			$term                       = get_term( $id );
87
-			$post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) );
88
-			$thumbnail_id               = get_term_meta( $id, 'thumbnail_id', true );
89
-			if ( ! empty( $thumbnail_id ) ) {
90
-				$post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id );
91
-			}
92
-		}
93
-
94
-		// More than 2 items are present, so construct the post_jsonld data
95
-		$post_jsonld['@context']        = 'https://schema.org';
96
-		$post_jsonld['@type']           = 'ItemList';
97
-		$post_jsonld['url']             = $this->get_term_url( $id );
98
-		$post_jsonld['itemListElement'] = array();
99
-		$position                       = 1;
100
-
101
-		foreach ( $posts as $post_id ) {
102
-			$result = array(
103
-				'@type'    => 'ListItem',
104
-				'position' => $position,
105
-				/**
106
-				 * We can't use `item` here unless we change the URL for the item to point to the current page.
107
-				 *
108
-				 * See https://developers.google.com/search/docs/data-types/carousel
109
-				 */
110
-				'url'      => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ),
111
-			);
112
-			array_push( $post_jsonld['itemListElement'], $result );
113
-			++ $position;
114
-		}
115
-
116
-		return $post_jsonld;
117
-	}
118
-
119
-	/**
120
-	 * Get posts.
121
-	 *
122
-	 * @param $id
123
-	 *
124
-	 * @return array|int[]|string[]|WP_Error|null
125
-	 */
126
-	private function get_posts( $id ) {
127
-		global $wp_query;
128
-
129
-		if ( is_array( $wp_query->posts ) ) {
130
-			return array_map(
131
-				function ( $post ) {
132
-					return $post->ID;
133
-				},
134
-				$wp_query->posts
135
-			);
136
-		}
137
-
138
-		if ( $id === null ) {
139
-			return null;
140
-		}
141
-
142
-		$term = get_term( $id );
143
-
144
-		return get_objects_in_term( $id, $term->taxonomy );
145
-	}
146
-
147
-	/**
148
-	 * Hook to `wp_head` to print the JSON-LD.
149
-	 *
150
-	 * @since 3.20.0
151
-	 */
152
-	public function wp_head() {
153
-		$query_object = get_queried_object();
154
-
155
-		// Check if it is a term page.
156
-		if ( ! $query_object instanceof WP_Term ) {
157
-			return;
158
-		}
159
-
160
-		// Bail out if `wl_jsonld_enabled` isn't enabled.
161
-		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
162
-			return;
163
-		}
164
-
165
-		$term_id = $query_object->term_id;
166
-
167
-		$jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE );
168
-
169
-		// Bail out if the JSON-LD is empty.
170
-		if ( empty( $jsonld ) ) {
171
-			return;
172
-		}
173
-
174
-		$jsonld_string = wp_json_encode( $jsonld );
175
-
176
-		$jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>';
177
-		$jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id );
178
-
179
-		echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output.
180
-
181
-	}
182
-
183
-	/**
184
-	 * Get.
185
-	 *
186
-	 * @param $id
187
-	 * @param $context
188
-	 * @param $is_recursive_call
189
-	 *
190
-	 * @return array
191
-	 */
192
-	public function get( $id, $context, $is_recursive_call = false ) {
193
-		/**
194
-		 * Support for carousel rich snippet, get jsonld data present
195
-		 * for all the posts shown in the term page, and add the jsonld data
196
-		 * to list
197
-		 *
198
-		 * see here: https://developers.google.com/search/docs/data-types/carousel
199
-		 *
200
-		 * @since 3.26.0
201
-		 */
202
-		$jsonld_array = array();
203
-
204
-		if ( Jsonld_Context_Enum::PAGE === $context ) {
205
-			$carousel_data = $this->get_carousel_jsonld( $id );
206
-			if ( $carousel_data ) {
207
-				$jsonld_array[] = $carousel_data;
208
-			}
209
-		}
210
-
211
-		$entities_jsonld_array = $this->get_entity_jsonld( $id, $context );
212
-
213
-		$result = array(
214
-			'jsonld'     => array_merge( $jsonld_array, $entities_jsonld_array ),
215
-			'references' => array(),
216
-		);
217
-
218
-		/**
219
-		 * @since 3.26.3
220
-		 * Filter: wl_term_jsonld_array
221
-		 * @var $id int Term id
222
-		 * @var $jsonld_array array An array containing jsonld for term and entities.
223
-		 * @var $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum
224
-		 */
225
-		$arr = apply_filters( 'wl_term_jsonld_array', $result, $id, $context );
226
-
227
-		$references = array();
228
-
229
-		// Don't expand nested references, it will lead to an infinite loop.
230
-		if ( ! $is_recursive_call ) {
231
-			/**
232
-			 * @since 3.32.0
233
-			 * Expand the references returned by this filter.
234
-			 */
235
-			$references = $this->expand_references( $arr['references'] );
236
-		}
237
-
238
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
239
-
240
-		return $jsonld_array;
241
-	}
242
-
243
-	/**
244
-	 * Get term url.
245
-	 *
246
-	 * @param $id
247
-	 *
248
-	 * @return array|false|int|mixed|string|WP_Error|WP_Term|null
249
-	 */
250
-	private function get_term_url( $id ) {
251
-		if ( null === $id ) {
252
-			return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : '';
253
-		}
254
-
255
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
256
-		if ( ! empty( $maybe_url ) ) {
257
-			return $maybe_url;
258
-		}
259
-
260
-		return get_term_link( $id );
261
-	}
262
-
263
-	/**
264
-	 * Return jsonld for entities bound to terms.
265
-	 *
266
-	 * @param int $term_id Term ID.
267
-	 * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
268
-	 *
269
-	 * @return array
270
-	 */
271
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
272
-	private function get_entity_jsonld( $term_id, $context ) {
273
-
274
-		// The `_wl_entity_id` are URIs.
275
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
276
-		$entity_uri_service = $this->entity_uri_service;
277
-
278
-		$wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance();
279
-
280
-		$local_entity_ids = array_filter(
281
-			$entity_ids,
282
-			function ( $uri ) use ( $entity_uri_service ) {
283
-				return $entity_uri_service->is_internal( $uri );
284
-			}
285
-		);
286
-
287
-		// Bail out if there are no entities.
288
-		if ( empty( $local_entity_ids ) ) {
289
-			return array();
290
-		}
291
-
292
-		$post            = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
293
-		$entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID );
294
-		// Reset the `url` to the term page.
295
-		$entities_jsonld[0]['url'] = get_term_link( $term_id );
296
-
297
-		return $entities_jsonld;
298
-	}
299
-
300
-	/**
301
-	 * @param $references
302
-	 *
303
-	 * @return array
304
-	 */
305
-	private function expand_references( $references ) {
306
-		if ( ! is_array( $references ) ) {
307
-			return array();
308
-		}
309
-		$references_jsonld = array();
310
-		// Expand the references.
311
-		foreach ( $references as $reference ) {
312
-			if ( $reference instanceof Term_Reference ) {
313
-				// Second level references won't be expanded.
314
-				$references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) );
315
-			} elseif ( is_numeric( $reference ) ) {
316
-				$ref_2               = array();
317
-				$ref_info_2          = array();
318
-				$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() );
319
-			} elseif ( $reference instanceof Post_Reference ) {
320
-				$ref_2               = array();
321
-				$ref_info_2          = array();
322
-				$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() );
323
-			}
324
-		}
325
-
326
-		return $references_jsonld;
327
-	}
24
+    /**
25
+     * The {@link Wordlift_Entity_Uri_Service} instance.
26
+     *
27
+     * @since 3.20.0
28
+     * @access private
29
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
30
+     */
31
+    private $entity_uri_service;
32
+
33
+    /**
34
+     * Instance.
35
+     *
36
+     * @var Wordlift_Term_JsonLd_Adapter
37
+     */
38
+    private static $instance;
39
+
40
+    /**
41
+     * @var Wordlift_Post_Converter
42
+     */
43
+    private $post_id_to_jsonld_converter;
44
+
45
+    /**
46
+     * Wordlift_Term_JsonLd_Adapter constructor.
47
+     *
48
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
49
+     * @param \Wordlift_Post_Converter     $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance.
50
+     *
51
+     * @since 3.20.0
52
+     */
53
+    public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) {
54
+
55
+        add_action( 'wp_head', array( $this, 'wp_head' ) );
56
+
57
+        $this->entity_uri_service          = $entity_uri_service;
58
+        $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter;
59
+
60
+        self::$instance = $this;
61
+    }
62
+
63
+    /**
64
+     * Get instance.
65
+     *
66
+     * @return Wordlift_Term_JsonLd_Adapter|static
67
+     */
68
+    public static function get_instance() {
69
+        return self::$instance;
70
+    }
71
+
72
+    /**
73
+     * Adds carousel json ld data to term page if the conditions match
74
+     *
75
+     * @return array|boolean
76
+     */
77
+    public function get_carousel_jsonld( $id = null ) {
78
+        $posts       = $this->get_posts( $id );
79
+        $post_jsonld = array();
80
+        if ( ! is_array( $posts ) || count( $posts ) < 2 ) {
81
+            // Bail out if no posts are present.
82
+            return false;
83
+        }
84
+
85
+        if ( $id !== null ) {
86
+            $term                       = get_term( $id );
87
+            $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) );
88
+            $thumbnail_id               = get_term_meta( $id, 'thumbnail_id', true );
89
+            if ( ! empty( $thumbnail_id ) ) {
90
+                $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id );
91
+            }
92
+        }
93
+
94
+        // More than 2 items are present, so construct the post_jsonld data
95
+        $post_jsonld['@context']        = 'https://schema.org';
96
+        $post_jsonld['@type']           = 'ItemList';
97
+        $post_jsonld['url']             = $this->get_term_url( $id );
98
+        $post_jsonld['itemListElement'] = array();
99
+        $position                       = 1;
100
+
101
+        foreach ( $posts as $post_id ) {
102
+            $result = array(
103
+                '@type'    => 'ListItem',
104
+                'position' => $position,
105
+                /**
106
+                 * We can't use `item` here unless we change the URL for the item to point to the current page.
107
+                 *
108
+                 * See https://developers.google.com/search/docs/data-types/carousel
109
+                 */
110
+                'url'      => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ),
111
+            );
112
+            array_push( $post_jsonld['itemListElement'], $result );
113
+            ++ $position;
114
+        }
115
+
116
+        return $post_jsonld;
117
+    }
118
+
119
+    /**
120
+     * Get posts.
121
+     *
122
+     * @param $id
123
+     *
124
+     * @return array|int[]|string[]|WP_Error|null
125
+     */
126
+    private function get_posts( $id ) {
127
+        global $wp_query;
128
+
129
+        if ( is_array( $wp_query->posts ) ) {
130
+            return array_map(
131
+                function ( $post ) {
132
+                    return $post->ID;
133
+                },
134
+                $wp_query->posts
135
+            );
136
+        }
137
+
138
+        if ( $id === null ) {
139
+            return null;
140
+        }
141
+
142
+        $term = get_term( $id );
143
+
144
+        return get_objects_in_term( $id, $term->taxonomy );
145
+    }
146
+
147
+    /**
148
+     * Hook to `wp_head` to print the JSON-LD.
149
+     *
150
+     * @since 3.20.0
151
+     */
152
+    public function wp_head() {
153
+        $query_object = get_queried_object();
154
+
155
+        // Check if it is a term page.
156
+        if ( ! $query_object instanceof WP_Term ) {
157
+            return;
158
+        }
159
+
160
+        // Bail out if `wl_jsonld_enabled` isn't enabled.
161
+        if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
162
+            return;
163
+        }
164
+
165
+        $term_id = $query_object->term_id;
166
+
167
+        $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE );
168
+
169
+        // Bail out if the JSON-LD is empty.
170
+        if ( empty( $jsonld ) ) {
171
+            return;
172
+        }
173
+
174
+        $jsonld_string = wp_json_encode( $jsonld );
175
+
176
+        $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>';
177
+        $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id );
178
+
179
+        echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output.
180
+
181
+    }
182
+
183
+    /**
184
+     * Get.
185
+     *
186
+     * @param $id
187
+     * @param $context
188
+     * @param $is_recursive_call
189
+     *
190
+     * @return array
191
+     */
192
+    public function get( $id, $context, $is_recursive_call = false ) {
193
+        /**
194
+         * Support for carousel rich snippet, get jsonld data present
195
+         * for all the posts shown in the term page, and add the jsonld data
196
+         * to list
197
+         *
198
+         * see here: https://developers.google.com/search/docs/data-types/carousel
199
+         *
200
+         * @since 3.26.0
201
+         */
202
+        $jsonld_array = array();
203
+
204
+        if ( Jsonld_Context_Enum::PAGE === $context ) {
205
+            $carousel_data = $this->get_carousel_jsonld( $id );
206
+            if ( $carousel_data ) {
207
+                $jsonld_array[] = $carousel_data;
208
+            }
209
+        }
210
+
211
+        $entities_jsonld_array = $this->get_entity_jsonld( $id, $context );
212
+
213
+        $result = array(
214
+            'jsonld'     => array_merge( $jsonld_array, $entities_jsonld_array ),
215
+            'references' => array(),
216
+        );
217
+
218
+        /**
219
+         * @since 3.26.3
220
+         * Filter: wl_term_jsonld_array
221
+         * @var $id int Term id
222
+         * @var $jsonld_array array An array containing jsonld for term and entities.
223
+         * @var $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum
224
+         */
225
+        $arr = apply_filters( 'wl_term_jsonld_array', $result, $id, $context );
226
+
227
+        $references = array();
228
+
229
+        // Don't expand nested references, it will lead to an infinite loop.
230
+        if ( ! $is_recursive_call ) {
231
+            /**
232
+             * @since 3.32.0
233
+             * Expand the references returned by this filter.
234
+             */
235
+            $references = $this->expand_references( $arr['references'] );
236
+        }
237
+
238
+        $jsonld_array = array_merge( $arr['jsonld'], $references );
239
+
240
+        return $jsonld_array;
241
+    }
242
+
243
+    /**
244
+     * Get term url.
245
+     *
246
+     * @param $id
247
+     *
248
+     * @return array|false|int|mixed|string|WP_Error|WP_Term|null
249
+     */
250
+    private function get_term_url( $id ) {
251
+        if ( null === $id ) {
252
+            return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : '';
253
+        }
254
+
255
+        $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
256
+        if ( ! empty( $maybe_url ) ) {
257
+            return $maybe_url;
258
+        }
259
+
260
+        return get_term_link( $id );
261
+    }
262
+
263
+    /**
264
+     * Return jsonld for entities bound to terms.
265
+     *
266
+     * @param int $term_id Term ID.
267
+     * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
268
+     *
269
+     * @return array
270
+     */
271
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
272
+    private function get_entity_jsonld( $term_id, $context ) {
273
+
274
+        // The `_wl_entity_id` are URIs.
275
+        $entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
276
+        $entity_uri_service = $this->entity_uri_service;
277
+
278
+        $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance();
279
+
280
+        $local_entity_ids = array_filter(
281
+            $entity_ids,
282
+            function ( $uri ) use ( $entity_uri_service ) {
283
+                return $entity_uri_service->is_internal( $uri );
284
+            }
285
+        );
286
+
287
+        // Bail out if there are no entities.
288
+        if ( empty( $local_entity_ids ) ) {
289
+            return array();
290
+        }
291
+
292
+        $post            = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
293
+        $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID );
294
+        // Reset the `url` to the term page.
295
+        $entities_jsonld[0]['url'] = get_term_link( $term_id );
296
+
297
+        return $entities_jsonld;
298
+    }
299
+
300
+    /**
301
+     * @param $references
302
+     *
303
+     * @return array
304
+     */
305
+    private function expand_references( $references ) {
306
+        if ( ! is_array( $references ) ) {
307
+            return array();
308
+        }
309
+        $references_jsonld = array();
310
+        // Expand the references.
311
+        foreach ( $references as $reference ) {
312
+            if ( $reference instanceof Term_Reference ) {
313
+                // Second level references won't be expanded.
314
+                $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) );
315
+            } elseif ( is_numeric( $reference ) ) {
316
+                $ref_2               = array();
317
+                $ref_info_2          = array();
318
+                $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() );
319
+            } elseif ( $reference instanceof Post_Reference ) {
320
+                $ref_2               = array();
321
+                $ref_info_2          = array();
322
+                $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() );
323
+            }
324
+        }
325
+
326
+        return $references_jsonld;
327
+    }
328 328
 
329 329
 }
Please login to merge, or discard this patch.
Spacing   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -50,9 +50,9 @@  discard block
 block discarded – undo
50 50
 	 *
51 51
 	 * @since 3.20.0
52 52
 	 */
53
-	public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) {
53
+	public function __construct($entity_uri_service, $post_id_to_jsonld_converter) {
54 54
 
55
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
55
+		add_action('wp_head', array($this, 'wp_head'));
56 56
 
57 57
 		$this->entity_uri_service          = $entity_uri_service;
58 58
 		$this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter;
@@ -74,31 +74,31 @@  discard block
 block discarded – undo
74 74
 	 *
75 75
 	 * @return array|boolean
76 76
 	 */
77
-	public function get_carousel_jsonld( $id = null ) {
78
-		$posts       = $this->get_posts( $id );
77
+	public function get_carousel_jsonld($id = null) {
78
+		$posts       = $this->get_posts($id);
79 79
 		$post_jsonld = array();
80
-		if ( ! is_array( $posts ) || count( $posts ) < 2 ) {
80
+		if ( ! is_array($posts) || count($posts) < 2) {
81 81
 			// Bail out if no posts are present.
82 82
 			return false;
83 83
 		}
84 84
 
85
-		if ( $id !== null ) {
86
-			$term                       = get_term( $id );
87
-			$post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) );
88
-			$thumbnail_id               = get_term_meta( $id, 'thumbnail_id', true );
89
-			if ( ! empty( $thumbnail_id ) ) {
90
-				$post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id );
85
+		if ($id !== null) {
86
+			$term                       = get_term($id);
87
+			$post_jsonld['description'] = wp_strip_all_tags(strip_shortcodes($term->description));
88
+			$thumbnail_id               = get_term_meta($id, 'thumbnail_id', true);
89
+			if ( ! empty($thumbnail_id)) {
90
+				$post_jsonld['image'] = wp_get_attachment_url($thumbnail_id);
91 91
 			}
92 92
 		}
93 93
 
94 94
 		// More than 2 items are present, so construct the post_jsonld data
95 95
 		$post_jsonld['@context']        = 'https://schema.org';
96 96
 		$post_jsonld['@type']           = 'ItemList';
97
-		$post_jsonld['url']             = $this->get_term_url( $id );
97
+		$post_jsonld['url']             = $this->get_term_url($id);
98 98
 		$post_jsonld['itemListElement'] = array();
99 99
 		$position                       = 1;
100 100
 
101
-		foreach ( $posts as $post_id ) {
101
+		foreach ($posts as $post_id) {
102 102
 			$result = array(
103 103
 				'@type'    => 'ListItem',
104 104
 				'position' => $position,
@@ -107,10 +107,10 @@  discard block
 block discarded – undo
107 107
 				 *
108 108
 				 * See https://developers.google.com/search/docs/data-types/carousel
109 109
 				 */
110
-				'url'      => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ),
110
+				'url'      => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id),
111 111
 			);
112
-			array_push( $post_jsonld['itemListElement'], $result );
113
-			++ $position;
112
+			array_push($post_jsonld['itemListElement'], $result);
113
+			++$position;
114 114
 		}
115 115
 
116 116
 		return $post_jsonld;
@@ -123,25 +123,25 @@  discard block
 block discarded – undo
123 123
 	 *
124 124
 	 * @return array|int[]|string[]|WP_Error|null
125 125
 	 */
126
-	private function get_posts( $id ) {
126
+	private function get_posts($id) {
127 127
 		global $wp_query;
128 128
 
129
-		if ( is_array( $wp_query->posts ) ) {
129
+		if (is_array($wp_query->posts)) {
130 130
 			return array_map(
131
-				function ( $post ) {
131
+				function($post) {
132 132
 					return $post->ID;
133 133
 				},
134 134
 				$wp_query->posts
135 135
 			);
136 136
 		}
137 137
 
138
-		if ( $id === null ) {
138
+		if ($id === null) {
139 139
 			return null;
140 140
 		}
141 141
 
142
-		$term = get_term( $id );
142
+		$term = get_term($id);
143 143
 
144
-		return get_objects_in_term( $id, $term->taxonomy );
144
+		return get_objects_in_term($id, $term->taxonomy);
145 145
 	}
146 146
 
147 147
 	/**
@@ -153,28 +153,28 @@  discard block
 block discarded – undo
153 153
 		$query_object = get_queried_object();
154 154
 
155 155
 		// Check if it is a term page.
156
-		if ( ! $query_object instanceof WP_Term ) {
156
+		if ( ! $query_object instanceof WP_Term) {
157 157
 			return;
158 158
 		}
159 159
 
160 160
 		// Bail out if `wl_jsonld_enabled` isn't enabled.
161
-		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
161
+		if ( ! apply_filters('wl_jsonld_enabled', true)) {
162 162
 			return;
163 163
 		}
164 164
 
165 165
 		$term_id = $query_object->term_id;
166 166
 
167
-		$jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE );
167
+		$jsonld = $this->get($term_id, Jsonld_Context_Enum::PAGE);
168 168
 
169 169
 		// Bail out if the JSON-LD is empty.
170
-		if ( empty( $jsonld ) ) {
170
+		if (empty($jsonld)) {
171 171
 			return;
172 172
 		}
173 173
 
174
-		$jsonld_string = wp_json_encode( $jsonld );
174
+		$jsonld_string = wp_json_encode($jsonld);
175 175
 
176
-		$jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>';
177
-		$jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id );
176
+		$jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">'.$jsonld_string.'</script>';
177
+		$jsonld_term_html_output = apply_filters('wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id);
178 178
 
179 179
 		echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output.
180 180
 
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
 	 *
190 190
 	 * @return array
191 191
 	 */
192
-	public function get( $id, $context, $is_recursive_call = false ) {
192
+	public function get($id, $context, $is_recursive_call = false) {
193 193
 		/**
194 194
 		 * Support for carousel rich snippet, get jsonld data present
195 195
 		 * for all the posts shown in the term page, and add the jsonld data
@@ -201,17 +201,17 @@  discard block
 block discarded – undo
201 201
 		 */
202 202
 		$jsonld_array = array();
203 203
 
204
-		if ( Jsonld_Context_Enum::PAGE === $context ) {
205
-			$carousel_data = $this->get_carousel_jsonld( $id );
206
-			if ( $carousel_data ) {
204
+		if (Jsonld_Context_Enum::PAGE === $context) {
205
+			$carousel_data = $this->get_carousel_jsonld($id);
206
+			if ($carousel_data) {
207 207
 				$jsonld_array[] = $carousel_data;
208 208
 			}
209 209
 		}
210 210
 
211
-		$entities_jsonld_array = $this->get_entity_jsonld( $id, $context );
211
+		$entities_jsonld_array = $this->get_entity_jsonld($id, $context);
212 212
 
213 213
 		$result = array(
214
-			'jsonld'     => array_merge( $jsonld_array, $entities_jsonld_array ),
214
+			'jsonld'     => array_merge($jsonld_array, $entities_jsonld_array),
215 215
 			'references' => array(),
216 216
 		);
217 217
 
@@ -222,20 +222,20 @@  discard block
 block discarded – undo
222 222
 		 * @var $jsonld_array array An array containing jsonld for term and entities.
223 223
 		 * @var $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum
224 224
 		 */
225
-		$arr = apply_filters( 'wl_term_jsonld_array', $result, $id, $context );
225
+		$arr = apply_filters('wl_term_jsonld_array', $result, $id, $context);
226 226
 
227 227
 		$references = array();
228 228
 
229 229
 		// Don't expand nested references, it will lead to an infinite loop.
230
-		if ( ! $is_recursive_call ) {
230
+		if ( ! $is_recursive_call) {
231 231
 			/**
232 232
 			 * @since 3.32.0
233 233
 			 * Expand the references returned by this filter.
234 234
 			 */
235
-			$references = $this->expand_references( $arr['references'] );
235
+			$references = $this->expand_references($arr['references']);
236 236
 		}
237 237
 
238
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
238
+		$jsonld_array = array_merge($arr['jsonld'], $references);
239 239
 
240 240
 		return $jsonld_array;
241 241
 	}
@@ -247,17 +247,17 @@  discard block
 block discarded – undo
247 247
 	 *
248 248
 	 * @return array|false|int|mixed|string|WP_Error|WP_Term|null
249 249
 	 */
250
-	private function get_term_url( $id ) {
251
-		if ( null === $id ) {
252
-			return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : '';
250
+	private function get_term_url($id) {
251
+		if (null === $id) {
252
+			return isset($_SERVER['REQUEST_URI']) ? filter_var(wp_unslash($_SERVER['REQUEST_URI']), FILTER_SANITIZE_URL) : '';
253 253
 		}
254 254
 
255
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
256
-		if ( ! empty( $maybe_url ) ) {
255
+		$maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true);
256
+		if ( ! empty($maybe_url)) {
257 257
 			return $maybe_url;
258 258
 		}
259 259
 
260
-		return get_term_link( $id );
260
+		return get_term_link($id);
261 261
 	}
262 262
 
263 263
 	/**
@@ -269,30 +269,30 @@  discard block
 block discarded – undo
269 269
 	 * @return array
270 270
 	 */
271 271
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
272
-	private function get_entity_jsonld( $term_id, $context ) {
272
+	private function get_entity_jsonld($term_id, $context) {
273 273
 
274 274
 		// The `_wl_entity_id` are URIs.
275
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
275
+		$entity_ids         = get_term_meta($term_id, '_wl_entity_id');
276 276
 		$entity_uri_service = $this->entity_uri_service;
277 277
 
278 278
 		$wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance();
279 279
 
280 280
 		$local_entity_ids = array_filter(
281 281
 			$entity_ids,
282
-			function ( $uri ) use ( $entity_uri_service ) {
283
-				return $entity_uri_service->is_internal( $uri );
282
+			function($uri) use ($entity_uri_service) {
283
+				return $entity_uri_service->is_internal($uri);
284 284
 			}
285 285
 		);
286 286
 
287 287
 		// Bail out if there are no entities.
288
-		if ( empty( $local_entity_ids ) ) {
288
+		if (empty($local_entity_ids)) {
289 289
 			return array();
290 290
 		}
291 291
 
292
-		$post            = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
293
-		$entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID );
292
+		$post            = $this->entity_uri_service->get_entity(array_shift($local_entity_ids));
293
+		$entities_jsonld = $wordlift_jsonld_service->get_jsonld(false, $post->ID);
294 294
 		// Reset the `url` to the term page.
295
-		$entities_jsonld[0]['url'] = get_term_link( $term_id );
295
+		$entities_jsonld[0]['url'] = get_term_link($term_id);
296 296
 
297 297
 		return $entities_jsonld;
298 298
 	}
@@ -302,24 +302,24 @@  discard block
 block discarded – undo
302 302
 	 *
303 303
 	 * @return array
304 304
 	 */
305
-	private function expand_references( $references ) {
306
-		if ( ! is_array( $references ) ) {
305
+	private function expand_references($references) {
306
+		if ( ! is_array($references)) {
307 307
 			return array();
308 308
 		}
309 309
 		$references_jsonld = array();
310 310
 		// Expand the references.
311
-		foreach ( $references as $reference ) {
312
-			if ( $reference instanceof Term_Reference ) {
311
+		foreach ($references as $reference) {
312
+			if ($reference instanceof Term_Reference) {
313 313
 				// Second level references won't be expanded.
314
-				$references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) );
315
-			} elseif ( is_numeric( $reference ) ) {
314
+				$references_jsonld[] = current($this->get($reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true));
315
+			} elseif (is_numeric($reference)) {
316 316
 				$ref_2               = array();
317 317
 				$ref_info_2          = array();
318
-				$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() );
319
-			} elseif ( $reference instanceof Post_Reference ) {
318
+				$references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference, $ref_2, $ref_info_2, new Relations());
319
+			} elseif ($reference instanceof Post_Reference) {
320 320
 				$ref_2               = array();
321 321
 				$ref_info_2          = array();
322
-				$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() );
322
+				$references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference->get_id(), $ref_2, $ref_info_2, new Relations());
323 323
 			}
324 324
 		}
325 325
 
Please login to merge, or discard this patch.