Completed
Pull Request — master (#1449)
by Naveen
02:50
created
src/public/class-wordlift-term-jsonld-adapter.php 2 patches
Indentation   +255 added lines, -255 removed lines patch added patch discarded remove patch
@@ -19,261 +19,261 @@
 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
-		 * @since 3.26.3
189
-		 * Filter: wl_term_jsonld_array
190
-		 * @var $id int Term id
191
-		 * @var $jsonld_array array An array containing jsonld for term and entities.
192
-		 */
193
-		$arr = apply_filters( 'wl_term_jsonld_array', $result, $id );
194
-
195
-		/**
196
-		 * @since 3.32.0
197
-		 * Expand the references returned by this filter.
198
-		 */
199
-		$references = $this->expand_references( $arr['references'] );
200
-
201
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
202
-
203
-		return $jsonld_array;
204
-	}
205
-
206
-	private function get_term_url( $id ) {
207
-		if ( is_null( $id ) ) {
208
-			return $_SERVER['REQUEST_URI'];
209
-		}
210
-
211
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
212
-		if ( ! empty( $maybe_url ) ) {
213
-			return $maybe_url;
214
-		}
215
-
216
-		return get_term_link( $id );
217
-	}
218
-
219
-	/**
220
-	 * Return jsonld for entities bound to terms.
221
-	 *
222
-	 * @param int $term_id Term ID.
223
-	 * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
224
-	 *
225
-	 * @return array
226
-	 */
227
-	private function get_entity_jsonld( $term_id, $context ) {
228
-
229
-		// The `_wl_entity_id` are URIs.
230
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
231
-		$entity_uri_service = $this->entity_uri_service;
232
-
233
-		$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
234
-			return $entity_uri_service->is_internal( $uri );
235
-		} );
236
-
237
-		// Bail out if there are no entities.
238
-		if ( empty( $local_entity_ids ) ) {
239
-			return array();
240
-		}
241
-
242
-		$post   = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
243
-		$jsonld = $this->post_id_to_jsonld_converter->convert( $post->ID );
244
-		// Reset the `url` to the term page.
245
-		$jsonld[0]['url'] = get_term_link( $term_id );
246
-
247
-		return array( $jsonld );
248
-	}
249
-
250
-
251
-	/**
252
-	 * @param $references
253
-	 *
254
-	 * @return array
255
-	 */
256
-	private function expand_references( $references ) {
257
-
258
-		// @TODO: we are assuming all the references are posts
259
-		// in this method, since now terms are getting converted to
260
-		// entities, this might not be true in all cases.
261
-		if ( ! is_array( $references ) ) {
262
-			return array();
263
-		}
264
-		$references_jsonld = array();
265
-		// Expand the references.
266
-		foreach ( $references as $reference ) {
267
-			$post_id = $reference;
268
-			if ( $reference instanceof Reference ) {
269
-				$post_id = $reference->get_id();
270
-			}
271
-			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
272
-		}
273
-
274
-		return $references_jsonld;
275
-
276
-	}
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
+         * @since 3.26.3
189
+         * Filter: wl_term_jsonld_array
190
+         * @var $id int Term id
191
+         * @var $jsonld_array array An array containing jsonld for term and entities.
192
+         */
193
+        $arr = apply_filters( 'wl_term_jsonld_array', $result, $id );
194
+
195
+        /**
196
+         * @since 3.32.0
197
+         * Expand the references returned by this filter.
198
+         */
199
+        $references = $this->expand_references( $arr['references'] );
200
+
201
+        $jsonld_array = array_merge( $arr['jsonld'], $references );
202
+
203
+        return $jsonld_array;
204
+    }
205
+
206
+    private function get_term_url( $id ) {
207
+        if ( is_null( $id ) ) {
208
+            return $_SERVER['REQUEST_URI'];
209
+        }
210
+
211
+        $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
212
+        if ( ! empty( $maybe_url ) ) {
213
+            return $maybe_url;
214
+        }
215
+
216
+        return get_term_link( $id );
217
+    }
218
+
219
+    /**
220
+     * Return jsonld for entities bound to terms.
221
+     *
222
+     * @param int $term_id Term ID.
223
+     * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
224
+     *
225
+     * @return array
226
+     */
227
+    private function get_entity_jsonld( $term_id, $context ) {
228
+
229
+        // The `_wl_entity_id` are URIs.
230
+        $entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
231
+        $entity_uri_service = $this->entity_uri_service;
232
+
233
+        $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
234
+            return $entity_uri_service->is_internal( $uri );
235
+        } );
236
+
237
+        // Bail out if there are no entities.
238
+        if ( empty( $local_entity_ids ) ) {
239
+            return array();
240
+        }
241
+
242
+        $post   = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
243
+        $jsonld = $this->post_id_to_jsonld_converter->convert( $post->ID );
244
+        // Reset the `url` to the term page.
245
+        $jsonld[0]['url'] = get_term_link( $term_id );
246
+
247
+        return array( $jsonld );
248
+    }
249
+
250
+
251
+    /**
252
+     * @param $references
253
+     *
254
+     * @return array
255
+     */
256
+    private function expand_references( $references ) {
257
+
258
+        // @TODO: we are assuming all the references are posts
259
+        // in this method, since now terms are getting converted to
260
+        // entities, this might not be true in all cases.
261
+        if ( ! is_array( $references ) ) {
262
+            return array();
263
+        }
264
+        $references_jsonld = array();
265
+        // Expand the references.
266
+        foreach ( $references as $reference ) {
267
+            $post_id = $reference;
268
+            if ( $reference instanceof Reference ) {
269
+                $post_id = $reference->get_id();
270
+            }
271
+            $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
272
+        }
273
+
274
+        return $references_jsonld;
275
+
276
+    }
277 277
 
278 278
 
279 279
 }
Please login to merge, or discard this patch.
Spacing   +55 added lines, -55 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
 
@@ -190,30 +190,30 @@  discard block
 block discarded – undo
190 190
 		 * @var $id int Term id
191 191
 		 * @var $jsonld_array array An array containing jsonld for term and entities.
192 192
 		 */
193
-		$arr = apply_filters( 'wl_term_jsonld_array', $result, $id );
193
+		$arr = apply_filters('wl_term_jsonld_array', $result, $id);
194 194
 
195 195
 		/**
196 196
 		 * @since 3.32.0
197 197
 		 * Expand the references returned by this filter.
198 198
 		 */
199
-		$references = $this->expand_references( $arr['references'] );
199
+		$references = $this->expand_references($arr['references']);
200 200
 
201
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
201
+		$jsonld_array = array_merge($arr['jsonld'], $references);
202 202
 
203 203
 		return $jsonld_array;
204 204
 	}
205 205
 
206
-	private function get_term_url( $id ) {
207
-		if ( is_null( $id ) ) {
206
+	private function get_term_url($id) {
207
+		if (is_null($id)) {
208 208
 			return $_SERVER['REQUEST_URI'];
209 209
 		}
210 210
 
211
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
212
-		if ( ! empty( $maybe_url ) ) {
211
+		$maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true);
212
+		if ( ! empty($maybe_url)) {
213 213
 			return $maybe_url;
214 214
 		}
215 215
 
216
-		return get_term_link( $id );
216
+		return get_term_link($id);
217 217
 	}
218 218
 
219 219
 	/**
@@ -224,27 +224,27 @@  discard block
 block discarded – undo
224 224
 	 *
225 225
 	 * @return array
226 226
 	 */
227
-	private function get_entity_jsonld( $term_id, $context ) {
227
+	private function get_entity_jsonld($term_id, $context) {
228 228
 
229 229
 		// The `_wl_entity_id` are URIs.
230
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
230
+		$entity_ids         = get_term_meta($term_id, '_wl_entity_id');
231 231
 		$entity_uri_service = $this->entity_uri_service;
232 232
 
233
-		$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
234
-			return $entity_uri_service->is_internal( $uri );
233
+		$local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) {
234
+			return $entity_uri_service->is_internal($uri);
235 235
 		} );
236 236
 
237 237
 		// Bail out if there are no entities.
238
-		if ( empty( $local_entity_ids ) ) {
238
+		if (empty($local_entity_ids)) {
239 239
 			return array();
240 240
 		}
241 241
 
242
-		$post   = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
243
-		$jsonld = $this->post_id_to_jsonld_converter->convert( $post->ID );
242
+		$post   = $this->entity_uri_service->get_entity(array_shift($local_entity_ids));
243
+		$jsonld = $this->post_id_to_jsonld_converter->convert($post->ID);
244 244
 		// Reset the `url` to the term page.
245
-		$jsonld[0]['url'] = get_term_link( $term_id );
245
+		$jsonld[0]['url'] = get_term_link($term_id);
246 246
 
247
-		return array( $jsonld );
247
+		return array($jsonld);
248 248
 	}
249 249
 
250 250
 
@@ -253,22 +253,22 @@  discard block
 block discarded – undo
253 253
 	 *
254 254
 	 * @return array
255 255
 	 */
256
-	private function expand_references( $references ) {
256
+	private function expand_references($references) {
257 257
 
258 258
 		// @TODO: we are assuming all the references are posts
259 259
 		// in this method, since now terms are getting converted to
260 260
 		// entities, this might not be true in all cases.
261
-		if ( ! is_array( $references ) ) {
261
+		if ( ! is_array($references)) {
262 262
 			return array();
263 263
 		}
264 264
 		$references_jsonld = array();
265 265
 		// Expand the references.
266
-		foreach ( $references as $reference ) {
266
+		foreach ($references as $reference) {
267 267
 			$post_id = $reference;
268
-			if ( $reference instanceof Reference ) {
268
+			if ($reference instanceof Reference) {
269 269
 				$post_id = $reference->get_id();
270 270
 			}
271
-			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
271
+			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert($post_id);
272 272
 		}
273 273
 
274 274
 		return $references_jsonld;
Please login to merge, or discard this patch.