@@ -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 | } |
@@ -19,147 +19,147 @@ discard block |
||
19 | 19 | */ |
20 | 20 | class Wordlift_Entity_Type_Service { |
21 | 21 | |
22 | - /** |
|
23 | - * The {@link Wordlift_Schema_Service} instance. |
|
24 | - * |
|
25 | - * @since 3.7.0 |
|
26 | - * @access private |
|
27 | - * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
28 | - */ |
|
29 | - private $schema_service; |
|
30 | - |
|
31 | - /** |
|
32 | - * A {@link Wordlift_Log_Service} instance. |
|
33 | - * |
|
34 | - * @since 3.8.0 |
|
35 | - * @access private |
|
36 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
37 | - */ |
|
38 | - private $log; |
|
39 | - |
|
40 | - /** |
|
41 | - * Wordlift_Entity_Type_Service constructor. |
|
42 | - * |
|
43 | - * @since 3.7.0 |
|
44 | - */ |
|
45 | - protected function __construct() { |
|
46 | - |
|
47 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
48 | - |
|
49 | - $this->schema_service = Wordlift_Schema_Service::get_instance(); |
|
50 | - |
|
51 | - $this->prepare_post_types(); |
|
52 | - |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Prepare post types for Gutenberg use |
|
57 | - * |
|
58 | - * @since 3.26.0 |
|
59 | - */ |
|
60 | - private function prepare_post_types() { |
|
61 | - |
|
62 | - add_action( |
|
63 | - 'init', |
|
64 | - function () { |
|
65 | - // Add post type support for 'custom-fields' for all post types. Specifically needed in Gutenberg |
|
66 | - $post_types = get_post_types(); |
|
67 | - foreach ( $post_types as $post_type ) { |
|
68 | - add_post_type_support( $post_type, 'custom-fields' ); |
|
69 | - } |
|
70 | - } |
|
71 | - ); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
76 | - * |
|
77 | - * @since 3.7.0 |
|
78 | - * @access private |
|
79 | - * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
80 | - */ |
|
81 | - private static $instance = null; |
|
82 | - |
|
83 | - /** |
|
84 | - * Get the {@link Wordlift_Entity_Type_Service} singleton instance. |
|
85 | - * |
|
86 | - * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
87 | - * @since 3.7.0 |
|
88 | - */ |
|
89 | - public static function get_instance() { |
|
90 | - |
|
91 | - if ( ! isset( self::$instance ) ) { |
|
92 | - self::$instance = new self(); |
|
93 | - } |
|
94 | - |
|
95 | - return self::$instance; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Get the types associated with the specified entity post id. |
|
100 | - * |
|
101 | - * We have a strategy to define the entity type, given that everything is |
|
102 | - * an entity, i.e. also posts/pages and custom post types. |
|
103 | - * |
|
104 | - * @param int $post_id The post id. |
|
105 | - * |
|
106 | - * @return array|null { |
|
107 | - * An array of type properties or null if no term is associated |
|
108 | - * |
|
109 | - * @type string css_class The css class, e.g. `wl-thing`. |
|
110 | - * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
111 | - * @type array same_as An array of same as attributes. |
|
112 | - * @type array custom_fields An array of custom fields. |
|
113 | - * } |
|
114 | - * @since 3.33.9 The `linked_data` key has been removed. |
|
115 | - * |
|
116 | - * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0. |
|
117 | - * |
|
118 | - * @since 3.18.0 The cases are the following: |
|
119 | - * 1. the post has a term from the Entity Types Taxonomy: the term defines |
|
120 | - * the entity type, e.g. Organization, Person, ... |
|
121 | - * 2. the post doesn't have a term from the Entity Types Taxonomy: |
|
122 | - * a) the post is a `wl_entity` custom post type, then the post is |
|
123 | - * assigned the `Thing` entity type by default. |
|
124 | - * b) the post is a `post` post type, then the post is |
|
125 | - * assigned the `Article` entity type by default. |
|
126 | - * c) the post is a custom post type then it is |
|
127 | - * assigned the `WebPage` entity type by default. |
|
128 | - */ |
|
129 | - public function get( $post_id ) { |
|
130 | - |
|
131 | - $this->log->trace( "Getting the post type for post $post_id..." ); |
|
132 | - |
|
133 | - // Get the post type. |
|
134 | - $post_type = get_post_type( $post_id ); |
|
135 | - |
|
136 | - // Return `web-page` for non entities. |
|
137 | - if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
138 | - $this->log->info( "Returning `web-page` for post $post_id." ); |
|
139 | - |
|
140 | - return $this->schema_service->get_schema( 'web-page' ); |
|
141 | - } |
|
142 | - |
|
143 | - // Get the type from the associated classification. |
|
144 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
145 | - |
|
146 | - // Return the schema type if there is a term found. |
|
147 | - if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
|
148 | - // Cycle through the terms and return the first one with a valid schema. |
|
149 | - foreach ( $terms as $term ) { |
|
150 | - $this->log->debug( "Found `{$term->slug}` term for post $post_id." ); |
|
151 | - |
|
152 | - // Try to get the schema for the term. |
|
153 | - $schema = $this->schema_service->get_schema( $term->slug ); |
|
154 | - |
|
155 | - // If found, return it, ignoring the other types. |
|
156 | - if ( null !== $schema ) { |
|
157 | - // Return the entity type with the specified id. |
|
158 | - return $schema; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /* |
|
22 | + /** |
|
23 | + * The {@link Wordlift_Schema_Service} instance. |
|
24 | + * |
|
25 | + * @since 3.7.0 |
|
26 | + * @access private |
|
27 | + * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
28 | + */ |
|
29 | + private $schema_service; |
|
30 | + |
|
31 | + /** |
|
32 | + * A {@link Wordlift_Log_Service} instance. |
|
33 | + * |
|
34 | + * @since 3.8.0 |
|
35 | + * @access private |
|
36 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
37 | + */ |
|
38 | + private $log; |
|
39 | + |
|
40 | + /** |
|
41 | + * Wordlift_Entity_Type_Service constructor. |
|
42 | + * |
|
43 | + * @since 3.7.0 |
|
44 | + */ |
|
45 | + protected function __construct() { |
|
46 | + |
|
47 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
48 | + |
|
49 | + $this->schema_service = Wordlift_Schema_Service::get_instance(); |
|
50 | + |
|
51 | + $this->prepare_post_types(); |
|
52 | + |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Prepare post types for Gutenberg use |
|
57 | + * |
|
58 | + * @since 3.26.0 |
|
59 | + */ |
|
60 | + private function prepare_post_types() { |
|
61 | + |
|
62 | + add_action( |
|
63 | + 'init', |
|
64 | + function () { |
|
65 | + // Add post type support for 'custom-fields' for all post types. Specifically needed in Gutenberg |
|
66 | + $post_types = get_post_types(); |
|
67 | + foreach ( $post_types as $post_type ) { |
|
68 | + add_post_type_support( $post_type, 'custom-fields' ); |
|
69 | + } |
|
70 | + } |
|
71 | + ); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
76 | + * |
|
77 | + * @since 3.7.0 |
|
78 | + * @access private |
|
79 | + * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
80 | + */ |
|
81 | + private static $instance = null; |
|
82 | + |
|
83 | + /** |
|
84 | + * Get the {@link Wordlift_Entity_Type_Service} singleton instance. |
|
85 | + * |
|
86 | + * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
87 | + * @since 3.7.0 |
|
88 | + */ |
|
89 | + public static function get_instance() { |
|
90 | + |
|
91 | + if ( ! isset( self::$instance ) ) { |
|
92 | + self::$instance = new self(); |
|
93 | + } |
|
94 | + |
|
95 | + return self::$instance; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Get the types associated with the specified entity post id. |
|
100 | + * |
|
101 | + * We have a strategy to define the entity type, given that everything is |
|
102 | + * an entity, i.e. also posts/pages and custom post types. |
|
103 | + * |
|
104 | + * @param int $post_id The post id. |
|
105 | + * |
|
106 | + * @return array|null { |
|
107 | + * An array of type properties or null if no term is associated |
|
108 | + * |
|
109 | + * @type string css_class The css class, e.g. `wl-thing`. |
|
110 | + * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
111 | + * @type array same_as An array of same as attributes. |
|
112 | + * @type array custom_fields An array of custom fields. |
|
113 | + * } |
|
114 | + * @since 3.33.9 The `linked_data` key has been removed. |
|
115 | + * |
|
116 | + * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0. |
|
117 | + * |
|
118 | + * @since 3.18.0 The cases are the following: |
|
119 | + * 1. the post has a term from the Entity Types Taxonomy: the term defines |
|
120 | + * the entity type, e.g. Organization, Person, ... |
|
121 | + * 2. the post doesn't have a term from the Entity Types Taxonomy: |
|
122 | + * a) the post is a `wl_entity` custom post type, then the post is |
|
123 | + * assigned the `Thing` entity type by default. |
|
124 | + * b) the post is a `post` post type, then the post is |
|
125 | + * assigned the `Article` entity type by default. |
|
126 | + * c) the post is a custom post type then it is |
|
127 | + * assigned the `WebPage` entity type by default. |
|
128 | + */ |
|
129 | + public function get( $post_id ) { |
|
130 | + |
|
131 | + $this->log->trace( "Getting the post type for post $post_id..." ); |
|
132 | + |
|
133 | + // Get the post type. |
|
134 | + $post_type = get_post_type( $post_id ); |
|
135 | + |
|
136 | + // Return `web-page` for non entities. |
|
137 | + if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
138 | + $this->log->info( "Returning `web-page` for post $post_id." ); |
|
139 | + |
|
140 | + return $this->schema_service->get_schema( 'web-page' ); |
|
141 | + } |
|
142 | + |
|
143 | + // Get the type from the associated classification. |
|
144 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
145 | + |
|
146 | + // Return the schema type if there is a term found. |
|
147 | + if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
|
148 | + // Cycle through the terms and return the first one with a valid schema. |
|
149 | + foreach ( $terms as $term ) { |
|
150 | + $this->log->debug( "Found `{$term->slug}` term for post $post_id." ); |
|
151 | + |
|
152 | + // Try to get the schema for the term. |
|
153 | + $schema = $this->schema_service->get_schema( $term->slug ); |
|
154 | + |
|
155 | + // If found, return it, ignoring the other types. |
|
156 | + if ( null !== $schema ) { |
|
157 | + // Return the entity type with the specified id. |
|
158 | + return $schema; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /* |
|
163 | 163 | * When a schema isn't found, we return `thing`. Schema may not be found because |
164 | 164 | * the new schema classes that we support since #852 aren't configured in the schema |
165 | 165 | * service. |
@@ -169,93 +169,93 @@ discard block |
||
169 | 169 | * @since 3.20.0 |
170 | 170 | */ |
171 | 171 | |
172 | - return $this->schema_service->get_schema( 'thing' ); |
|
173 | - } |
|
174 | - |
|
175 | - // If it's a page or post return `Article`. |
|
176 | - if ( in_array( $post_type, array( 'post', 'page' ), true ) ) { |
|
177 | - $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." ); |
|
178 | - |
|
179 | - // Return "Article" schema type for posts. |
|
180 | - return $this->schema_service->get_schema( 'article' ); |
|
181 | - } |
|
182 | - |
|
183 | - // Return "Thing" schema type for entities. |
|
184 | - $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." ); |
|
185 | - |
|
186 | - // Return the entity type with the specified id. |
|
187 | - return $this->schema_service->get_schema( 'thing' ); |
|
188 | - |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get the term ids of the entity types associated to the specified post. |
|
193 | - * |
|
194 | - * @param int $post_id The post id. |
|
195 | - * |
|
196 | - * @return array|WP_Error An array of entity types ids or a {@link WP_Error}. |
|
197 | - * @since 3.20.0 |
|
198 | - */ |
|
199 | - public function get_ids( $post_id ) { |
|
200 | - |
|
201 | - return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids' ) ); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Get the camel case names of the entity types associated to the specified post. |
|
206 | - * |
|
207 | - * @param int $post_id The post id. |
|
208 | - * |
|
209 | - * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}. |
|
210 | - * @since 3.20.0 |
|
211 | - */ |
|
212 | - public function get_names( $post_id ) { |
|
213 | - |
|
214 | - $ids = $this->get_ids( $post_id ); |
|
215 | - |
|
216 | - // Filter out invalid terms (ones without _wl_name term meta) |
|
217 | - return array_values( |
|
218 | - array_filter( |
|
219 | - array_map( |
|
220 | - function ( $id ) { |
|
221 | - return get_term_meta( $id, '_wl_name', true ); |
|
222 | - }, |
|
223 | - $ids |
|
224 | - ) |
|
225 | - ) |
|
226 | - ); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Set the main type for the specified entity post, given the type URI. |
|
231 | - * |
|
232 | - * @param int $post_id The post id. |
|
233 | - * @param string $type_uri The type URI. |
|
234 | - * @param bool $replace Whether the provided type must replace the existing types, by default `true`. |
|
235 | - * |
|
236 | - * @since 3.8.0 |
|
237 | - */ |
|
238 | - public function set( $post_id, $type_uri, $replace = true ) { |
|
239 | - |
|
240 | - // If the type URI is empty we remove the type. |
|
241 | - if ( empty( $type_uri ) ) { |
|
242 | - $this->log->debug( "Removing entity type for post $post_id..." ); |
|
243 | - |
|
244 | - wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
245 | - |
|
246 | - return; |
|
247 | - } |
|
248 | - |
|
249 | - $this->log->debug( "Setting entity type for post $post_id..." ); |
|
250 | - |
|
251 | - // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug. |
|
252 | - $term = ( 0 === strpos( $type_uri, 'wl-' ) ) |
|
253 | - // Get term by slug. |
|
254 | - ? $this->get_term_by_slug( substr( $type_uri, 3 ) ) |
|
255 | - // Get term by URI. |
|
256 | - : $this->get_term_by_uri( $type_uri ); |
|
257 | - |
|
258 | - /* |
|
172 | + return $this->schema_service->get_schema( 'thing' ); |
|
173 | + } |
|
174 | + |
|
175 | + // If it's a page or post return `Article`. |
|
176 | + if ( in_array( $post_type, array( 'post', 'page' ), true ) ) { |
|
177 | + $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." ); |
|
178 | + |
|
179 | + // Return "Article" schema type for posts. |
|
180 | + return $this->schema_service->get_schema( 'article' ); |
|
181 | + } |
|
182 | + |
|
183 | + // Return "Thing" schema type for entities. |
|
184 | + $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." ); |
|
185 | + |
|
186 | + // Return the entity type with the specified id. |
|
187 | + return $this->schema_service->get_schema( 'thing' ); |
|
188 | + |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get the term ids of the entity types associated to the specified post. |
|
193 | + * |
|
194 | + * @param int $post_id The post id. |
|
195 | + * |
|
196 | + * @return array|WP_Error An array of entity types ids or a {@link WP_Error}. |
|
197 | + * @since 3.20.0 |
|
198 | + */ |
|
199 | + public function get_ids( $post_id ) { |
|
200 | + |
|
201 | + return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids' ) ); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Get the camel case names of the entity types associated to the specified post. |
|
206 | + * |
|
207 | + * @param int $post_id The post id. |
|
208 | + * |
|
209 | + * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}. |
|
210 | + * @since 3.20.0 |
|
211 | + */ |
|
212 | + public function get_names( $post_id ) { |
|
213 | + |
|
214 | + $ids = $this->get_ids( $post_id ); |
|
215 | + |
|
216 | + // Filter out invalid terms (ones without _wl_name term meta) |
|
217 | + return array_values( |
|
218 | + array_filter( |
|
219 | + array_map( |
|
220 | + function ( $id ) { |
|
221 | + return get_term_meta( $id, '_wl_name', true ); |
|
222 | + }, |
|
223 | + $ids |
|
224 | + ) |
|
225 | + ) |
|
226 | + ); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Set the main type for the specified entity post, given the type URI. |
|
231 | + * |
|
232 | + * @param int $post_id The post id. |
|
233 | + * @param string $type_uri The type URI. |
|
234 | + * @param bool $replace Whether the provided type must replace the existing types, by default `true`. |
|
235 | + * |
|
236 | + * @since 3.8.0 |
|
237 | + */ |
|
238 | + public function set( $post_id, $type_uri, $replace = true ) { |
|
239 | + |
|
240 | + // If the type URI is empty we remove the type. |
|
241 | + if ( empty( $type_uri ) ) { |
|
242 | + $this->log->debug( "Removing entity type for post $post_id..." ); |
|
243 | + |
|
244 | + wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
245 | + |
|
246 | + return; |
|
247 | + } |
|
248 | + |
|
249 | + $this->log->debug( "Setting entity type for post $post_id..." ); |
|
250 | + |
|
251 | + // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug. |
|
252 | + $term = ( 0 === strpos( $type_uri, 'wl-' ) ) |
|
253 | + // Get term by slug. |
|
254 | + ? $this->get_term_by_slug( substr( $type_uri, 3 ) ) |
|
255 | + // Get term by URI. |
|
256 | + : $this->get_term_by_uri( $type_uri ); |
|
257 | + |
|
258 | + /* |
|
259 | 259 | * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be |
260 | 260 | * connected to Articles via mentions. We realized that the client JS code is passing `wl-other` when the |
261 | 261 | * entity type isn't "notable". In which case we couldn't find an entity type. |
@@ -266,245 +266,245 @@ discard block |
||
266 | 266 | * |
267 | 267 | * @since 3.23.4 |
268 | 268 | */ |
269 | - if ( false === $term ) { |
|
270 | - $this->log->warn( "No term found for URI $type_uri, will use Thing." ); |
|
271 | - |
|
272 | - $term = $this->get_term_by_slug( 'thing' ); |
|
273 | - |
|
274 | - // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy |
|
275 | - // is installed. |
|
276 | - if ( false === $term ) { |
|
277 | - return; |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." ); |
|
282 | - |
|
283 | - // `$replace` is passed to decide whether to replace or append the term. |
|
284 | - wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace ); |
|
285 | - |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Get an entity type term given its slug. |
|
290 | - * |
|
291 | - * @param string $slug The slug. |
|
292 | - * |
|
293 | - * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist |
|
294 | - * or `$term` was not found. |
|
295 | - * @since 3.20.0 |
|
296 | - */ |
|
297 | - private function get_term_by_slug( $slug ) { |
|
298 | - |
|
299 | - return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Get an entity type term given its URI. |
|
304 | - * |
|
305 | - * @param string $uri The uri. |
|
306 | - * |
|
307 | - * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist |
|
308 | - * or `$term` was not found. |
|
309 | - * @since 3.20.0 |
|
310 | - */ |
|
311 | - public function get_term_by_uri( $uri ) { |
|
312 | - |
|
313 | - $terms = get_terms( |
|
314 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
315 | - array( |
|
316 | - 'fields' => 'all', |
|
317 | - 'get' => 'all', |
|
318 | - 'number' => 1, |
|
319 | - 'meta_query' => array( |
|
320 | - array( |
|
321 | - // Don't use a reference to Wordlift_Schemaorg_Class_Service, unless |
|
322 | - // `WL_ALL_ENTITY_TYPES` is set to true. |
|
323 | - 'key' => '_wl_uri', |
|
324 | - 'value' => $uri, |
|
325 | - ), |
|
326 | - ), |
|
327 | - 'orderby' => 'term_id', |
|
328 | - 'order' => 'ASC', |
|
329 | - ) |
|
330 | - ); |
|
331 | - |
|
332 | - return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Check whether an entity type is set for the {@link WP_Post} with the |
|
337 | - * specified id. |
|
338 | - * |
|
339 | - * @param int $post_id The {@link WP_Post}'s `id`. |
|
340 | - * @param string $uri The entity type URI. |
|
341 | - * |
|
342 | - * @return bool True if an entity type is set otherwise false. |
|
343 | - * @since 3.15.0 |
|
344 | - */ |
|
345 | - public function has_entity_type( $post_id, $uri = null ) { |
|
346 | - |
|
347 | - $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
348 | - |
|
349 | - // If an URI hasn't been specified just check whether we have at least |
|
350 | - // one entity type. |
|
351 | - if ( null === $uri ) { |
|
352 | - return has_term( '', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ); |
|
353 | - } |
|
354 | - |
|
355 | - $has_entity_type = $this->has_post_term_by_uri( $post_id, $uri ); |
|
356 | - |
|
357 | - $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
358 | - |
|
359 | - // Check whether the post has an entity type with that URI. |
|
360 | - return $has_entity_type; |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Get the list of entity types' terms for the specified {@link WP_Post}. |
|
365 | - * |
|
366 | - * @param int $post_id The {@link WP_Post} id. |
|
367 | - * |
|
368 | - * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
|
369 | - * @since 3.15.0 |
|
370 | - */ |
|
371 | - private function get_post_terms( $post_id ) { |
|
372 | - |
|
373 | - return wp_get_object_terms( |
|
374 | - $post_id, |
|
375 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
376 | - array( |
|
377 | - 'hide_empty' => false, |
|
378 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
379 | - // An issue has been opened with the AAM plugin author as well. |
|
380 | - // |
|
381 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
382 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
383 | - 'fields' => 'all', |
|
384 | - ) |
|
385 | - ); |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * Get an entity type term given its URI. |
|
390 | - * |
|
391 | - * @param int $post_id The {@link WP_Post} id. |
|
392 | - * @param string $uri The entity type URI. |
|
393 | - * |
|
394 | - * @return bool True if the post has that type URI bound to it otherwise false. |
|
395 | - * @since 3.15.0 |
|
396 | - * |
|
397 | - * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`. |
|
398 | - */ |
|
399 | - private function has_post_term_by_uri( $post_id, $uri ) { |
|
400 | - |
|
401 | - // Get the post terms bound to the specified post. |
|
402 | - $terms = $this->get_post_terms( $post_id ); |
|
403 | - |
|
404 | - // Look for a term if the specified URI. |
|
405 | - foreach ( $terms as $term ) { |
|
406 | - $term_uri = get_term_meta( $term->term_id, '_wl_uri', true ); |
|
407 | - if ( $uri === $term_uri ) { |
|
408 | - return true; |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - // Return null. |
|
413 | - return false; |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * Get the custom fields for a specific post. |
|
418 | - * |
|
419 | - * @param int $post_id The post ID. |
|
420 | - * |
|
421 | - * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
422 | - * @since 3.25.2 |
|
423 | - */ |
|
424 | - public function get_custom_fields_for_post( $post_id ) { |
|
425 | - |
|
426 | - // Return custom fields for this specific entity's type. |
|
427 | - $types = $this->get_ids( $post_id ); |
|
428 | - |
|
429 | - /** @var WP_Term[] $terms */ |
|
430 | - $terms = array_filter( |
|
431 | - array_map( |
|
432 | - function ( $item ) { |
|
433 | - return get_term( $item ); |
|
434 | - }, |
|
435 | - $types |
|
436 | - ), |
|
437 | - function ( $item ) { |
|
438 | - return isset( $item ) && is_a( $item, 'WP_Term' ); |
|
439 | - } |
|
440 | - ); |
|
441 | - |
|
442 | - $term_slugs = array_map( |
|
443 | - function ( $item ) { |
|
444 | - return $item->slug; |
|
445 | - }, |
|
446 | - $terms |
|
447 | - ); |
|
448 | - |
|
449 | - $term_slugs[] = 'thing'; |
|
450 | - |
|
451 | - return $this->get_custom_fields_by_term_slugs( $term_slugs ); |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Get the custom fields for a specific term. |
|
456 | - * |
|
457 | - * @param int $term_id The term ID. |
|
458 | - * |
|
459 | - * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
460 | - * @since 3.32.0 |
|
461 | - */ |
|
462 | - public function get_custom_fields_for_term( $term_id ) { |
|
463 | - $selected_entity_types = get_term_meta( $term_id, 'wl_entity_type' ); |
|
464 | - $selected_entity_types[] = 'thing'; |
|
465 | - $selected_entity_types = array_unique( $selected_entity_types ); |
|
466 | - |
|
467 | - return $this->get_custom_fields_by_term_slugs( $selected_entity_types ); |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * Determines whether a post type can be used for entities. |
|
472 | - * |
|
473 | - * Criteria is that the post type is public. The list of valid post types |
|
474 | - * can be overridden with a filter. |
|
475 | - * |
|
476 | - * @param string $post_type A post type name. |
|
477 | - * |
|
478 | - * @return bool Return true if the post type can be used for entities, otherwise false. |
|
479 | - * @since 3.15.0 |
|
480 | - */ |
|
481 | - public static function is_valid_entity_post_type( $post_type ) { |
|
482 | - |
|
483 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * @param $term_slugs |
|
488 | - * |
|
489 | - * @return array |
|
490 | - */ |
|
491 | - private function get_custom_fields_by_term_slugs( $term_slugs ) { |
|
492 | - $schema_service = Wordlift_Schema_Service::get_instance(); |
|
493 | - |
|
494 | - return array_reduce( |
|
495 | - $term_slugs, |
|
496 | - function ( $carry, $item ) use ( $schema_service ) { |
|
497 | - |
|
498 | - $schema = $schema_service->get_schema( $item ); |
|
499 | - |
|
500 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
501 | - return $carry; |
|
502 | - } |
|
503 | - |
|
504 | - return $carry + $schema['custom_fields']; |
|
505 | - }, |
|
506 | - array() |
|
507 | - ); |
|
508 | - } |
|
269 | + if ( false === $term ) { |
|
270 | + $this->log->warn( "No term found for URI $type_uri, will use Thing." ); |
|
271 | + |
|
272 | + $term = $this->get_term_by_slug( 'thing' ); |
|
273 | + |
|
274 | + // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy |
|
275 | + // is installed. |
|
276 | + if ( false === $term ) { |
|
277 | + return; |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." ); |
|
282 | + |
|
283 | + // `$replace` is passed to decide whether to replace or append the term. |
|
284 | + wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace ); |
|
285 | + |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Get an entity type term given its slug. |
|
290 | + * |
|
291 | + * @param string $slug The slug. |
|
292 | + * |
|
293 | + * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist |
|
294 | + * or `$term` was not found. |
|
295 | + * @since 3.20.0 |
|
296 | + */ |
|
297 | + private function get_term_by_slug( $slug ) { |
|
298 | + |
|
299 | + return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Get an entity type term given its URI. |
|
304 | + * |
|
305 | + * @param string $uri The uri. |
|
306 | + * |
|
307 | + * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist |
|
308 | + * or `$term` was not found. |
|
309 | + * @since 3.20.0 |
|
310 | + */ |
|
311 | + public function get_term_by_uri( $uri ) { |
|
312 | + |
|
313 | + $terms = get_terms( |
|
314 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
315 | + array( |
|
316 | + 'fields' => 'all', |
|
317 | + 'get' => 'all', |
|
318 | + 'number' => 1, |
|
319 | + 'meta_query' => array( |
|
320 | + array( |
|
321 | + // Don't use a reference to Wordlift_Schemaorg_Class_Service, unless |
|
322 | + // `WL_ALL_ENTITY_TYPES` is set to true. |
|
323 | + 'key' => '_wl_uri', |
|
324 | + 'value' => $uri, |
|
325 | + ), |
|
326 | + ), |
|
327 | + 'orderby' => 'term_id', |
|
328 | + 'order' => 'ASC', |
|
329 | + ) |
|
330 | + ); |
|
331 | + |
|
332 | + return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Check whether an entity type is set for the {@link WP_Post} with the |
|
337 | + * specified id. |
|
338 | + * |
|
339 | + * @param int $post_id The {@link WP_Post}'s `id`. |
|
340 | + * @param string $uri The entity type URI. |
|
341 | + * |
|
342 | + * @return bool True if an entity type is set otherwise false. |
|
343 | + * @since 3.15.0 |
|
344 | + */ |
|
345 | + public function has_entity_type( $post_id, $uri = null ) { |
|
346 | + |
|
347 | + $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
348 | + |
|
349 | + // If an URI hasn't been specified just check whether we have at least |
|
350 | + // one entity type. |
|
351 | + if ( null === $uri ) { |
|
352 | + return has_term( '', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ); |
|
353 | + } |
|
354 | + |
|
355 | + $has_entity_type = $this->has_post_term_by_uri( $post_id, $uri ); |
|
356 | + |
|
357 | + $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
358 | + |
|
359 | + // Check whether the post has an entity type with that URI. |
|
360 | + return $has_entity_type; |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Get the list of entity types' terms for the specified {@link WP_Post}. |
|
365 | + * |
|
366 | + * @param int $post_id The {@link WP_Post} id. |
|
367 | + * |
|
368 | + * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
|
369 | + * @since 3.15.0 |
|
370 | + */ |
|
371 | + private function get_post_terms( $post_id ) { |
|
372 | + |
|
373 | + return wp_get_object_terms( |
|
374 | + $post_id, |
|
375 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
376 | + array( |
|
377 | + 'hide_empty' => false, |
|
378 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
379 | + // An issue has been opened with the AAM plugin author as well. |
|
380 | + // |
|
381 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
382 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
383 | + 'fields' => 'all', |
|
384 | + ) |
|
385 | + ); |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * Get an entity type term given its URI. |
|
390 | + * |
|
391 | + * @param int $post_id The {@link WP_Post} id. |
|
392 | + * @param string $uri The entity type URI. |
|
393 | + * |
|
394 | + * @return bool True if the post has that type URI bound to it otherwise false. |
|
395 | + * @since 3.15.0 |
|
396 | + * |
|
397 | + * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`. |
|
398 | + */ |
|
399 | + private function has_post_term_by_uri( $post_id, $uri ) { |
|
400 | + |
|
401 | + // Get the post terms bound to the specified post. |
|
402 | + $terms = $this->get_post_terms( $post_id ); |
|
403 | + |
|
404 | + // Look for a term if the specified URI. |
|
405 | + foreach ( $terms as $term ) { |
|
406 | + $term_uri = get_term_meta( $term->term_id, '_wl_uri', true ); |
|
407 | + if ( $uri === $term_uri ) { |
|
408 | + return true; |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + // Return null. |
|
413 | + return false; |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * Get the custom fields for a specific post. |
|
418 | + * |
|
419 | + * @param int $post_id The post ID. |
|
420 | + * |
|
421 | + * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
422 | + * @since 3.25.2 |
|
423 | + */ |
|
424 | + public function get_custom_fields_for_post( $post_id ) { |
|
425 | + |
|
426 | + // Return custom fields for this specific entity's type. |
|
427 | + $types = $this->get_ids( $post_id ); |
|
428 | + |
|
429 | + /** @var WP_Term[] $terms */ |
|
430 | + $terms = array_filter( |
|
431 | + array_map( |
|
432 | + function ( $item ) { |
|
433 | + return get_term( $item ); |
|
434 | + }, |
|
435 | + $types |
|
436 | + ), |
|
437 | + function ( $item ) { |
|
438 | + return isset( $item ) && is_a( $item, 'WP_Term' ); |
|
439 | + } |
|
440 | + ); |
|
441 | + |
|
442 | + $term_slugs = array_map( |
|
443 | + function ( $item ) { |
|
444 | + return $item->slug; |
|
445 | + }, |
|
446 | + $terms |
|
447 | + ); |
|
448 | + |
|
449 | + $term_slugs[] = 'thing'; |
|
450 | + |
|
451 | + return $this->get_custom_fields_by_term_slugs( $term_slugs ); |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Get the custom fields for a specific term. |
|
456 | + * |
|
457 | + * @param int $term_id The term ID. |
|
458 | + * |
|
459 | + * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
460 | + * @since 3.32.0 |
|
461 | + */ |
|
462 | + public function get_custom_fields_for_term( $term_id ) { |
|
463 | + $selected_entity_types = get_term_meta( $term_id, 'wl_entity_type' ); |
|
464 | + $selected_entity_types[] = 'thing'; |
|
465 | + $selected_entity_types = array_unique( $selected_entity_types ); |
|
466 | + |
|
467 | + return $this->get_custom_fields_by_term_slugs( $selected_entity_types ); |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * Determines whether a post type can be used for entities. |
|
472 | + * |
|
473 | + * Criteria is that the post type is public. The list of valid post types |
|
474 | + * can be overridden with a filter. |
|
475 | + * |
|
476 | + * @param string $post_type A post type name. |
|
477 | + * |
|
478 | + * @return bool Return true if the post type can be used for entities, otherwise false. |
|
479 | + * @since 3.15.0 |
|
480 | + */ |
|
481 | + public static function is_valid_entity_post_type( $post_type ) { |
|
482 | + |
|
483 | + return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * @param $term_slugs |
|
488 | + * |
|
489 | + * @return array |
|
490 | + */ |
|
491 | + private function get_custom_fields_by_term_slugs( $term_slugs ) { |
|
492 | + $schema_service = Wordlift_Schema_Service::get_instance(); |
|
493 | + |
|
494 | + return array_reduce( |
|
495 | + $term_slugs, |
|
496 | + function ( $carry, $item ) use ( $schema_service ) { |
|
497 | + |
|
498 | + $schema = $schema_service->get_schema( $item ); |
|
499 | + |
|
500 | + if ( ! isset( $schema['custom_fields'] ) ) { |
|
501 | + return $carry; |
|
502 | + } |
|
503 | + |
|
504 | + return $carry + $schema['custom_fields']; |
|
505 | + }, |
|
506 | + array() |
|
507 | + ); |
|
508 | + } |
|
509 | 509 | |
510 | 510 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | */ |
45 | 45 | protected function __construct() { |
46 | 46 | |
47 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
47 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service'); |
|
48 | 48 | |
49 | 49 | $this->schema_service = Wordlift_Schema_Service::get_instance(); |
50 | 50 | |
@@ -61,11 +61,11 @@ discard block |
||
61 | 61 | |
62 | 62 | add_action( |
63 | 63 | 'init', |
64 | - function () { |
|
64 | + function() { |
|
65 | 65 | // Add post type support for 'custom-fields' for all post types. Specifically needed in Gutenberg |
66 | 66 | $post_types = get_post_types(); |
67 | - foreach ( $post_types as $post_type ) { |
|
68 | - add_post_type_support( $post_type, 'custom-fields' ); |
|
67 | + foreach ($post_types as $post_type) { |
|
68 | + add_post_type_support($post_type, 'custom-fields'); |
|
69 | 69 | } |
70 | 70 | } |
71 | 71 | ); |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | */ |
89 | 89 | public static function get_instance() { |
90 | 90 | |
91 | - if ( ! isset( self::$instance ) ) { |
|
91 | + if ( ! isset(self::$instance)) { |
|
92 | 92 | self::$instance = new self(); |
93 | 93 | } |
94 | 94 | |
@@ -126,34 +126,34 @@ discard block |
||
126 | 126 | * c) the post is a custom post type then it is |
127 | 127 | * assigned the `WebPage` entity type by default. |
128 | 128 | */ |
129 | - public function get( $post_id ) { |
|
129 | + public function get($post_id) { |
|
130 | 130 | |
131 | - $this->log->trace( "Getting the post type for post $post_id..." ); |
|
131 | + $this->log->trace("Getting the post type for post $post_id..."); |
|
132 | 132 | |
133 | 133 | // Get the post type. |
134 | - $post_type = get_post_type( $post_id ); |
|
134 | + $post_type = get_post_type($post_id); |
|
135 | 135 | |
136 | 136 | // Return `web-page` for non entities. |
137 | - if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
138 | - $this->log->info( "Returning `web-page` for post $post_id." ); |
|
137 | + if ( ! self::is_valid_entity_post_type($post_type)) { |
|
138 | + $this->log->info("Returning `web-page` for post $post_id."); |
|
139 | 139 | |
140 | - return $this->schema_service->get_schema( 'web-page' ); |
|
140 | + return $this->schema_service->get_schema('web-page'); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | // Get the type from the associated classification. |
144 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
144 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
145 | 145 | |
146 | 146 | // Return the schema type if there is a term found. |
147 | - if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
|
147 | + if ( ! is_wp_error($terms) && ! empty($terms)) { |
|
148 | 148 | // Cycle through the terms and return the first one with a valid schema. |
149 | - foreach ( $terms as $term ) { |
|
150 | - $this->log->debug( "Found `{$term->slug}` term for post $post_id." ); |
|
149 | + foreach ($terms as $term) { |
|
150 | + $this->log->debug("Found `{$term->slug}` term for post $post_id."); |
|
151 | 151 | |
152 | 152 | // Try to get the schema for the term. |
153 | - $schema = $this->schema_service->get_schema( $term->slug ); |
|
153 | + $schema = $this->schema_service->get_schema($term->slug); |
|
154 | 154 | |
155 | 155 | // If found, return it, ignoring the other types. |
156 | - if ( null !== $schema ) { |
|
156 | + if (null !== $schema) { |
|
157 | 157 | // Return the entity type with the specified id. |
158 | 158 | return $schema; |
159 | 159 | } |
@@ -169,22 +169,22 @@ discard block |
||
169 | 169 | * @since 3.20.0 |
170 | 170 | */ |
171 | 171 | |
172 | - return $this->schema_service->get_schema( 'thing' ); |
|
172 | + return $this->schema_service->get_schema('thing'); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | // If it's a page or post return `Article`. |
176 | - if ( in_array( $post_type, array( 'post', 'page' ), true ) ) { |
|
177 | - $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." ); |
|
176 | + if (in_array($post_type, array('post', 'page'), true)) { |
|
177 | + $this->log->debug("Post $post_id has no terms, and it's a `post` type, returning `Article`."); |
|
178 | 178 | |
179 | 179 | // Return "Article" schema type for posts. |
180 | - return $this->schema_service->get_schema( 'article' ); |
|
180 | + return $this->schema_service->get_schema('article'); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | // Return "Thing" schema type for entities. |
184 | - $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." ); |
|
184 | + $this->log->debug("Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`."); |
|
185 | 185 | |
186 | 186 | // Return the entity type with the specified id. |
187 | - return $this->schema_service->get_schema( 'thing' ); |
|
187 | + return $this->schema_service->get_schema('thing'); |
|
188 | 188 | |
189 | 189 | } |
190 | 190 | |
@@ -196,9 +196,9 @@ discard block |
||
196 | 196 | * @return array|WP_Error An array of entity types ids or a {@link WP_Error}. |
197 | 197 | * @since 3.20.0 |
198 | 198 | */ |
199 | - public function get_ids( $post_id ) { |
|
199 | + public function get_ids($post_id) { |
|
200 | 200 | |
201 | - return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids' ) ); |
|
201 | + return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('fields' => 'ids')); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -209,16 +209,16 @@ discard block |
||
209 | 209 | * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}. |
210 | 210 | * @since 3.20.0 |
211 | 211 | */ |
212 | - public function get_names( $post_id ) { |
|
212 | + public function get_names($post_id) { |
|
213 | 213 | |
214 | - $ids = $this->get_ids( $post_id ); |
|
214 | + $ids = $this->get_ids($post_id); |
|
215 | 215 | |
216 | 216 | // Filter out invalid terms (ones without _wl_name term meta) |
217 | 217 | return array_values( |
218 | 218 | array_filter( |
219 | 219 | array_map( |
220 | - function ( $id ) { |
|
221 | - return get_term_meta( $id, '_wl_name', true ); |
|
220 | + function($id) { |
|
221 | + return get_term_meta($id, '_wl_name', true); |
|
222 | 222 | }, |
223 | 223 | $ids |
224 | 224 | ) |
@@ -235,25 +235,25 @@ discard block |
||
235 | 235 | * |
236 | 236 | * @since 3.8.0 |
237 | 237 | */ |
238 | - public function set( $post_id, $type_uri, $replace = true ) { |
|
238 | + public function set($post_id, $type_uri, $replace = true) { |
|
239 | 239 | |
240 | 240 | // If the type URI is empty we remove the type. |
241 | - if ( empty( $type_uri ) ) { |
|
242 | - $this->log->debug( "Removing entity type for post $post_id..." ); |
|
241 | + if (empty($type_uri)) { |
|
242 | + $this->log->debug("Removing entity type for post $post_id..."); |
|
243 | 243 | |
244 | - wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
244 | + wp_set_object_terms($post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
245 | 245 | |
246 | 246 | return; |
247 | 247 | } |
248 | 248 | |
249 | - $this->log->debug( "Setting entity type for post $post_id..." ); |
|
249 | + $this->log->debug("Setting entity type for post $post_id..."); |
|
250 | 250 | |
251 | 251 | // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug. |
252 | - $term = ( 0 === strpos( $type_uri, 'wl-' ) ) |
|
252 | + $term = (0 === strpos($type_uri, 'wl-')) |
|
253 | 253 | // Get term by slug. |
254 | - ? $this->get_term_by_slug( substr( $type_uri, 3 ) ) |
|
254 | + ? $this->get_term_by_slug(substr($type_uri, 3)) |
|
255 | 255 | // Get term by URI. |
256 | - : $this->get_term_by_uri( $type_uri ); |
|
256 | + : $this->get_term_by_uri($type_uri); |
|
257 | 257 | |
258 | 258 | /* |
259 | 259 | * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be |
@@ -266,22 +266,22 @@ discard block |
||
266 | 266 | * |
267 | 267 | * @since 3.23.4 |
268 | 268 | */ |
269 | - if ( false === $term ) { |
|
270 | - $this->log->warn( "No term found for URI $type_uri, will use Thing." ); |
|
269 | + if (false === $term) { |
|
270 | + $this->log->warn("No term found for URI $type_uri, will use Thing."); |
|
271 | 271 | |
272 | - $term = $this->get_term_by_slug( 'thing' ); |
|
272 | + $term = $this->get_term_by_slug('thing'); |
|
273 | 273 | |
274 | 274 | // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy |
275 | 275 | // is installed. |
276 | - if ( false === $term ) { |
|
276 | + if (false === $term) { |
|
277 | 277 | return; |
278 | 278 | } |
279 | 279 | } |
280 | 280 | |
281 | - $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." ); |
|
281 | + $this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..."); |
|
282 | 282 | |
283 | 283 | // `$replace` is passed to decide whether to replace or append the term. |
284 | - wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace ); |
|
284 | + wp_set_object_terms($post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace); |
|
285 | 285 | |
286 | 286 | } |
287 | 287 | |
@@ -294,9 +294,9 @@ discard block |
||
294 | 294 | * or `$term` was not found. |
295 | 295 | * @since 3.20.0 |
296 | 296 | */ |
297 | - private function get_term_by_slug( $slug ) { |
|
297 | + private function get_term_by_slug($slug) { |
|
298 | 298 | |
299 | - return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
299 | + return get_term_by('slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
300 | 300 | } |
301 | 301 | |
302 | 302 | /** |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | * or `$term` was not found. |
309 | 309 | * @since 3.20.0 |
310 | 310 | */ |
311 | - public function get_term_by_uri( $uri ) { |
|
311 | + public function get_term_by_uri($uri) { |
|
312 | 312 | |
313 | 313 | $terms = get_terms( |
314 | 314 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | ) |
330 | 330 | ); |
331 | 331 | |
332 | - return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false; |
|
332 | + return is_array($terms) && ! empty($terms) ? $terms[0] : false; |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | /** |
@@ -342,19 +342,19 @@ discard block |
||
342 | 342 | * @return bool True if an entity type is set otherwise false. |
343 | 343 | * @since 3.15.0 |
344 | 344 | */ |
345 | - public function has_entity_type( $post_id, $uri = null ) { |
|
345 | + public function has_entity_type($post_id, $uri = null) { |
|
346 | 346 | |
347 | - $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
347 | + $this->log->debug("Checking if post $post_id has an entity type [ $uri ]..."); |
|
348 | 348 | |
349 | 349 | // If an URI hasn't been specified just check whether we have at least |
350 | 350 | // one entity type. |
351 | - if ( null === $uri ) { |
|
352 | - return has_term( '', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ); |
|
351 | + if (null === $uri) { |
|
352 | + return has_term('', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id); |
|
353 | 353 | } |
354 | 354 | |
355 | - $has_entity_type = $this->has_post_term_by_uri( $post_id, $uri ); |
|
355 | + $has_entity_type = $this->has_post_term_by_uri($post_id, $uri); |
|
356 | 356 | |
357 | - $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
357 | + $this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no')); |
|
358 | 358 | |
359 | 359 | // Check whether the post has an entity type with that URI. |
360 | 360 | return $has_entity_type; |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
369 | 369 | * @since 3.15.0 |
370 | 370 | */ |
371 | - private function get_post_terms( $post_id ) { |
|
371 | + private function get_post_terms($post_id) { |
|
372 | 372 | |
373 | 373 | return wp_get_object_terms( |
374 | 374 | $post_id, |
@@ -396,15 +396,15 @@ discard block |
||
396 | 396 | * |
397 | 397 | * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`. |
398 | 398 | */ |
399 | - private function has_post_term_by_uri( $post_id, $uri ) { |
|
399 | + private function has_post_term_by_uri($post_id, $uri) { |
|
400 | 400 | |
401 | 401 | // Get the post terms bound to the specified post. |
402 | - $terms = $this->get_post_terms( $post_id ); |
|
402 | + $terms = $this->get_post_terms($post_id); |
|
403 | 403 | |
404 | 404 | // Look for a term if the specified URI. |
405 | - foreach ( $terms as $term ) { |
|
406 | - $term_uri = get_term_meta( $term->term_id, '_wl_uri', true ); |
|
407 | - if ( $uri === $term_uri ) { |
|
405 | + foreach ($terms as $term) { |
|
406 | + $term_uri = get_term_meta($term->term_id, '_wl_uri', true); |
|
407 | + if ($uri === $term_uri) { |
|
408 | 408 | return true; |
409 | 409 | } |
410 | 410 | } |
@@ -421,26 +421,26 @@ discard block |
||
421 | 421 | * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
422 | 422 | * @since 3.25.2 |
423 | 423 | */ |
424 | - public function get_custom_fields_for_post( $post_id ) { |
|
424 | + public function get_custom_fields_for_post($post_id) { |
|
425 | 425 | |
426 | 426 | // Return custom fields for this specific entity's type. |
427 | - $types = $this->get_ids( $post_id ); |
|
427 | + $types = $this->get_ids($post_id); |
|
428 | 428 | |
429 | 429 | /** @var WP_Term[] $terms */ |
430 | 430 | $terms = array_filter( |
431 | 431 | array_map( |
432 | - function ( $item ) { |
|
433 | - return get_term( $item ); |
|
432 | + function($item) { |
|
433 | + return get_term($item); |
|
434 | 434 | }, |
435 | 435 | $types |
436 | 436 | ), |
437 | - function ( $item ) { |
|
438 | - return isset( $item ) && is_a( $item, 'WP_Term' ); |
|
437 | + function($item) { |
|
438 | + return isset($item) && is_a($item, 'WP_Term'); |
|
439 | 439 | } |
440 | 440 | ); |
441 | 441 | |
442 | 442 | $term_slugs = array_map( |
443 | - function ( $item ) { |
|
443 | + function($item) { |
|
444 | 444 | return $item->slug; |
445 | 445 | }, |
446 | 446 | $terms |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | |
449 | 449 | $term_slugs[] = 'thing'; |
450 | 450 | |
451 | - return $this->get_custom_fields_by_term_slugs( $term_slugs ); |
|
451 | + return $this->get_custom_fields_by_term_slugs($term_slugs); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | /** |
@@ -459,12 +459,12 @@ discard block |
||
459 | 459 | * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
460 | 460 | * @since 3.32.0 |
461 | 461 | */ |
462 | - public function get_custom_fields_for_term( $term_id ) { |
|
463 | - $selected_entity_types = get_term_meta( $term_id, 'wl_entity_type' ); |
|
462 | + public function get_custom_fields_for_term($term_id) { |
|
463 | + $selected_entity_types = get_term_meta($term_id, 'wl_entity_type'); |
|
464 | 464 | $selected_entity_types[] = 'thing'; |
465 | - $selected_entity_types = array_unique( $selected_entity_types ); |
|
465 | + $selected_entity_types = array_unique($selected_entity_types); |
|
466 | 466 | |
467 | - return $this->get_custom_fields_by_term_slugs( $selected_entity_types ); |
|
467 | + return $this->get_custom_fields_by_term_slugs($selected_entity_types); |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | /** |
@@ -478,9 +478,9 @@ discard block |
||
478 | 478 | * @return bool Return true if the post type can be used for entities, otherwise false. |
479 | 479 | * @since 3.15.0 |
480 | 480 | */ |
481 | - public static function is_valid_entity_post_type( $post_type ) { |
|
481 | + public static function is_valid_entity_post_type($post_type) { |
|
482 | 482 | |
483 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
483 | + return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true); |
|
484 | 484 | } |
485 | 485 | |
486 | 486 | /** |
@@ -488,16 +488,16 @@ discard block |
||
488 | 488 | * |
489 | 489 | * @return array |
490 | 490 | */ |
491 | - private function get_custom_fields_by_term_slugs( $term_slugs ) { |
|
491 | + private function get_custom_fields_by_term_slugs($term_slugs) { |
|
492 | 492 | $schema_service = Wordlift_Schema_Service::get_instance(); |
493 | 493 | |
494 | 494 | return array_reduce( |
495 | 495 | $term_slugs, |
496 | - function ( $carry, $item ) use ( $schema_service ) { |
|
496 | + function($carry, $item) use ($schema_service) { |
|
497 | 497 | |
498 | - $schema = $schema_service->get_schema( $item ); |
|
498 | + $schema = $schema_service->get_schema($item); |
|
499 | 499 | |
500 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
500 | + if ( ! isset($schema['custom_fields'])) { |
|
501 | 501 | return $carry; |
502 | 502 | } |
503 | 503 |