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