Completed
Push — master ( 21015d...33e5aa )
by Naveen
31s queued 13s
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,268 +19,268 @@
 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'] = 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 += 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
-		$jsonld_term_html_output = <<<EOF
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'] = 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 += 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
+        $jsonld_term_html_output = <<<EOF
158 158
 <script type="application/ld+json" id="wl-jsonld-term">$jsonld_string</script>
159 159
 EOF;
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;
163
-
164
-	}
165
-
166
-	public function get( $id, $context ) {
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
-		/**
201
-		 * @since 3.32.0
202
-		 * Expand the references returned by this filter.
203
-		 */
204
-		$references = $this->expand_references( $arr['references'] );
205
-
206
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
207
-
208
-		return $jsonld_array;
209
-	}
210
-
211
-	private function get_term_url( $id ) {
212
-		if ( is_null( $id ) ) {
213
-			return $_SERVER['REQUEST_URI'];
214
-		}
215
-
216
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
217
-		if ( ! empty( $maybe_url ) ) {
218
-			return $maybe_url;
219
-		}
220
-
221
-		return get_term_link( $id );
222
-	}
223
-
224
-	/**
225
-	 * Return jsonld for entities bound to terms.
226
-	 *
227
-	 * @param int $term_id Term ID.
228
-	 * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
229
-	 *
230
-	 * @return array
231
-	 */
232
-	private function get_entity_jsonld( $term_id, $context ) {
233
-
234
-		// The `_wl_entity_id` are URIs.
235
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
236
-		$entity_uri_service = $this->entity_uri_service;
237
-
238
-		$wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance();
239
-
240
-		$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
241
-			return $entity_uri_service->is_internal( $uri );
242
-		} );
243
-
244
-		// Bail out if there are no entities.
245
-		if ( empty( $local_entity_ids ) ) {
246
-			return array();
247
-		}
248
-
249
-		$post            = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
250
-		$entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID );
251
-		// Reset the `url` to the term page.
252
-		$entities_jsonld[0]['url'] = get_term_link( $term_id );
253
-
254
-		return $entities_jsonld;
255
-	}
256
-
257
-
258
-	/**
259
-	 * @param $references
260
-	 *
261
-	 * @return array
262
-	 */
263
-	private function expand_references( $references ) {
264
-
265
-		// @TODO: we are assuming all the references are posts
266
-		// in this method, since now terms are getting converted to
267
-		// entities, this might not be true in all cases.
268
-		if ( ! is_array( $references ) ) {
269
-			return array();
270
-		}
271
-		$references_jsonld = array();
272
-		// Expand the references.
273
-		foreach ( $references as $reference ) {
274
-			$post_id = $reference;
275
-			if ( $reference instanceof Reference ) {
276
-				$post_id = $reference->get_id();
277
-			}
278
-			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
279
-		}
280
-
281
-		return $references_jsonld;
282
-
283
-	}
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;
163
+
164
+    }
165
+
166
+    public function get( $id, $context ) {
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
+        /**
201
+         * @since 3.32.0
202
+         * Expand the references returned by this filter.
203
+         */
204
+        $references = $this->expand_references( $arr['references'] );
205
+
206
+        $jsonld_array = array_merge( $arr['jsonld'], $references );
207
+
208
+        return $jsonld_array;
209
+    }
210
+
211
+    private function get_term_url( $id ) {
212
+        if ( is_null( $id ) ) {
213
+            return $_SERVER['REQUEST_URI'];
214
+        }
215
+
216
+        $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
217
+        if ( ! empty( $maybe_url ) ) {
218
+            return $maybe_url;
219
+        }
220
+
221
+        return get_term_link( $id );
222
+    }
223
+
224
+    /**
225
+     * Return jsonld for entities bound to terms.
226
+     *
227
+     * @param int $term_id Term ID.
228
+     * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
229
+     *
230
+     * @return array
231
+     */
232
+    private function get_entity_jsonld( $term_id, $context ) {
233
+
234
+        // The `_wl_entity_id` are URIs.
235
+        $entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
236
+        $entity_uri_service = $this->entity_uri_service;
237
+
238
+        $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance();
239
+
240
+        $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
241
+            return $entity_uri_service->is_internal( $uri );
242
+        } );
243
+
244
+        // Bail out if there are no entities.
245
+        if ( empty( $local_entity_ids ) ) {
246
+            return array();
247
+        }
248
+
249
+        $post            = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
250
+        $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID );
251
+        // Reset the `url` to the term page.
252
+        $entities_jsonld[0]['url'] = get_term_link( $term_id );
253
+
254
+        return $entities_jsonld;
255
+    }
256
+
257
+
258
+    /**
259
+     * @param $references
260
+     *
261
+     * @return array
262
+     */
263
+    private function expand_references( $references ) {
264
+
265
+        // @TODO: we are assuming all the references are posts
266
+        // in this method, since now terms are getting converted to
267
+        // entities, this might not be true in all cases.
268
+        if ( ! is_array( $references ) ) {
269
+            return array();
270
+        }
271
+        $references_jsonld = array();
272
+        // Expand the references.
273
+        foreach ( $references as $reference ) {
274
+            $post_id = $reference;
275
+            if ( $reference instanceof Reference ) {
276
+                $post_id = $reference->get_id();
277
+            }
278
+            $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
279
+        }
280
+
281
+        return $references_jsonld;
282
+
283
+    }
284 284
 
285 285
 
286 286
 }
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'] = 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 ( ! is_null($id)) {
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,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,36 +134,36 @@  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
 		$jsonld_term_html_output = <<<EOF
158 158
 <script type="application/ld+json" id="wl-jsonld-term">$jsonld_string</script>
159 159
 EOF;
160
-		$jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id );
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;
163 163
 
164 164
 	}
165 165
 
166
-	public function get( $id, $context ) {
166
+	public function get($id, $context) {
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,30 +195,30 @@  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
 		/**
201 201
 		 * @since 3.32.0
202 202
 		 * Expand the references returned by this filter.
203 203
 		 */
204
-		$references = $this->expand_references( $arr['references'] );
204
+		$references = $this->expand_references($arr['references']);
205 205
 
206
-		$jsonld_array = array_merge( $arr['jsonld'], $references );
206
+		$jsonld_array = array_merge($arr['jsonld'], $references);
207 207
 
208 208
 		return $jsonld_array;
209 209
 	}
210 210
 
211
-	private function get_term_url( $id ) {
212
-		if ( is_null( $id ) ) {
211
+	private function get_term_url($id) {
212
+		if (is_null($id)) {
213 213
 			return $_SERVER['REQUEST_URI'];
214 214
 		}
215 215
 
216
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
217
-		if ( ! empty( $maybe_url ) ) {
216
+		$maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true);
217
+		if ( ! empty($maybe_url)) {
218 218
 			return $maybe_url;
219 219
 		}
220 220
 
221
-		return get_term_link( $id );
221
+		return get_term_link($id);
222 222
 	}
223 223
 
224 224
 	/**
@@ -229,27 +229,27 @@  discard block
 block discarded – undo
229 229
 	 *
230 230
 	 * @return array
231 231
 	 */
232
-	private function get_entity_jsonld( $term_id, $context ) {
232
+	private function get_entity_jsonld($term_id, $context) {
233 233
 
234 234
 		// The `_wl_entity_id` are URIs.
235
-		$entity_ids         = get_term_meta( $term_id, '_wl_entity_id' );
235
+		$entity_ids         = get_term_meta($term_id, '_wl_entity_id');
236 236
 		$entity_uri_service = $this->entity_uri_service;
237 237
 
238 238
 		$wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance();
239 239
 
240
-		$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
241
-			return $entity_uri_service->is_internal( $uri );
240
+		$local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) {
241
+			return $entity_uri_service->is_internal($uri);
242 242
 		} );
243 243
 
244 244
 		// Bail out if there are no entities.
245
-		if ( empty( $local_entity_ids ) ) {
245
+		if (empty($local_entity_ids)) {
246 246
 			return array();
247 247
 		}
248 248
 
249
-		$post            = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) );
250
-		$entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID );
249
+		$post            = $this->entity_uri_service->get_entity(array_shift($local_entity_ids));
250
+		$entities_jsonld = $wordlift_jsonld_service->get_jsonld(false, $post->ID);
251 251
 		// Reset the `url` to the term page.
252
-		$entities_jsonld[0]['url'] = get_term_link( $term_id );
252
+		$entities_jsonld[0]['url'] = get_term_link($term_id);
253 253
 
254 254
 		return $entities_jsonld;
255 255
 	}
@@ -260,22 +260,22 @@  discard block
 block discarded – undo
260 260
 	 *
261 261
 	 * @return array
262 262
 	 */
263
-	private function expand_references( $references ) {
263
+	private function expand_references($references) {
264 264
 
265 265
 		// @TODO: we are assuming all the references are posts
266 266
 		// in this method, since now terms are getting converted to
267 267
 		// entities, this might not be true in all cases.
268
-		if ( ! is_array( $references ) ) {
268
+		if ( ! is_array($references)) {
269 269
 			return array();
270 270
 		}
271 271
 		$references_jsonld = array();
272 272
 		// Expand the references.
273
-		foreach ( $references as $reference ) {
273
+		foreach ($references as $reference) {
274 274
 			$post_id = $reference;
275
-			if ( $reference instanceof Reference ) {
275
+			if ($reference instanceof Reference) {
276 276
 				$post_id = $reference->get_id();
277 277
 			}
278
-			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id );
278
+			$references_jsonld[] = $this->post_id_to_jsonld_converter->convert($post_id);
279 279
 		}
280 280
 
281 281
 		return $references_jsonld;
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-jsonld-adapter.php 2 patches
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -21,48 +21,48 @@
 block discarded – undo
21 21
  */
22 22
 class Jsonld_Adapter {
23 23
 
24
-	/**
25
-	 * @var Wordlift_Jsonld_Service
26
-	 */
27
-	private $jsonld_service;
24
+    /**
25
+     * @var Wordlift_Jsonld_Service
26
+     */
27
+    private $jsonld_service;
28 28
 
29
-	/**
30
-	 * Jsonld_Adapter constructor.
31
-	 *
32
-	 * @param \Wordlift_Jsonld_Service $jsonld_service
33
-	 */
34
-	public function __construct( $jsonld_service ) {
29
+    /**
30
+     * Jsonld_Adapter constructor.
31
+     *
32
+     * @param \Wordlift_Jsonld_Service $jsonld_service
33
+     */
34
+    public function __construct( $jsonld_service ) {
35 35
 
36
-		$this->jsonld_service = $jsonld_service;
36
+        $this->jsonld_service = $jsonld_service;
37 37
 
38
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
39
-		add_action( 'amp_post_template_head', array( $this, 'wp_head' ) );
38
+        add_action( 'wp_head', array( $this, 'wp_head' ) );
39
+        add_action( 'amp_post_template_head', array( $this, 'wp_head' ) );
40 40
 
41
-	}
41
+    }
42 42
 
43
-	public function wp_head() {
43
+    public function wp_head() {
44 44
 
45
-		// Bail out if `wl_jsonld_enabled` isn't enabled.
46
-		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
47
-			return;
48
-		}
45
+        // Bail out if `wl_jsonld_enabled` isn't enabled.
46
+        if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
47
+            return;
48
+        }
49 49
 
50
-		// Determine whether this is the home page or whether we're displaying a single post.
51
-		$is_homepage = is_home() || is_front_page();
52
-		$post_id     = is_singular() ? get_the_ID() : null;
50
+        // Determine whether this is the home page or whether we're displaying a single post.
51
+        $is_homepage = is_home() || is_front_page();
52
+        $post_id     = is_singular() ? get_the_ID() : null;
53 53
 
54
-		// Get the JSON-LD.
55
-		$jsonld = json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) );
54
+        // Get the JSON-LD.
55
+        $jsonld = json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) );
56 56
 
57
-		// Finally print the JSON-LD out.
58
-		$jsonld_post_html_output = <<<EOF
57
+        // Finally print the JSON-LD out.
58
+        $jsonld_post_html_output = <<<EOF
59 59
         <script type="application/ld+json" id="wl-jsonld">$jsonld</script>
60 60
 EOF;
61
-		$jsonld_post_html_output = apply_filters( 'wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id )
62
-		?>
61
+        $jsonld_post_html_output = apply_filters( 'wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id )
62
+        ?>
63 63
 		<?php echo $jsonld_post_html_output; ?>
64 64
 		<?php
65 65
 
66
-	}
66
+    }
67 67
 
68 68
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -31,19 +31,19 @@  discard block
 block discarded – undo
31 31
 	 *
32 32
 	 * @param \Wordlift_Jsonld_Service $jsonld_service
33 33
 	 */
34
-	public function __construct( $jsonld_service ) {
34
+	public function __construct($jsonld_service) {
35 35
 
36 36
 		$this->jsonld_service = $jsonld_service;
37 37
 
38
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
39
-		add_action( 'amp_post_template_head', array( $this, 'wp_head' ) );
38
+		add_action('wp_head', array($this, 'wp_head'));
39
+		add_action('amp_post_template_head', array($this, 'wp_head'));
40 40
 
41 41
 	}
42 42
 
43 43
 	public function wp_head() {
44 44
 
45 45
 		// Bail out if `wl_jsonld_enabled` isn't enabled.
46
-		if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) {
46
+		if ( ! apply_filters('wl_jsonld_enabled', true)) {
47 47
 			return;
48 48
 		}
49 49
 
@@ -52,13 +52,13 @@  discard block
 block discarded – undo
52 52
 		$post_id     = is_singular() ? get_the_ID() : null;
53 53
 
54 54
 		// Get the JSON-LD.
55
-		$jsonld = json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) );
55
+		$jsonld = json_encode($this->jsonld_service->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE));
56 56
 
57 57
 		// Finally print the JSON-LD out.
58 58
 		$jsonld_post_html_output = <<<EOF
59 59
         <script type="application/ld+json" id="wl-jsonld">$jsonld</script>
60 60
 EOF;
61
-		$jsonld_post_html_output = apply_filters( 'wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id )
61
+		$jsonld_post_html_output = apply_filters('wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id)
62 62
 		?>
63 63
 		<?php echo $jsonld_post_html_output; ?>
64 64
 		<?php
Please login to merge, or discard this patch.