@@ -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,246 +266,246 @@ 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 = ( null !== $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 | - |
|
408 | - if ( $uri === $term_uri ) { |
|
409 | - return true; |
|
410 | - } |
|
411 | - } |
|
412 | - |
|
413 | - // Return null. |
|
414 | - return false; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Get the custom fields for a specific post. |
|
419 | - * |
|
420 | - * @param int $post_id The post ID. |
|
421 | - * |
|
422 | - * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
423 | - * @since 3.25.2 |
|
424 | - */ |
|
425 | - public function get_custom_fields_for_post( $post_id ) { |
|
426 | - |
|
427 | - // Return custom fields for this specific entity's type. |
|
428 | - $types = $this->get_ids( $post_id ); |
|
429 | - |
|
430 | - /** @var WP_Term[] $terms */ |
|
431 | - $terms = array_filter( |
|
432 | - array_map( |
|
433 | - function ( $item ) { |
|
434 | - return get_term( $item ); |
|
435 | - }, |
|
436 | - $types |
|
437 | - ), |
|
438 | - function ( $item ) { |
|
439 | - return isset( $item ) && is_a( $item, 'WP_Term' ); |
|
440 | - } |
|
441 | - ); |
|
442 | - |
|
443 | - $term_slugs = array_map( |
|
444 | - function ( $item ) { |
|
445 | - return $item->slug; |
|
446 | - }, |
|
447 | - $terms |
|
448 | - ); |
|
449 | - |
|
450 | - $term_slugs[] = 'thing'; |
|
451 | - |
|
452 | - return $this->get_custom_fields_by_term_slugs( $term_slugs ); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Get the custom fields for a specific term. |
|
457 | - * |
|
458 | - * @param int $term_id The term ID. |
|
459 | - * |
|
460 | - * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
461 | - * @since 3.32.0 |
|
462 | - */ |
|
463 | - public function get_custom_fields_for_term( $term_id ) { |
|
464 | - $selected_entity_types = get_term_meta( $term_id, 'wl_entity_type' ); |
|
465 | - $selected_entity_types[] = 'thing'; |
|
466 | - $selected_entity_types = array_unique( $selected_entity_types ); |
|
467 | - |
|
468 | - return $this->get_custom_fields_by_term_slugs( $selected_entity_types ); |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Determines whether a post type can be used for entities. |
|
473 | - * |
|
474 | - * Criteria is that the post type is public. The list of valid post types |
|
475 | - * can be overridden with a filter. |
|
476 | - * |
|
477 | - * @param string $post_type A post type name. |
|
478 | - * |
|
479 | - * @return bool Return true if the post type can be used for entities, otherwise false. |
|
480 | - * @since 3.15.0 |
|
481 | - */ |
|
482 | - public static function is_valid_entity_post_type( $post_type ) { |
|
483 | - |
|
484 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * @param $term_slugs |
|
489 | - * |
|
490 | - * @return array |
|
491 | - */ |
|
492 | - private function get_custom_fields_by_term_slugs( $term_slugs ) { |
|
493 | - $schema_service = Wordlift_Schema_Service::get_instance(); |
|
494 | - |
|
495 | - return array_reduce( |
|
496 | - $term_slugs, |
|
497 | - function ( $carry, $item ) use ( $schema_service ) { |
|
498 | - |
|
499 | - $schema = $schema_service->get_schema( $item ); |
|
500 | - |
|
501 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
502 | - return $carry; |
|
503 | - } |
|
504 | - |
|
505 | - return $carry + $schema['custom_fields']; |
|
506 | - }, |
|
507 | - array() |
|
508 | - ); |
|
509 | - } |
|
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 = ( null !== $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 | + |
|
408 | + if ( $uri === $term_uri ) { |
|
409 | + return true; |
|
410 | + } |
|
411 | + } |
|
412 | + |
|
413 | + // Return null. |
|
414 | + return false; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Get the custom fields for a specific post. |
|
419 | + * |
|
420 | + * @param int $post_id The post ID. |
|
421 | + * |
|
422 | + * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
423 | + * @since 3.25.2 |
|
424 | + */ |
|
425 | + public function get_custom_fields_for_post( $post_id ) { |
|
426 | + |
|
427 | + // Return custom fields for this specific entity's type. |
|
428 | + $types = $this->get_ids( $post_id ); |
|
429 | + |
|
430 | + /** @var WP_Term[] $terms */ |
|
431 | + $terms = array_filter( |
|
432 | + array_map( |
|
433 | + function ( $item ) { |
|
434 | + return get_term( $item ); |
|
435 | + }, |
|
436 | + $types |
|
437 | + ), |
|
438 | + function ( $item ) { |
|
439 | + return isset( $item ) && is_a( $item, 'WP_Term' ); |
|
440 | + } |
|
441 | + ); |
|
442 | + |
|
443 | + $term_slugs = array_map( |
|
444 | + function ( $item ) { |
|
445 | + return $item->slug; |
|
446 | + }, |
|
447 | + $terms |
|
448 | + ); |
|
449 | + |
|
450 | + $term_slugs[] = 'thing'; |
|
451 | + |
|
452 | + return $this->get_custom_fields_by_term_slugs( $term_slugs ); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Get the custom fields for a specific term. |
|
457 | + * |
|
458 | + * @param int $term_id The term ID. |
|
459 | + * |
|
460 | + * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
|
461 | + * @since 3.32.0 |
|
462 | + */ |
|
463 | + public function get_custom_fields_for_term( $term_id ) { |
|
464 | + $selected_entity_types = get_term_meta( $term_id, 'wl_entity_type' ); |
|
465 | + $selected_entity_types[] = 'thing'; |
|
466 | + $selected_entity_types = array_unique( $selected_entity_types ); |
|
467 | + |
|
468 | + return $this->get_custom_fields_by_term_slugs( $selected_entity_types ); |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Determines whether a post type can be used for entities. |
|
473 | + * |
|
474 | + * Criteria is that the post type is public. The list of valid post types |
|
475 | + * can be overridden with a filter. |
|
476 | + * |
|
477 | + * @param string $post_type A post type name. |
|
478 | + * |
|
479 | + * @return bool Return true if the post type can be used for entities, otherwise false. |
|
480 | + * @since 3.15.0 |
|
481 | + */ |
|
482 | + public static function is_valid_entity_post_type( $post_type ) { |
|
483 | + |
|
484 | + return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * @param $term_slugs |
|
489 | + * |
|
490 | + * @return array |
|
491 | + */ |
|
492 | + private function get_custom_fields_by_term_slugs( $term_slugs ) { |
|
493 | + $schema_service = Wordlift_Schema_Service::get_instance(); |
|
494 | + |
|
495 | + return array_reduce( |
|
496 | + $term_slugs, |
|
497 | + function ( $carry, $item ) use ( $schema_service ) { |
|
498 | + |
|
499 | + $schema = $schema_service->get_schema( $item ); |
|
500 | + |
|
501 | + if ( ! isset( $schema['custom_fields'] ) ) { |
|
502 | + return $carry; |
|
503 | + } |
|
504 | + |
|
505 | + return $carry + $schema['custom_fields']; |
|
506 | + }, |
|
507 | + array() |
|
508 | + ); |
|
509 | + } |
|
510 | 510 | |
511 | 511 | } |
@@ -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 = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) ); |
|
355 | + $has_entity_type = (null !== $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,16 +396,16 @@ 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 ); |
|
405 | + foreach ($terms as $term) { |
|
406 | + $term_uri = get_term_meta($term->term_id, '_wl_uri', true); |
|
407 | 407 | |
408 | - if ( $uri === $term_uri ) { |
|
408 | + if ($uri === $term_uri) { |
|
409 | 409 | return true; |
410 | 410 | } |
411 | 411 | } |
@@ -422,26 +422,26 @@ discard block |
||
422 | 422 | * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
423 | 423 | * @since 3.25.2 |
424 | 424 | */ |
425 | - public function get_custom_fields_for_post( $post_id ) { |
|
425 | + public function get_custom_fields_for_post($post_id) { |
|
426 | 426 | |
427 | 427 | // Return custom fields for this specific entity's type. |
428 | - $types = $this->get_ids( $post_id ); |
|
428 | + $types = $this->get_ids($post_id); |
|
429 | 429 | |
430 | 430 | /** @var WP_Term[] $terms */ |
431 | 431 | $terms = array_filter( |
432 | 432 | array_map( |
433 | - function ( $item ) { |
|
434 | - return get_term( $item ); |
|
433 | + function($item) { |
|
434 | + return get_term($item); |
|
435 | 435 | }, |
436 | 436 | $types |
437 | 437 | ), |
438 | - function ( $item ) { |
|
439 | - return isset( $item ) && is_a( $item, 'WP_Term' ); |
|
438 | + function($item) { |
|
439 | + return isset($item) && is_a($item, 'WP_Term'); |
|
440 | 440 | } |
441 | 441 | ); |
442 | 442 | |
443 | 443 | $term_slugs = array_map( |
444 | - function ( $item ) { |
|
444 | + function($item) { |
|
445 | 445 | return $item->slug; |
446 | 446 | }, |
447 | 447 | $terms |
@@ -449,7 +449,7 @@ discard block |
||
449 | 449 | |
450 | 450 | $term_slugs[] = 'thing'; |
451 | 451 | |
452 | - return $this->get_custom_fields_by_term_slugs( $term_slugs ); |
|
452 | + return $this->get_custom_fields_by_term_slugs($term_slugs); |
|
453 | 453 | } |
454 | 454 | |
455 | 455 | /** |
@@ -460,12 +460,12 @@ discard block |
||
460 | 460 | * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service). |
461 | 461 | * @since 3.32.0 |
462 | 462 | */ |
463 | - public function get_custom_fields_for_term( $term_id ) { |
|
464 | - $selected_entity_types = get_term_meta( $term_id, 'wl_entity_type' ); |
|
463 | + public function get_custom_fields_for_term($term_id) { |
|
464 | + $selected_entity_types = get_term_meta($term_id, 'wl_entity_type'); |
|
465 | 465 | $selected_entity_types[] = 'thing'; |
466 | - $selected_entity_types = array_unique( $selected_entity_types ); |
|
466 | + $selected_entity_types = array_unique($selected_entity_types); |
|
467 | 467 | |
468 | - return $this->get_custom_fields_by_term_slugs( $selected_entity_types ); |
|
468 | + return $this->get_custom_fields_by_term_slugs($selected_entity_types); |
|
469 | 469 | } |
470 | 470 | |
471 | 471 | /** |
@@ -479,9 +479,9 @@ discard block |
||
479 | 479 | * @return bool Return true if the post type can be used for entities, otherwise false. |
480 | 480 | * @since 3.15.0 |
481 | 481 | */ |
482 | - public static function is_valid_entity_post_type( $post_type ) { |
|
482 | + public static function is_valid_entity_post_type($post_type) { |
|
483 | 483 | |
484 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
484 | + return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true); |
|
485 | 485 | } |
486 | 486 | |
487 | 487 | /** |
@@ -489,16 +489,16 @@ discard block |
||
489 | 489 | * |
490 | 490 | * @return array |
491 | 491 | */ |
492 | - private function get_custom_fields_by_term_slugs( $term_slugs ) { |
|
492 | + private function get_custom_fields_by_term_slugs($term_slugs) { |
|
493 | 493 | $schema_service = Wordlift_Schema_Service::get_instance(); |
494 | 494 | |
495 | 495 | return array_reduce( |
496 | 496 | $term_slugs, |
497 | - function ( $carry, $item ) use ( $schema_service ) { |
|
497 | + function($carry, $item) use ($schema_service) { |
|
498 | 498 | |
499 | - $schema = $schema_service->get_schema( $item ); |
|
499 | + $schema = $schema_service->get_schema($item); |
|
500 | 500 | |
501 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
501 | + if ( ! isset($schema['custom_fields'])) { |
|
502 | 502 | return $carry; |
503 | 503 | } |
504 | 504 |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | use Wordlift\Autocomplete\Autocomplete_Service; |
14 | 14 | |
15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
16 | - exit; |
|
16 | + exit; |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | /** |
@@ -23,79 +23,79 @@ discard block |
||
23 | 23 | */ |
24 | 24 | class Wordlift_Autocomplete_Adapter { |
25 | 25 | |
26 | - /** |
|
27 | - * The {@link Autocomplete_Service} instance. |
|
28 | - * |
|
29 | - * @since 3.15.0 |
|
30 | - * @access private |
|
31 | - * @var Autocomplete_Service $configuration_service The {@link Autocomplete_Service} instance. |
|
32 | - */ |
|
33 | - private $autocomplete_service; |
|
34 | - |
|
35 | - /** |
|
36 | - * Wordlift_Autocomplete_Adapter constructor. |
|
37 | - * |
|
38 | - * @param Autocomplete_Service $autocomplete_service The {@link Autocomplete_Service} instance. |
|
39 | - * |
|
40 | - * @since 3.14.2 |
|
41 | - */ |
|
42 | - public function __construct( $autocomplete_service ) { |
|
43 | - $this->autocomplete_service = $autocomplete_service; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Handle the autocomplete ajax request. |
|
48 | - * |
|
49 | - * @since 3.15.0 |
|
50 | - */ |
|
51 | - public function wl_autocomplete() { |
|
52 | - |
|
53 | - check_ajax_referer( 'wl_autocomplete' ); |
|
54 | - |
|
55 | - // Return error if the query param is empty. |
|
56 | - if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
57 | - $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
58 | - } else { |
|
59 | - wp_send_json_error( |
|
60 | - array( |
|
61 | - 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
62 | - ) |
|
63 | - ); |
|
64 | - } |
|
65 | - |
|
66 | - // Get the exclude parameter. |
|
67 | - $exclude = ! empty( $_REQUEST['exclude'] ) |
|
68 | - ? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : ''; |
|
69 | - |
|
70 | - $scope = ! empty( $_REQUEST['scope'] ) |
|
71 | - ? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE; |
|
72 | - |
|
73 | - /** |
|
74 | - * @since 3.26.1 |
|
75 | - * Providing a way for term pages to show and save local entities. |
|
76 | - */ |
|
77 | - $show_local_entities = false; |
|
78 | - |
|
79 | - if ( isset( $_REQUEST['show_local_entities'] ) |
|
80 | - && ! empty( $_REQUEST['show_local_entities'] ) ) { // Make request. |
|
81 | - $show_local_entities = filter_var( wp_unslash( $_REQUEST['show_local_entities'] ), FILTER_VALIDATE_BOOLEAN ); |
|
82 | - } |
|
83 | - |
|
84 | - // Add the filter to check if we need to show local entities or not. |
|
85 | - add_filter( |
|
86 | - 'wl_show_local_entities', |
|
87 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
88 | - function ( $state ) use ( $show_local_entities ) { |
|
89 | - return $show_local_entities; |
|
90 | - } |
|
91 | - ); |
|
92 | - |
|
93 | - $results = $this->autocomplete_service->query( $query, $scope, $exclude ); |
|
94 | - |
|
95 | - // Clear any buffer. |
|
96 | - ob_clean(); |
|
97 | - |
|
98 | - wp_send_json_success( $results ); |
|
99 | - |
|
100 | - } |
|
26 | + /** |
|
27 | + * The {@link Autocomplete_Service} instance. |
|
28 | + * |
|
29 | + * @since 3.15.0 |
|
30 | + * @access private |
|
31 | + * @var Autocomplete_Service $configuration_service The {@link Autocomplete_Service} instance. |
|
32 | + */ |
|
33 | + private $autocomplete_service; |
|
34 | + |
|
35 | + /** |
|
36 | + * Wordlift_Autocomplete_Adapter constructor. |
|
37 | + * |
|
38 | + * @param Autocomplete_Service $autocomplete_service The {@link Autocomplete_Service} instance. |
|
39 | + * |
|
40 | + * @since 3.14.2 |
|
41 | + */ |
|
42 | + public function __construct( $autocomplete_service ) { |
|
43 | + $this->autocomplete_service = $autocomplete_service; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Handle the autocomplete ajax request. |
|
48 | + * |
|
49 | + * @since 3.15.0 |
|
50 | + */ |
|
51 | + public function wl_autocomplete() { |
|
52 | + |
|
53 | + check_ajax_referer( 'wl_autocomplete' ); |
|
54 | + |
|
55 | + // Return error if the query param is empty. |
|
56 | + if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
57 | + $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
58 | + } else { |
|
59 | + wp_send_json_error( |
|
60 | + array( |
|
61 | + 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
62 | + ) |
|
63 | + ); |
|
64 | + } |
|
65 | + |
|
66 | + // Get the exclude parameter. |
|
67 | + $exclude = ! empty( $_REQUEST['exclude'] ) |
|
68 | + ? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : ''; |
|
69 | + |
|
70 | + $scope = ! empty( $_REQUEST['scope'] ) |
|
71 | + ? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE; |
|
72 | + |
|
73 | + /** |
|
74 | + * @since 3.26.1 |
|
75 | + * Providing a way for term pages to show and save local entities. |
|
76 | + */ |
|
77 | + $show_local_entities = false; |
|
78 | + |
|
79 | + if ( isset( $_REQUEST['show_local_entities'] ) |
|
80 | + && ! empty( $_REQUEST['show_local_entities'] ) ) { // Make request. |
|
81 | + $show_local_entities = filter_var( wp_unslash( $_REQUEST['show_local_entities'] ), FILTER_VALIDATE_BOOLEAN ); |
|
82 | + } |
|
83 | + |
|
84 | + // Add the filter to check if we need to show local entities or not. |
|
85 | + add_filter( |
|
86 | + 'wl_show_local_entities', |
|
87 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
88 | + function ( $state ) use ( $show_local_entities ) { |
|
89 | + return $show_local_entities; |
|
90 | + } |
|
91 | + ); |
|
92 | + |
|
93 | + $results = $this->autocomplete_service->query( $query, $scope, $exclude ); |
|
94 | + |
|
95 | + // Clear any buffer. |
|
96 | + ob_clean(); |
|
97 | + |
|
98 | + wp_send_json_success( $results ); |
|
99 | + |
|
100 | + } |
|
101 | 101 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | use Wordlift\Autocomplete\Autocomplete_Service; |
14 | 14 | |
15 | -if ( ! defined( 'ABSPATH' ) ) { |
|
15 | +if ( ! defined('ABSPATH')) { |
|
16 | 16 | exit; |
17 | 17 | } |
18 | 18 | |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @since 3.14.2 |
41 | 41 | */ |
42 | - public function __construct( $autocomplete_service ) { |
|
42 | + public function __construct($autocomplete_service) { |
|
43 | 43 | $this->autocomplete_service = $autocomplete_service; |
44 | 44 | } |
45 | 45 | |
@@ -50,25 +50,25 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function wl_autocomplete() { |
52 | 52 | |
53 | - check_ajax_referer( 'wl_autocomplete' ); |
|
53 | + check_ajax_referer('wl_autocomplete'); |
|
54 | 54 | |
55 | 55 | // Return error if the query param is empty. |
56 | - if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
57 | - $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
56 | + if ( ! empty($_REQUEST['query'])) { // Input var okay. |
|
57 | + $query = sanitize_text_field(wp_unslash($_REQUEST['query'])); // Input var okay. |
|
58 | 58 | } else { |
59 | 59 | wp_send_json_error( |
60 | 60 | array( |
61 | - 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
61 | + 'message' => __('The query param is empty.', 'wordlift'), |
|
62 | 62 | ) |
63 | 63 | ); |
64 | 64 | } |
65 | 65 | |
66 | 66 | // Get the exclude parameter. |
67 | - $exclude = ! empty( $_REQUEST['exclude'] ) |
|
68 | - ? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : ''; |
|
67 | + $exclude = ! empty($_REQUEST['exclude']) |
|
68 | + ? sanitize_text_field(wp_unslash($_REQUEST['exclude'])) : ''; |
|
69 | 69 | |
70 | - $scope = ! empty( $_REQUEST['scope'] ) |
|
71 | - ? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE; |
|
70 | + $scope = ! empty($_REQUEST['scope']) |
|
71 | + ? sanitize_text_field(wp_unslash($_REQUEST['scope'])) : WL_AUTOCOMPLETE_SCOPE; |
|
72 | 72 | |
73 | 73 | /** |
74 | 74 | * @since 3.26.1 |
@@ -76,26 +76,26 @@ discard block |
||
76 | 76 | */ |
77 | 77 | $show_local_entities = false; |
78 | 78 | |
79 | - if ( isset( $_REQUEST['show_local_entities'] ) |
|
80 | - && ! empty( $_REQUEST['show_local_entities'] ) ) { // Make request. |
|
81 | - $show_local_entities = filter_var( wp_unslash( $_REQUEST['show_local_entities'] ), FILTER_VALIDATE_BOOLEAN ); |
|
79 | + if (isset($_REQUEST['show_local_entities']) |
|
80 | + && ! empty($_REQUEST['show_local_entities'])) { // Make request. |
|
81 | + $show_local_entities = filter_var(wp_unslash($_REQUEST['show_local_entities']), FILTER_VALIDATE_BOOLEAN); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Add the filter to check if we need to show local entities or not. |
85 | 85 | add_filter( |
86 | 86 | 'wl_show_local_entities', |
87 | 87 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
88 | - function ( $state ) use ( $show_local_entities ) { |
|
88 | + function($state) use ($show_local_entities) { |
|
89 | 89 | return $show_local_entities; |
90 | 90 | } |
91 | 91 | ); |
92 | 92 | |
93 | - $results = $this->autocomplete_service->query( $query, $scope, $exclude ); |
|
93 | + $results = $this->autocomplete_service->query($query, $scope, $exclude); |
|
94 | 94 | |
95 | 95 | // Clear any buffer. |
96 | 96 | ob_clean(); |
97 | 97 | |
98 | - wp_send_json_success( $results ); |
|
98 | + wp_send_json_success($results); |
|
99 | 99 | |
100 | 100 | } |
101 | 101 | } |
@@ -13,87 +13,87 @@ |
||
13 | 13 | */ |
14 | 14 | class Wordlift_Schema_Location_Property_Service extends Wordlift_Property_Service { |
15 | 15 | |
16 | - /** |
|
17 | - * The meta key used to store data for this property. We don't use wl_url to |
|
18 | - * avoid potential confusion about other URLs. |
|
19 | - * |
|
20 | - * @since 3.7.0 |
|
21 | - */ |
|
22 | - const META_KEY = 'wl_location'; |
|
23 | - |
|
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function get_rdf_predicate() { |
|
28 | - |
|
29 | - return 'http://schema.org/location'; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * {@inheritdoc} |
|
34 | - */ |
|
35 | - public function get_rdf_data_type() { |
|
36 | - |
|
37 | - return 'xsd:anyURI'; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - public function get_data_type() { |
|
44 | - |
|
45 | - return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - public function get_cardinality() { |
|
52 | - |
|
53 | - return INF; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - public function get_metabox_class() { |
|
60 | - |
|
61 | - return 'Wl_Metabox_Field'; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function get_metabox_label() { |
|
68 | - |
|
69 | - return 'Location(s)'; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Get the schema:url value for the specified post/entity. |
|
74 | - * |
|
75 | - * @param int $post_id The post id. |
|
76 | - * |
|
77 | - * @return array|NULL The schema:url value or NULL if not set. |
|
78 | - * @since 3.7.0 |
|
79 | - */ |
|
80 | - public function get( $post_id ) { |
|
81 | - |
|
82 | - // Get the schema:url values set in WP. |
|
83 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
84 | - |
|
85 | - // Finally return whatever values the editor set. |
|
86 | - return $values; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * {@inheritdoc} |
|
91 | - */ |
|
92 | - public function sanitize( $value ) { |
|
93 | - |
|
94 | - // TODO: check that it's an URL or that is <permalink> |
|
95 | - |
|
96 | - return $value; |
|
97 | - } |
|
16 | + /** |
|
17 | + * The meta key used to store data for this property. We don't use wl_url to |
|
18 | + * avoid potential confusion about other URLs. |
|
19 | + * |
|
20 | + * @since 3.7.0 |
|
21 | + */ |
|
22 | + const META_KEY = 'wl_location'; |
|
23 | + |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function get_rdf_predicate() { |
|
28 | + |
|
29 | + return 'http://schema.org/location'; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * {@inheritdoc} |
|
34 | + */ |
|
35 | + public function get_rdf_data_type() { |
|
36 | + |
|
37 | + return 'xsd:anyURI'; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + public function get_data_type() { |
|
44 | + |
|
45 | + return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + public function get_cardinality() { |
|
52 | + |
|
53 | + return INF; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + public function get_metabox_class() { |
|
60 | + |
|
61 | + return 'Wl_Metabox_Field'; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function get_metabox_label() { |
|
68 | + |
|
69 | + return 'Location(s)'; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Get the schema:url value for the specified post/entity. |
|
74 | + * |
|
75 | + * @param int $post_id The post id. |
|
76 | + * |
|
77 | + * @return array|NULL The schema:url value or NULL if not set. |
|
78 | + * @since 3.7.0 |
|
79 | + */ |
|
80 | + public function get( $post_id ) { |
|
81 | + |
|
82 | + // Get the schema:url values set in WP. |
|
83 | + $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
84 | + |
|
85 | + // Finally return whatever values the editor set. |
|
86 | + return $values; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * {@inheritdoc} |
|
91 | + */ |
|
92 | + public function sanitize( $value ) { |
|
93 | + |
|
94 | + // TODO: check that it's an URL or that is <permalink> |
|
95 | + |
|
96 | + return $value; |
|
97 | + } |
|
98 | 98 | |
99 | 99 | } |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | * @return array|NULL The schema:url value or NULL if not set. |
78 | 78 | * @since 3.7.0 |
79 | 79 | */ |
80 | - public function get( $post_id ) { |
|
80 | + public function get($post_id) { |
|
81 | 81 | |
82 | 82 | // Get the schema:url values set in WP. |
83 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
83 | + $values = get_post_meta($post_id, self::META_KEY, false); |
|
84 | 84 | |
85 | 85 | // Finally return whatever values the editor set. |
86 | 86 | return $values; |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | /** |
90 | 90 | * {@inheritdoc} |
91 | 91 | */ |
92 | - public function sanitize( $value ) { |
|
92 | + public function sanitize($value) { |
|
93 | 93 | |
94 | 94 | // TODO: check that it's an URL or that is <permalink> |
95 | 95 |
@@ -17,52 +17,52 @@ |
||
17 | 17 | */ |
18 | 18 | interface Wordlift_Cache_Service { |
19 | 19 | |
20 | - /** |
|
21 | - * Get the cached response for the specified `id`. |
|
22 | - * |
|
23 | - * @since 3.16.0 |
|
24 | - * |
|
25 | - * @param string $id The cache `id`. |
|
26 | - * |
|
27 | - * @return mixed|false The cached contents or false if the cache isn't found. |
|
28 | - */ |
|
29 | - public function get_cache( $id ); |
|
20 | + /** |
|
21 | + * Get the cached response for the specified `id`. |
|
22 | + * |
|
23 | + * @since 3.16.0 |
|
24 | + * |
|
25 | + * @param string $id The cache `id`. |
|
26 | + * |
|
27 | + * @return mixed|false The cached contents or false if the cache isn't found. |
|
28 | + */ |
|
29 | + public function get_cache( $id ); |
|
30 | 30 | |
31 | - /** |
|
32 | - * Check whether we have cached results for the provided id. |
|
33 | - * |
|
34 | - * @since 3.16.3 |
|
35 | - * |
|
36 | - * @param string $id The cache `id`. |
|
37 | - * |
|
38 | - * @return bool True if we have cached results otherwise false. |
|
39 | - */ |
|
40 | - public function has_cache( $id ); |
|
31 | + /** |
|
32 | + * Check whether we have cached results for the provided id. |
|
33 | + * |
|
34 | + * @since 3.16.3 |
|
35 | + * |
|
36 | + * @param string $id The cache `id`. |
|
37 | + * |
|
38 | + * @return bool True if we have cached results otherwise false. |
|
39 | + */ |
|
40 | + public function has_cache( $id ); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Set the cache contents for the specified `id`. |
|
44 | - * |
|
45 | - * @since 3.16.0 |
|
46 | - * |
|
47 | - * @param string $id The cache id. |
|
48 | - * @param mixed $contents The cache contents. |
|
49 | - */ |
|
50 | - public function set_cache( $id, $contents ); |
|
42 | + /** |
|
43 | + * Set the cache contents for the specified `id`. |
|
44 | + * |
|
45 | + * @since 3.16.0 |
|
46 | + * |
|
47 | + * @param string $id The cache id. |
|
48 | + * @param mixed $contents The cache contents. |
|
49 | + */ |
|
50 | + public function set_cache( $id, $contents ); |
|
51 | 51 | |
52 | - /** |
|
53 | - * Delete the cache for the specified `id`. |
|
54 | - * |
|
55 | - * @since 3.16.0 |
|
56 | - * |
|
57 | - * @param string $id The cache `id`. |
|
58 | - */ |
|
59 | - public function delete_cache( $id ); |
|
52 | + /** |
|
53 | + * Delete the cache for the specified `id`. |
|
54 | + * |
|
55 | + * @since 3.16.0 |
|
56 | + * |
|
57 | + * @param string $id The cache `id`. |
|
58 | + */ |
|
59 | + public function delete_cache( $id ); |
|
60 | 60 | |
61 | - /** |
|
62 | - * Flush the whole cache. |
|
63 | - * |
|
64 | - * @since 3.16.0 |
|
65 | - */ |
|
66 | - public function flush(); |
|
61 | + /** |
|
62 | + * Flush the whole cache. |
|
63 | + * |
|
64 | + * @since 3.16.0 |
|
65 | + */ |
|
66 | + public function flush(); |
|
67 | 67 | |
68 | 68 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @return mixed|false The cached contents or false if the cache isn't found. |
28 | 28 | */ |
29 | - public function get_cache( $id ); |
|
29 | + public function get_cache($id); |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Check whether we have cached results for the provided id. |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * |
38 | 38 | * @return bool True if we have cached results otherwise false. |
39 | 39 | */ |
40 | - public function has_cache( $id ); |
|
40 | + public function has_cache($id); |
|
41 | 41 | |
42 | 42 | /** |
43 | 43 | * Set the cache contents for the specified `id`. |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * @param string $id The cache id. |
48 | 48 | * @param mixed $contents The cache contents. |
49 | 49 | */ |
50 | - public function set_cache( $id, $contents ); |
|
50 | + public function set_cache($id, $contents); |
|
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Delete the cache for the specified `id`. |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @param string $id The cache `id`. |
58 | 58 | */ |
59 | - public function delete_cache( $id ); |
|
59 | + public function delete_cache($id); |
|
60 | 60 | |
61 | 61 | /** |
62 | 62 | * Flush the whole cache. |
@@ -8,7 +8,7 @@ |
||
8 | 8 | * @package Wordlift |
9 | 9 | * @subpackage Wordlift/includes/cache |
10 | 10 | */ |
11 | -require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/intf-wordlift-cache-service.php'; |
|
12 | -require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/class-wordlift-file-cache-service.php'; |
|
13 | -require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/class-wordlift-cached-post-converter.php'; |
|
14 | -require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/class-wordlift-cached-entity-uri-service.php'; |
|
11 | +require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/intf-wordlift-cache-service.php'; |
|
12 | +require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/class-wordlift-file-cache-service.php'; |
|
13 | +require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/class-wordlift-cached-post-converter.php'; |
|
14 | +require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/class-wordlift-cached-entity-uri-service.php'; |
@@ -14,203 +14,203 @@ |
||
14 | 14 | */ |
15 | 15 | class Wordlift_Cached_Entity_Uri_Service extends Wordlift_Entity_Uri_Service { |
16 | 16 | |
17 | - /** |
|
18 | - * The {@link Wordlift_Cache_Service} instance. |
|
19 | - * |
|
20 | - * @since 3.16.3 |
|
21 | - * @access private |
|
22 | - * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
|
23 | - */ |
|
24 | - private $cache_service; |
|
25 | - |
|
26 | - /** |
|
27 | - * A {@link Wordlift_Log_Service} instance. |
|
28 | - * |
|
29 | - * @since 3.16.3 |
|
30 | - * @access private |
|
31 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
32 | - */ |
|
33 | - private $log; |
|
34 | - |
|
35 | - /** |
|
36 | - * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance. |
|
37 | - * |
|
38 | - * @param \Wordlift_Cache_Service $cache_service |
|
39 | - * |
|
40 | - * @since 3.16.3 |
|
41 | - */ |
|
42 | - public function __construct( $cache_service ) { |
|
43 | - parent::__construct(); |
|
44 | - |
|
45 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
46 | - |
|
47 | - // Add hooks for meta being added/modified/deleted. |
|
48 | - $this->cache_service = $cache_service; |
|
49 | - |
|
50 | - add_action( 'add_post_meta', array( $this, 'on_before_post_meta_add' ), 10, 3 ); |
|
51 | - add_action( 'update_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 ); |
|
52 | - add_action( 'delete_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 ); |
|
53 | - |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Preload the URIs. |
|
58 | - * |
|
59 | - * @param array $uris Preload an array of URIs. |
|
60 | - * |
|
61 | - * @since 3.16.3 |
|
62 | - */ |
|
63 | - public function preload_uris( $uris ) { |
|
64 | - |
|
65 | - // Filter the URIs which aren't yet cached. |
|
66 | - $cache_service = $this->cache_service; |
|
67 | - $uris_to_cache = array_filter( |
|
68 | - (array) $uris, |
|
69 | - function ( $item ) use ( $cache_service ) { |
|
70 | - return ! $cache_service->has_cache( $item ); |
|
71 | - } |
|
72 | - ); |
|
73 | - |
|
74 | - // Preload the URIs. |
|
75 | - parent::preload_uris( $uris_to_cache ); |
|
76 | - |
|
77 | - // Store them in cache. |
|
78 | - if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
79 | - foreach ( $this->uri_to_post as $uri => $post_id ) { |
|
80 | - $this->set_cache( $uri, $post_id ); |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Get the entity post for the specified URI. |
|
88 | - * |
|
89 | - * @param string $uri The URI. |
|
90 | - * |
|
91 | - * @return null|WP_Post The {@link WP_Post} or null if not found. |
|
92 | - * @since 3.16.3 |
|
93 | - */ |
|
94 | - public function get_entity( $uri ) { |
|
95 | - |
|
96 | - $this->log->trace( "Getting entity for uri $uri..." ); |
|
97 | - |
|
98 | - // Get the cached post for the specified URI. |
|
99 | - $cache = $this->cache_service->get_cache( $uri ); |
|
100 | - |
|
101 | - // Return the cached data if valid. |
|
102 | - if ( false !== $cache && is_numeric( $cache ) ) { |
|
103 | - $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
104 | - |
|
105 | - return get_post( $cache ); |
|
106 | - } |
|
107 | - |
|
108 | - // Get the actual result. |
|
109 | - $post = parent::get_entity( $uri ); |
|
110 | - |
|
111 | - // Cache the result. |
|
112 | - if ( null !== $post ) { |
|
113 | - $this->set_cache( $uri, $post->ID ); |
|
114 | - } |
|
115 | - |
|
116 | - // Return the result. |
|
117 | - return $post; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Set the cached URI for the specified {@link WP_Post}. |
|
122 | - * |
|
123 | - * @param string $uri The URI. |
|
124 | - * @param int $post_id The post ID. |
|
125 | - * |
|
126 | - * @since 3.16.3 |
|
127 | - * @since 3.29.0 takes a post ID as input. |
|
128 | - */ |
|
129 | - private function set_cache( $uri, $post_id ) { |
|
130 | - |
|
131 | - // Cache the result. |
|
132 | - $this->cache_service->set_cache( $uri, $post_id ); |
|
133 | - |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Delete the cache for the specified URIs. |
|
138 | - * |
|
139 | - * @param array $uris An array of URIs. |
|
140 | - * |
|
141 | - * @since 3.16.3 |
|
142 | - */ |
|
143 | - private function delete_cache( $uris ) { |
|
144 | - |
|
145 | - // Delete the cache for each URI. |
|
146 | - foreach ( $uris as $uri ) { |
|
147 | - // Delete the single cache file. |
|
148 | - $this->cache_service->delete_cache( $uri ); |
|
149 | - } |
|
150 | - |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Before post meta change. |
|
155 | - * |
|
156 | - * When a post meta is going to be changed, we check if the `meta_key` is |
|
157 | - * either the `entity_url` or the `same_as` in which case we delete the cache |
|
158 | - * for all the associated URIs. |
|
159 | - * |
|
160 | - * @param int|array $meta_ids The {@link WP_Post} meta id(s). |
|
161 | - * @param int $post_id The {@link WP_Post} id. |
|
162 | - * @param string $meta_key The meta key. |
|
163 | - * @param mixed $meta_value The meta value(s). |
|
164 | - * |
|
165 | - * @since 3.16.3 |
|
166 | - */ |
|
167 | - public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
168 | - |
|
169 | - // Bail out if we're not interested in the meta key. |
|
170 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
175 | - |
|
176 | - // The list of existing URIs, plus the list of URIs being deleted/updated. |
|
177 | - $old_value = get_post_meta( $post_id, $meta_key ); |
|
178 | - |
|
179 | - // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects |
|
180 | - // because of filters. So if it's not we return an empty array. |
|
181 | - if ( ! is_array( $old_value ) ) { |
|
182 | - $old_value = array(); |
|
183 | - } |
|
184 | - $new_value = isset( $meta_value ) ? (array) $meta_value : array(); |
|
185 | - $uris = array_merge( $old_value, $new_value ); |
|
186 | - |
|
187 | - // Delete the cache for those URIs. |
|
188 | - $this->delete_cache( $uris ); |
|
189 | - |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Hook to meta add for a {@link WP_Post}, will cause the cache to |
|
194 | - * invalidate. |
|
195 | - * |
|
196 | - * @param int $post_id The {@link WP_Post} id. |
|
197 | - * @param string $meta_key The meta key. |
|
198 | - * @param mixed $meta_value The meta value(s). |
|
199 | - * |
|
200 | - * @since 3.16.3 |
|
201 | - */ |
|
202 | - public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
203 | - |
|
204 | - // Bail out if we're not interested in the meta key. |
|
205 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
206 | - return; |
|
207 | - } |
|
208 | - |
|
209 | - $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
210 | - |
|
211 | - // Delete the cache for the URIs being added. |
|
212 | - $this->delete_cache( (array) $meta_value ); |
|
213 | - |
|
214 | - } |
|
17 | + /** |
|
18 | + * The {@link Wordlift_Cache_Service} instance. |
|
19 | + * |
|
20 | + * @since 3.16.3 |
|
21 | + * @access private |
|
22 | + * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
|
23 | + */ |
|
24 | + private $cache_service; |
|
25 | + |
|
26 | + /** |
|
27 | + * A {@link Wordlift_Log_Service} instance. |
|
28 | + * |
|
29 | + * @since 3.16.3 |
|
30 | + * @access private |
|
31 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
32 | + */ |
|
33 | + private $log; |
|
34 | + |
|
35 | + /** |
|
36 | + * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance. |
|
37 | + * |
|
38 | + * @param \Wordlift_Cache_Service $cache_service |
|
39 | + * |
|
40 | + * @since 3.16.3 |
|
41 | + */ |
|
42 | + public function __construct( $cache_service ) { |
|
43 | + parent::__construct(); |
|
44 | + |
|
45 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
46 | + |
|
47 | + // Add hooks for meta being added/modified/deleted. |
|
48 | + $this->cache_service = $cache_service; |
|
49 | + |
|
50 | + add_action( 'add_post_meta', array( $this, 'on_before_post_meta_add' ), 10, 3 ); |
|
51 | + add_action( 'update_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 ); |
|
52 | + add_action( 'delete_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 ); |
|
53 | + |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Preload the URIs. |
|
58 | + * |
|
59 | + * @param array $uris Preload an array of URIs. |
|
60 | + * |
|
61 | + * @since 3.16.3 |
|
62 | + */ |
|
63 | + public function preload_uris( $uris ) { |
|
64 | + |
|
65 | + // Filter the URIs which aren't yet cached. |
|
66 | + $cache_service = $this->cache_service; |
|
67 | + $uris_to_cache = array_filter( |
|
68 | + (array) $uris, |
|
69 | + function ( $item ) use ( $cache_service ) { |
|
70 | + return ! $cache_service->has_cache( $item ); |
|
71 | + } |
|
72 | + ); |
|
73 | + |
|
74 | + // Preload the URIs. |
|
75 | + parent::preload_uris( $uris_to_cache ); |
|
76 | + |
|
77 | + // Store them in cache. |
|
78 | + if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
79 | + foreach ( $this->uri_to_post as $uri => $post_id ) { |
|
80 | + $this->set_cache( $uri, $post_id ); |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Get the entity post for the specified URI. |
|
88 | + * |
|
89 | + * @param string $uri The URI. |
|
90 | + * |
|
91 | + * @return null|WP_Post The {@link WP_Post} or null if not found. |
|
92 | + * @since 3.16.3 |
|
93 | + */ |
|
94 | + public function get_entity( $uri ) { |
|
95 | + |
|
96 | + $this->log->trace( "Getting entity for uri $uri..." ); |
|
97 | + |
|
98 | + // Get the cached post for the specified URI. |
|
99 | + $cache = $this->cache_service->get_cache( $uri ); |
|
100 | + |
|
101 | + // Return the cached data if valid. |
|
102 | + if ( false !== $cache && is_numeric( $cache ) ) { |
|
103 | + $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
104 | + |
|
105 | + return get_post( $cache ); |
|
106 | + } |
|
107 | + |
|
108 | + // Get the actual result. |
|
109 | + $post = parent::get_entity( $uri ); |
|
110 | + |
|
111 | + // Cache the result. |
|
112 | + if ( null !== $post ) { |
|
113 | + $this->set_cache( $uri, $post->ID ); |
|
114 | + } |
|
115 | + |
|
116 | + // Return the result. |
|
117 | + return $post; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Set the cached URI for the specified {@link WP_Post}. |
|
122 | + * |
|
123 | + * @param string $uri The URI. |
|
124 | + * @param int $post_id The post ID. |
|
125 | + * |
|
126 | + * @since 3.16.3 |
|
127 | + * @since 3.29.0 takes a post ID as input. |
|
128 | + */ |
|
129 | + private function set_cache( $uri, $post_id ) { |
|
130 | + |
|
131 | + // Cache the result. |
|
132 | + $this->cache_service->set_cache( $uri, $post_id ); |
|
133 | + |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Delete the cache for the specified URIs. |
|
138 | + * |
|
139 | + * @param array $uris An array of URIs. |
|
140 | + * |
|
141 | + * @since 3.16.3 |
|
142 | + */ |
|
143 | + private function delete_cache( $uris ) { |
|
144 | + |
|
145 | + // Delete the cache for each URI. |
|
146 | + foreach ( $uris as $uri ) { |
|
147 | + // Delete the single cache file. |
|
148 | + $this->cache_service->delete_cache( $uri ); |
|
149 | + } |
|
150 | + |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Before post meta change. |
|
155 | + * |
|
156 | + * When a post meta is going to be changed, we check if the `meta_key` is |
|
157 | + * either the `entity_url` or the `same_as` in which case we delete the cache |
|
158 | + * for all the associated URIs. |
|
159 | + * |
|
160 | + * @param int|array $meta_ids The {@link WP_Post} meta id(s). |
|
161 | + * @param int $post_id The {@link WP_Post} id. |
|
162 | + * @param string $meta_key The meta key. |
|
163 | + * @param mixed $meta_value The meta value(s). |
|
164 | + * |
|
165 | + * @since 3.16.3 |
|
166 | + */ |
|
167 | + public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
168 | + |
|
169 | + // Bail out if we're not interested in the meta key. |
|
170 | + if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
175 | + |
|
176 | + // The list of existing URIs, plus the list of URIs being deleted/updated. |
|
177 | + $old_value = get_post_meta( $post_id, $meta_key ); |
|
178 | + |
|
179 | + // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects |
|
180 | + // because of filters. So if it's not we return an empty array. |
|
181 | + if ( ! is_array( $old_value ) ) { |
|
182 | + $old_value = array(); |
|
183 | + } |
|
184 | + $new_value = isset( $meta_value ) ? (array) $meta_value : array(); |
|
185 | + $uris = array_merge( $old_value, $new_value ); |
|
186 | + |
|
187 | + // Delete the cache for those URIs. |
|
188 | + $this->delete_cache( $uris ); |
|
189 | + |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Hook to meta add for a {@link WP_Post}, will cause the cache to |
|
194 | + * invalidate. |
|
195 | + * |
|
196 | + * @param int $post_id The {@link WP_Post} id. |
|
197 | + * @param string $meta_key The meta key. |
|
198 | + * @param mixed $meta_value The meta value(s). |
|
199 | + * |
|
200 | + * @since 3.16.3 |
|
201 | + */ |
|
202 | + public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
203 | + |
|
204 | + // Bail out if we're not interested in the meta key. |
|
205 | + if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
206 | + return; |
|
207 | + } |
|
208 | + |
|
209 | + $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
210 | + |
|
211 | + // Delete the cache for the URIs being added. |
|
212 | + $this->delete_cache( (array) $meta_value ); |
|
213 | + |
|
214 | + } |
|
215 | 215 | |
216 | 216 | } |
@@ -39,17 +39,17 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @since 3.16.3 |
41 | 41 | */ |
42 | - public function __construct( $cache_service ) { |
|
42 | + public function __construct($cache_service) { |
|
43 | 43 | parent::__construct(); |
44 | 44 | |
45 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
45 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
46 | 46 | |
47 | 47 | // Add hooks for meta being added/modified/deleted. |
48 | 48 | $this->cache_service = $cache_service; |
49 | 49 | |
50 | - add_action( 'add_post_meta', array( $this, 'on_before_post_meta_add' ), 10, 3 ); |
|
51 | - add_action( 'update_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 ); |
|
52 | - add_action( 'delete_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 ); |
|
50 | + add_action('add_post_meta', array($this, 'on_before_post_meta_add'), 10, 3); |
|
51 | + add_action('update_post_meta', array($this, 'on_before_post_meta_change'), 10, 4); |
|
52 | + add_action('delete_post_meta', array($this, 'on_before_post_meta_change'), 10, 4); |
|
53 | 53 | |
54 | 54 | } |
55 | 55 | |
@@ -60,24 +60,24 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @since 3.16.3 |
62 | 62 | */ |
63 | - public function preload_uris( $uris ) { |
|
63 | + public function preload_uris($uris) { |
|
64 | 64 | |
65 | 65 | // Filter the URIs which aren't yet cached. |
66 | 66 | $cache_service = $this->cache_service; |
67 | 67 | $uris_to_cache = array_filter( |
68 | 68 | (array) $uris, |
69 | - function ( $item ) use ( $cache_service ) { |
|
70 | - return ! $cache_service->has_cache( $item ); |
|
69 | + function($item) use ($cache_service) { |
|
70 | + return ! $cache_service->has_cache($item); |
|
71 | 71 | } |
72 | 72 | ); |
73 | 73 | |
74 | 74 | // Preload the URIs. |
75 | - parent::preload_uris( $uris_to_cache ); |
|
75 | + parent::preload_uris($uris_to_cache); |
|
76 | 76 | |
77 | 77 | // Store them in cache. |
78 | - if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
79 | - foreach ( $this->uri_to_post as $uri => $post_id ) { |
|
80 | - $this->set_cache( $uri, $post_id ); |
|
78 | + if (is_array($this->uri_to_post) && ! empty($this->uri_to_post)) { |
|
79 | + foreach ($this->uri_to_post as $uri => $post_id) { |
|
80 | + $this->set_cache($uri, $post_id); |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
@@ -91,26 +91,26 @@ discard block |
||
91 | 91 | * @return null|WP_Post The {@link WP_Post} or null if not found. |
92 | 92 | * @since 3.16.3 |
93 | 93 | */ |
94 | - public function get_entity( $uri ) { |
|
94 | + public function get_entity($uri) { |
|
95 | 95 | |
96 | - $this->log->trace( "Getting entity for uri $uri..." ); |
|
96 | + $this->log->trace("Getting entity for uri $uri..."); |
|
97 | 97 | |
98 | 98 | // Get the cached post for the specified URI. |
99 | - $cache = $this->cache_service->get_cache( $uri ); |
|
99 | + $cache = $this->cache_service->get_cache($uri); |
|
100 | 100 | |
101 | 101 | // Return the cached data if valid. |
102 | - if ( false !== $cache && is_numeric( $cache ) ) { |
|
103 | - $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
102 | + if (false !== $cache && is_numeric($cache)) { |
|
103 | + $this->log->debug("Cached entity $cache for uri $uri found."); |
|
104 | 104 | |
105 | - return get_post( $cache ); |
|
105 | + return get_post($cache); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | // Get the actual result. |
109 | - $post = parent::get_entity( $uri ); |
|
109 | + $post = parent::get_entity($uri); |
|
110 | 110 | |
111 | 111 | // Cache the result. |
112 | - if ( null !== $post ) { |
|
113 | - $this->set_cache( $uri, $post->ID ); |
|
112 | + if (null !== $post) { |
|
113 | + $this->set_cache($uri, $post->ID); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | // Return the result. |
@@ -126,10 +126,10 @@ discard block |
||
126 | 126 | * @since 3.16.3 |
127 | 127 | * @since 3.29.0 takes a post ID as input. |
128 | 128 | */ |
129 | - private function set_cache( $uri, $post_id ) { |
|
129 | + private function set_cache($uri, $post_id) { |
|
130 | 130 | |
131 | 131 | // Cache the result. |
132 | - $this->cache_service->set_cache( $uri, $post_id ); |
|
132 | + $this->cache_service->set_cache($uri, $post_id); |
|
133 | 133 | |
134 | 134 | } |
135 | 135 | |
@@ -140,12 +140,12 @@ discard block |
||
140 | 140 | * |
141 | 141 | * @since 3.16.3 |
142 | 142 | */ |
143 | - private function delete_cache( $uris ) { |
|
143 | + private function delete_cache($uris) { |
|
144 | 144 | |
145 | 145 | // Delete the cache for each URI. |
146 | - foreach ( $uris as $uri ) { |
|
146 | + foreach ($uris as $uri) { |
|
147 | 147 | // Delete the single cache file. |
148 | - $this->cache_service->delete_cache( $uri ); |
|
148 | + $this->cache_service->delete_cache($uri); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | } |
@@ -164,28 +164,28 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @since 3.16.3 |
166 | 166 | */ |
167 | - public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
167 | + public function on_before_post_meta_change($meta_ids, $post_id, $meta_key, $meta_value) { |
|
168 | 168 | |
169 | 169 | // Bail out if we're not interested in the meta key. |
170 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
170 | + if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) { |
|
171 | 171 | return; |
172 | 172 | } |
173 | 173 | |
174 | - $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
174 | + $this->log->trace("Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..."); |
|
175 | 175 | |
176 | 176 | // The list of existing URIs, plus the list of URIs being deleted/updated. |
177 | - $old_value = get_post_meta( $post_id, $meta_key ); |
|
177 | + $old_value = get_post_meta($post_id, $meta_key); |
|
178 | 178 | |
179 | 179 | // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects |
180 | 180 | // because of filters. So if it's not we return an empty array. |
181 | - if ( ! is_array( $old_value ) ) { |
|
181 | + if ( ! is_array($old_value)) { |
|
182 | 182 | $old_value = array(); |
183 | 183 | } |
184 | - $new_value = isset( $meta_value ) ? (array) $meta_value : array(); |
|
185 | - $uris = array_merge( $old_value, $new_value ); |
|
184 | + $new_value = isset($meta_value) ? (array) $meta_value : array(); |
|
185 | + $uris = array_merge($old_value, $new_value); |
|
186 | 186 | |
187 | 187 | // Delete the cache for those URIs. |
188 | - $this->delete_cache( $uris ); |
|
188 | + $this->delete_cache($uris); |
|
189 | 189 | |
190 | 190 | } |
191 | 191 | |
@@ -199,17 +199,17 @@ discard block |
||
199 | 199 | * |
200 | 200 | * @since 3.16.3 |
201 | 201 | */ |
202 | - public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
202 | + public function on_before_post_meta_add($post_id, $meta_key, $meta_value) { |
|
203 | 203 | |
204 | 204 | // Bail out if we're not interested in the meta key. |
205 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
205 | + if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) { |
|
206 | 206 | return; |
207 | 207 | } |
208 | 208 | |
209 | - $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
209 | + $this->log->trace("Adding $meta_key for post $post_id ($meta_key), invalidating cache..."); |
|
210 | 210 | |
211 | 211 | // Delete the cache for the URIs being added. |
212 | - $this->delete_cache( (array) $meta_value ); |
|
212 | + $this->delete_cache((array) $meta_value); |
|
213 | 213 | |
214 | 214 | } |
215 | 215 |
@@ -21,354 +21,354 @@ |
||
21 | 21 | */ |
22 | 22 | class Wordlift_Cached_Post_Converter implements Wordlift_Post_Converter { |
23 | 23 | |
24 | - /** |
|
25 | - * A {@link Wordlift_Post_Converter} instance. |
|
26 | - * |
|
27 | - * @since 3.16.0 |
|
28 | - * |
|
29 | - * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
30 | - */ |
|
31 | - private $converter; |
|
32 | - |
|
33 | - /** |
|
34 | - * A {@link Wordlift_Log_Service} instance. |
|
35 | - * |
|
36 | - * @since 3.16.0 |
|
37 | - * |
|
38 | - * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
39 | - */ |
|
40 | - private $log; |
|
41 | - |
|
42 | - /** |
|
43 | - * A list of meta keys that do not cause the cache to update. |
|
44 | - * |
|
45 | - * @since 3.17.3 |
|
46 | - * @var array An array of ignored meta keys. |
|
47 | - */ |
|
48 | - private static $ignored_meta_keys = array( |
|
49 | - '_edit_lock', |
|
50 | - '_edit_last', |
|
51 | - '_wp_page_template', |
|
52 | - '_wp_attachment_is_custom_background', |
|
53 | - '_wp_attachment_backup_sizes', |
|
54 | - '_wp_attachment_is_custom_header', |
|
55 | - ); |
|
56 | - /** |
|
57 | - * @var Ttl_Cache |
|
58 | - */ |
|
59 | - private $cache; |
|
60 | - /** |
|
61 | - * @var Reference_Processor |
|
62 | - */ |
|
63 | - private $reference_processor; |
|
64 | - |
|
65 | - /** |
|
66 | - * Wordlift_Cached_Post_Converter constructor. |
|
67 | - * |
|
68 | - * @param \Wordlift_Post_Converter $converter The {@link Wordlift_Post_Converter} implementation. |
|
69 | - * @param Ttl_Cache $cache The {@link Ttl_Cache} cache instance. |
|
70 | - */ |
|
71 | - public function __construct( $converter, $cache ) { |
|
72 | - |
|
73 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
74 | - |
|
75 | - $this->cache = $cache; |
|
76 | - $this->converter = $converter; |
|
77 | - $this->reference_processor = Reference_Processor::get_instance(); |
|
78 | - $this->init_hooks(); |
|
79 | - |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Hooks to catch post/post meta changes in order to invalidate the cache. |
|
84 | - * |
|
85 | - * @since 3.16.0 |
|
86 | - */ |
|
87 | - private function init_hooks() { |
|
88 | - |
|
89 | - // Hook on post save to flush relevant cache. |
|
90 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
91 | - |
|
92 | - add_action( |
|
93 | - 'added_post_meta', |
|
94 | - array( |
|
95 | - $this, |
|
96 | - 'changed_post_meta', |
|
97 | - ), |
|
98 | - 10, |
|
99 | - 3 |
|
100 | - ); |
|
101 | - add_action( |
|
102 | - 'updated_post_meta', |
|
103 | - array( |
|
104 | - $this, |
|
105 | - 'changed_post_meta', |
|
106 | - ), |
|
107 | - 10, |
|
108 | - 3 |
|
109 | - ); |
|
110 | - add_action( |
|
111 | - 'deleted_post_meta', |
|
112 | - array( |
|
113 | - $this, |
|
114 | - 'changed_post_meta', |
|
115 | - ), |
|
116 | - 10, |
|
117 | - 3 |
|
118 | - ); |
|
119 | - |
|
120 | - // Flush cache when wordlift settings were updated. |
|
121 | - add_action( |
|
122 | - 'update_option_wl_general_settings', |
|
123 | - array( |
|
124 | - $this, |
|
125 | - 'update_option_wl_general_settings', |
|
126 | - ) |
|
127 | - ); |
|
128 | - |
|
129 | - // Flushes the cache when permalink structure is changed. |
|
130 | - add_action( |
|
131 | - 'update_option_permalink_structure', |
|
132 | - array( |
|
133 | - $this, |
|
134 | - 'permalinks_structure_changed', |
|
135 | - ) |
|
136 | - ); |
|
137 | - |
|
138 | - // Invalid cache on relationship change. |
|
139 | - add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
140 | - add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
141 | - |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Note that the `&$cache` parameter here is used only to report whether the response comes from the cache. It |
|
146 | - * used by `test-issue-626.php` and nowhere else in code. |
|
147 | - * |
|
148 | - * @inheritdoc |
|
149 | - */ |
|
150 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
151 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), &$cache = false ) { |
|
152 | - |
|
153 | - // Ensure post ID is `int`. Otherwise we may have issues with caching, since caching is strict about |
|
154 | - // key var types. |
|
155 | - $post_id = (int) $post_id; |
|
156 | - |
|
157 | - $this->log->trace( "Converting post $post_id..." ); |
|
158 | - |
|
159 | - // Try to get a cached result. |
|
160 | - $contents = $this->get_cache( $post_id, $references ); |
|
161 | - |
|
162 | - // Return the cached contents if any. |
|
163 | - if ( false !== $contents ) { |
|
164 | - $this->log->debug( "Cached contents found for post $post_id." ); |
|
165 | - |
|
166 | - // Inform the caller that this is cached result. |
|
167 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
168 | - $cache = true; |
|
169 | - $this->add_http_header( $post_id, true ); |
|
170 | - |
|
171 | - // Return the contents. |
|
172 | - return $contents; |
|
173 | - } |
|
174 | - |
|
175 | - // Set cached to false. |
|
176 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
177 | - $cache = false; |
|
178 | - $this->add_http_header( $post_id, false ); |
|
179 | - |
|
180 | - // Convert the post. |
|
181 | - $jsonld = $this->converter->convert( $post_id, $references, $references_infos ); |
|
182 | - |
|
183 | - /** |
|
184 | - * @since 3.32.0 |
|
185 | - * We cant apply json_encode on the objects {@link \Wordlift\Jsonld\Reference}, so we decode |
|
186 | - * it here before saving it on cache. |
|
187 | - */ |
|
188 | - // Cache the results. |
|
189 | - $this->set_cache( $post_id, $references, $jsonld ); |
|
190 | - |
|
191 | - // Finally return the JSON-LD. |
|
192 | - return $jsonld; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Try to get the cached contents. |
|
197 | - * |
|
198 | - * @param int $post_id The {@link WP_Post} id. |
|
199 | - * @param array $references The referenced posts. |
|
200 | - * |
|
201 | - * @return mixed|bool The cached contents or false if the cached isn't found. |
|
202 | - * @since 3.16.0 |
|
203 | - */ |
|
204 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
205 | - private function get_cache( $post_id, &$references = array() ) { |
|
206 | - |
|
207 | - // Ensure post ID is int, because cache is strict about var types. |
|
208 | - $post_id = (int) $post_id; |
|
209 | - |
|
210 | - $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
211 | - |
|
212 | - // Get the cache. |
|
213 | - $contents = $this->cache->get( $post_id ); |
|
214 | - |
|
215 | - // Bail out if we don't have cached contents or the cached contents are |
|
216 | - // invalid. |
|
217 | - if ( null === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
218 | - $this->log->debug( "Cached contents for post $post_id not found." ); |
|
219 | - |
|
220 | - return false; |
|
221 | - } |
|
222 | - |
|
223 | - // Remap the cache. |
|
224 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
225 | - $references = $this->reference_processor->deserialize_references( $contents['references'] ); |
|
226 | - |
|
227 | - return $contents['jsonld']; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Set the cache with the provided results. |
|
232 | - * |
|
233 | - * The function will prepare the provided results and will ask the {@link Ttl_Cache} to cache them. |
|
234 | - * |
|
235 | - * @param int $post_id The {@link WP_Post} id. |
|
236 | - * @param array $references An array of references. |
|
237 | - * @param array $jsonld A JSON-LD structure. |
|
238 | - * |
|
239 | - * @since 3.16.0 |
|
240 | - */ |
|
241 | - private function set_cache( $post_id, $references, $jsonld ) { |
|
242 | - |
|
243 | - $this->log->trace( "Caching result for post $post_id..." ); |
|
244 | - |
|
245 | - $this->cache->put( |
|
246 | - $post_id, |
|
247 | - array( |
|
248 | - 'references' => $this->reference_processor->serialize_references( $references ), |
|
249 | - 'jsonld' => $jsonld, |
|
250 | - ) |
|
251 | - ); |
|
252 | - |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Hook to 'save_post', will invalidate the cache for that post. |
|
257 | - * |
|
258 | - * @param int $post_id The {@link WP_Post} id. |
|
259 | - * |
|
260 | - * @since 3.16.0 |
|
261 | - */ |
|
262 | - public function save_post( $post_id ) { |
|
263 | - |
|
264 | - $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
265 | - |
|
266 | - $this->cache->delete( $post_id ); |
|
267 | - |
|
268 | - $this->flush_cache_if_publisher( $post_id ); |
|
269 | - |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
274 | - * invalidate. |
|
275 | - * |
|
276 | - * @param int $id The {@link WP_Post} meta id. |
|
277 | - * @param int $post_id The {@link WP_Post} id. |
|
278 | - * @param string $meta_key The meta key. |
|
279 | - * |
|
280 | - * @since 3.16.0 |
|
281 | - */ |
|
282 | - public function changed_post_meta( $id, $post_id, $meta_key ) { |
|
283 | - |
|
284 | - if ( in_array( $meta_key, self::$ignored_meta_keys, true ) ) { |
|
285 | - $this->log->trace( "Post $post_id meta $meta_key ignored." ); |
|
286 | - |
|
287 | - return; |
|
288 | - } |
|
289 | - |
|
290 | - $this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." ); |
|
291 | - |
|
292 | - // Delete the single cache file. |
|
293 | - $this->cache->delete( $post_id ); |
|
294 | - |
|
295 | - // Flush the cache if it's the publisher. |
|
296 | - $this->flush_cache_if_publisher( $post_id ); |
|
297 | - |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Hook to WordLift's options changes, will flush the cache. |
|
302 | - * |
|
303 | - * @since 3.16.0 |
|
304 | - */ |
|
305 | - public function update_option_wl_general_settings() { |
|
306 | - $this->log->trace( 'WordLift options changed, flushing cache...' ); |
|
307 | - |
|
308 | - $this->cache->flush(); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Hook when permalinks are changed, will flush the cache. |
|
313 | - * |
|
314 | - * @since 3.17.0 |
|
315 | - */ |
|
316 | - public function permalinks_structure_changed() { |
|
317 | - $this->log->trace( 'Permalinks structure changed, flushing cache...' ); |
|
318 | - |
|
319 | - $this->cache->flush(); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
324 | - * |
|
325 | - * @param int $post_id The {@link WP_Post} id. |
|
326 | - * |
|
327 | - * @since 3.16.0 |
|
328 | - */ |
|
329 | - public function relation_changed( $post_id ) { |
|
330 | - $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
331 | - |
|
332 | - $this->cache->delete( $post_id ); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * When in Ajax, prints an http header with the information whether the |
|
337 | - * response is cached or not. |
|
338 | - * |
|
339 | - * @param int $post_id The {@link WP_Post} id. |
|
340 | - * @param bool $cache Whether the response fragment is cached. |
|
341 | - * |
|
342 | - * @since 3.16.0 |
|
343 | - */ |
|
344 | - private function add_http_header( $post_id, $cache ) { |
|
345 | - |
|
346 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) { |
|
347 | - return; |
|
348 | - } |
|
349 | - |
|
350 | - header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) ); |
|
351 | - |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Call the `flush` operation on the {@link Ttl_Cache} if |
|
356 | - * the publisher has changed. |
|
357 | - * |
|
358 | - * @param int $post_id The changed {@link WP_Post}'s id. |
|
359 | - * |
|
360 | - * @since 3.16.0 |
|
361 | - */ |
|
362 | - private function flush_cache_if_publisher( $post_id ) { |
|
363 | - |
|
364 | - // Bail out if it's not the publisher. |
|
365 | - if ( Wordlift_Configuration_Service::get_instance()->get_publisher_id() !== $post_id ) { |
|
366 | - return; |
|
367 | - } |
|
368 | - |
|
369 | - // Flush the cache, since the publisher has changed. |
|
370 | - $this->cache->flush(); |
|
371 | - |
|
372 | - } |
|
24 | + /** |
|
25 | + * A {@link Wordlift_Post_Converter} instance. |
|
26 | + * |
|
27 | + * @since 3.16.0 |
|
28 | + * |
|
29 | + * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
30 | + */ |
|
31 | + private $converter; |
|
32 | + |
|
33 | + /** |
|
34 | + * A {@link Wordlift_Log_Service} instance. |
|
35 | + * |
|
36 | + * @since 3.16.0 |
|
37 | + * |
|
38 | + * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
39 | + */ |
|
40 | + private $log; |
|
41 | + |
|
42 | + /** |
|
43 | + * A list of meta keys that do not cause the cache to update. |
|
44 | + * |
|
45 | + * @since 3.17.3 |
|
46 | + * @var array An array of ignored meta keys. |
|
47 | + */ |
|
48 | + private static $ignored_meta_keys = array( |
|
49 | + '_edit_lock', |
|
50 | + '_edit_last', |
|
51 | + '_wp_page_template', |
|
52 | + '_wp_attachment_is_custom_background', |
|
53 | + '_wp_attachment_backup_sizes', |
|
54 | + '_wp_attachment_is_custom_header', |
|
55 | + ); |
|
56 | + /** |
|
57 | + * @var Ttl_Cache |
|
58 | + */ |
|
59 | + private $cache; |
|
60 | + /** |
|
61 | + * @var Reference_Processor |
|
62 | + */ |
|
63 | + private $reference_processor; |
|
64 | + |
|
65 | + /** |
|
66 | + * Wordlift_Cached_Post_Converter constructor. |
|
67 | + * |
|
68 | + * @param \Wordlift_Post_Converter $converter The {@link Wordlift_Post_Converter} implementation. |
|
69 | + * @param Ttl_Cache $cache The {@link Ttl_Cache} cache instance. |
|
70 | + */ |
|
71 | + public function __construct( $converter, $cache ) { |
|
72 | + |
|
73 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
74 | + |
|
75 | + $this->cache = $cache; |
|
76 | + $this->converter = $converter; |
|
77 | + $this->reference_processor = Reference_Processor::get_instance(); |
|
78 | + $this->init_hooks(); |
|
79 | + |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Hooks to catch post/post meta changes in order to invalidate the cache. |
|
84 | + * |
|
85 | + * @since 3.16.0 |
|
86 | + */ |
|
87 | + private function init_hooks() { |
|
88 | + |
|
89 | + // Hook on post save to flush relevant cache. |
|
90 | + add_action( 'save_post', array( $this, 'save_post' ) ); |
|
91 | + |
|
92 | + add_action( |
|
93 | + 'added_post_meta', |
|
94 | + array( |
|
95 | + $this, |
|
96 | + 'changed_post_meta', |
|
97 | + ), |
|
98 | + 10, |
|
99 | + 3 |
|
100 | + ); |
|
101 | + add_action( |
|
102 | + 'updated_post_meta', |
|
103 | + array( |
|
104 | + $this, |
|
105 | + 'changed_post_meta', |
|
106 | + ), |
|
107 | + 10, |
|
108 | + 3 |
|
109 | + ); |
|
110 | + add_action( |
|
111 | + 'deleted_post_meta', |
|
112 | + array( |
|
113 | + $this, |
|
114 | + 'changed_post_meta', |
|
115 | + ), |
|
116 | + 10, |
|
117 | + 3 |
|
118 | + ); |
|
119 | + |
|
120 | + // Flush cache when wordlift settings were updated. |
|
121 | + add_action( |
|
122 | + 'update_option_wl_general_settings', |
|
123 | + array( |
|
124 | + $this, |
|
125 | + 'update_option_wl_general_settings', |
|
126 | + ) |
|
127 | + ); |
|
128 | + |
|
129 | + // Flushes the cache when permalink structure is changed. |
|
130 | + add_action( |
|
131 | + 'update_option_permalink_structure', |
|
132 | + array( |
|
133 | + $this, |
|
134 | + 'permalinks_structure_changed', |
|
135 | + ) |
|
136 | + ); |
|
137 | + |
|
138 | + // Invalid cache on relationship change. |
|
139 | + add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
140 | + add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
141 | + |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Note that the `&$cache` parameter here is used only to report whether the response comes from the cache. It |
|
146 | + * used by `test-issue-626.php` and nowhere else in code. |
|
147 | + * |
|
148 | + * @inheritdoc |
|
149 | + */ |
|
150 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
151 | + public function convert( $post_id, &$references = array(), &$references_infos = array(), &$cache = false ) { |
|
152 | + |
|
153 | + // Ensure post ID is `int`. Otherwise we may have issues with caching, since caching is strict about |
|
154 | + // key var types. |
|
155 | + $post_id = (int) $post_id; |
|
156 | + |
|
157 | + $this->log->trace( "Converting post $post_id..." ); |
|
158 | + |
|
159 | + // Try to get a cached result. |
|
160 | + $contents = $this->get_cache( $post_id, $references ); |
|
161 | + |
|
162 | + // Return the cached contents if any. |
|
163 | + if ( false !== $contents ) { |
|
164 | + $this->log->debug( "Cached contents found for post $post_id." ); |
|
165 | + |
|
166 | + // Inform the caller that this is cached result. |
|
167 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
168 | + $cache = true; |
|
169 | + $this->add_http_header( $post_id, true ); |
|
170 | + |
|
171 | + // Return the contents. |
|
172 | + return $contents; |
|
173 | + } |
|
174 | + |
|
175 | + // Set cached to false. |
|
176 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
177 | + $cache = false; |
|
178 | + $this->add_http_header( $post_id, false ); |
|
179 | + |
|
180 | + // Convert the post. |
|
181 | + $jsonld = $this->converter->convert( $post_id, $references, $references_infos ); |
|
182 | + |
|
183 | + /** |
|
184 | + * @since 3.32.0 |
|
185 | + * We cant apply json_encode on the objects {@link \Wordlift\Jsonld\Reference}, so we decode |
|
186 | + * it here before saving it on cache. |
|
187 | + */ |
|
188 | + // Cache the results. |
|
189 | + $this->set_cache( $post_id, $references, $jsonld ); |
|
190 | + |
|
191 | + // Finally return the JSON-LD. |
|
192 | + return $jsonld; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Try to get the cached contents. |
|
197 | + * |
|
198 | + * @param int $post_id The {@link WP_Post} id. |
|
199 | + * @param array $references The referenced posts. |
|
200 | + * |
|
201 | + * @return mixed|bool The cached contents or false if the cached isn't found. |
|
202 | + * @since 3.16.0 |
|
203 | + */ |
|
204 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
205 | + private function get_cache( $post_id, &$references = array() ) { |
|
206 | + |
|
207 | + // Ensure post ID is int, because cache is strict about var types. |
|
208 | + $post_id = (int) $post_id; |
|
209 | + |
|
210 | + $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
211 | + |
|
212 | + // Get the cache. |
|
213 | + $contents = $this->cache->get( $post_id ); |
|
214 | + |
|
215 | + // Bail out if we don't have cached contents or the cached contents are |
|
216 | + // invalid. |
|
217 | + if ( null === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
218 | + $this->log->debug( "Cached contents for post $post_id not found." ); |
|
219 | + |
|
220 | + return false; |
|
221 | + } |
|
222 | + |
|
223 | + // Remap the cache. |
|
224 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
225 | + $references = $this->reference_processor->deserialize_references( $contents['references'] ); |
|
226 | + |
|
227 | + return $contents['jsonld']; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Set the cache with the provided results. |
|
232 | + * |
|
233 | + * The function will prepare the provided results and will ask the {@link Ttl_Cache} to cache them. |
|
234 | + * |
|
235 | + * @param int $post_id The {@link WP_Post} id. |
|
236 | + * @param array $references An array of references. |
|
237 | + * @param array $jsonld A JSON-LD structure. |
|
238 | + * |
|
239 | + * @since 3.16.0 |
|
240 | + */ |
|
241 | + private function set_cache( $post_id, $references, $jsonld ) { |
|
242 | + |
|
243 | + $this->log->trace( "Caching result for post $post_id..." ); |
|
244 | + |
|
245 | + $this->cache->put( |
|
246 | + $post_id, |
|
247 | + array( |
|
248 | + 'references' => $this->reference_processor->serialize_references( $references ), |
|
249 | + 'jsonld' => $jsonld, |
|
250 | + ) |
|
251 | + ); |
|
252 | + |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Hook to 'save_post', will invalidate the cache for that post. |
|
257 | + * |
|
258 | + * @param int $post_id The {@link WP_Post} id. |
|
259 | + * |
|
260 | + * @since 3.16.0 |
|
261 | + */ |
|
262 | + public function save_post( $post_id ) { |
|
263 | + |
|
264 | + $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
265 | + |
|
266 | + $this->cache->delete( $post_id ); |
|
267 | + |
|
268 | + $this->flush_cache_if_publisher( $post_id ); |
|
269 | + |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
274 | + * invalidate. |
|
275 | + * |
|
276 | + * @param int $id The {@link WP_Post} meta id. |
|
277 | + * @param int $post_id The {@link WP_Post} id. |
|
278 | + * @param string $meta_key The meta key. |
|
279 | + * |
|
280 | + * @since 3.16.0 |
|
281 | + */ |
|
282 | + public function changed_post_meta( $id, $post_id, $meta_key ) { |
|
283 | + |
|
284 | + if ( in_array( $meta_key, self::$ignored_meta_keys, true ) ) { |
|
285 | + $this->log->trace( "Post $post_id meta $meta_key ignored." ); |
|
286 | + |
|
287 | + return; |
|
288 | + } |
|
289 | + |
|
290 | + $this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." ); |
|
291 | + |
|
292 | + // Delete the single cache file. |
|
293 | + $this->cache->delete( $post_id ); |
|
294 | + |
|
295 | + // Flush the cache if it's the publisher. |
|
296 | + $this->flush_cache_if_publisher( $post_id ); |
|
297 | + |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Hook to WordLift's options changes, will flush the cache. |
|
302 | + * |
|
303 | + * @since 3.16.0 |
|
304 | + */ |
|
305 | + public function update_option_wl_general_settings() { |
|
306 | + $this->log->trace( 'WordLift options changed, flushing cache...' ); |
|
307 | + |
|
308 | + $this->cache->flush(); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Hook when permalinks are changed, will flush the cache. |
|
313 | + * |
|
314 | + * @since 3.17.0 |
|
315 | + */ |
|
316 | + public function permalinks_structure_changed() { |
|
317 | + $this->log->trace( 'Permalinks structure changed, flushing cache...' ); |
|
318 | + |
|
319 | + $this->cache->flush(); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
324 | + * |
|
325 | + * @param int $post_id The {@link WP_Post} id. |
|
326 | + * |
|
327 | + * @since 3.16.0 |
|
328 | + */ |
|
329 | + public function relation_changed( $post_id ) { |
|
330 | + $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
331 | + |
|
332 | + $this->cache->delete( $post_id ); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * When in Ajax, prints an http header with the information whether the |
|
337 | + * response is cached or not. |
|
338 | + * |
|
339 | + * @param int $post_id The {@link WP_Post} id. |
|
340 | + * @param bool $cache Whether the response fragment is cached. |
|
341 | + * |
|
342 | + * @since 3.16.0 |
|
343 | + */ |
|
344 | + private function add_http_header( $post_id, $cache ) { |
|
345 | + |
|
346 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) { |
|
347 | + return; |
|
348 | + } |
|
349 | + |
|
350 | + header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) ); |
|
351 | + |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Call the `flush` operation on the {@link Ttl_Cache} if |
|
356 | + * the publisher has changed. |
|
357 | + * |
|
358 | + * @param int $post_id The changed {@link WP_Post}'s id. |
|
359 | + * |
|
360 | + * @since 3.16.0 |
|
361 | + */ |
|
362 | + private function flush_cache_if_publisher( $post_id ) { |
|
363 | + |
|
364 | + // Bail out if it's not the publisher. |
|
365 | + if ( Wordlift_Configuration_Service::get_instance()->get_publisher_id() !== $post_id ) { |
|
366 | + return; |
|
367 | + } |
|
368 | + |
|
369 | + // Flush the cache, since the publisher has changed. |
|
370 | + $this->cache->flush(); |
|
371 | + |
|
372 | + } |
|
373 | 373 | |
374 | 374 | } |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | * @param \Wordlift_Post_Converter $converter The {@link Wordlift_Post_Converter} implementation. |
69 | 69 | * @param Ttl_Cache $cache The {@link Ttl_Cache} cache instance. |
70 | 70 | */ |
71 | - public function __construct( $converter, $cache ) { |
|
71 | + public function __construct($converter, $cache) { |
|
72 | 72 | |
73 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
73 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
74 | 74 | |
75 | 75 | $this->cache = $cache; |
76 | 76 | $this->converter = $converter; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | private function init_hooks() { |
88 | 88 | |
89 | 89 | // Hook on post save to flush relevant cache. |
90 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
90 | + add_action('save_post', array($this, 'save_post')); |
|
91 | 91 | |
92 | 92 | add_action( |
93 | 93 | 'added_post_meta', |
@@ -136,8 +136,8 @@ discard block |
||
136 | 136 | ); |
137 | 137 | |
138 | 138 | // Invalid cache on relationship change. |
139 | - add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
140 | - add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
139 | + add_action('wl_relation_added', array($this, 'relation_changed')); |
|
140 | + add_action('wl_relation_deleted', array($this, 'relation_changed')); |
|
141 | 141 | |
142 | 142 | } |
143 | 143 | |
@@ -148,25 +148,25 @@ discard block |
||
148 | 148 | * @inheritdoc |
149 | 149 | */ |
150 | 150 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
151 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), &$cache = false ) { |
|
151 | + public function convert($post_id, &$references = array(), &$references_infos = array(), &$cache = false) { |
|
152 | 152 | |
153 | 153 | // Ensure post ID is `int`. Otherwise we may have issues with caching, since caching is strict about |
154 | 154 | // key var types. |
155 | 155 | $post_id = (int) $post_id; |
156 | 156 | |
157 | - $this->log->trace( "Converting post $post_id..." ); |
|
157 | + $this->log->trace("Converting post $post_id..."); |
|
158 | 158 | |
159 | 159 | // Try to get a cached result. |
160 | - $contents = $this->get_cache( $post_id, $references ); |
|
160 | + $contents = $this->get_cache($post_id, $references); |
|
161 | 161 | |
162 | 162 | // Return the cached contents if any. |
163 | - if ( false !== $contents ) { |
|
164 | - $this->log->debug( "Cached contents found for post $post_id." ); |
|
163 | + if (false !== $contents) { |
|
164 | + $this->log->debug("Cached contents found for post $post_id."); |
|
165 | 165 | |
166 | 166 | // Inform the caller that this is cached result. |
167 | 167 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
168 | 168 | $cache = true; |
169 | - $this->add_http_header( $post_id, true ); |
|
169 | + $this->add_http_header($post_id, true); |
|
170 | 170 | |
171 | 171 | // Return the contents. |
172 | 172 | return $contents; |
@@ -175,10 +175,10 @@ discard block |
||
175 | 175 | // Set cached to false. |
176 | 176 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
177 | 177 | $cache = false; |
178 | - $this->add_http_header( $post_id, false ); |
|
178 | + $this->add_http_header($post_id, false); |
|
179 | 179 | |
180 | 180 | // Convert the post. |
181 | - $jsonld = $this->converter->convert( $post_id, $references, $references_infos ); |
|
181 | + $jsonld = $this->converter->convert($post_id, $references, $references_infos); |
|
182 | 182 | |
183 | 183 | /** |
184 | 184 | * @since 3.32.0 |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | * it here before saving it on cache. |
187 | 187 | */ |
188 | 188 | // Cache the results. |
189 | - $this->set_cache( $post_id, $references, $jsonld ); |
|
189 | + $this->set_cache($post_id, $references, $jsonld); |
|
190 | 190 | |
191 | 191 | // Finally return the JSON-LD. |
192 | 192 | return $jsonld; |
@@ -202,27 +202,27 @@ discard block |
||
202 | 202 | * @since 3.16.0 |
203 | 203 | */ |
204 | 204 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
205 | - private function get_cache( $post_id, &$references = array() ) { |
|
205 | + private function get_cache($post_id, &$references = array()) { |
|
206 | 206 | |
207 | 207 | // Ensure post ID is int, because cache is strict about var types. |
208 | 208 | $post_id = (int) $post_id; |
209 | 209 | |
210 | - $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
210 | + $this->log->trace("Getting cached contents for post $post_id..."); |
|
211 | 211 | |
212 | 212 | // Get the cache. |
213 | - $contents = $this->cache->get( $post_id ); |
|
213 | + $contents = $this->cache->get($post_id); |
|
214 | 214 | |
215 | 215 | // Bail out if we don't have cached contents or the cached contents are |
216 | 216 | // invalid. |
217 | - if ( null === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
218 | - $this->log->debug( "Cached contents for post $post_id not found." ); |
|
217 | + if (null === $contents || ! isset($contents['jsonld']) || ! isset($contents['references'])) { |
|
218 | + $this->log->debug("Cached contents for post $post_id not found."); |
|
219 | 219 | |
220 | 220 | return false; |
221 | 221 | } |
222 | 222 | |
223 | 223 | // Remap the cache. |
224 | 224 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
225 | - $references = $this->reference_processor->deserialize_references( $contents['references'] ); |
|
225 | + $references = $this->reference_processor->deserialize_references($contents['references']); |
|
226 | 226 | |
227 | 227 | return $contents['jsonld']; |
228 | 228 | } |
@@ -238,14 +238,14 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @since 3.16.0 |
240 | 240 | */ |
241 | - private function set_cache( $post_id, $references, $jsonld ) { |
|
241 | + private function set_cache($post_id, $references, $jsonld) { |
|
242 | 242 | |
243 | - $this->log->trace( "Caching result for post $post_id..." ); |
|
243 | + $this->log->trace("Caching result for post $post_id..."); |
|
244 | 244 | |
245 | 245 | $this->cache->put( |
246 | 246 | $post_id, |
247 | 247 | array( |
248 | - 'references' => $this->reference_processor->serialize_references( $references ), |
|
248 | + 'references' => $this->reference_processor->serialize_references($references), |
|
249 | 249 | 'jsonld' => $jsonld, |
250 | 250 | ) |
251 | 251 | ); |
@@ -259,13 +259,13 @@ discard block |
||
259 | 259 | * |
260 | 260 | * @since 3.16.0 |
261 | 261 | */ |
262 | - public function save_post( $post_id ) { |
|
262 | + public function save_post($post_id) { |
|
263 | 263 | |
264 | - $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
264 | + $this->log->trace("Post $post_id saved, invalidating cache..."); |
|
265 | 265 | |
266 | - $this->cache->delete( $post_id ); |
|
266 | + $this->cache->delete($post_id); |
|
267 | 267 | |
268 | - $this->flush_cache_if_publisher( $post_id ); |
|
268 | + $this->flush_cache_if_publisher($post_id); |
|
269 | 269 | |
270 | 270 | } |
271 | 271 | |
@@ -279,21 +279,21 @@ discard block |
||
279 | 279 | * |
280 | 280 | * @since 3.16.0 |
281 | 281 | */ |
282 | - public function changed_post_meta( $id, $post_id, $meta_key ) { |
|
282 | + public function changed_post_meta($id, $post_id, $meta_key) { |
|
283 | 283 | |
284 | - if ( in_array( $meta_key, self::$ignored_meta_keys, true ) ) { |
|
285 | - $this->log->trace( "Post $post_id meta $meta_key ignored." ); |
|
284 | + if (in_array($meta_key, self::$ignored_meta_keys, true)) { |
|
285 | + $this->log->trace("Post $post_id meta $meta_key ignored."); |
|
286 | 286 | |
287 | 287 | return; |
288 | 288 | } |
289 | 289 | |
290 | - $this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." ); |
|
290 | + $this->log->trace("Post $post_id meta $meta_key changed, invalidating cache..."); |
|
291 | 291 | |
292 | 292 | // Delete the single cache file. |
293 | - $this->cache->delete( $post_id ); |
|
293 | + $this->cache->delete($post_id); |
|
294 | 294 | |
295 | 295 | // Flush the cache if it's the publisher. |
296 | - $this->flush_cache_if_publisher( $post_id ); |
|
296 | + $this->flush_cache_if_publisher($post_id); |
|
297 | 297 | |
298 | 298 | } |
299 | 299 | |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | * @since 3.16.0 |
304 | 304 | */ |
305 | 305 | public function update_option_wl_general_settings() { |
306 | - $this->log->trace( 'WordLift options changed, flushing cache...' ); |
|
306 | + $this->log->trace('WordLift options changed, flushing cache...'); |
|
307 | 307 | |
308 | 308 | $this->cache->flush(); |
309 | 309 | } |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | * @since 3.17.0 |
315 | 315 | */ |
316 | 316 | public function permalinks_structure_changed() { |
317 | - $this->log->trace( 'Permalinks structure changed, flushing cache...' ); |
|
317 | + $this->log->trace('Permalinks structure changed, flushing cache...'); |
|
318 | 318 | |
319 | 319 | $this->cache->flush(); |
320 | 320 | } |
@@ -326,10 +326,10 @@ discard block |
||
326 | 326 | * |
327 | 327 | * @since 3.16.0 |
328 | 328 | */ |
329 | - public function relation_changed( $post_id ) { |
|
330 | - $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
329 | + public function relation_changed($post_id) { |
|
330 | + $this->log->trace("Post $post_id relations changed, invalidating cache..."); |
|
331 | 331 | |
332 | - $this->cache->delete( $post_id ); |
|
332 | + $this->cache->delete($post_id); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | /** |
@@ -341,13 +341,13 @@ discard block |
||
341 | 341 | * |
342 | 342 | * @since 3.16.0 |
343 | 343 | */ |
344 | - private function add_http_header( $post_id, $cache ) { |
|
344 | + private function add_http_header($post_id, $cache) { |
|
345 | 345 | |
346 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) { |
|
346 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || headers_sent()) { |
|
347 | 347 | return; |
348 | 348 | } |
349 | 349 | |
350 | - header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) ); |
|
350 | + header("X-WordLift-JsonLd-Cache-$post_id: ".($cache ? 'HIT' : 'MISS')); |
|
351 | 351 | |
352 | 352 | } |
353 | 353 | |
@@ -359,10 +359,10 @@ discard block |
||
359 | 359 | * |
360 | 360 | * @since 3.16.0 |
361 | 361 | */ |
362 | - private function flush_cache_if_publisher( $post_id ) { |
|
362 | + private function flush_cache_if_publisher($post_id) { |
|
363 | 363 | |
364 | 364 | // Bail out if it's not the publisher. |
365 | - if ( Wordlift_Configuration_Service::get_instance()->get_publisher_id() !== $post_id ) { |
|
365 | + if (Wordlift_Configuration_Service::get_instance()->get_publisher_id() !== $post_id) { |
|
366 | 366 | return; |
367 | 367 | } |
368 | 368 |
@@ -16,185 +16,185 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Http_Api { |
18 | 18 | |
19 | - /** |
|
20 | - * A {@link Wordlift_Log_Service} instance. |
|
21 | - * |
|
22 | - * @since 3.15.3 |
|
23 | - * |
|
24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25 | - */ |
|
26 | - private $log; |
|
27 | - |
|
28 | - /** |
|
29 | - * Create a {@link Wordlift_End_Point} instance. |
|
30 | - * |
|
31 | - * @since 3.15.3 |
|
32 | - */ |
|
33 | - public function __construct() { |
|
34 | - |
|
35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36 | - |
|
37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39 | - |
|
40 | - // region SAMPLE ACTIONS. |
|
41 | - add_action( |
|
42 | - 'admin_post_wl_hello_world', |
|
43 | - array( |
|
44 | - $this, |
|
45 | - 'hello_world', |
|
46 | - ) |
|
47 | - ); |
|
48 | - add_action( |
|
49 | - 'admin_post_nopriv_wl_hello_world', |
|
50 | - array( |
|
51 | - $this, |
|
52 | - 'nopriv_hello_world', |
|
53 | - ) |
|
54 | - ); |
|
55 | - // endregion |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Add the `wl-api` rewrite end-point. |
|
60 | - * |
|
61 | - * @since 3.15.3 |
|
62 | - */ |
|
63 | - public function add_rewrite_endpoint() { |
|
64 | - |
|
65 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
66 | - $this->ensure_rewrite_rules_are_flushed(); |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Handle `template_redirect` hooks. |
|
72 | - * |
|
73 | - * @since 3.15.3 |
|
74 | - */ |
|
75 | - public function template_redirect() { |
|
76 | - |
|
77 | - global $wp_query; |
|
78 | - |
|
79 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
81 | - |
|
82 | - return; |
|
83 | - } |
|
84 | - |
|
85 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | - $this->do_action( $action ); |
|
87 | - |
|
88 | - exit; |
|
89 | - |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Do the requested action. |
|
94 | - * |
|
95 | - * @param string $action The action to execute. |
|
96 | - * |
|
97 | - * @since 3.15.3 |
|
98 | - */ |
|
99 | - private function do_action( $action ) { |
|
100 | - |
|
101 | - if ( empty( $action ) ) { |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
106 | - /** |
|
107 | - * Fires on a non-authenticated admin post request for the given action. |
|
108 | - * |
|
109 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
110 | - * request action. |
|
111 | - * |
|
112 | - * @since 2.6.0 |
|
113 | - */ |
|
114 | - do_action( "admin_post_nopriv_{$action}" ); |
|
115 | - } else { |
|
116 | - /** |
|
117 | - * Fires on an authenticated admin post request for the given action. |
|
118 | - * |
|
119 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
120 | - * request action. |
|
121 | - * |
|
122 | - * @since 2.6.0 |
|
123 | - */ |
|
124 | - do_action( "admin_post_{$action}" ); |
|
125 | - } |
|
126 | - |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Test function, anonymous. |
|
131 | - * |
|
132 | - * @since 3.15.3 |
|
133 | - */ |
|
134 | - public function nopriv_hello_world() { |
|
135 | - |
|
136 | - wp_die( 'Hello World! (from anonymous)' ); |
|
137 | - |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Test function, authenticated. |
|
142 | - * |
|
143 | - * @since 3.15.3 |
|
144 | - */ |
|
145 | - public function hello_world() { |
|
146 | - |
|
147 | - wp_die( 'Hello World! (from authenticated)' ); |
|
148 | - |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Ensure that the rewrite rules are flushed the first time. |
|
153 | - * |
|
154 | - * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
155 | - * @since 3.15.3 |
|
156 | - */ |
|
157 | - public static function ensure_rewrite_rules_are_flushed() { |
|
158 | - |
|
159 | - // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
160 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | - update_option( 'wl_http_api', 'yes' ); |
|
162 | - add_action( |
|
163 | - 'wp_loaded', |
|
164 | - function () { |
|
165 | - flush_rewrite_rules(); |
|
166 | - } |
|
167 | - ); |
|
168 | - } |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
174 | - * reinitialize the `wl-api` route. |
|
175 | - * |
|
176 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
177 | - * |
|
178 | - * @since 3.19.2 |
|
179 | - */ |
|
180 | - public static function activate() { |
|
181 | - |
|
182 | - // Force the plugin to reinitialize the rewrite rules. |
|
183 | - update_option( 'wl_http_api', 'no' ); |
|
184 | - |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Delete the option when the plugin is deactivated. |
|
189 | - * |
|
190 | - * @since 3.19.4 |
|
191 | - * |
|
192 | - * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
193 | - */ |
|
194 | - public static function deactivate() { |
|
195 | - |
|
196 | - delete_option( 'wl_http_api' ); |
|
197 | - |
|
198 | - } |
|
19 | + /** |
|
20 | + * A {@link Wordlift_Log_Service} instance. |
|
21 | + * |
|
22 | + * @since 3.15.3 |
|
23 | + * |
|
24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25 | + */ |
|
26 | + private $log; |
|
27 | + |
|
28 | + /** |
|
29 | + * Create a {@link Wordlift_End_Point} instance. |
|
30 | + * |
|
31 | + * @since 3.15.3 |
|
32 | + */ |
|
33 | + public function __construct() { |
|
34 | + |
|
35 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36 | + |
|
37 | + add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39 | + |
|
40 | + // region SAMPLE ACTIONS. |
|
41 | + add_action( |
|
42 | + 'admin_post_wl_hello_world', |
|
43 | + array( |
|
44 | + $this, |
|
45 | + 'hello_world', |
|
46 | + ) |
|
47 | + ); |
|
48 | + add_action( |
|
49 | + 'admin_post_nopriv_wl_hello_world', |
|
50 | + array( |
|
51 | + $this, |
|
52 | + 'nopriv_hello_world', |
|
53 | + ) |
|
54 | + ); |
|
55 | + // endregion |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Add the `wl-api` rewrite end-point. |
|
60 | + * |
|
61 | + * @since 3.15.3 |
|
62 | + */ |
|
63 | + public function add_rewrite_endpoint() { |
|
64 | + |
|
65 | + add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
66 | + $this->ensure_rewrite_rules_are_flushed(); |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Handle `template_redirect` hooks. |
|
72 | + * |
|
73 | + * @since 3.15.3 |
|
74 | + */ |
|
75 | + public function template_redirect() { |
|
76 | + |
|
77 | + global $wp_query; |
|
78 | + |
|
79 | + if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | + $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
81 | + |
|
82 | + return; |
|
83 | + } |
|
84 | + |
|
85 | + $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | + $this->do_action( $action ); |
|
87 | + |
|
88 | + exit; |
|
89 | + |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Do the requested action. |
|
94 | + * |
|
95 | + * @param string $action The action to execute. |
|
96 | + * |
|
97 | + * @since 3.15.3 |
|
98 | + */ |
|
99 | + private function do_action( $action ) { |
|
100 | + |
|
101 | + if ( empty( $action ) ) { |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
106 | + /** |
|
107 | + * Fires on a non-authenticated admin post request for the given action. |
|
108 | + * |
|
109 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
110 | + * request action. |
|
111 | + * |
|
112 | + * @since 2.6.0 |
|
113 | + */ |
|
114 | + do_action( "admin_post_nopriv_{$action}" ); |
|
115 | + } else { |
|
116 | + /** |
|
117 | + * Fires on an authenticated admin post request for the given action. |
|
118 | + * |
|
119 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
120 | + * request action. |
|
121 | + * |
|
122 | + * @since 2.6.0 |
|
123 | + */ |
|
124 | + do_action( "admin_post_{$action}" ); |
|
125 | + } |
|
126 | + |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Test function, anonymous. |
|
131 | + * |
|
132 | + * @since 3.15.3 |
|
133 | + */ |
|
134 | + public function nopriv_hello_world() { |
|
135 | + |
|
136 | + wp_die( 'Hello World! (from anonymous)' ); |
|
137 | + |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Test function, authenticated. |
|
142 | + * |
|
143 | + * @since 3.15.3 |
|
144 | + */ |
|
145 | + public function hello_world() { |
|
146 | + |
|
147 | + wp_die( 'Hello World! (from authenticated)' ); |
|
148 | + |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Ensure that the rewrite rules are flushed the first time. |
|
153 | + * |
|
154 | + * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
155 | + * @since 3.15.3 |
|
156 | + */ |
|
157 | + public static function ensure_rewrite_rules_are_flushed() { |
|
158 | + |
|
159 | + // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
160 | + if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | + update_option( 'wl_http_api', 'yes' ); |
|
162 | + add_action( |
|
163 | + 'wp_loaded', |
|
164 | + function () { |
|
165 | + flush_rewrite_rules(); |
|
166 | + } |
|
167 | + ); |
|
168 | + } |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
174 | + * reinitialize the `wl-api` route. |
|
175 | + * |
|
176 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
177 | + * |
|
178 | + * @since 3.19.2 |
|
179 | + */ |
|
180 | + public static function activate() { |
|
181 | + |
|
182 | + // Force the plugin to reinitialize the rewrite rules. |
|
183 | + update_option( 'wl_http_api', 'no' ); |
|
184 | + |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Delete the option when the plugin is deactivated. |
|
189 | + * |
|
190 | + * @since 3.19.4 |
|
191 | + * |
|
192 | + * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
193 | + */ |
|
194 | + public static function deactivate() { |
|
195 | + |
|
196 | + delete_option( 'wl_http_api' ); |
|
197 | + |
|
198 | + } |
|
199 | 199 | |
200 | 200 | } |
@@ -32,10 +32,10 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public function __construct() { |
34 | 34 | |
35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
35 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
36 | 36 | |
37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
37 | + add_action('init', array($this, 'add_rewrite_endpoint')); |
|
38 | + add_action('template_redirect', array($this, 'template_redirect')); |
|
39 | 39 | |
40 | 40 | // region SAMPLE ACTIONS. |
41 | 41 | add_action( |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function add_rewrite_endpoint() { |
64 | 64 | |
65 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
65 | + add_rewrite_endpoint('wl-api', EP_ROOT); |
|
66 | 66 | $this->ensure_rewrite_rules_are_flushed(); |
67 | 67 | |
68 | 68 | } |
@@ -76,14 +76,14 @@ discard block |
||
76 | 76 | |
77 | 77 | global $wp_query; |
78 | 78 | |
79 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
79 | + if ( ! isset($wp_query->query_vars['wl-api'])) { |
|
80 | + $this->log->trace('Skipping, not a `wl-api` call.'); |
|
81 | 81 | |
82 | 82 | return; |
83 | 83 | } |
84 | 84 | |
85 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | - $this->do_action( $action ); |
|
85 | + $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | + $this->do_action($action); |
|
87 | 87 | |
88 | 88 | exit; |
89 | 89 | |
@@ -96,13 +96,13 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @since 3.15.3 |
98 | 98 | */ |
99 | - private function do_action( $action ) { |
|
99 | + private function do_action($action) { |
|
100 | 100 | |
101 | - if ( empty( $action ) ) { |
|
101 | + if (empty($action)) { |
|
102 | 102 | return; |
103 | 103 | } |
104 | 104 | |
105 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
105 | + if ( ! wp_validate_auth_cookie('', 'logged_in')) { |
|
106 | 106 | /** |
107 | 107 | * Fires on a non-authenticated admin post request for the given action. |
108 | 108 | * |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @since 2.6.0 |
113 | 113 | */ |
114 | - do_action( "admin_post_nopriv_{$action}" ); |
|
114 | + do_action("admin_post_nopriv_{$action}"); |
|
115 | 115 | } else { |
116 | 116 | /** |
117 | 117 | * Fires on an authenticated admin post request for the given action. |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * |
122 | 122 | * @since 2.6.0 |
123 | 123 | */ |
124 | - do_action( "admin_post_{$action}" ); |
|
124 | + do_action("admin_post_{$action}"); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function nopriv_hello_world() { |
135 | 135 | |
136 | - wp_die( 'Hello World! (from anonymous)' ); |
|
136 | + wp_die('Hello World! (from anonymous)'); |
|
137 | 137 | |
138 | 138 | } |
139 | 139 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | */ |
145 | 145 | public function hello_world() { |
146 | 146 | |
147 | - wp_die( 'Hello World! (from authenticated)' ); |
|
147 | + wp_die('Hello World! (from authenticated)'); |
|
148 | 148 | |
149 | 149 | } |
150 | 150 | |
@@ -157,11 +157,11 @@ discard block |
||
157 | 157 | public static function ensure_rewrite_rules_are_flushed() { |
158 | 158 | |
159 | 159 | // See https://github.com/insideout10/wordlift-plugin/issues/698. |
160 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | - update_option( 'wl_http_api', 'yes' ); |
|
160 | + if ('yes' !== get_option('wl_http_api')) { |
|
161 | + update_option('wl_http_api', 'yes'); |
|
162 | 162 | add_action( |
163 | 163 | 'wp_loaded', |
164 | - function () { |
|
164 | + function() { |
|
165 | 165 | flush_rewrite_rules(); |
166 | 166 | } |
167 | 167 | ); |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | public static function activate() { |
181 | 181 | |
182 | 182 | // Force the plugin to reinitialize the rewrite rules. |
183 | - update_option( 'wl_http_api', 'no' ); |
|
183 | + update_option('wl_http_api', 'no'); |
|
184 | 184 | |
185 | 185 | } |
186 | 186 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | public static function deactivate() { |
195 | 195 | |
196 | - delete_option( 'wl_http_api' ); |
|
196 | + delete_option('wl_http_api'); |
|
197 | 197 | |
198 | 198 | } |
199 | 199 |
@@ -8,80 +8,80 @@ |
||
8 | 8 | |
9 | 9 | class Wordlift_Mapping_Ajax_Adapter { |
10 | 10 | |
11 | - /** |
|
12 | - * The {@link Wordlift_Mapping_Service} instance. |
|
13 | - * |
|
14 | - * @since 3.20.0 |
|
15 | - * @access private |
|
16 | - * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
17 | - */ |
|
18 | - private $mapping_service; |
|
11 | + /** |
|
12 | + * The {@link Wordlift_Mapping_Service} instance. |
|
13 | + * |
|
14 | + * @since 3.20.0 |
|
15 | + * @access private |
|
16 | + * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
17 | + */ |
|
18 | + private $mapping_service; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
22 | - * |
|
23 | - * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
24 | - * |
|
25 | - * @since 3.20.0 |
|
26 | - */ |
|
27 | - public function __construct( $mapping_service ) { |
|
20 | + /** |
|
21 | + * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
22 | + * |
|
23 | + * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
24 | + * |
|
25 | + * @since 3.20.0 |
|
26 | + */ |
|
27 | + public function __construct( $mapping_service ) { |
|
28 | 28 | |
29 | - $this->mapping_service = $mapping_service; |
|
29 | + $this->mapping_service = $mapping_service; |
|
30 | 30 | |
31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
31 | + add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | + add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
33 | 33 | |
34 | - } |
|
34 | + } |
|
35 | 35 | |
36 | - public function set_entity_types_for_post_type() { |
|
36 | + public function set_entity_types_for_post_type() { |
|
37 | 37 | |
38 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | - return; |
|
40 | - } |
|
38 | + if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | + return; |
|
40 | + } |
|
41 | 41 | |
42 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | - $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
42 | + $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | + $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
44 | 44 | |
45 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
45 | + $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
46 | 46 | |
47 | - wp_send_json_success(); |
|
47 | + wp_send_json_success(); |
|
48 | 48 | |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - public function update_post_type_entity_types() { |
|
51 | + public function update_post_type_entity_types() { |
|
52 | 52 | |
53 | - // If the nonce is invalid, return an error. |
|
54 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
57 | - } |
|
53 | + // If the nonce is invalid, return an error. |
|
54 | + $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | + if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
57 | + } |
|
58 | 58 | |
59 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
61 | - } |
|
59 | + if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | + wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
61 | + } |
|
62 | 62 | |
63 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
65 | - } |
|
63 | + if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | + wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
65 | + } |
|
66 | 66 | |
67 | - // Get the post type. |
|
68 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
67 | + // Get the post type. |
|
68 | + $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
69 | 69 | |
70 | - // Get the entity types URIs. |
|
71 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
70 | + // Get the entity types URIs. |
|
71 | + $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
72 | 72 | |
73 | - // Get the offset. |
|
74 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
73 | + // Get the offset. |
|
74 | + $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
75 | 75 | |
76 | - // Update and get the results. |
|
77 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
76 | + // Update and get the results. |
|
77 | + $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
78 | 78 | |
79 | - // Add our nonce to the result. |
|
80 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
79 | + // Add our nonce to the result. |
|
80 | + $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
81 | 81 | |
82 | - // Finally send the results. |
|
83 | - wp_send_json_success( $result ); |
|
82 | + // Finally send the results. |
|
83 | + wp_send_json_success( $result ); |
|
84 | 84 | |
85 | - } |
|
85 | + } |
|
86 | 86 | |
87 | 87 | } |
@@ -24,25 +24,25 @@ discard block |
||
24 | 24 | * |
25 | 25 | * @since 3.20.0 |
26 | 26 | */ |
27 | - public function __construct( $mapping_service ) { |
|
27 | + public function __construct($mapping_service) { |
|
28 | 28 | |
29 | 29 | $this->mapping_service = $mapping_service; |
30 | 30 | |
31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
31 | + add_action('wp_ajax_wl_set_entity_types_for_post_type', array($this, 'set_entity_types_for_post_type')); |
|
32 | + add_action('wp_ajax_wl_update_post_type_entity_types', array($this, 'update_post_type_entity_types')); |
|
33 | 33 | |
34 | 34 | } |
35 | 35 | |
36 | 36 | public function set_entity_types_for_post_type() { |
37 | 37 | |
38 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
38 | + if ( ! isset($_REQUEST['post_type']) || ! isset($_REQUEST['entity_types'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | - $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
42 | + $post_type = sanitize_text_field(wp_unslash($_REQUEST['post_type'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | + $entity_types = array_map('sanitize_text_field', wp_unslash((array) $_REQUEST['entity_types'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
44 | 44 | |
45 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
45 | + $this->mapping_service->set_entity_types_for_post_type($post_type, $entity_types); |
|
46 | 46 | |
47 | 47 | wp_send_json_success(); |
48 | 48 | |
@@ -51,36 +51,36 @@ discard block |
||
51 | 51 | public function update_post_type_entity_types() { |
52 | 52 | |
53 | 53 | // If the nonce is invalid, return an error. |
54 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
54 | + $nonce = isset($_REQUEST['_nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_nonce'])) : ''; |
|
55 | + if ( ! wp_verify_nonce($nonce, 'update_post_type_entity_types')) { |
|
56 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
57 | 57 | } |
58 | 58 | |
59 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
59 | + if (empty($_REQUEST['post_type'])) { |
|
60 | + wp_send_json_error(__('`post_type` is required', 'wordlift')); |
|
61 | 61 | } |
62 | 62 | |
63 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
63 | + if (empty($_REQUEST['entity_types'])) { |
|
64 | + wp_send_json_error(__('`entity_types` is required', 'wordlift')); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | // Get the post type. |
68 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
68 | + $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field(wp_unslash($_REQUEST['post_type'])) : ''; |
|
69 | 69 | |
70 | 70 | // Get the entity types URIs. |
71 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
71 | + $entity_types = isset($_REQUEST['entity_types']) ? filter_var(wp_unslash($_REQUEST['entity_types']), FILTER_REQUIRE_ARRAY) : array(); |
|
72 | 72 | |
73 | 73 | // Get the offset. |
74 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
74 | + $offset = isset($_REQUEST['offset']) ? intval($_REQUEST['offset']) : 0; |
|
75 | 75 | |
76 | 76 | // Update and get the results. |
77 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
77 | + $result = $this->mapping_service->update($post_type, $entity_types, $offset); |
|
78 | 78 | |
79 | 79 | // Add our nonce to the result. |
80 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
80 | + $result['_nonce'] = wp_create_nonce('update_post_type_entity_types'); |
|
81 | 81 | |
82 | 82 | // Finally send the results. |
83 | - wp_send_json_success( $result ); |
|
83 | + wp_send_json_success($result); |
|
84 | 84 | |
85 | 85 | } |
86 | 86 |