@@ -21,277 +21,277 @@ |
||
21 | 21 | */ |
22 | 22 | class Wordlift_Term_JsonLd_Adapter { |
23 | 23 | |
24 | - /** |
|
25 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
26 | - * |
|
27 | - * @since 3.20.0 |
|
28 | - * @access private |
|
29 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
30 | - */ |
|
31 | - private $entity_uri_service; |
|
32 | - |
|
33 | - private static $instance; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var Wordlift_Post_Converter |
|
37 | - */ |
|
38 | - private $post_id_to_jsonld_converter; |
|
39 | - |
|
40 | - /** |
|
41 | - * Wordlift_Term_JsonLd_Adapter constructor. |
|
42 | - * |
|
43 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
44 | - * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
45 | - * |
|
46 | - * @since 3.20.0 |
|
47 | - */ |
|
48 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
49 | - |
|
50 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
51 | - |
|
52 | - $this->entity_uri_service = $entity_uri_service; |
|
53 | - $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
54 | - |
|
55 | - self::$instance = $this; |
|
56 | - } |
|
57 | - |
|
58 | - public static function get_instance() { |
|
59 | - |
|
60 | - return self::$instance; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Adds carousel json ld data to term page if the conditions match |
|
65 | - * |
|
66 | - * @return array|boolean |
|
67 | - */ |
|
68 | - public function get_carousel_jsonld( $id = null ) { |
|
69 | - $posts = $this->get_posts( $id ); |
|
70 | - $post_jsonld = array(); |
|
71 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
72 | - // Bail out if no posts are present. |
|
73 | - return false; |
|
74 | - } |
|
75 | - |
|
76 | - if ( $id !== null ) { |
|
77 | - $term = get_term( $id ); |
|
78 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
79 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
80 | - if ( ! empty( $thumbnail_id ) ) { |
|
81 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - // More than 2 items are present, so construct the post_jsonld data |
|
86 | - $post_jsonld['@context'] = 'https://schema.org'; |
|
87 | - $post_jsonld['@type'] = 'ItemList'; |
|
88 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
89 | - $post_jsonld['itemListElement'] = array(); |
|
90 | - $position = 1; |
|
91 | - |
|
92 | - foreach ( $posts as $post_id ) { |
|
93 | - $result = array( |
|
94 | - '@type' => 'ListItem', |
|
95 | - 'position' => $position, |
|
96 | - /** |
|
97 | - * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
98 | - * |
|
99 | - * See https://developers.google.com/search/docs/data-types/carousel |
|
100 | - */ |
|
101 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
102 | - ); |
|
103 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
104 | - ++ $position; |
|
105 | - } |
|
106 | - |
|
107 | - return $post_jsonld; |
|
108 | - } |
|
109 | - |
|
110 | - private function get_posts( $id ) { |
|
111 | - global $wp_query; |
|
112 | - |
|
113 | - if ( $wp_query->posts !== null ) { |
|
114 | - return array_map( |
|
115 | - function ( $post ) { |
|
116 | - return $post->ID; |
|
117 | - }, |
|
118 | - $wp_query->posts |
|
119 | - ); |
|
120 | - } |
|
121 | - |
|
122 | - if ( $id === null ) { |
|
123 | - return null; |
|
124 | - } |
|
125 | - |
|
126 | - $term = get_term( $id ); |
|
127 | - |
|
128 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Hook to `wp_head` to print the JSON-LD. |
|
133 | - * |
|
134 | - * @since 3.20.0 |
|
135 | - */ |
|
136 | - public function wp_head() { |
|
137 | - $query_object = get_queried_object(); |
|
138 | - |
|
139 | - // Check if it is a term page. |
|
140 | - if ( ! $query_object instanceof WP_Term ) { |
|
141 | - return; |
|
142 | - } |
|
143 | - |
|
144 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
145 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
146 | - return; |
|
147 | - } |
|
148 | - |
|
149 | - $term_id = $query_object->term_id; |
|
150 | - |
|
151 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
152 | - |
|
153 | - // Bail out if the JSON-LD is empty. |
|
154 | - if ( empty( $jsonld ) ) { |
|
155 | - return; |
|
156 | - } |
|
157 | - |
|
158 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
159 | - |
|
160 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
161 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
162 | - |
|
163 | - echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
164 | - |
|
165 | - } |
|
166 | - |
|
167 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
168 | - /** |
|
169 | - * Support for carousel rich snippet, get jsonld data present |
|
170 | - * for all the posts shown in the term page, and add the jsonld data |
|
171 | - * to list |
|
172 | - * |
|
173 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
174 | - * |
|
175 | - * @since 3.26.0 |
|
176 | - */ |
|
177 | - $jsonld_array = array(); |
|
178 | - |
|
179 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
180 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
181 | - if ( $carousel_data ) { |
|
182 | - $jsonld_array[] = $carousel_data; |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
187 | - |
|
188 | - $result = array( |
|
189 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
190 | - 'references' => array(), |
|
191 | - ); |
|
192 | - |
|
193 | - /** |
|
194 | - * @since 3.26.3 |
|
195 | - * Filter: wl_term_jsonld_array |
|
196 | - * @var $id int Term id |
|
197 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
198 | - */ |
|
199 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
200 | - |
|
201 | - $references = array(); |
|
202 | - |
|
203 | - // Don't expand nested references, it will lead to an infinite loop. |
|
204 | - if ( ! $is_recursive_call ) { |
|
205 | - /** |
|
206 | - * @since 3.32.0 |
|
207 | - * Expand the references returned by this filter. |
|
208 | - */ |
|
209 | - $references = $this->expand_references( $arr['references'] ); |
|
210 | - } |
|
211 | - |
|
212 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
213 | - |
|
214 | - return $jsonld_array; |
|
215 | - } |
|
216 | - |
|
217 | - private function get_term_url( $id ) { |
|
218 | - if ( null === $id ) { |
|
219 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
220 | - } |
|
221 | - |
|
222 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
223 | - if ( ! empty( $maybe_url ) ) { |
|
224 | - return $maybe_url; |
|
225 | - } |
|
226 | - |
|
227 | - return get_term_link( $id ); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Return jsonld for entities bound to terms. |
|
232 | - * |
|
233 | - * @param int $term_id Term ID. |
|
234 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
235 | - * |
|
236 | - * @return array |
|
237 | - */ |
|
238 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
239 | - private function get_entity_jsonld( $term_id, $context ) { |
|
240 | - |
|
241 | - // The `_wl_entity_id` are URIs. |
|
242 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
243 | - $entity_uri_service = $this->entity_uri_service; |
|
244 | - |
|
245 | - $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
246 | - |
|
247 | - $local_entity_ids = array_filter( |
|
248 | - $entity_ids, |
|
249 | - function ( $uri ) use ( $entity_uri_service ) { |
|
250 | - return $entity_uri_service->is_internal( $uri ); |
|
251 | - } |
|
252 | - ); |
|
253 | - |
|
254 | - // Bail out if there are no entities. |
|
255 | - if ( empty( $local_entity_ids ) ) { |
|
256 | - return array(); |
|
257 | - } |
|
258 | - |
|
259 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
260 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
261 | - // Reset the `url` to the term page. |
|
262 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
263 | - |
|
264 | - return $entities_jsonld; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * @param $references |
|
269 | - * |
|
270 | - * @return array |
|
271 | - */ |
|
272 | - private function expand_references( $references ) { |
|
273 | - if ( ! is_array( $references ) ) { |
|
274 | - return array(); |
|
275 | - } |
|
276 | - $references_jsonld = array(); |
|
277 | - // Expand the references. |
|
278 | - foreach ( $references as $reference ) { |
|
279 | - if ( $reference instanceof Term_Reference ) { |
|
280 | - // Second level references won't be expanded. |
|
281 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
282 | - } elseif ( is_numeric( $reference ) ) { |
|
283 | - $ref_2 = array(); |
|
284 | - $ref_info_2 = array(); |
|
285 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
286 | - } elseif ( $reference instanceof Post_Reference ) { |
|
287 | - $ref_2 = array(); |
|
288 | - $ref_info_2 = array(); |
|
289 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() ); |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - return $references_jsonld; |
|
294 | - |
|
295 | - } |
|
24 | + /** |
|
25 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
26 | + * |
|
27 | + * @since 3.20.0 |
|
28 | + * @access private |
|
29 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
30 | + */ |
|
31 | + private $entity_uri_service; |
|
32 | + |
|
33 | + private static $instance; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var Wordlift_Post_Converter |
|
37 | + */ |
|
38 | + private $post_id_to_jsonld_converter; |
|
39 | + |
|
40 | + /** |
|
41 | + * Wordlift_Term_JsonLd_Adapter constructor. |
|
42 | + * |
|
43 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
44 | + * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
45 | + * |
|
46 | + * @since 3.20.0 |
|
47 | + */ |
|
48 | + public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
49 | + |
|
50 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
51 | + |
|
52 | + $this->entity_uri_service = $entity_uri_service; |
|
53 | + $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
54 | + |
|
55 | + self::$instance = $this; |
|
56 | + } |
|
57 | + |
|
58 | + public static function get_instance() { |
|
59 | + |
|
60 | + return self::$instance; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Adds carousel json ld data to term page if the conditions match |
|
65 | + * |
|
66 | + * @return array|boolean |
|
67 | + */ |
|
68 | + public function get_carousel_jsonld( $id = null ) { |
|
69 | + $posts = $this->get_posts( $id ); |
|
70 | + $post_jsonld = array(); |
|
71 | + if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
72 | + // Bail out if no posts are present. |
|
73 | + return false; |
|
74 | + } |
|
75 | + |
|
76 | + if ( $id !== null ) { |
|
77 | + $term = get_term( $id ); |
|
78 | + $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
79 | + $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
80 | + if ( ! empty( $thumbnail_id ) ) { |
|
81 | + $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + // More than 2 items are present, so construct the post_jsonld data |
|
86 | + $post_jsonld['@context'] = 'https://schema.org'; |
|
87 | + $post_jsonld['@type'] = 'ItemList'; |
|
88 | + $post_jsonld['url'] = $this->get_term_url( $id ); |
|
89 | + $post_jsonld['itemListElement'] = array(); |
|
90 | + $position = 1; |
|
91 | + |
|
92 | + foreach ( $posts as $post_id ) { |
|
93 | + $result = array( |
|
94 | + '@type' => 'ListItem', |
|
95 | + 'position' => $position, |
|
96 | + /** |
|
97 | + * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
98 | + * |
|
99 | + * See https://developers.google.com/search/docs/data-types/carousel |
|
100 | + */ |
|
101 | + 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
102 | + ); |
|
103 | + array_push( $post_jsonld['itemListElement'], $result ); |
|
104 | + ++ $position; |
|
105 | + } |
|
106 | + |
|
107 | + return $post_jsonld; |
|
108 | + } |
|
109 | + |
|
110 | + private function get_posts( $id ) { |
|
111 | + global $wp_query; |
|
112 | + |
|
113 | + if ( $wp_query->posts !== null ) { |
|
114 | + return array_map( |
|
115 | + function ( $post ) { |
|
116 | + return $post->ID; |
|
117 | + }, |
|
118 | + $wp_query->posts |
|
119 | + ); |
|
120 | + } |
|
121 | + |
|
122 | + if ( $id === null ) { |
|
123 | + return null; |
|
124 | + } |
|
125 | + |
|
126 | + $term = get_term( $id ); |
|
127 | + |
|
128 | + return get_objects_in_term( $id, $term->taxonomy ); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Hook to `wp_head` to print the JSON-LD. |
|
133 | + * |
|
134 | + * @since 3.20.0 |
|
135 | + */ |
|
136 | + public function wp_head() { |
|
137 | + $query_object = get_queried_object(); |
|
138 | + |
|
139 | + // Check if it is a term page. |
|
140 | + if ( ! $query_object instanceof WP_Term ) { |
|
141 | + return; |
|
142 | + } |
|
143 | + |
|
144 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
145 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
146 | + return; |
|
147 | + } |
|
148 | + |
|
149 | + $term_id = $query_object->term_id; |
|
150 | + |
|
151 | + $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
152 | + |
|
153 | + // Bail out if the JSON-LD is empty. |
|
154 | + if ( empty( $jsonld ) ) { |
|
155 | + return; |
|
156 | + } |
|
157 | + |
|
158 | + $jsonld_string = wp_json_encode( $jsonld ); |
|
159 | + |
|
160 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
161 | + $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
162 | + |
|
163 | + echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
164 | + |
|
165 | + } |
|
166 | + |
|
167 | + public function get( $id, $context, $is_recursive_call = false ) { |
|
168 | + /** |
|
169 | + * Support for carousel rich snippet, get jsonld data present |
|
170 | + * for all the posts shown in the term page, and add the jsonld data |
|
171 | + * to list |
|
172 | + * |
|
173 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
174 | + * |
|
175 | + * @since 3.26.0 |
|
176 | + */ |
|
177 | + $jsonld_array = array(); |
|
178 | + |
|
179 | + if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
180 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
181 | + if ( $carousel_data ) { |
|
182 | + $jsonld_array[] = $carousel_data; |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
187 | + |
|
188 | + $result = array( |
|
189 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
190 | + 'references' => array(), |
|
191 | + ); |
|
192 | + |
|
193 | + /** |
|
194 | + * @since 3.26.3 |
|
195 | + * Filter: wl_term_jsonld_array |
|
196 | + * @var $id int Term id |
|
197 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
198 | + */ |
|
199 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
200 | + |
|
201 | + $references = array(); |
|
202 | + |
|
203 | + // Don't expand nested references, it will lead to an infinite loop. |
|
204 | + if ( ! $is_recursive_call ) { |
|
205 | + /** |
|
206 | + * @since 3.32.0 |
|
207 | + * Expand the references returned by this filter. |
|
208 | + */ |
|
209 | + $references = $this->expand_references( $arr['references'] ); |
|
210 | + } |
|
211 | + |
|
212 | + $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
213 | + |
|
214 | + return $jsonld_array; |
|
215 | + } |
|
216 | + |
|
217 | + private function get_term_url( $id ) { |
|
218 | + if ( null === $id ) { |
|
219 | + return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
220 | + } |
|
221 | + |
|
222 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
223 | + if ( ! empty( $maybe_url ) ) { |
|
224 | + return $maybe_url; |
|
225 | + } |
|
226 | + |
|
227 | + return get_term_link( $id ); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Return jsonld for entities bound to terms. |
|
232 | + * |
|
233 | + * @param int $term_id Term ID. |
|
234 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
235 | + * |
|
236 | + * @return array |
|
237 | + */ |
|
238 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
239 | + private function get_entity_jsonld( $term_id, $context ) { |
|
240 | + |
|
241 | + // The `_wl_entity_id` are URIs. |
|
242 | + $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
243 | + $entity_uri_service = $this->entity_uri_service; |
|
244 | + |
|
245 | + $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
246 | + |
|
247 | + $local_entity_ids = array_filter( |
|
248 | + $entity_ids, |
|
249 | + function ( $uri ) use ( $entity_uri_service ) { |
|
250 | + return $entity_uri_service->is_internal( $uri ); |
|
251 | + } |
|
252 | + ); |
|
253 | + |
|
254 | + // Bail out if there are no entities. |
|
255 | + if ( empty( $local_entity_ids ) ) { |
|
256 | + return array(); |
|
257 | + } |
|
258 | + |
|
259 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
260 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
261 | + // Reset the `url` to the term page. |
|
262 | + $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
263 | + |
|
264 | + return $entities_jsonld; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * @param $references |
|
269 | + * |
|
270 | + * @return array |
|
271 | + */ |
|
272 | + private function expand_references( $references ) { |
|
273 | + if ( ! is_array( $references ) ) { |
|
274 | + return array(); |
|
275 | + } |
|
276 | + $references_jsonld = array(); |
|
277 | + // Expand the references. |
|
278 | + foreach ( $references as $reference ) { |
|
279 | + if ( $reference instanceof Term_Reference ) { |
|
280 | + // Second level references won't be expanded. |
|
281 | + $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
282 | + } elseif ( is_numeric( $reference ) ) { |
|
283 | + $ref_2 = array(); |
|
284 | + $ref_info_2 = array(); |
|
285 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
286 | + } elseif ( $reference instanceof Post_Reference ) { |
|
287 | + $ref_2 = array(); |
|
288 | + $ref_info_2 = array(); |
|
289 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() ); |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + return $references_jsonld; |
|
294 | + |
|
295 | + } |
|
296 | 296 | |
297 | 297 | } |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @since 3.20.0 |
47 | 47 | */ |
48 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
48 | + public function __construct($entity_uri_service, $post_id_to_jsonld_converter) { |
|
49 | 49 | |
50 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
50 | + add_action('wp_head', array($this, 'wp_head')); |
|
51 | 51 | |
52 | 52 | $this->entity_uri_service = $entity_uri_service; |
53 | 53 | $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
@@ -65,31 +65,31 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @return array|boolean |
67 | 67 | */ |
68 | - public function get_carousel_jsonld( $id = null ) { |
|
69 | - $posts = $this->get_posts( $id ); |
|
68 | + public function get_carousel_jsonld($id = null) { |
|
69 | + $posts = $this->get_posts($id); |
|
70 | 70 | $post_jsonld = array(); |
71 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
71 | + if ( ! is_array($posts) || count($posts) < 2) { |
|
72 | 72 | // Bail out if no posts are present. |
73 | 73 | return false; |
74 | 74 | } |
75 | 75 | |
76 | - if ( $id !== null ) { |
|
77 | - $term = get_term( $id ); |
|
78 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
79 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
80 | - if ( ! empty( $thumbnail_id ) ) { |
|
81 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
76 | + if ($id !== null) { |
|
77 | + $term = get_term($id); |
|
78 | + $post_jsonld['description'] = wp_strip_all_tags(strip_shortcodes($term->description)); |
|
79 | + $thumbnail_id = get_term_meta($id, 'thumbnail_id', true); |
|
80 | + if ( ! empty($thumbnail_id)) { |
|
81 | + $post_jsonld['image'] = wp_get_attachment_url($thumbnail_id); |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | |
85 | 85 | // More than 2 items are present, so construct the post_jsonld data |
86 | 86 | $post_jsonld['@context'] = 'https://schema.org'; |
87 | 87 | $post_jsonld['@type'] = 'ItemList'; |
88 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
88 | + $post_jsonld['url'] = $this->get_term_url($id); |
|
89 | 89 | $post_jsonld['itemListElement'] = array(); |
90 | 90 | $position = 1; |
91 | 91 | |
92 | - foreach ( $posts as $post_id ) { |
|
92 | + foreach ($posts as $post_id) { |
|
93 | 93 | $result = array( |
94 | 94 | '@type' => 'ListItem', |
95 | 95 | 'position' => $position, |
@@ -98,34 +98,34 @@ discard block |
||
98 | 98 | * |
99 | 99 | * See https://developers.google.com/search/docs/data-types/carousel |
100 | 100 | */ |
101 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
101 | + 'url' => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id), |
|
102 | 102 | ); |
103 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
104 | - ++ $position; |
|
103 | + array_push($post_jsonld['itemListElement'], $result); |
|
104 | + ++$position; |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | return $post_jsonld; |
108 | 108 | } |
109 | 109 | |
110 | - private function get_posts( $id ) { |
|
110 | + private function get_posts($id) { |
|
111 | 111 | global $wp_query; |
112 | 112 | |
113 | - if ( $wp_query->posts !== null ) { |
|
113 | + if ($wp_query->posts !== null) { |
|
114 | 114 | return array_map( |
115 | - function ( $post ) { |
|
115 | + function($post) { |
|
116 | 116 | return $post->ID; |
117 | 117 | }, |
118 | 118 | $wp_query->posts |
119 | 119 | ); |
120 | 120 | } |
121 | 121 | |
122 | - if ( $id === null ) { |
|
122 | + if ($id === null) { |
|
123 | 123 | return null; |
124 | 124 | } |
125 | 125 | |
126 | - $term = get_term( $id ); |
|
126 | + $term = get_term($id); |
|
127 | 127 | |
128 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
128 | + return get_objects_in_term($id, $term->taxonomy); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
@@ -137,34 +137,34 @@ discard block |
||
137 | 137 | $query_object = get_queried_object(); |
138 | 138 | |
139 | 139 | // Check if it is a term page. |
140 | - if ( ! $query_object instanceof WP_Term ) { |
|
140 | + if ( ! $query_object instanceof WP_Term) { |
|
141 | 141 | return; |
142 | 142 | } |
143 | 143 | |
144 | 144 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
145 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
145 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
146 | 146 | return; |
147 | 147 | } |
148 | 148 | |
149 | 149 | $term_id = $query_object->term_id; |
150 | 150 | |
151 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
151 | + $jsonld = $this->get($term_id, Jsonld_Context_Enum::PAGE); |
|
152 | 152 | |
153 | 153 | // Bail out if the JSON-LD is empty. |
154 | - if ( empty( $jsonld ) ) { |
|
154 | + if (empty($jsonld)) { |
|
155 | 155 | return; |
156 | 156 | } |
157 | 157 | |
158 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
158 | + $jsonld_string = wp_json_encode($jsonld); |
|
159 | 159 | |
160 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
161 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
160 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">'.$jsonld_string.'</script>'; |
|
161 | + $jsonld_term_html_output = apply_filters('wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id); |
|
162 | 162 | |
163 | 163 | echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
164 | 164 | |
165 | 165 | } |
166 | 166 | |
167 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
167 | + public function get($id, $context, $is_recursive_call = false) { |
|
168 | 168 | /** |
169 | 169 | * Support for carousel rich snippet, get jsonld data present |
170 | 170 | * for all the posts shown in the term page, and add the jsonld data |
@@ -176,17 +176,17 @@ discard block |
||
176 | 176 | */ |
177 | 177 | $jsonld_array = array(); |
178 | 178 | |
179 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
180 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
181 | - if ( $carousel_data ) { |
|
179 | + if (Jsonld_Context_Enum::PAGE === $context) { |
|
180 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
181 | + if ($carousel_data) { |
|
182 | 182 | $jsonld_array[] = $carousel_data; |
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
186 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
186 | + $entities_jsonld_array = $this->get_entity_jsonld($id, $context); |
|
187 | 187 | |
188 | 188 | $result = array( |
189 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
189 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
190 | 190 | 'references' => array(), |
191 | 191 | ); |
192 | 192 | |
@@ -196,35 +196,35 @@ discard block |
||
196 | 196 | * @var $id int Term id |
197 | 197 | * @var $jsonld_array array An array containing jsonld for term and entities. |
198 | 198 | */ |
199 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
199 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
200 | 200 | |
201 | 201 | $references = array(); |
202 | 202 | |
203 | 203 | // Don't expand nested references, it will lead to an infinite loop. |
204 | - if ( ! $is_recursive_call ) { |
|
204 | + if ( ! $is_recursive_call) { |
|
205 | 205 | /** |
206 | 206 | * @since 3.32.0 |
207 | 207 | * Expand the references returned by this filter. |
208 | 208 | */ |
209 | - $references = $this->expand_references( $arr['references'] ); |
|
209 | + $references = $this->expand_references($arr['references']); |
|
210 | 210 | } |
211 | 211 | |
212 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
212 | + $jsonld_array = array_merge($arr['jsonld'], $references); |
|
213 | 213 | |
214 | 214 | return $jsonld_array; |
215 | 215 | } |
216 | 216 | |
217 | - private function get_term_url( $id ) { |
|
218 | - if ( null === $id ) { |
|
219 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
217 | + private function get_term_url($id) { |
|
218 | + if (null === $id) { |
|
219 | + return isset($_SERVER['REQUEST_URI']) ? filter_var(wp_unslash($_SERVER['REQUEST_URI']), FILTER_SANITIZE_URL) : ''; |
|
220 | 220 | } |
221 | 221 | |
222 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
223 | - if ( ! empty( $maybe_url ) ) { |
|
222 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
223 | + if ( ! empty($maybe_url)) { |
|
224 | 224 | return $maybe_url; |
225 | 225 | } |
226 | 226 | |
227 | - return get_term_link( $id ); |
|
227 | + return get_term_link($id); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
@@ -236,30 +236,30 @@ discard block |
||
236 | 236 | * @return array |
237 | 237 | */ |
238 | 238 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
239 | - private function get_entity_jsonld( $term_id, $context ) { |
|
239 | + private function get_entity_jsonld($term_id, $context) { |
|
240 | 240 | |
241 | 241 | // The `_wl_entity_id` are URIs. |
242 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
242 | + $entity_ids = get_term_meta($term_id, '_wl_entity_id'); |
|
243 | 243 | $entity_uri_service = $this->entity_uri_service; |
244 | 244 | |
245 | 245 | $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
246 | 246 | |
247 | 247 | $local_entity_ids = array_filter( |
248 | 248 | $entity_ids, |
249 | - function ( $uri ) use ( $entity_uri_service ) { |
|
250 | - return $entity_uri_service->is_internal( $uri ); |
|
249 | + function($uri) use ($entity_uri_service) { |
|
250 | + return $entity_uri_service->is_internal($uri); |
|
251 | 251 | } |
252 | 252 | ); |
253 | 253 | |
254 | 254 | // Bail out if there are no entities. |
255 | - if ( empty( $local_entity_ids ) ) { |
|
255 | + if (empty($local_entity_ids)) { |
|
256 | 256 | return array(); |
257 | 257 | } |
258 | 258 | |
259 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
260 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
259 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
260 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld(false, $post->ID); |
|
261 | 261 | // Reset the `url` to the term page. |
262 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
262 | + $entities_jsonld[0]['url'] = get_term_link($term_id); |
|
263 | 263 | |
264 | 264 | return $entities_jsonld; |
265 | 265 | } |
@@ -269,24 +269,24 @@ discard block |
||
269 | 269 | * |
270 | 270 | * @return array |
271 | 271 | */ |
272 | - private function expand_references( $references ) { |
|
273 | - if ( ! is_array( $references ) ) { |
|
272 | + private function expand_references($references) { |
|
273 | + if ( ! is_array($references)) { |
|
274 | 274 | return array(); |
275 | 275 | } |
276 | 276 | $references_jsonld = array(); |
277 | 277 | // Expand the references. |
278 | - foreach ( $references as $reference ) { |
|
279 | - if ( $reference instanceof Term_Reference ) { |
|
278 | + foreach ($references as $reference) { |
|
279 | + if ($reference instanceof Term_Reference) { |
|
280 | 280 | // Second level references won't be expanded. |
281 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
282 | - } elseif ( is_numeric( $reference ) ) { |
|
281 | + $references_jsonld[] = current($this->get($reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true)); |
|
282 | + } elseif (is_numeric($reference)) { |
|
283 | 283 | $ref_2 = array(); |
284 | 284 | $ref_info_2 = array(); |
285 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
286 | - } elseif ( $reference instanceof Post_Reference ) { |
|
285 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference, $ref_2, $ref_info_2, new Relations()); |
|
286 | + } elseif ($reference instanceof Post_Reference) { |
|
287 | 287 | $ref_2 = array(); |
288 | 288 | $ref_info_2 = array(); |
289 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() ); |
|
289 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference->get_id(), $ref_2, $ref_info_2, new Relations()); |
|
290 | 290 | } |
291 | 291 | } |
292 | 292 |
@@ -20,466 +20,466 @@ |
||
20 | 20 | */ |
21 | 21 | class Wordlift_Jsonld_Service { |
22 | 22 | |
23 | - private static $creative_work_types = array( |
|
24 | - 'AmpStory', |
|
25 | - 'ArchiveComponent', |
|
26 | - 'Article', |
|
27 | - 'Atlas', |
|
28 | - 'Blog', |
|
29 | - 'Book', |
|
30 | - 'Chapter', |
|
31 | - 'Claim', |
|
32 | - 'Clip', |
|
33 | - 'Code', |
|
34 | - 'Collection', |
|
35 | - 'ComicStory', |
|
36 | - 'Comment', |
|
37 | - 'Conversation', |
|
38 | - 'Course', |
|
39 | - 'CreativeWork', |
|
40 | - 'CreativeWorkSeason', |
|
41 | - 'CreativeWorkSeries', |
|
42 | - 'DataCatalog', |
|
43 | - 'Dataset', |
|
44 | - 'DefinedTermSet', |
|
45 | - 'Diet', |
|
46 | - 'DigitalDocument', |
|
47 | - 'Drawing', |
|
48 | - 'EducationalOccupationalCredential', |
|
49 | - 'Episode', |
|
50 | - 'ExercisePlan', |
|
51 | - 'Game', |
|
52 | - 'Guide', |
|
53 | - 'HowTo', |
|
54 | - 'HowToDirection', |
|
55 | - 'HowToSection', |
|
56 | - 'HowToStep', |
|
57 | - 'HowToTip', |
|
58 | - 'HyperToc', |
|
59 | - 'HyperTocEntry', |
|
60 | - 'LearningResource', |
|
61 | - 'Legislation', |
|
62 | - 'Manuscript', |
|
63 | - 'Map', |
|
64 | - 'MathSolver', |
|
65 | - 'MediaObject', |
|
66 | - 'Menu', |
|
67 | - 'MenuSection', |
|
68 | - 'Message', |
|
69 | - 'Movie', |
|
70 | - 'MusicComposition', |
|
71 | - 'MusicPlaylist', |
|
72 | - 'MusicRecording', |
|
73 | - 'Painting', |
|
74 | - 'Photograph', |
|
75 | - 'Play', |
|
76 | - 'Poster', |
|
77 | - 'PublicationIssue', |
|
78 | - 'PublicationVolume', |
|
79 | - 'Quotation', |
|
80 | - 'Review', |
|
81 | - 'Sculpture', |
|
82 | - 'Season', |
|
83 | - 'SheetMusic', |
|
84 | - 'ShortStory', |
|
85 | - 'SoftwareApplication', |
|
86 | - 'SoftwareSourceCode', |
|
87 | - 'SpecialAnnouncement', |
|
88 | - 'Thesis', |
|
89 | - 'TvSeason', |
|
90 | - 'TvSeries', |
|
91 | - 'VisualArtwork', |
|
92 | - 'WebContent', |
|
93 | - 'WebPage', |
|
94 | - 'WebPageElement', |
|
95 | - 'WebSite', |
|
96 | - 'AdvertiserContentArticle', |
|
97 | - 'NewsArticle', |
|
98 | - 'Report', |
|
99 | - 'SatiricalArticle', |
|
100 | - 'ScholarlyArticle', |
|
101 | - 'SocialMediaPosting', |
|
102 | - 'TechArticle', |
|
103 | - 'AnalysisNewsArticle', |
|
104 | - 'AskPublicNewsArticle', |
|
105 | - 'BackgroundNewsArticle', |
|
106 | - 'OpinionNewsArticle', |
|
107 | - 'ReportageNewsArticle', |
|
108 | - 'ReviewNewsArticle', |
|
109 | - 'MedicalScholarlyArticle', |
|
110 | - 'BlogPosting', |
|
111 | - 'DiscussionForumPosting', |
|
112 | - 'LiveBlogPosting', |
|
113 | - 'ApiReference', |
|
114 | - 'Audiobook', |
|
115 | - 'MovieClip', |
|
116 | - 'RadioClip', |
|
117 | - 'TvClip', |
|
118 | - 'VideoGameClip', |
|
119 | - 'ProductCollection', |
|
120 | - 'ComicCoverArt', |
|
121 | - 'Answer', |
|
122 | - 'CorrectionComment', |
|
123 | - 'Question', |
|
124 | - 'PodcastSeason', |
|
125 | - 'RadioSeason', |
|
126 | - 'TvSeason', |
|
127 | - 'BookSeries', |
|
128 | - 'MovieSeries', |
|
129 | - 'Periodical', |
|
130 | - 'PodcastSeries', |
|
131 | - 'RadioSeries', |
|
132 | - 'TvSeries', |
|
133 | - 'VideoGameSeries', |
|
134 | - 'ComicSeries', |
|
135 | - 'Newspaper', |
|
136 | - 'DataFeed', |
|
137 | - 'CompleteDataFeed', |
|
138 | - 'CategoryCodeSet', |
|
139 | - 'NoteDigitalDocument', |
|
140 | - 'PresentationDigitalDocument', |
|
141 | - 'SpreadsheetDigitalDocument', |
|
142 | - 'TextDigitalDocument', |
|
143 | - 'PodcastEpisode', |
|
144 | - 'RadioEpisode', |
|
145 | - 'TvEpisode', |
|
146 | - 'VideoGame', |
|
147 | - 'Recipe', |
|
148 | - 'Course', |
|
149 | - 'Quiz', |
|
150 | - 'LegislationObject', |
|
151 | - 'AudioObject', |
|
152 | - 'DModel', |
|
153 | - 'DataDownload', |
|
154 | - 'ImageObject', |
|
155 | - 'LegislationObject', |
|
156 | - 'MusicVideoObject', |
|
157 | - 'VideoObject', |
|
158 | - 'Audiobook', |
|
159 | - 'Barcode', |
|
160 | - 'EmailMessage', |
|
161 | - 'MusicAlbum', |
|
162 | - 'MusicRelease', |
|
163 | - 'ComicIssue', |
|
164 | - 'ClaimReview', |
|
165 | - 'CriticReview', |
|
166 | - 'EmployerReview', |
|
167 | - 'MediaReview', |
|
168 | - 'Recommendation', |
|
169 | - 'UserReview', |
|
170 | - 'ReviewNewsArticle', |
|
171 | - 'MobileApplication', |
|
172 | - 'VideoGame', |
|
173 | - 'WebApplication', |
|
174 | - 'CoverArt', |
|
175 | - 'ComicCoverArt', |
|
176 | - 'HealthTopicContent', |
|
177 | - 'AboutPage', |
|
178 | - 'CheckoutPage', |
|
179 | - 'CollectionPage', |
|
180 | - 'ContactPage', |
|
181 | - 'FaqPage', |
|
182 | - 'ItemPage', |
|
183 | - 'MedicalWebPage', |
|
184 | - 'ProfilePage', |
|
185 | - 'QaPage', |
|
186 | - 'RealEstateListing', |
|
187 | - 'SearchResultsPage', |
|
188 | - 'MediaGallery', |
|
189 | - 'ImageGallery', |
|
190 | - 'VideoGallery', |
|
191 | - 'SiteNavigationElement', |
|
192 | - 'Table', |
|
193 | - 'WpAdBlock', |
|
194 | - 'WpFooter', |
|
195 | - 'WpHeader', |
|
196 | - 'WpSideBar', |
|
197 | - ); |
|
198 | - |
|
199 | - /** |
|
200 | - * A {@link Wordlift_Entity_Service} instance. |
|
201 | - * |
|
202 | - * @since 3.8.0 |
|
203 | - * @access private |
|
204 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
205 | - */ |
|
206 | - private $entity_service; |
|
207 | - |
|
208 | - /** |
|
209 | - * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
210 | - * |
|
211 | - * @since 3.32.0 |
|
212 | - * @access private |
|
213 | - * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
214 | - */ |
|
215 | - private $term_jsonld_adapter; |
|
216 | - |
|
217 | - /** |
|
218 | - * A {@link Wordlift_Post_Converter} instance. |
|
219 | - * |
|
220 | - * @since 3.8.0 |
|
221 | - * @access private |
|
222 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
223 | - */ |
|
224 | - private $converter; |
|
225 | - |
|
226 | - /** |
|
227 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
228 | - * |
|
229 | - * @since 3.14.0 |
|
230 | - * @access private |
|
231 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
232 | - */ |
|
233 | - private $website_converter; |
|
234 | - |
|
235 | - /** |
|
236 | - * The singleton instance for the JSON-LD service. |
|
237 | - * |
|
238 | - * @since 3.15.1 |
|
239 | - * |
|
240 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
241 | - */ |
|
242 | - private static $instance; |
|
243 | - |
|
244 | - /** |
|
245 | - * Create a JSON-LD service. |
|
246 | - * |
|
247 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
248 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
249 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
250 | - * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
251 | - * |
|
252 | - * @since 3.8.0 |
|
253 | - */ |
|
254 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
255 | - |
|
256 | - $this->entity_service = $entity_service; |
|
257 | - $this->converter = $converter; |
|
258 | - $this->website_converter = $website_converter; |
|
259 | - $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
260 | - self::$instance = $this; |
|
261 | - |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Get the singleton instance for the JSON-LD service. |
|
266 | - * |
|
267 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
268 | - * @since 3.15.1 |
|
269 | - */ |
|
270 | - public static function get_instance() { |
|
271 | - |
|
272 | - return self::$instance; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
277 | - * |
|
278 | - * @since 3.8.0 |
|
279 | - */ |
|
280 | - public function get() { |
|
281 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
282 | - // |
|
283 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
284 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
285 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
286 | - @ob_clean(); |
|
287 | - |
|
288 | - // Get the parameter from the request. |
|
289 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
290 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
291 | - |
|
292 | - // Send the generated JSON-LD. |
|
293 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
294 | - |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
299 | - * |
|
300 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
301 | - * then print and die. |
|
302 | - * |
|
303 | - * @since 3.18.5 |
|
304 | - */ |
|
305 | - private function send_jsonld( $response ) { |
|
306 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
307 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
308 | - echo wp_json_encode( $response ); |
|
309 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
310 | - wp_die(); |
|
311 | - } else { |
|
312 | - die; |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Get the JSON-LD. |
|
318 | - * |
|
319 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
320 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
321 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
322 | - * |
|
323 | - * @return array A JSON-LD structure. |
|
324 | - * @since 3.15.1 |
|
325 | - */ |
|
326 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
327 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
328 | - // e-mails with a low apdex score. |
|
329 | - // |
|
330 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
331 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
332 | - |
|
333 | - // Switch to Website converter if is home page. |
|
334 | - if ( $is_homepage ) { |
|
335 | - /** |
|
336 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
337 | - * |
|
338 | - * @since 3.14.0 |
|
339 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
340 | - */ |
|
341 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
342 | - return array(); |
|
343 | - } |
|
344 | - |
|
345 | - // Set a reference to the website_converter. |
|
346 | - $website_converter = $this->website_converter; |
|
347 | - |
|
348 | - // Send JSON-LD. |
|
349 | - return $website_converter->create_schema(); |
|
350 | - } |
|
351 | - |
|
352 | - // If no id has been provided return an empty array. |
|
353 | - if ( ! isset( $post_id ) ) { |
|
354 | - return array(); |
|
355 | - } |
|
356 | - |
|
357 | - // An array of references which is captured when converting an URI to a |
|
358 | - // json which we gather to further expand our json-ld. |
|
359 | - $references = array(); |
|
360 | - $references_infos = array(); |
|
361 | - |
|
362 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
363 | - $entity_to_jsonld_converter = $this->converter; |
|
364 | - |
|
365 | - $relations = new Relations(); |
|
366 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
367 | - |
|
368 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
369 | - |
|
370 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
371 | - |
|
372 | - // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
373 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
374 | - |
|
375 | - foreach ( $relations->toArray() as $relation ) { |
|
376 | - |
|
377 | - // Setting about or mentions by label match is currently supported only for posts |
|
378 | - if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
379 | - continue; |
|
380 | - } |
|
381 | - |
|
382 | - // Add the `mentions`/`about` prop. |
|
383 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
384 | - } |
|
385 | - $graph->set_main_jsonld( $jsonld ); |
|
386 | - } |
|
387 | - |
|
388 | - $jsonld_arr = $graph->add_references( $references ) |
|
389 | - ->add_relations( $relations ) |
|
390 | - ->add_required_reference_infos( $references_infos ) |
|
391 | - ->render( $context ); |
|
392 | - |
|
393 | - /** |
|
394 | - * Filter name: wl_after_get_jsonld |
|
395 | - * |
|
396 | - * @return array |
|
397 | - * @since 3.27.2 |
|
398 | - * @var $jsonld array The final jsonld before outputting to page. |
|
399 | - * @var $post_id int The post id for which the jsonld is generated. |
|
400 | - */ |
|
401 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
402 | - |
|
403 | - return $jsonld_arr; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Write the JSON-LD in the head. |
|
408 | - * |
|
409 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
410 | - * document head, using the `wp_head` hook. |
|
411 | - * |
|
412 | - * @since 3.18.5 |
|
413 | - */ |
|
414 | - public function wp_head() { |
|
415 | - |
|
416 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
417 | - $is_homepage = is_home() || is_front_page(); |
|
418 | - $post_id = is_singular() ? get_the_ID() : null; |
|
419 | - |
|
420 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
421 | - ?> |
|
23 | + private static $creative_work_types = array( |
|
24 | + 'AmpStory', |
|
25 | + 'ArchiveComponent', |
|
26 | + 'Article', |
|
27 | + 'Atlas', |
|
28 | + 'Blog', |
|
29 | + 'Book', |
|
30 | + 'Chapter', |
|
31 | + 'Claim', |
|
32 | + 'Clip', |
|
33 | + 'Code', |
|
34 | + 'Collection', |
|
35 | + 'ComicStory', |
|
36 | + 'Comment', |
|
37 | + 'Conversation', |
|
38 | + 'Course', |
|
39 | + 'CreativeWork', |
|
40 | + 'CreativeWorkSeason', |
|
41 | + 'CreativeWorkSeries', |
|
42 | + 'DataCatalog', |
|
43 | + 'Dataset', |
|
44 | + 'DefinedTermSet', |
|
45 | + 'Diet', |
|
46 | + 'DigitalDocument', |
|
47 | + 'Drawing', |
|
48 | + 'EducationalOccupationalCredential', |
|
49 | + 'Episode', |
|
50 | + 'ExercisePlan', |
|
51 | + 'Game', |
|
52 | + 'Guide', |
|
53 | + 'HowTo', |
|
54 | + 'HowToDirection', |
|
55 | + 'HowToSection', |
|
56 | + 'HowToStep', |
|
57 | + 'HowToTip', |
|
58 | + 'HyperToc', |
|
59 | + 'HyperTocEntry', |
|
60 | + 'LearningResource', |
|
61 | + 'Legislation', |
|
62 | + 'Manuscript', |
|
63 | + 'Map', |
|
64 | + 'MathSolver', |
|
65 | + 'MediaObject', |
|
66 | + 'Menu', |
|
67 | + 'MenuSection', |
|
68 | + 'Message', |
|
69 | + 'Movie', |
|
70 | + 'MusicComposition', |
|
71 | + 'MusicPlaylist', |
|
72 | + 'MusicRecording', |
|
73 | + 'Painting', |
|
74 | + 'Photograph', |
|
75 | + 'Play', |
|
76 | + 'Poster', |
|
77 | + 'PublicationIssue', |
|
78 | + 'PublicationVolume', |
|
79 | + 'Quotation', |
|
80 | + 'Review', |
|
81 | + 'Sculpture', |
|
82 | + 'Season', |
|
83 | + 'SheetMusic', |
|
84 | + 'ShortStory', |
|
85 | + 'SoftwareApplication', |
|
86 | + 'SoftwareSourceCode', |
|
87 | + 'SpecialAnnouncement', |
|
88 | + 'Thesis', |
|
89 | + 'TvSeason', |
|
90 | + 'TvSeries', |
|
91 | + 'VisualArtwork', |
|
92 | + 'WebContent', |
|
93 | + 'WebPage', |
|
94 | + 'WebPageElement', |
|
95 | + 'WebSite', |
|
96 | + 'AdvertiserContentArticle', |
|
97 | + 'NewsArticle', |
|
98 | + 'Report', |
|
99 | + 'SatiricalArticle', |
|
100 | + 'ScholarlyArticle', |
|
101 | + 'SocialMediaPosting', |
|
102 | + 'TechArticle', |
|
103 | + 'AnalysisNewsArticle', |
|
104 | + 'AskPublicNewsArticle', |
|
105 | + 'BackgroundNewsArticle', |
|
106 | + 'OpinionNewsArticle', |
|
107 | + 'ReportageNewsArticle', |
|
108 | + 'ReviewNewsArticle', |
|
109 | + 'MedicalScholarlyArticle', |
|
110 | + 'BlogPosting', |
|
111 | + 'DiscussionForumPosting', |
|
112 | + 'LiveBlogPosting', |
|
113 | + 'ApiReference', |
|
114 | + 'Audiobook', |
|
115 | + 'MovieClip', |
|
116 | + 'RadioClip', |
|
117 | + 'TvClip', |
|
118 | + 'VideoGameClip', |
|
119 | + 'ProductCollection', |
|
120 | + 'ComicCoverArt', |
|
121 | + 'Answer', |
|
122 | + 'CorrectionComment', |
|
123 | + 'Question', |
|
124 | + 'PodcastSeason', |
|
125 | + 'RadioSeason', |
|
126 | + 'TvSeason', |
|
127 | + 'BookSeries', |
|
128 | + 'MovieSeries', |
|
129 | + 'Periodical', |
|
130 | + 'PodcastSeries', |
|
131 | + 'RadioSeries', |
|
132 | + 'TvSeries', |
|
133 | + 'VideoGameSeries', |
|
134 | + 'ComicSeries', |
|
135 | + 'Newspaper', |
|
136 | + 'DataFeed', |
|
137 | + 'CompleteDataFeed', |
|
138 | + 'CategoryCodeSet', |
|
139 | + 'NoteDigitalDocument', |
|
140 | + 'PresentationDigitalDocument', |
|
141 | + 'SpreadsheetDigitalDocument', |
|
142 | + 'TextDigitalDocument', |
|
143 | + 'PodcastEpisode', |
|
144 | + 'RadioEpisode', |
|
145 | + 'TvEpisode', |
|
146 | + 'VideoGame', |
|
147 | + 'Recipe', |
|
148 | + 'Course', |
|
149 | + 'Quiz', |
|
150 | + 'LegislationObject', |
|
151 | + 'AudioObject', |
|
152 | + 'DModel', |
|
153 | + 'DataDownload', |
|
154 | + 'ImageObject', |
|
155 | + 'LegislationObject', |
|
156 | + 'MusicVideoObject', |
|
157 | + 'VideoObject', |
|
158 | + 'Audiobook', |
|
159 | + 'Barcode', |
|
160 | + 'EmailMessage', |
|
161 | + 'MusicAlbum', |
|
162 | + 'MusicRelease', |
|
163 | + 'ComicIssue', |
|
164 | + 'ClaimReview', |
|
165 | + 'CriticReview', |
|
166 | + 'EmployerReview', |
|
167 | + 'MediaReview', |
|
168 | + 'Recommendation', |
|
169 | + 'UserReview', |
|
170 | + 'ReviewNewsArticle', |
|
171 | + 'MobileApplication', |
|
172 | + 'VideoGame', |
|
173 | + 'WebApplication', |
|
174 | + 'CoverArt', |
|
175 | + 'ComicCoverArt', |
|
176 | + 'HealthTopicContent', |
|
177 | + 'AboutPage', |
|
178 | + 'CheckoutPage', |
|
179 | + 'CollectionPage', |
|
180 | + 'ContactPage', |
|
181 | + 'FaqPage', |
|
182 | + 'ItemPage', |
|
183 | + 'MedicalWebPage', |
|
184 | + 'ProfilePage', |
|
185 | + 'QaPage', |
|
186 | + 'RealEstateListing', |
|
187 | + 'SearchResultsPage', |
|
188 | + 'MediaGallery', |
|
189 | + 'ImageGallery', |
|
190 | + 'VideoGallery', |
|
191 | + 'SiteNavigationElement', |
|
192 | + 'Table', |
|
193 | + 'WpAdBlock', |
|
194 | + 'WpFooter', |
|
195 | + 'WpHeader', |
|
196 | + 'WpSideBar', |
|
197 | + ); |
|
198 | + |
|
199 | + /** |
|
200 | + * A {@link Wordlift_Entity_Service} instance. |
|
201 | + * |
|
202 | + * @since 3.8.0 |
|
203 | + * @access private |
|
204 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
205 | + */ |
|
206 | + private $entity_service; |
|
207 | + |
|
208 | + /** |
|
209 | + * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
210 | + * |
|
211 | + * @since 3.32.0 |
|
212 | + * @access private |
|
213 | + * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
214 | + */ |
|
215 | + private $term_jsonld_adapter; |
|
216 | + |
|
217 | + /** |
|
218 | + * A {@link Wordlift_Post_Converter} instance. |
|
219 | + * |
|
220 | + * @since 3.8.0 |
|
221 | + * @access private |
|
222 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
223 | + */ |
|
224 | + private $converter; |
|
225 | + |
|
226 | + /** |
|
227 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
228 | + * |
|
229 | + * @since 3.14.0 |
|
230 | + * @access private |
|
231 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
232 | + */ |
|
233 | + private $website_converter; |
|
234 | + |
|
235 | + /** |
|
236 | + * The singleton instance for the JSON-LD service. |
|
237 | + * |
|
238 | + * @since 3.15.1 |
|
239 | + * |
|
240 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
241 | + */ |
|
242 | + private static $instance; |
|
243 | + |
|
244 | + /** |
|
245 | + * Create a JSON-LD service. |
|
246 | + * |
|
247 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
248 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
249 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
250 | + * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
251 | + * |
|
252 | + * @since 3.8.0 |
|
253 | + */ |
|
254 | + public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
255 | + |
|
256 | + $this->entity_service = $entity_service; |
|
257 | + $this->converter = $converter; |
|
258 | + $this->website_converter = $website_converter; |
|
259 | + $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
260 | + self::$instance = $this; |
|
261 | + |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Get the singleton instance for the JSON-LD service. |
|
266 | + * |
|
267 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
268 | + * @since 3.15.1 |
|
269 | + */ |
|
270 | + public static function get_instance() { |
|
271 | + |
|
272 | + return self::$instance; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
277 | + * |
|
278 | + * @since 3.8.0 |
|
279 | + */ |
|
280 | + public function get() { |
|
281 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
282 | + // |
|
283 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
284 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
285 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
286 | + @ob_clean(); |
|
287 | + |
|
288 | + // Get the parameter from the request. |
|
289 | + $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
290 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
291 | + |
|
292 | + // Send the generated JSON-LD. |
|
293 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
294 | + |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
299 | + * |
|
300 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
301 | + * then print and die. |
|
302 | + * |
|
303 | + * @since 3.18.5 |
|
304 | + */ |
|
305 | + private function send_jsonld( $response ) { |
|
306 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
307 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
308 | + echo wp_json_encode( $response ); |
|
309 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
310 | + wp_die(); |
|
311 | + } else { |
|
312 | + die; |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Get the JSON-LD. |
|
318 | + * |
|
319 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
320 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
321 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
322 | + * |
|
323 | + * @return array A JSON-LD structure. |
|
324 | + * @since 3.15.1 |
|
325 | + */ |
|
326 | + public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
327 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
328 | + // e-mails with a low apdex score. |
|
329 | + // |
|
330 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
331 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
332 | + |
|
333 | + // Switch to Website converter if is home page. |
|
334 | + if ( $is_homepage ) { |
|
335 | + /** |
|
336 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
337 | + * |
|
338 | + * @since 3.14.0 |
|
339 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
340 | + */ |
|
341 | + if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
342 | + return array(); |
|
343 | + } |
|
344 | + |
|
345 | + // Set a reference to the website_converter. |
|
346 | + $website_converter = $this->website_converter; |
|
347 | + |
|
348 | + // Send JSON-LD. |
|
349 | + return $website_converter->create_schema(); |
|
350 | + } |
|
351 | + |
|
352 | + // If no id has been provided return an empty array. |
|
353 | + if ( ! isset( $post_id ) ) { |
|
354 | + return array(); |
|
355 | + } |
|
356 | + |
|
357 | + // An array of references which is captured when converting an URI to a |
|
358 | + // json which we gather to further expand our json-ld. |
|
359 | + $references = array(); |
|
360 | + $references_infos = array(); |
|
361 | + |
|
362 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
363 | + $entity_to_jsonld_converter = $this->converter; |
|
364 | + |
|
365 | + $relations = new Relations(); |
|
366 | + $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
367 | + |
|
368 | + $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
369 | + |
|
370 | + $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
371 | + |
|
372 | + // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
373 | + if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
374 | + |
|
375 | + foreach ( $relations->toArray() as $relation ) { |
|
376 | + |
|
377 | + // Setting about or mentions by label match is currently supported only for posts |
|
378 | + if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
379 | + continue; |
|
380 | + } |
|
381 | + |
|
382 | + // Add the `mentions`/`about` prop. |
|
383 | + $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
384 | + } |
|
385 | + $graph->set_main_jsonld( $jsonld ); |
|
386 | + } |
|
387 | + |
|
388 | + $jsonld_arr = $graph->add_references( $references ) |
|
389 | + ->add_relations( $relations ) |
|
390 | + ->add_required_reference_infos( $references_infos ) |
|
391 | + ->render( $context ); |
|
392 | + |
|
393 | + /** |
|
394 | + * Filter name: wl_after_get_jsonld |
|
395 | + * |
|
396 | + * @return array |
|
397 | + * @since 3.27.2 |
|
398 | + * @var $jsonld array The final jsonld before outputting to page. |
|
399 | + * @var $post_id int The post id for which the jsonld is generated. |
|
400 | + */ |
|
401 | + $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
402 | + |
|
403 | + return $jsonld_arr; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Write the JSON-LD in the head. |
|
408 | + * |
|
409 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
410 | + * document head, using the `wp_head` hook. |
|
411 | + * |
|
412 | + * @since 3.18.5 |
|
413 | + */ |
|
414 | + public function wp_head() { |
|
415 | + |
|
416 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
417 | + $is_homepage = is_home() || is_front_page(); |
|
418 | + $post_id = is_singular() ? get_the_ID() : null; |
|
419 | + |
|
420 | + $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
421 | + ?> |
|
422 | 422 | <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
423 | 423 | <?php |
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * @param array $jsonld |
|
428 | - * @param Relation $relation |
|
429 | - * |
|
430 | - * @return void |
|
431 | - */ |
|
432 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
433 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
434 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
435 | - |
|
436 | - $object = $relation->get_object(); |
|
437 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
438 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
439 | - |
|
440 | - $escaped_labels = array_map( |
|
441 | - function ( $value ) { |
|
442 | - return preg_quote( $value, '/' ); |
|
443 | - }, |
|
444 | - $labels |
|
445 | - ); |
|
446 | - |
|
447 | - $matches = false; |
|
448 | - |
|
449 | - // When the title is empty, then we shouldn't yield a match to about section. |
|
450 | - if ( array_filter( $escaped_labels ) ) { |
|
451 | - // Check if the labels match any part of the title. |
|
452 | - $post = get_post( $post_id ); |
|
453 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
454 | - } |
|
455 | - |
|
456 | - if ( $entity_uri ) { |
|
457 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
458 | - $property_name = $matches ? 'about' : 'mentions'; |
|
459 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
460 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
461 | - } |
|
462 | - |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Check if the labels match any part of the title. |
|
467 | - * |
|
468 | - * @param $labels array The labels to check. |
|
469 | - * @param $title string The title to check. |
|
470 | - * |
|
471 | - * @return boolean |
|
472 | - */ |
|
473 | - public function check_title_match( $labels, $title ) { |
|
474 | - |
|
475 | - // If the title is empty, then we shouldn't yield a match to about section. |
|
476 | - if ( empty( $title ) ) { |
|
477 | - return false; |
|
478 | - } |
|
479 | - |
|
480 | - // Check if the labels match any part of the title. |
|
481 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
482 | - |
|
483 | - } |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * @param array $jsonld |
|
428 | + * @param Relation $relation |
|
429 | + * |
|
430 | + * @return void |
|
431 | + */ |
|
432 | + private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
433 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
434 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
435 | + |
|
436 | + $object = $relation->get_object(); |
|
437 | + $entity_uri = $content_service->get_entity_id( $object ); |
|
438 | + $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
439 | + |
|
440 | + $escaped_labels = array_map( |
|
441 | + function ( $value ) { |
|
442 | + return preg_quote( $value, '/' ); |
|
443 | + }, |
|
444 | + $labels |
|
445 | + ); |
|
446 | + |
|
447 | + $matches = false; |
|
448 | + |
|
449 | + // When the title is empty, then we shouldn't yield a match to about section. |
|
450 | + if ( array_filter( $escaped_labels ) ) { |
|
451 | + // Check if the labels match any part of the title. |
|
452 | + $post = get_post( $post_id ); |
|
453 | + $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
454 | + } |
|
455 | + |
|
456 | + if ( $entity_uri ) { |
|
457 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
458 | + $property_name = $matches ? 'about' : 'mentions'; |
|
459 | + $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
460 | + $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
461 | + } |
|
462 | + |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Check if the labels match any part of the title. |
|
467 | + * |
|
468 | + * @param $labels array The labels to check. |
|
469 | + * @param $title string The title to check. |
|
470 | + * |
|
471 | + * @return boolean |
|
472 | + */ |
|
473 | + public function check_title_match( $labels, $title ) { |
|
474 | + |
|
475 | + // If the title is empty, then we shouldn't yield a match to about section. |
|
476 | + if ( empty( $title ) ) { |
|
477 | + return false; |
|
478 | + } |
|
479 | + |
|
480 | + // Check if the labels match any part of the title. |
|
481 | + return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
482 | + |
|
483 | + } |
|
484 | 484 | |
485 | 485 | } |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * |
252 | 252 | * @since 3.8.0 |
253 | 253 | */ |
254 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
254 | + public function __construct($entity_service, $converter, $website_converter, $term_jsonld_adapter) { |
|
255 | 255 | |
256 | 256 | $this->entity_service = $entity_service; |
257 | 257 | $this->converter = $converter; |
@@ -286,11 +286,11 @@ discard block |
||
286 | 286 | @ob_clean(); |
287 | 287 | |
288 | 288 | // Get the parameter from the request. |
289 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
290 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
289 | + $is_homepage = isset($_REQUEST['homepage']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
290 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
291 | 291 | |
292 | 292 | // Send the generated JSON-LD. |
293 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
293 | + $this->send_jsonld($this->get_jsonld($is_homepage, $post_id)); |
|
294 | 294 | |
295 | 295 | } |
296 | 296 | |
@@ -302,11 +302,11 @@ discard block |
||
302 | 302 | * |
303 | 303 | * @since 3.18.5 |
304 | 304 | */ |
305 | - private function send_jsonld( $response ) { |
|
305 | + private function send_jsonld($response) { |
|
306 | 306 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
307 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
308 | - echo wp_json_encode( $response ); |
|
309 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
307 | + @header('Content-Type: application/ld+json; charset='.get_option('blog_charset')); |
|
308 | + echo wp_json_encode($response); |
|
309 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
310 | 310 | wp_die(); |
311 | 311 | } else { |
312 | 312 | die; |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | * @return array A JSON-LD structure. |
324 | 324 | * @since 3.15.1 |
325 | 325 | */ |
326 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
326 | + public function get_jsonld($is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN) { |
|
327 | 327 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
328 | 328 | // e-mails with a low apdex score. |
329 | 329 | // |
@@ -331,14 +331,14 @@ discard block |
||
331 | 331 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
332 | 332 | |
333 | 333 | // Switch to Website converter if is home page. |
334 | - if ( $is_homepage ) { |
|
334 | + if ($is_homepage) { |
|
335 | 335 | /** |
336 | 336 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
337 | 337 | * |
338 | 338 | * @since 3.14.0 |
339 | 339 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
340 | 340 | */ |
341 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
341 | + if (apply_filters('wordlift_disable_website_json_ld', false)) { |
|
342 | 342 | return array(); |
343 | 343 | } |
344 | 344 | |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | } |
351 | 351 | |
352 | 352 | // If no id has been provided return an empty array. |
353 | - if ( ! isset( $post_id ) ) { |
|
353 | + if ( ! isset($post_id)) { |
|
354 | 354 | return array(); |
355 | 355 | } |
356 | 356 | |
@@ -363,32 +363,32 @@ discard block |
||
363 | 363 | $entity_to_jsonld_converter = $this->converter; |
364 | 364 | |
365 | 365 | $relations = new Relations(); |
366 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
366 | + $jsonld = $entity_to_jsonld_converter->convert($post_id, $references, $references_infos, $relations); |
|
367 | 367 | |
368 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
368 | + $graph = new Graph($jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance()); |
|
369 | 369 | |
370 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
370 | + $schema_type = is_array($jsonld['@type']) ? $jsonld['@type'] : array($jsonld['@type']); |
|
371 | 371 | |
372 | 372 | // Add `about`/`mentions` only for `CreativeWork` and descendants. |
373 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
373 | + if (array_intersect($schema_type, self::$creative_work_types)) { |
|
374 | 374 | |
375 | - foreach ( $relations->toArray() as $relation ) { |
|
375 | + foreach ($relations->toArray() as $relation) { |
|
376 | 376 | |
377 | 377 | // Setting about or mentions by label match is currently supported only for posts |
378 | - if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
378 | + if (Object_Type_Enum::POST !== $relation->get_object()->get_type()) { |
|
379 | 379 | continue; |
380 | 380 | } |
381 | 381 | |
382 | 382 | // Add the `mentions`/`about` prop. |
383 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
383 | + $this->add_mention_or_about($jsonld, $post_id, $relation); |
|
384 | 384 | } |
385 | - $graph->set_main_jsonld( $jsonld ); |
|
385 | + $graph->set_main_jsonld($jsonld); |
|
386 | 386 | } |
387 | 387 | |
388 | - $jsonld_arr = $graph->add_references( $references ) |
|
389 | - ->add_relations( $relations ) |
|
390 | - ->add_required_reference_infos( $references_infos ) |
|
391 | - ->render( $context ); |
|
388 | + $jsonld_arr = $graph->add_references($references) |
|
389 | + ->add_relations($relations) |
|
390 | + ->add_required_reference_infos($references_infos) |
|
391 | + ->render($context); |
|
392 | 392 | |
393 | 393 | /** |
394 | 394 | * Filter name: wl_after_get_jsonld |
@@ -398,7 +398,7 @@ discard block |
||
398 | 398 | * @var $jsonld array The final jsonld before outputting to page. |
399 | 399 | * @var $post_id int The post id for which the jsonld is generated. |
400 | 400 | */ |
401 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
401 | + $jsonld_arr = apply_filters('wl_after_get_jsonld', $jsonld_arr, $post_id, $context); |
|
402 | 402 | |
403 | 403 | return $jsonld_arr; |
404 | 404 | } |
@@ -417,9 +417,9 @@ discard block |
||
417 | 417 | $is_homepage = is_home() || is_front_page(); |
418 | 418 | $post_id = is_singular() ? get_the_ID() : null; |
419 | 419 | |
420 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
420 | + $jsonld = wp_json_encode($this->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE)); |
|
421 | 421 | ?> |
422 | - <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
|
422 | + <script type="application/ld+json"><?php echo esc_html($jsonld); ?></script> |
|
423 | 423 | <?php |
424 | 424 | } |
425 | 425 | |
@@ -429,17 +429,17 @@ discard block |
||
429 | 429 | * |
430 | 430 | * @return void |
431 | 431 | */ |
432 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
432 | + private function add_mention_or_about(&$jsonld, $post_id, $relation) { |
|
433 | 433 | $content_service = Wordpress_Content_Service::get_instance(); |
434 | 434 | $entity_service = Wordlift_Entity_Service::get_instance(); |
435 | 435 | |
436 | 436 | $object = $relation->get_object(); |
437 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
438 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
437 | + $entity_uri = $content_service->get_entity_id($object); |
|
438 | + $labels = $entity_service->get_labels($object->get_id(), $object->get_type()); |
|
439 | 439 | |
440 | 440 | $escaped_labels = array_map( |
441 | - function ( $value ) { |
|
442 | - return preg_quote( $value, '/' ); |
|
441 | + function($value) { |
|
442 | + return preg_quote($value, '/'); |
|
443 | 443 | }, |
444 | 444 | $labels |
445 | 445 | ); |
@@ -447,17 +447,17 @@ discard block |
||
447 | 447 | $matches = false; |
448 | 448 | |
449 | 449 | // When the title is empty, then we shouldn't yield a match to about section. |
450 | - if ( array_filter( $escaped_labels ) ) { |
|
450 | + if (array_filter($escaped_labels)) { |
|
451 | 451 | // Check if the labels match any part of the title. |
452 | - $post = get_post( $post_id ); |
|
453 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
452 | + $post = get_post($post_id); |
|
453 | + $matches = $this->check_title_match($escaped_labels, $post->post_title); |
|
454 | 454 | } |
455 | 455 | |
456 | - if ( $entity_uri ) { |
|
456 | + if ($entity_uri) { |
|
457 | 457 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
458 | 458 | $property_name = $matches ? 'about' : 'mentions'; |
459 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
460 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
459 | + $jsonld[$property_name] = isset($jsonld[$property_name]) ? (array) $jsonld[$property_name] : array(); |
|
460 | + $jsonld[$property_name][] = array('@id' => $entity_uri); |
|
461 | 461 | } |
462 | 462 | |
463 | 463 | } |
@@ -470,15 +470,15 @@ discard block |
||
470 | 470 | * |
471 | 471 | * @return boolean |
472 | 472 | */ |
473 | - public function check_title_match( $labels, $title ) { |
|
473 | + public function check_title_match($labels, $title) { |
|
474 | 474 | |
475 | 475 | // If the title is empty, then we shouldn't yield a match to about section. |
476 | - if ( empty( $title ) ) { |
|
476 | + if (empty($title)) { |
|
477 | 477 | return false; |
478 | 478 | } |
479 | 479 | |
480 | 480 | // Check if the labels match any part of the title. |
481 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
481 | + return 1 === preg_match('/\b('.implode('|', $labels).')\b/iu', $title); |
|
482 | 482 | |
483 | 483 | } |
484 | 484 |
@@ -14,183 +14,183 @@ |
||
14 | 14 | */ |
15 | 15 | class Graph { |
16 | 16 | |
17 | - /** |
|
18 | - * A single item in jsonld ( associative array ) |
|
19 | - * |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - private $main_jsonld; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var array<Content_Id> |
|
26 | - */ |
|
27 | - private $referenced_content_ids = array(); |
|
28 | - /** |
|
29 | - * @var \Wordlift_Post_Converter |
|
30 | - */ |
|
31 | - private $post_converter; |
|
32 | - /** |
|
33 | - * @var \Wordlift_Term_JsonLd_Adapter |
|
34 | - */ |
|
35 | - private $term_converter; |
|
36 | - |
|
37 | - public function __construct( $main_jsonld, $post_converter, $term_converter ) { |
|
38 | - $this->main_jsonld = $main_jsonld; |
|
39 | - $this->post_converter = $post_converter; |
|
40 | - $this->term_converter = $term_converter; |
|
41 | - } |
|
42 | - |
|
43 | - public function set_main_jsonld( $main_jsonld ) { |
|
44 | - $this->main_jsonld = $main_jsonld; |
|
45 | - } |
|
46 | - |
|
47 | - public function get_main_jsonld() { |
|
48 | - return $this->main_jsonld; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param $references array<int> |
|
53 | - * |
|
54 | - * @return Graph |
|
55 | - */ |
|
56 | - public function add_references( $refs ) { |
|
57 | - Assertions::is_array( $refs ); |
|
58 | - |
|
59 | - foreach ( $refs as $ref ) { |
|
60 | - $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref ); |
|
61 | - } |
|
62 | - return $this; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param $reference_infos array |
|
67 | - * Structure: [ |
|
68 | - * [ |
|
69 | - * 'reference' => \Wordlift_Property_Entity_Reference, |
|
70 | - * ], |
|
71 | - * // more array items ... |
|
72 | - * ] |
|
73 | - * |
|
74 | - * @return Graph |
|
75 | - */ |
|
76 | - public function add_required_reference_infos( $references_infos ) { |
|
77 | - |
|
78 | - /** |
|
79 | - * @var $required_references array<\Wordlift_Property_Entity_Reference> |
|
80 | - */ |
|
81 | - $required_references = array_filter( |
|
82 | - $references_infos, |
|
83 | - function ( $item ) { |
|
84 | - return isset( $item['reference'] ) && |
|
85 | - // Check that the reference is required |
|
86 | - $item['reference']->get_required(); |
|
87 | - } |
|
88 | - ); |
|
89 | - |
|
90 | - foreach ( $required_references as $required_reference ) { |
|
91 | - $this->referenced_content_ids[] = new Wordpress_Content_Id( |
|
92 | - $required_reference->get_id(), |
|
93 | - $required_reference->get_type() |
|
94 | - ); |
|
95 | - } |
|
96 | - return $this; |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @param $relations Relations |
|
102 | - * |
|
103 | - * @return Graph |
|
104 | - */ |
|
105 | - public function add_relations( $relations ) { |
|
106 | - |
|
107 | - foreach ( $relations->toArray() as $relation ) { |
|
108 | - |
|
109 | - $this->referenced_content_ids[] = $relation->get_object(); |
|
110 | - |
|
111 | - } |
|
112 | - |
|
113 | - return $this; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * This method expands the content id and return only the jsonld. |
|
118 | - * |
|
119 | - * @param $content_id Wordpress_Content_Id |
|
120 | - * @param $context int |
|
121 | - * @return array|bool |
|
122 | - */ |
|
123 | - private function expand( $content_id, $context ) { |
|
124 | - $object_id = $content_id->get_id(); |
|
125 | - $object_type = $content_id->get_type(); |
|
126 | - |
|
127 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
128 | - return $this->expand_post( $object_id ); |
|
129 | - } elseif ( $object_type === Object_Type_Enum::TERM ) { |
|
130 | - return $this->expand_term( $object_id, $context ); |
|
131 | - } |
|
132 | - |
|
133 | - return false; |
|
134 | - |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param $context int Instance of Jsonld_Context_Enum |
|
139 | - * |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - public function render( $context ) { |
|
143 | - |
|
144 | - /** |
|
145 | - * This is possible because the toString() method of |
|
146 | - * Wordpress_Content_Id is used to get the unique value. |
|
147 | - */ |
|
148 | - $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING ); |
|
149 | - |
|
150 | - $result = array( $this->main_jsonld ); |
|
151 | - |
|
152 | - foreach ( $unique_content_ids as $unique_content_id ) { |
|
153 | - $result[] = $this->expand( $unique_content_id, $context ); |
|
154 | - } |
|
155 | - |
|
156 | - // Filter out the false and empty results. |
|
157 | - return array_filter( $result ); |
|
158 | - |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @param $object_id |
|
163 | - * |
|
164 | - * @return false|mixed |
|
165 | - */ |
|
166 | - public function expand_post( $object_id ) { |
|
167 | - if ( get_post_status( $object_id ) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type( |
|
168 | - $object_id, |
|
169 | - 'http://schema.org/Article' |
|
170 | - ) ) { |
|
171 | - return false; |
|
172 | - } |
|
173 | - |
|
174 | - $references = array(); |
|
175 | - $reference_info = array(); |
|
176 | - $relations = new Relations(); |
|
177 | - |
|
178 | - return $this->post_converter->convert( $object_id, $references, $reference_info, $relations ); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @param $object_id |
|
183 | - * @param $context |
|
184 | - * |
|
185 | - * @return false|mixed |
|
186 | - */ |
|
187 | - public function expand_term( $object_id, $context ) { |
|
188 | - // Skip the Uncategorized term. |
|
189 | - if ( 1 === $object_id ) { |
|
190 | - return false; |
|
191 | - } |
|
192 | - |
|
193 | - return current( $this->term_converter->get( $object_id, $context ) ); |
|
194 | - } |
|
17 | + /** |
|
18 | + * A single item in jsonld ( associative array ) |
|
19 | + * |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + private $main_jsonld; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var array<Content_Id> |
|
26 | + */ |
|
27 | + private $referenced_content_ids = array(); |
|
28 | + /** |
|
29 | + * @var \Wordlift_Post_Converter |
|
30 | + */ |
|
31 | + private $post_converter; |
|
32 | + /** |
|
33 | + * @var \Wordlift_Term_JsonLd_Adapter |
|
34 | + */ |
|
35 | + private $term_converter; |
|
36 | + |
|
37 | + public function __construct( $main_jsonld, $post_converter, $term_converter ) { |
|
38 | + $this->main_jsonld = $main_jsonld; |
|
39 | + $this->post_converter = $post_converter; |
|
40 | + $this->term_converter = $term_converter; |
|
41 | + } |
|
42 | + |
|
43 | + public function set_main_jsonld( $main_jsonld ) { |
|
44 | + $this->main_jsonld = $main_jsonld; |
|
45 | + } |
|
46 | + |
|
47 | + public function get_main_jsonld() { |
|
48 | + return $this->main_jsonld; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param $references array<int> |
|
53 | + * |
|
54 | + * @return Graph |
|
55 | + */ |
|
56 | + public function add_references( $refs ) { |
|
57 | + Assertions::is_array( $refs ); |
|
58 | + |
|
59 | + foreach ( $refs as $ref ) { |
|
60 | + $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref ); |
|
61 | + } |
|
62 | + return $this; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param $reference_infos array |
|
67 | + * Structure: [ |
|
68 | + * [ |
|
69 | + * 'reference' => \Wordlift_Property_Entity_Reference, |
|
70 | + * ], |
|
71 | + * // more array items ... |
|
72 | + * ] |
|
73 | + * |
|
74 | + * @return Graph |
|
75 | + */ |
|
76 | + public function add_required_reference_infos( $references_infos ) { |
|
77 | + |
|
78 | + /** |
|
79 | + * @var $required_references array<\Wordlift_Property_Entity_Reference> |
|
80 | + */ |
|
81 | + $required_references = array_filter( |
|
82 | + $references_infos, |
|
83 | + function ( $item ) { |
|
84 | + return isset( $item['reference'] ) && |
|
85 | + // Check that the reference is required |
|
86 | + $item['reference']->get_required(); |
|
87 | + } |
|
88 | + ); |
|
89 | + |
|
90 | + foreach ( $required_references as $required_reference ) { |
|
91 | + $this->referenced_content_ids[] = new Wordpress_Content_Id( |
|
92 | + $required_reference->get_id(), |
|
93 | + $required_reference->get_type() |
|
94 | + ); |
|
95 | + } |
|
96 | + return $this; |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @param $relations Relations |
|
102 | + * |
|
103 | + * @return Graph |
|
104 | + */ |
|
105 | + public function add_relations( $relations ) { |
|
106 | + |
|
107 | + foreach ( $relations->toArray() as $relation ) { |
|
108 | + |
|
109 | + $this->referenced_content_ids[] = $relation->get_object(); |
|
110 | + |
|
111 | + } |
|
112 | + |
|
113 | + return $this; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * This method expands the content id and return only the jsonld. |
|
118 | + * |
|
119 | + * @param $content_id Wordpress_Content_Id |
|
120 | + * @param $context int |
|
121 | + * @return array|bool |
|
122 | + */ |
|
123 | + private function expand( $content_id, $context ) { |
|
124 | + $object_id = $content_id->get_id(); |
|
125 | + $object_type = $content_id->get_type(); |
|
126 | + |
|
127 | + if ( $object_type === Object_Type_Enum::POST ) { |
|
128 | + return $this->expand_post( $object_id ); |
|
129 | + } elseif ( $object_type === Object_Type_Enum::TERM ) { |
|
130 | + return $this->expand_term( $object_id, $context ); |
|
131 | + } |
|
132 | + |
|
133 | + return false; |
|
134 | + |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param $context int Instance of Jsonld_Context_Enum |
|
139 | + * |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + public function render( $context ) { |
|
143 | + |
|
144 | + /** |
|
145 | + * This is possible because the toString() method of |
|
146 | + * Wordpress_Content_Id is used to get the unique value. |
|
147 | + */ |
|
148 | + $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING ); |
|
149 | + |
|
150 | + $result = array( $this->main_jsonld ); |
|
151 | + |
|
152 | + foreach ( $unique_content_ids as $unique_content_id ) { |
|
153 | + $result[] = $this->expand( $unique_content_id, $context ); |
|
154 | + } |
|
155 | + |
|
156 | + // Filter out the false and empty results. |
|
157 | + return array_filter( $result ); |
|
158 | + |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @param $object_id |
|
163 | + * |
|
164 | + * @return false|mixed |
|
165 | + */ |
|
166 | + public function expand_post( $object_id ) { |
|
167 | + if ( get_post_status( $object_id ) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type( |
|
168 | + $object_id, |
|
169 | + 'http://schema.org/Article' |
|
170 | + ) ) { |
|
171 | + return false; |
|
172 | + } |
|
173 | + |
|
174 | + $references = array(); |
|
175 | + $reference_info = array(); |
|
176 | + $relations = new Relations(); |
|
177 | + |
|
178 | + return $this->post_converter->convert( $object_id, $references, $reference_info, $relations ); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @param $object_id |
|
183 | + * @param $context |
|
184 | + * |
|
185 | + * @return false|mixed |
|
186 | + */ |
|
187 | + public function expand_term( $object_id, $context ) { |
|
188 | + // Skip the Uncategorized term. |
|
189 | + if ( 1 === $object_id ) { |
|
190 | + return false; |
|
191 | + } |
|
192 | + |
|
193 | + return current( $this->term_converter->get( $object_id, $context ) ); |
|
194 | + } |
|
195 | 195 | |
196 | 196 | } |
@@ -34,13 +34,13 @@ discard block |
||
34 | 34 | */ |
35 | 35 | private $term_converter; |
36 | 36 | |
37 | - public function __construct( $main_jsonld, $post_converter, $term_converter ) { |
|
37 | + public function __construct($main_jsonld, $post_converter, $term_converter) { |
|
38 | 38 | $this->main_jsonld = $main_jsonld; |
39 | 39 | $this->post_converter = $post_converter; |
40 | 40 | $this->term_converter = $term_converter; |
41 | 41 | } |
42 | 42 | |
43 | - public function set_main_jsonld( $main_jsonld ) { |
|
43 | + public function set_main_jsonld($main_jsonld) { |
|
44 | 44 | $this->main_jsonld = $main_jsonld; |
45 | 45 | } |
46 | 46 | |
@@ -53,11 +53,11 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return Graph |
55 | 55 | */ |
56 | - public function add_references( $refs ) { |
|
57 | - Assertions::is_array( $refs ); |
|
56 | + public function add_references($refs) { |
|
57 | + Assertions::is_array($refs); |
|
58 | 58 | |
59 | - foreach ( $refs as $ref ) { |
|
60 | - $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref ); |
|
59 | + foreach ($refs as $ref) { |
|
60 | + $this->referenced_content_ids[] = Wordpress_Content_Id::create_post($ref); |
|
61 | 61 | } |
62 | 62 | return $this; |
63 | 63 | } |
@@ -73,21 +73,21 @@ discard block |
||
73 | 73 | * |
74 | 74 | * @return Graph |
75 | 75 | */ |
76 | - public function add_required_reference_infos( $references_infos ) { |
|
76 | + public function add_required_reference_infos($references_infos) { |
|
77 | 77 | |
78 | 78 | /** |
79 | 79 | * @var $required_references array<\Wordlift_Property_Entity_Reference> |
80 | 80 | */ |
81 | 81 | $required_references = array_filter( |
82 | 82 | $references_infos, |
83 | - function ( $item ) { |
|
84 | - return isset( $item['reference'] ) && |
|
83 | + function($item) { |
|
84 | + return isset($item['reference']) && |
|
85 | 85 | // Check that the reference is required |
86 | 86 | $item['reference']->get_required(); |
87 | 87 | } |
88 | 88 | ); |
89 | 89 | |
90 | - foreach ( $required_references as $required_reference ) { |
|
90 | + foreach ($required_references as $required_reference) { |
|
91 | 91 | $this->referenced_content_ids[] = new Wordpress_Content_Id( |
92 | 92 | $required_reference->get_id(), |
93 | 93 | $required_reference->get_type() |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | * |
103 | 103 | * @return Graph |
104 | 104 | */ |
105 | - public function add_relations( $relations ) { |
|
105 | + public function add_relations($relations) { |
|
106 | 106 | |
107 | - foreach ( $relations->toArray() as $relation ) { |
|
107 | + foreach ($relations->toArray() as $relation) { |
|
108 | 108 | |
109 | 109 | $this->referenced_content_ids[] = $relation->get_object(); |
110 | 110 | |
@@ -120,14 +120,14 @@ discard block |
||
120 | 120 | * @param $context int |
121 | 121 | * @return array|bool |
122 | 122 | */ |
123 | - private function expand( $content_id, $context ) { |
|
123 | + private function expand($content_id, $context) { |
|
124 | 124 | $object_id = $content_id->get_id(); |
125 | 125 | $object_type = $content_id->get_type(); |
126 | 126 | |
127 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
128 | - return $this->expand_post( $object_id ); |
|
129 | - } elseif ( $object_type === Object_Type_Enum::TERM ) { |
|
130 | - return $this->expand_term( $object_id, $context ); |
|
127 | + if ($object_type === Object_Type_Enum::POST) { |
|
128 | + return $this->expand_post($object_id); |
|
129 | + } elseif ($object_type === Object_Type_Enum::TERM) { |
|
130 | + return $this->expand_term($object_id, $context); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | return false; |
@@ -139,22 +139,22 @@ discard block |
||
139 | 139 | * |
140 | 140 | * @return array |
141 | 141 | */ |
142 | - public function render( $context ) { |
|
142 | + public function render($context) { |
|
143 | 143 | |
144 | 144 | /** |
145 | 145 | * This is possible because the toString() method of |
146 | 146 | * Wordpress_Content_Id is used to get the unique value. |
147 | 147 | */ |
148 | - $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING ); |
|
148 | + $unique_content_ids = array_unique($this->referenced_content_ids, SORT_STRING); |
|
149 | 149 | |
150 | - $result = array( $this->main_jsonld ); |
|
150 | + $result = array($this->main_jsonld); |
|
151 | 151 | |
152 | - foreach ( $unique_content_ids as $unique_content_id ) { |
|
153 | - $result[] = $this->expand( $unique_content_id, $context ); |
|
152 | + foreach ($unique_content_ids as $unique_content_id) { |
|
153 | + $result[] = $this->expand($unique_content_id, $context); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | // Filter out the false and empty results. |
157 | - return array_filter( $result ); |
|
157 | + return array_filter($result); |
|
158 | 158 | |
159 | 159 | } |
160 | 160 | |
@@ -163,11 +163,11 @@ discard block |
||
163 | 163 | * |
164 | 164 | * @return false|mixed |
165 | 165 | */ |
166 | - public function expand_post( $object_id ) { |
|
167 | - if ( get_post_status( $object_id ) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type( |
|
166 | + public function expand_post($object_id) { |
|
167 | + if (get_post_status($object_id) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type( |
|
168 | 168 | $object_id, |
169 | 169 | 'http://schema.org/Article' |
170 | - ) ) { |
|
170 | + )) { |
|
171 | 171 | return false; |
172 | 172 | } |
173 | 173 | |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | $reference_info = array(); |
176 | 176 | $relations = new Relations(); |
177 | 177 | |
178 | - return $this->post_converter->convert( $object_id, $references, $reference_info, $relations ); |
|
178 | + return $this->post_converter->convert($object_id, $references, $reference_info, $relations); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
@@ -184,13 +184,13 @@ discard block |
||
184 | 184 | * |
185 | 185 | * @return false|mixed |
186 | 186 | */ |
187 | - public function expand_term( $object_id, $context ) { |
|
187 | + public function expand_term($object_id, $context) { |
|
188 | 188 | // Skip the Uncategorized term. |
189 | - if ( 1 === $object_id ) { |
|
189 | + if (1 === $object_id) { |
|
190 | 190 | return false; |
191 | 191 | } |
192 | 192 | |
193 | - return current( $this->term_converter->get( $object_id, $context ) ); |
|
193 | + return current($this->term_converter->get($object_id, $context)); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | } |