Completed
Pull Request — master (#1449)
by Naveen
01:07
created
src/public/class-wordlift-term-jsonld-adapter.php 2 patches
Indentation   +260 added lines, -260 removed lines patch added patch discarded remove patch
@@ -19,265 +19,265 @@
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Term_JsonLd_Adapter {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Entity_Uri_Service} instance.
24
-	 *
25
-	 * @since 3.20.0
26
-	 * @access private
27
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
28
-	 */
29
-	private $entity_uri_service;
30
-
31
-	private static $instance;
32
-
33
-	/**
34
-	 * @var Wordlift_Post_Converter
35
-	 */
36
-	private $post_id_to_jsonld_converter;
37
-
38
-	/**
39
-	 * Wordlift_Term_JsonLd_Adapter constructor.
40
-	 *
41
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
42
-	 * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance.
43
-	 *
44
-	 * @since 3.20.0
45
-	 *
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 ( ! is_null( $id ) ) {
76
-			$term                       = get_term( $id );
77
-			$post_jsonld['description'] = strip_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 += 1;
104
-		}
105
-
106
-		return $post_jsonld;
107
-	}
108
-
109
-	private function get_posts( $id ) {
110
-		global $wp_query;
111
-
112
-		if ( ! is_null( $wp_query->posts ) ) {
113
-			return array_map( function ( $post ) {
114
-				return $post->ID;
115
-			}, $wp_query->posts );
116
-		}
117
-
118
-		if ( is_null( $id ) ) {
119
-			return null;
120
-		}
121
-
122
-		$term = get_term( $id );
123
-
124
-		return get_objects_in_term( $id, $term->taxonomy );
125
-	}
126
-
127
-	/**
128
-	 * Hook to `wp_head` to print the JSON-LD.
129
-	 *
130
-	 * @since 3.20.0
131
-	 */
132
-	public function wp_head() {
133
-		$query_object = get_queried_object();
134
-
135
-
136
-		// Check if it is a term page.
137
-		if ( ! $query_object instanceof WP_Term ) {
138
-			return;
139
-		}
140
-
141
-		// Bail out if `wl_jsonld_enabled` isn't enabled.
142
-		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
143
-			return;
144
-		}
145
-
146
-		$term_id = $query_object->term_id;
147
-
148
-		$jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE );
149
-
150
-		// Bail out if the JSON-LD is empty.
151
-		if ( empty( $jsonld ) ) {
152
-			return;
153
-		}
154
-
155
-		$jsonld_string = wp_json_encode( $jsonld );
156
-
157
-		echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>";
158
-
159
-	}
160
-
161
-	public function get( $id, $context ) {
162
-		/**
163
-		 * Support for carousel rich snippet, get jsonld data present
164
-		 * for all the posts shown in the term page, and add the jsonld data
165
-		 * to list
166
-		 *
167
-		 * see here: https://developers.google.com/search/docs/data-types/carousel
168
-		 *
169
-		 * @since 3.26.0
170
-		 */
171
-		$jsonld_array = array();
172
-
173
-		if ( Jsonld_Context_Enum::PAGE === $context ) {
174
-			$carousel_data = $this->get_carousel_jsonld( $id );
175
-			if ( $carousel_data ) {
176
-				$jsonld_array[] = $carousel_data;
177
-			}
178
-		}
179
-
180
-		$entities_jsonld_array = $this->get_entity_jsonld( $id, $context );
181
-
182
-		$result = array(
183
-			'jsonld'     => array_merge( $jsonld_array, $entities_jsonld_array ),
184
-			'references' => array()
185
-		);
186
-
187
-
188
-		/**
189
-		 * @since 3.26.3
190
-		 * Filter: wl_term_jsonld_array
191
-		 * @var $id int Term id
192
-		 * @var $jsonld_array array An array containing jsonld for term and entities.
193
-		 */
194
-		$arr = apply_filters( 'wl_term_jsonld_array', $result, $id );
195
-
196
-		/**
197
-		 * @since 3.32.0
198
-		 * Expand the references returned by this filter.
199
-		 */
200
-		$references = $this->expand_references( $arr['references'] );
201
-
202
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
203
-
204
-		return $jsonld_array;
205
-	}
206
-
207
-	private function get_term_url( $id ) {
208
-		if ( is_null( $id ) ) {
209
-			return $_SERVER['REQUEST_URI'];
210
-		}
211
-
212
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
213
-		if ( ! empty( $maybe_url ) ) {
214
-			return $maybe_url;
215
-		}
216
-
217
-		return get_term_link( $id );
218
-	}
219
-
220
-	/**
221
-	 * Return jsonld for entities bound to terms.
222
-	 *
223
-	 * @param int $term_id Term ID.
224
-	 * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
225
-	 *
226
-	 * @return array
227
-	 */
228
-	private function get_entity_jsonld( $term_id, $context ) {
229
-
230
-		// The `_wl_entity_id` are URIs.
231
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
232
-		$entity_uri_service = $this->entity_uri_service;
233
-
234
-		$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
235
-			return $entity_uri_service->is_internal( $uri );
236
-		} );
237
-
238
-		// Bail out if there are no entities.
239
-		if ( empty( $local_entity_ids ) ) {
240
-			return array();
241
-		}
242
-
243
-		$post   = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
244
-		$jsonld = $this->post_id_to_jsonld_converter->convert( $post->ID );
245
-		// Reset the `url` to the term page.
246
-		$jsonld[0]['url'] = get_term_link( $term_id );
247
-
248
-		return $jsonld;
249
-	}
250
-
251
-
252
-	/**
253
-	 * @param $references
254
-	 *
255
-	 * @return array
256
-	 */
257
-	private function expand_references( $references ) {
258
-
259
-		// @TODO: we are assuming all the references are posts
260
-		// in this method, since now terms are getting converted to
261
-		// entities, this might not be true in all cases.
262
-		if ( ! is_array( $references ) ) {
263
-			return array();
264
-		}
265
-		$references_jsonld = array();
266
-		// Expand the references.
267
-		foreach ( $references as $reference ) {
268
-			$post_id = $reference;
269
-			if ( $reference instanceof Reference ) {
270
-				$post_id = $reference->get_id();
271
-			}
272
-			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
273
-		}
274
-
275
-		return $references_jsonld;
276
-
277
-	}
278
-
279
-	private function make_one( $value ) {
280
-		return count( $value ) === 1 ? $value[0] : $value;
281
-	}
22
+    /**
23
+     * The {@link Wordlift_Entity_Uri_Service} instance.
24
+     *
25
+     * @since 3.20.0
26
+     * @access private
27
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
28
+     */
29
+    private $entity_uri_service;
30
+
31
+    private static $instance;
32
+
33
+    /**
34
+     * @var Wordlift_Post_Converter
35
+     */
36
+    private $post_id_to_jsonld_converter;
37
+
38
+    /**
39
+     * Wordlift_Term_JsonLd_Adapter constructor.
40
+     *
41
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
42
+     * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance.
43
+     *
44
+     * @since 3.20.0
45
+     *
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 ( ! is_null( $id ) ) {
76
+            $term                       = get_term( $id );
77
+            $post_jsonld['description'] = strip_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 += 1;
104
+        }
105
+
106
+        return $post_jsonld;
107
+    }
108
+
109
+    private function get_posts( $id ) {
110
+        global $wp_query;
111
+
112
+        if ( ! is_null( $wp_query->posts ) ) {
113
+            return array_map( function ( $post ) {
114
+                return $post->ID;
115
+            }, $wp_query->posts );
116
+        }
117
+
118
+        if ( is_null( $id ) ) {
119
+            return null;
120
+        }
121
+
122
+        $term = get_term( $id );
123
+
124
+        return get_objects_in_term( $id, $term->taxonomy );
125
+    }
126
+
127
+    /**
128
+     * Hook to `wp_head` to print the JSON-LD.
129
+     *
130
+     * @since 3.20.0
131
+     */
132
+    public function wp_head() {
133
+        $query_object = get_queried_object();
134
+
135
+
136
+        // Check if it is a term page.
137
+        if ( ! $query_object instanceof WP_Term ) {
138
+            return;
139
+        }
140
+
141
+        // Bail out if `wl_jsonld_enabled` isn't enabled.
142
+        if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
143
+            return;
144
+        }
145
+
146
+        $term_id = $query_object->term_id;
147
+
148
+        $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE );
149
+
150
+        // Bail out if the JSON-LD is empty.
151
+        if ( empty( $jsonld ) ) {
152
+            return;
153
+        }
154
+
155
+        $jsonld_string = wp_json_encode( $jsonld );
156
+
157
+        echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>";
158
+
159
+    }
160
+
161
+    public function get( $id, $context ) {
162
+        /**
163
+         * Support for carousel rich snippet, get jsonld data present
164
+         * for all the posts shown in the term page, and add the jsonld data
165
+         * to list
166
+         *
167
+         * see here: https://developers.google.com/search/docs/data-types/carousel
168
+         *
169
+         * @since 3.26.0
170
+         */
171
+        $jsonld_array = array();
172
+
173
+        if ( Jsonld_Context_Enum::PAGE === $context ) {
174
+            $carousel_data = $this->get_carousel_jsonld( $id );
175
+            if ( $carousel_data ) {
176
+                $jsonld_array[] = $carousel_data;
177
+            }
178
+        }
179
+
180
+        $entities_jsonld_array = $this->get_entity_jsonld( $id, $context );
181
+
182
+        $result = array(
183
+            'jsonld'     => array_merge( $jsonld_array, $entities_jsonld_array ),
184
+            'references' => array()
185
+        );
186
+
187
+
188
+        /**
189
+         * @since 3.26.3
190
+         * Filter: wl_term_jsonld_array
191
+         * @var $id int Term id
192
+         * @var $jsonld_array array An array containing jsonld for term and entities.
193
+         */
194
+        $arr = apply_filters( 'wl_term_jsonld_array', $result, $id );
195
+
196
+        /**
197
+         * @since 3.32.0
198
+         * Expand the references returned by this filter.
199
+         */
200
+        $references = $this->expand_references( $arr['references'] );
201
+
202
+        $jsonld_array = array_merge( $arr['jsonld'], $references );
203
+
204
+        return $jsonld_array;
205
+    }
206
+
207
+    private function get_term_url( $id ) {
208
+        if ( is_null( $id ) ) {
209
+            return $_SERVER['REQUEST_URI'];
210
+        }
211
+
212
+        $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
213
+        if ( ! empty( $maybe_url ) ) {
214
+            return $maybe_url;
215
+        }
216
+
217
+        return get_term_link( $id );
218
+    }
219
+
220
+    /**
221
+     * Return jsonld for entities bound to terms.
222
+     *
223
+     * @param int $term_id Term ID.
224
+     * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
225
+     *
226
+     * @return array
227
+     */
228
+    private function get_entity_jsonld( $term_id, $context ) {
229
+
230
+        // The `_wl_entity_id` are URIs.
231
+        $entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
232
+        $entity_uri_service = $this->entity_uri_service;
233
+
234
+        $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
235
+            return $entity_uri_service->is_internal( $uri );
236
+        } );
237
+
238
+        // Bail out if there are no entities.
239
+        if ( empty( $local_entity_ids ) ) {
240
+            return array();
241
+        }
242
+
243
+        $post   = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
244
+        $jsonld = $this->post_id_to_jsonld_converter->convert( $post->ID );
245
+        // Reset the `url` to the term page.
246
+        $jsonld[0]['url'] = get_term_link( $term_id );
247
+
248
+        return $jsonld;
249
+    }
250
+
251
+
252
+    /**
253
+     * @param $references
254
+     *
255
+     * @return array
256
+     */
257
+    private function expand_references( $references ) {
258
+
259
+        // @TODO: we are assuming all the references are posts
260
+        // in this method, since now terms are getting converted to
261
+        // entities, this might not be true in all cases.
262
+        if ( ! is_array( $references ) ) {
263
+            return array();
264
+        }
265
+        $references_jsonld = array();
266
+        // Expand the references.
267
+        foreach ( $references as $reference ) {
268
+            $post_id = $reference;
269
+            if ( $reference instanceof Reference ) {
270
+                $post_id = $reference->get_id();
271
+            }
272
+            $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
273
+        }
274
+
275
+        return $references_jsonld;
276
+
277
+    }
278
+
279
+    private function make_one( $value ) {
280
+        return count( $value ) === 1 ? $value[0] : $value;
281
+    }
282 282
 
283 283
 }
Please login to merge, or discard this patch.
Spacing   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -44,9 +44,9 @@  discard block
 block discarded – undo
44 44
 	 * @since 3.20.0
45 45
 	 *
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 ( ! is_null( $id ) ) {
76
-			$term                       = get_term( $id );
77
-			$post_jsonld['description'] = strip_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 ( ! is_null($id)) {
76
+			$term                       = get_term($id);
77
+			$post_jsonld['description'] = strip_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,31 +97,31 @@  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 );
102
+			array_push($post_jsonld['itemListElement'], $result);
103 103
 			$position += 1;
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 ( ! is_null( $wp_query->posts ) ) {
113
-			return array_map( function ( $post ) {
112
+		if ( ! is_null($wp_query->posts)) {
113
+			return array_map(function($post) {
114 114
 				return $post->ID;
115
-			}, $wp_query->posts );
115
+			}, $wp_query->posts);
116 116
 		}
117 117
 
118
-		if ( is_null( $id ) ) {
118
+		if (is_null($id)) {
119 119
 			return null;
120 120
 		}
121 121
 
122
-		$term = get_term( $id );
122
+		$term = get_term($id);
123 123
 
124
-		return get_objects_in_term( $id, $term->taxonomy );
124
+		return get_objects_in_term($id, $term->taxonomy);
125 125
 	}
126 126
 
127 127
 	/**
@@ -134,31 +134,31 @@  discard block
 block discarded – undo
134 134
 
135 135
 
136 136
 		// Check if it is a term page.
137
-		if ( ! $query_object instanceof WP_Term ) {
137
+		if ( ! $query_object instanceof WP_Term) {
138 138
 			return;
139 139
 		}
140 140
 
141 141
 		// Bail out if `wl_jsonld_enabled` isn't enabled.
142
-		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
142
+		if ( ! apply_filters('wl_jsonld_enabled', true)) {
143 143
 			return;
144 144
 		}
145 145
 
146 146
 		$term_id = $query_object->term_id;
147 147
 
148
-		$jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE );
148
+		$jsonld = $this->get($term_id, Jsonld_Context_Enum::PAGE);
149 149
 
150 150
 		// Bail out if the JSON-LD is empty.
151
-		if ( empty( $jsonld ) ) {
151
+		if (empty($jsonld)) {
152 152
 			return;
153 153
 		}
154 154
 
155
-		$jsonld_string = wp_json_encode( $jsonld );
155
+		$jsonld_string = wp_json_encode($jsonld);
156 156
 
157 157
 		echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>";
158 158
 
159 159
 	}
160 160
 
161
-	public function get( $id, $context ) {
161
+	public function get($id, $context) {
162 162
 		/**
163 163
 		 * Support for carousel rich snippet, get jsonld data present
164 164
 		 * for all the posts shown in the term page, and add the jsonld data
@@ -170,17 +170,17 @@  discard block
 block discarded – undo
170 170
 		 */
171 171
 		$jsonld_array = array();
172 172
 
173
-		if ( Jsonld_Context_Enum::PAGE === $context ) {
174
-			$carousel_data = $this->get_carousel_jsonld( $id );
175
-			if ( $carousel_data ) {
173
+		if (Jsonld_Context_Enum::PAGE === $context) {
174
+			$carousel_data = $this->get_carousel_jsonld($id);
175
+			if ($carousel_data) {
176 176
 				$jsonld_array[] = $carousel_data;
177 177
 			}
178 178
 		}
179 179
 
180
-		$entities_jsonld_array = $this->get_entity_jsonld( $id, $context );
180
+		$entities_jsonld_array = $this->get_entity_jsonld($id, $context);
181 181
 
182 182
 		$result = array(
183
-			'jsonld'     => array_merge( $jsonld_array, $entities_jsonld_array ),
183
+			'jsonld'     => array_merge($jsonld_array, $entities_jsonld_array),
184 184
 			'references' => array()
185 185
 		);
186 186
 
@@ -191,30 +191,30 @@  discard block
 block discarded – undo
191 191
 		 * @var $id int Term id
192 192
 		 * @var $jsonld_array array An array containing jsonld for term and entities.
193 193
 		 */
194
-		$arr = apply_filters( 'wl_term_jsonld_array', $result, $id );
194
+		$arr = apply_filters('wl_term_jsonld_array', $result, $id);
195 195
 
196 196
 		/**
197 197
 		 * @since 3.32.0
198 198
 		 * Expand the references returned by this filter.
199 199
 		 */
200
-		$references = $this->expand_references( $arr['references'] );
200
+		$references = $this->expand_references($arr['references']);
201 201
 
202
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
202
+		$jsonld_array = array_merge($arr['jsonld'], $references);
203 203
 
204 204
 		return $jsonld_array;
205 205
 	}
206 206
 
207
-	private function get_term_url( $id ) {
208
-		if ( is_null( $id ) ) {
207
+	private function get_term_url($id) {
208
+		if (is_null($id)) {
209 209
 			return $_SERVER['REQUEST_URI'];
210 210
 		}
211 211
 
212
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
213
-		if ( ! empty( $maybe_url ) ) {
212
+		$maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true);
213
+		if ( ! empty($maybe_url)) {
214 214
 			return $maybe_url;
215 215
 		}
216 216
 
217
-		return get_term_link( $id );
217
+		return get_term_link($id);
218 218
 	}
219 219
 
220 220
 	/**
@@ -225,25 +225,25 @@  discard block
 block discarded – undo
225 225
 	 *
226 226
 	 * @return array
227 227
 	 */
228
-	private function get_entity_jsonld( $term_id, $context ) {
228
+	private function get_entity_jsonld($term_id, $context) {
229 229
 
230 230
 		// The `_wl_entity_id` are URIs.
231
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
231
+		$entity_ids         = get_term_meta($term_id, '_wl_entity_id');
232 232
 		$entity_uri_service = $this->entity_uri_service;
233 233
 
234
-		$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
235
-			return $entity_uri_service->is_internal( $uri );
234
+		$local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) {
235
+			return $entity_uri_service->is_internal($uri);
236 236
 		} );
237 237
 
238 238
 		// Bail out if there are no entities.
239
-		if ( empty( $local_entity_ids ) ) {
239
+		if (empty($local_entity_ids)) {
240 240
 			return array();
241 241
 		}
242 242
 
243
-		$post   = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
244
-		$jsonld = $this->post_id_to_jsonld_converter->convert( $post->ID );
243
+		$post   = $this->entity_uri_service->get_entity(array_shift($local_entity_ids));
244
+		$jsonld = $this->post_id_to_jsonld_converter->convert($post->ID);
245 245
 		// Reset the `url` to the term page.
246
-		$jsonld[0]['url'] = get_term_link( $term_id );
246
+		$jsonld[0]['url'] = get_term_link($term_id);
247 247
 
248 248
 		return $jsonld;
249 249
 	}
@@ -254,30 +254,30 @@  discard block
 block discarded – undo
254 254
 	 *
255 255
 	 * @return array
256 256
 	 */
257
-	private function expand_references( $references ) {
257
+	private function expand_references($references) {
258 258
 
259 259
 		// @TODO: we are assuming all the references are posts
260 260
 		// in this method, since now terms are getting converted to
261 261
 		// entities, this might not be true in all cases.
262
-		if ( ! is_array( $references ) ) {
262
+		if ( ! is_array($references)) {
263 263
 			return array();
264 264
 		}
265 265
 		$references_jsonld = array();
266 266
 		// Expand the references.
267
-		foreach ( $references as $reference ) {
267
+		foreach ($references as $reference) {
268 268
 			$post_id = $reference;
269
-			if ( $reference instanceof Reference ) {
269
+			if ($reference instanceof Reference) {
270 270
 				$post_id = $reference->get_id();
271 271
 			}
272
-			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
272
+			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert($post_id);
273 273
 		}
274 274
 
275 275
 		return $references_jsonld;
276 276
 
277 277
 	}
278 278
 
279
-	private function make_one( $value ) {
280
-		return count( $value ) === 1 ? $value[0] : $value;
279
+	private function make_one($value) {
280
+		return count($value) === 1 ? $value[0] : $value;
281 281
 	}
282 282
 
283 283
 }
Please login to merge, or discard this patch.