@@ -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 |
@@ -21,500 +21,500 @@ discard block |
||
21 | 21 | */ |
22 | 22 | class Wordlift_Entity_Service { |
23 | 23 | |
24 | - /** |
|
25 | - * The Log service. |
|
26 | - * |
|
27 | - * @since 3.2.0 |
|
28 | - * @access private |
|
29 | - * @var \Wordlift_Log_Service $log The Log service. |
|
30 | - */ |
|
31 | - private $log; |
|
32 | - |
|
33 | - /** |
|
34 | - * The {@link Wordlift_Relation_Service} instance. |
|
35 | - * |
|
36 | - * @since 3.15.0 |
|
37 | - * @access private |
|
38 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
39 | - */ |
|
40 | - private $relation_service; |
|
41 | - |
|
42 | - /** |
|
43 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
44 | - * |
|
45 | - * @since 3.16.3 |
|
46 | - * @access private |
|
47 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
48 | - */ |
|
49 | - private $entity_uri_service; |
|
50 | - |
|
51 | - /** |
|
52 | - * The entity post type name. |
|
53 | - * |
|
54 | - * @since 3.1.0 |
|
55 | - */ |
|
56 | - const TYPE_NAME = 'entity'; |
|
57 | - |
|
58 | - /** |
|
59 | - * The alternative label meta key. |
|
60 | - * |
|
61 | - * @since 3.2.0 |
|
62 | - */ |
|
63 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
64 | - |
|
65 | - /** |
|
66 | - * The alternative label input template. |
|
67 | - * |
|
68 | - * @since 3.2.0 |
|
69 | - */ |
|
70 | - // TODO: this should be moved to a class that deals with HTML code. |
|
71 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
24 | + /** |
|
25 | + * The Log service. |
|
26 | + * |
|
27 | + * @since 3.2.0 |
|
28 | + * @access private |
|
29 | + * @var \Wordlift_Log_Service $log The Log service. |
|
30 | + */ |
|
31 | + private $log; |
|
32 | + |
|
33 | + /** |
|
34 | + * The {@link Wordlift_Relation_Service} instance. |
|
35 | + * |
|
36 | + * @since 3.15.0 |
|
37 | + * @access private |
|
38 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
39 | + */ |
|
40 | + private $relation_service; |
|
41 | + |
|
42 | + /** |
|
43 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
44 | + * |
|
45 | + * @since 3.16.3 |
|
46 | + * @access private |
|
47 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
48 | + */ |
|
49 | + private $entity_uri_service; |
|
50 | + |
|
51 | + /** |
|
52 | + * The entity post type name. |
|
53 | + * |
|
54 | + * @since 3.1.0 |
|
55 | + */ |
|
56 | + const TYPE_NAME = 'entity'; |
|
57 | + |
|
58 | + /** |
|
59 | + * The alternative label meta key. |
|
60 | + * |
|
61 | + * @since 3.2.0 |
|
62 | + */ |
|
63 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
64 | + |
|
65 | + /** |
|
66 | + * The alternative label input template. |
|
67 | + * |
|
68 | + * @since 3.2.0 |
|
69 | + */ |
|
70 | + // TODO: this should be moved to a class that deals with HTML code. |
|
71 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
72 | 72 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
73 | 73 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
74 | 74 | <button class="button wl-delete-button">%s</button> |
75 | 75 | </div>'; |
76 | 76 | |
77 | - /** |
|
78 | - * Create a Wordlift_Entity_Service instance. |
|
79 | - * |
|
80 | - * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
81 | - * @since 3.2.0 |
|
82 | - */ |
|
83 | - protected function __construct() { |
|
84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
85 | - |
|
86 | - $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
87 | - $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
88 | - |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * A singleton instance of the Entity service. |
|
93 | - * |
|
94 | - * @since 3.2.0 |
|
95 | - * @access private |
|
96 | - * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
97 | - */ |
|
98 | - private static $instance = null; |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the singleton instance of the Entity service. |
|
102 | - * |
|
103 | - * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
104 | - * @since 3.2.0 |
|
105 | - */ |
|
106 | - public static function get_instance() { |
|
107 | - |
|
108 | - if ( ! isset( self::$instance ) ) { |
|
109 | - self::$instance = new self(); |
|
110 | - } |
|
111 | - |
|
112 | - return self::$instance; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Determines whether a post is an entity or not. Entity is in this context |
|
117 | - * something which is not an article. |
|
118 | - * |
|
119 | - * @param int $post_id A post id. |
|
120 | - * |
|
121 | - * @return bool Return true if the post is an entity otherwise false. |
|
122 | - * @since 3.1.0 |
|
123 | - */ |
|
124 | - public function is_entity( $post_id ) { |
|
125 | - |
|
126 | - // Improve performance by giving for granted that a product is an entity. |
|
127 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
128 | - return true; |
|
129 | - } |
|
130 | - |
|
131 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
132 | - |
|
133 | - if ( is_wp_error( $terms ) ) { |
|
134 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
135 | - |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - if ( empty( $terms ) ) { |
|
140 | - return false; |
|
141 | - } |
|
142 | - |
|
143 | - /* |
|
77 | + /** |
|
78 | + * Create a Wordlift_Entity_Service instance. |
|
79 | + * |
|
80 | + * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
81 | + * @since 3.2.0 |
|
82 | + */ |
|
83 | + protected function __construct() { |
|
84 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
85 | + |
|
86 | + $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
87 | + $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
88 | + |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * A singleton instance of the Entity service. |
|
93 | + * |
|
94 | + * @since 3.2.0 |
|
95 | + * @access private |
|
96 | + * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
97 | + */ |
|
98 | + private static $instance = null; |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the singleton instance of the Entity service. |
|
102 | + * |
|
103 | + * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
104 | + * @since 3.2.0 |
|
105 | + */ |
|
106 | + public static function get_instance() { |
|
107 | + |
|
108 | + if ( ! isset( self::$instance ) ) { |
|
109 | + self::$instance = new self(); |
|
110 | + } |
|
111 | + |
|
112 | + return self::$instance; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Determines whether a post is an entity or not. Entity is in this context |
|
117 | + * something which is not an article. |
|
118 | + * |
|
119 | + * @param int $post_id A post id. |
|
120 | + * |
|
121 | + * @return bool Return true if the post is an entity otherwise false. |
|
122 | + * @since 3.1.0 |
|
123 | + */ |
|
124 | + public function is_entity( $post_id ) { |
|
125 | + |
|
126 | + // Improve performance by giving for granted that a product is an entity. |
|
127 | + if ( 'product' === get_post_type( $post_id ) ) { |
|
128 | + return true; |
|
129 | + } |
|
130 | + |
|
131 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
132 | + |
|
133 | + if ( is_wp_error( $terms ) ) { |
|
134 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
135 | + |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + if ( empty( $terms ) ) { |
|
140 | + return false; |
|
141 | + } |
|
142 | + |
|
143 | + /* |
|
144 | 144 | * We don't consider an `article` to be an entity. |
145 | 145 | * |
146 | 146 | * @since 3.20.0 At least one associated mustn't be an `article`. |
147 | 147 | * |
148 | 148 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
149 | 149 | */ |
150 | - foreach ( $terms as $term ) { |
|
151 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
152 | - return true; |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Get the proper classification scope for a given entity post |
|
161 | - * |
|
162 | - * @param integer $post_id An entity post id. |
|
163 | - * |
|
164 | - * @param string $default The default classification scope, `what` if not |
|
165 | - * provided. |
|
166 | - * |
|
167 | - * @return string Returns a classification scope (e.g. 'what'). |
|
168 | - * @since 3.5.0 |
|
169 | - */ |
|
170 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
171 | - |
|
172 | - if ( false === $this->is_entity( $post_id ) ) { |
|
173 | - return $default; |
|
174 | - } |
|
175 | - |
|
176 | - // Retrieve the entity type |
|
177 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
178 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
179 | - // Retrieve classification boxes configuration |
|
180 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
181 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
182 | - foreach ( $classification_boxes as $cb ) { |
|
183 | - if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
184 | - return $cb['id']; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - return $default; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Check whether a {@link WP_Post} is used. |
|
193 | - * |
|
194 | - * @param int $post_id The {@link WP_Post}'s id. |
|
195 | - * |
|
196 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
197 | - */ |
|
198 | - public function is_used( $post_id ) { |
|
199 | - |
|
200 | - if ( false === $this->is_entity( $post_id ) ) { |
|
201 | - return null; |
|
202 | - } |
|
203 | - // Retrieve the post |
|
204 | - $entity = get_post( $post_id ); |
|
205 | - |
|
206 | - global $wpdb; |
|
207 | - // Retrieve Wordlift relation instances table name |
|
208 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
209 | - |
|
210 | - // Perform the query |
|
211 | - $relation_instances = (int) $wpdb->get_var( |
|
212 | - $wpdb->prepare( |
|
213 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
214 | - $entity->ID |
|
215 | - ) |
|
216 | - ); |
|
217 | - // If there is at least one relation instance for the current entity, then it's used |
|
218 | - if ( 0 < $relation_instances ) { |
|
219 | - return true; |
|
220 | - } |
|
221 | - |
|
222 | - // Perform the query |
|
223 | - $meta_instances = (int) $wpdb->get_var( |
|
224 | - $wpdb->prepare( |
|
225 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
226 | - $entity->ID, |
|
227 | - wl_get_entity_uri( $entity->ID ) |
|
228 | - ) |
|
229 | - ); |
|
230 | - |
|
231 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
232 | - if ( 0 < $meta_instances ) { |
|
233 | - return true; |
|
234 | - } |
|
235 | - |
|
236 | - // If we are here, it means the current entity is not used at the moment |
|
237 | - return false; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
242 | - * |
|
243 | - * @param string $uri The entity URI. |
|
244 | - * |
|
245 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
246 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
247 | - * |
|
248 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
249 | - * @since 3.2.0 |
|
250 | - */ |
|
251 | - public function get_entity_post_by_uri( $uri ) { |
|
252 | - |
|
253 | - return $this->entity_uri_service->get_entity( $uri ); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
258 | - * we check that the post we're saving is the current post. |
|
259 | - * |
|
260 | - * This function is called by a hook, so we're not the ones that need to check the `nonce`. |
|
261 | - * |
|
262 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
263 | - * |
|
264 | - * @since 3.2.0 |
|
265 | - * |
|
266 | - * @param int $post_id Post ID. |
|
267 | - * @param WP_Post $post Post object. |
|
268 | - */ |
|
269 | - public function save_post( $post_id, $post ) { |
|
270 | - // Avoid doing anything if post is autosave or a revision. |
|
271 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
272 | - return; |
|
273 | - } |
|
274 | - |
|
275 | - // We expect a numeric value here. |
|
276 | - if ( ! isset( $_REQUEST['post_ID'] ) || ! is_numeric( $_REQUEST['post_ID'] ) ) { |
|
277 | - return; |
|
278 | - } |
|
279 | - |
|
280 | - // Get the numeric post ID from the request. |
|
281 | - $request_post_id = intval( $_REQUEST['post_ID'] ); |
|
282 | - |
|
283 | - // We're setting the alternative label that have been provided via the UI |
|
284 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
285 | - // programmatically by some other function: we need to check therefore if |
|
286 | - // the $post_id in the save_post call matches the post id set in the request. |
|
287 | - // |
|
288 | - // If this is not the current post being saved or if it's not an entity, return. |
|
289 | - if ( $request_post_id !== $post_id || ! $this->is_entity( $post_id ) ) { |
|
290 | - return; |
|
291 | - } |
|
292 | - |
|
293 | - if ( isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
294 | - $data = filter_var_array( $_REQUEST, array( 'wl_alternative_label' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); |
|
295 | - // Get the alt labels from the request (or empty array). |
|
296 | - $alt_labels = isset( $data['wl_alternative_label'] ) ? $data['wl_alternative_label'] : array(); |
|
297 | - // This is via classic editor, so set the alternative labels. |
|
298 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
299 | - } |
|
300 | - |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Set the alternative labels. |
|
305 | - * |
|
306 | - * @param int $post_id The post id. |
|
307 | - * @param array $alt_labels An array of labels. |
|
308 | - * |
|
309 | - * @since 3.2.0 |
|
310 | - */ |
|
311 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
312 | - |
|
313 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
314 | - // instead of post id. |
|
315 | - if ( ! is_numeric( $post_id ) ) { |
|
316 | - return; |
|
317 | - } |
|
318 | - |
|
319 | - // Force $alt_labels to be an array |
|
320 | - if ( ! is_array( $alt_labels ) ) { |
|
321 | - $alt_labels = array( $alt_labels ); |
|
322 | - } |
|
323 | - |
|
324 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . ' ]' ); |
|
325 | - |
|
326 | - // Delete all the existing alternate labels. |
|
327 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
328 | - |
|
329 | - // Save only unique synonymns. |
|
330 | - $alt_labels = array_unique( $alt_labels ); |
|
331 | - |
|
332 | - // Set the alternative labels. |
|
333 | - foreach ( $alt_labels as $alt_label ) { |
|
334 | - |
|
335 | - // Strip html code from synonym. |
|
336 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
337 | - |
|
338 | - if ( ! empty( $alt_label ) ) { |
|
339 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - } |
|
344 | - |
|
345 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
346 | - |
|
347 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
348 | - |
|
349 | - // Append new synonyms to the end. |
|
350 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
351 | - |
|
352 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
353 | - |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Retrieve the alternate labels. |
|
358 | - * |
|
359 | - * @param int $post_id Post id. |
|
360 | - * |
|
361 | - * @return mixed An array of alternative labels. |
|
362 | - * @since 3.2.0 |
|
363 | - */ |
|
364 | - public function get_alternative_labels( $post_id ) { |
|
365 | - |
|
366 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
371 | - * |
|
372 | - * @param int $post_id The entity {@link WP_Post} id. |
|
373 | - * @param int $object_type The object type {@link Object_Type_Enum} |
|
374 | - * |
|
375 | - * @return array An array with the entity title and labels. |
|
376 | - * @since 3.12.0 |
|
377 | - */ |
|
378 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
379 | - if ( Object_Type_Enum::POST === $object_type ) { |
|
380 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
381 | - } |
|
382 | - |
|
383 | - // Term Reference dont have synonyms yet. |
|
384 | - return array(); |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
389 | - * |
|
390 | - * @param WP_Post $post Post object. |
|
391 | - * |
|
392 | - * @since 3.2.0 |
|
393 | - */ |
|
394 | - public function edit_form_before_permalink( $post ) { |
|
395 | - |
|
396 | - // If it's not an entity, return. |
|
397 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
398 | - return; |
|
399 | - } |
|
400 | - |
|
401 | - // If disabled by filter, return. |
|
402 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
403 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
404 | - return; |
|
405 | - } |
|
406 | - |
|
407 | - // Print the input template. |
|
408 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
409 | - |
|
410 | - // Print all the currently set alternative labels. |
|
411 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
412 | - |
|
413 | - echo wp_kses( $this->get_alternative_label_input( $alt_label ), wp_kses_allowed_html( 'post' ) ); |
|
414 | - |
|
415 | - }; |
|
416 | - |
|
417 | - // Print the button. |
|
418 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
419 | - |
|
420 | - } |
|
421 | - |
|
422 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
423 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
424 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
425 | - $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
426 | - |
|
427 | - if ( ! isset( $entity_id ) || |
|
428 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
429 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
430 | - try { |
|
431 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
432 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
433 | - } catch ( Exception $e ) { |
|
434 | - return null; |
|
435 | - } |
|
436 | - } |
|
437 | - |
|
438 | - return $entity_id; |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Get the alternative label input HTML code. |
|
443 | - * |
|
444 | - * @param string $value The input value. |
|
445 | - * |
|
446 | - * @return string The input HTML code. |
|
447 | - * @since 3.2.0 |
|
448 | - */ |
|
449 | - private function get_alternative_label_input( $value = '' ) { |
|
450 | - |
|
451 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Get the number of entity posts published in this blog. |
|
456 | - * |
|
457 | - * @return int The number of published entity posts. |
|
458 | - * @since 3.6.0 |
|
459 | - */ |
|
460 | - public function count() { |
|
461 | - global $wpdb; |
|
462 | - |
|
463 | - // Try to get the count from the transient. |
|
464 | - $count = get_transient( '_wl_entity_service__count' ); |
|
465 | - if ( false !== $count ) { |
|
466 | - return $count; |
|
467 | - } |
|
468 | - |
|
469 | - // Query the count. |
|
470 | - $count = $wpdb->get_var( |
|
471 | - $wpdb->prepare( |
|
472 | - 'SELECT COUNT( DISTINCT( tr.object_id ) )' |
|
473 | - . " FROM {$wpdb->term_relationships} tr" |
|
474 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
475 | - . ' ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
476 | - . " INNER JOIN {$wpdb->terms} t" |
|
477 | - . ' ON t.term_id = tt.term_id AND t.name != %s', |
|
478 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
479 | - 'article' |
|
480 | - ) |
|
481 | - ); |
|
482 | - |
|
483 | - // Store the count in cache. |
|
484 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
485 | - |
|
486 | - return $count; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
491 | - * call. |
|
492 | - * |
|
493 | - * @param array $args The arguments for a `get_posts` call. |
|
494 | - * |
|
495 | - * @return array The arguments for a `get_posts` call. |
|
496 | - * @since 3.15.0 |
|
497 | - */ |
|
498 | - public static function add_criterias( $args ) { |
|
499 | - |
|
500 | - // Build an optimal tax-query. |
|
501 | - $tax_query = array( |
|
502 | - 'relation' => 'AND', |
|
503 | - array( |
|
504 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
505 | - 'operator' => 'EXISTS', |
|
506 | - ), |
|
507 | - array( |
|
508 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
509 | - 'field' => 'slug', |
|
510 | - 'terms' => 'article', |
|
511 | - 'operator' => 'NOT IN', |
|
512 | - ), |
|
513 | - ); |
|
514 | - |
|
515 | - return $args + array( |
|
516 | - 'post_type' => self::valid_entity_post_types(), |
|
517 | - /* |
|
150 | + foreach ( $terms as $term ) { |
|
151 | + if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
152 | + return true; |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Get the proper classification scope for a given entity post |
|
161 | + * |
|
162 | + * @param integer $post_id An entity post id. |
|
163 | + * |
|
164 | + * @param string $default The default classification scope, `what` if not |
|
165 | + * provided. |
|
166 | + * |
|
167 | + * @return string Returns a classification scope (e.g. 'what'). |
|
168 | + * @since 3.5.0 |
|
169 | + */ |
|
170 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
171 | + |
|
172 | + if ( false === $this->is_entity( $post_id ) ) { |
|
173 | + return $default; |
|
174 | + } |
|
175 | + |
|
176 | + // Retrieve the entity type |
|
177 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
178 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
179 | + // Retrieve classification boxes configuration |
|
180 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
181 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
182 | + foreach ( $classification_boxes as $cb ) { |
|
183 | + if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
184 | + return $cb['id']; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + return $default; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Check whether a {@link WP_Post} is used. |
|
193 | + * |
|
194 | + * @param int $post_id The {@link WP_Post}'s id. |
|
195 | + * |
|
196 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
197 | + */ |
|
198 | + public function is_used( $post_id ) { |
|
199 | + |
|
200 | + if ( false === $this->is_entity( $post_id ) ) { |
|
201 | + return null; |
|
202 | + } |
|
203 | + // Retrieve the post |
|
204 | + $entity = get_post( $post_id ); |
|
205 | + |
|
206 | + global $wpdb; |
|
207 | + // Retrieve Wordlift relation instances table name |
|
208 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
209 | + |
|
210 | + // Perform the query |
|
211 | + $relation_instances = (int) $wpdb->get_var( |
|
212 | + $wpdb->prepare( |
|
213 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
214 | + $entity->ID |
|
215 | + ) |
|
216 | + ); |
|
217 | + // If there is at least one relation instance for the current entity, then it's used |
|
218 | + if ( 0 < $relation_instances ) { |
|
219 | + return true; |
|
220 | + } |
|
221 | + |
|
222 | + // Perform the query |
|
223 | + $meta_instances = (int) $wpdb->get_var( |
|
224 | + $wpdb->prepare( |
|
225 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
226 | + $entity->ID, |
|
227 | + wl_get_entity_uri( $entity->ID ) |
|
228 | + ) |
|
229 | + ); |
|
230 | + |
|
231 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
232 | + if ( 0 < $meta_instances ) { |
|
233 | + return true; |
|
234 | + } |
|
235 | + |
|
236 | + // If we are here, it means the current entity is not used at the moment |
|
237 | + return false; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
242 | + * |
|
243 | + * @param string $uri The entity URI. |
|
244 | + * |
|
245 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
246 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
247 | + * |
|
248 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
249 | + * @since 3.2.0 |
|
250 | + */ |
|
251 | + public function get_entity_post_by_uri( $uri ) { |
|
252 | + |
|
253 | + return $this->entity_uri_service->get_entity( $uri ); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
258 | + * we check that the post we're saving is the current post. |
|
259 | + * |
|
260 | + * This function is called by a hook, so we're not the ones that need to check the `nonce`. |
|
261 | + * |
|
262 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
263 | + * |
|
264 | + * @since 3.2.0 |
|
265 | + * |
|
266 | + * @param int $post_id Post ID. |
|
267 | + * @param WP_Post $post Post object. |
|
268 | + */ |
|
269 | + public function save_post( $post_id, $post ) { |
|
270 | + // Avoid doing anything if post is autosave or a revision. |
|
271 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
272 | + return; |
|
273 | + } |
|
274 | + |
|
275 | + // We expect a numeric value here. |
|
276 | + if ( ! isset( $_REQUEST['post_ID'] ) || ! is_numeric( $_REQUEST['post_ID'] ) ) { |
|
277 | + return; |
|
278 | + } |
|
279 | + |
|
280 | + // Get the numeric post ID from the request. |
|
281 | + $request_post_id = intval( $_REQUEST['post_ID'] ); |
|
282 | + |
|
283 | + // We're setting the alternative label that have been provided via the UI |
|
284 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
285 | + // programmatically by some other function: we need to check therefore if |
|
286 | + // the $post_id in the save_post call matches the post id set in the request. |
|
287 | + // |
|
288 | + // If this is not the current post being saved or if it's not an entity, return. |
|
289 | + if ( $request_post_id !== $post_id || ! $this->is_entity( $post_id ) ) { |
|
290 | + return; |
|
291 | + } |
|
292 | + |
|
293 | + if ( isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
294 | + $data = filter_var_array( $_REQUEST, array( 'wl_alternative_label' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); |
|
295 | + // Get the alt labels from the request (or empty array). |
|
296 | + $alt_labels = isset( $data['wl_alternative_label'] ) ? $data['wl_alternative_label'] : array(); |
|
297 | + // This is via classic editor, so set the alternative labels. |
|
298 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
299 | + } |
|
300 | + |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Set the alternative labels. |
|
305 | + * |
|
306 | + * @param int $post_id The post id. |
|
307 | + * @param array $alt_labels An array of labels. |
|
308 | + * |
|
309 | + * @since 3.2.0 |
|
310 | + */ |
|
311 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
312 | + |
|
313 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
314 | + // instead of post id. |
|
315 | + if ( ! is_numeric( $post_id ) ) { |
|
316 | + return; |
|
317 | + } |
|
318 | + |
|
319 | + // Force $alt_labels to be an array |
|
320 | + if ( ! is_array( $alt_labels ) ) { |
|
321 | + $alt_labels = array( $alt_labels ); |
|
322 | + } |
|
323 | + |
|
324 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . ' ]' ); |
|
325 | + |
|
326 | + // Delete all the existing alternate labels. |
|
327 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
328 | + |
|
329 | + // Save only unique synonymns. |
|
330 | + $alt_labels = array_unique( $alt_labels ); |
|
331 | + |
|
332 | + // Set the alternative labels. |
|
333 | + foreach ( $alt_labels as $alt_label ) { |
|
334 | + |
|
335 | + // Strip html code from synonym. |
|
336 | + $alt_label = wp_strip_all_tags( $alt_label ); |
|
337 | + |
|
338 | + if ( ! empty( $alt_label ) ) { |
|
339 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + } |
|
344 | + |
|
345 | + public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
346 | + |
|
347 | + $merged_labels = $this->get_alternative_labels( $post_id ); |
|
348 | + |
|
349 | + // Append new synonyms to the end. |
|
350 | + $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
351 | + |
|
352 | + $this->set_alternative_labels( $post_id, $merged_labels ); |
|
353 | + |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Retrieve the alternate labels. |
|
358 | + * |
|
359 | + * @param int $post_id Post id. |
|
360 | + * |
|
361 | + * @return mixed An array of alternative labels. |
|
362 | + * @since 3.2.0 |
|
363 | + */ |
|
364 | + public function get_alternative_labels( $post_id ) { |
|
365 | + |
|
366 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
371 | + * |
|
372 | + * @param int $post_id The entity {@link WP_Post} id. |
|
373 | + * @param int $object_type The object type {@link Object_Type_Enum} |
|
374 | + * |
|
375 | + * @return array An array with the entity title and labels. |
|
376 | + * @since 3.12.0 |
|
377 | + */ |
|
378 | + public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
379 | + if ( Object_Type_Enum::POST === $object_type ) { |
|
380 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
381 | + } |
|
382 | + |
|
383 | + // Term Reference dont have synonyms yet. |
|
384 | + return array(); |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
389 | + * |
|
390 | + * @param WP_Post $post Post object. |
|
391 | + * |
|
392 | + * @since 3.2.0 |
|
393 | + */ |
|
394 | + public function edit_form_before_permalink( $post ) { |
|
395 | + |
|
396 | + // If it's not an entity, return. |
|
397 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
398 | + return; |
|
399 | + } |
|
400 | + |
|
401 | + // If disabled by filter, return. |
|
402 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
403 | + if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
404 | + return; |
|
405 | + } |
|
406 | + |
|
407 | + // Print the input template. |
|
408 | + Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
409 | + |
|
410 | + // Print all the currently set alternative labels. |
|
411 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
412 | + |
|
413 | + echo wp_kses( $this->get_alternative_label_input( $alt_label ), wp_kses_allowed_html( 'post' ) ); |
|
414 | + |
|
415 | + }; |
|
416 | + |
|
417 | + // Print the button. |
|
418 | + Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
419 | + |
|
420 | + } |
|
421 | + |
|
422 | + public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
423 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
424 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
425 | + $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
426 | + |
|
427 | + if ( ! isset( $entity_id ) || |
|
428 | + ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
429 | + $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
430 | + try { |
|
431 | + $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
432 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
433 | + } catch ( Exception $e ) { |
|
434 | + return null; |
|
435 | + } |
|
436 | + } |
|
437 | + |
|
438 | + return $entity_id; |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Get the alternative label input HTML code. |
|
443 | + * |
|
444 | + * @param string $value The input value. |
|
445 | + * |
|
446 | + * @return string The input HTML code. |
|
447 | + * @since 3.2.0 |
|
448 | + */ |
|
449 | + private function get_alternative_label_input( $value = '' ) { |
|
450 | + |
|
451 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Get the number of entity posts published in this blog. |
|
456 | + * |
|
457 | + * @return int The number of published entity posts. |
|
458 | + * @since 3.6.0 |
|
459 | + */ |
|
460 | + public function count() { |
|
461 | + global $wpdb; |
|
462 | + |
|
463 | + // Try to get the count from the transient. |
|
464 | + $count = get_transient( '_wl_entity_service__count' ); |
|
465 | + if ( false !== $count ) { |
|
466 | + return $count; |
|
467 | + } |
|
468 | + |
|
469 | + // Query the count. |
|
470 | + $count = $wpdb->get_var( |
|
471 | + $wpdb->prepare( |
|
472 | + 'SELECT COUNT( DISTINCT( tr.object_id ) )' |
|
473 | + . " FROM {$wpdb->term_relationships} tr" |
|
474 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
475 | + . ' ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
476 | + . " INNER JOIN {$wpdb->terms} t" |
|
477 | + . ' ON t.term_id = tt.term_id AND t.name != %s', |
|
478 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
479 | + 'article' |
|
480 | + ) |
|
481 | + ); |
|
482 | + |
|
483 | + // Store the count in cache. |
|
484 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
485 | + |
|
486 | + return $count; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
491 | + * call. |
|
492 | + * |
|
493 | + * @param array $args The arguments for a `get_posts` call. |
|
494 | + * |
|
495 | + * @return array The arguments for a `get_posts` call. |
|
496 | + * @since 3.15.0 |
|
497 | + */ |
|
498 | + public static function add_criterias( $args ) { |
|
499 | + |
|
500 | + // Build an optimal tax-query. |
|
501 | + $tax_query = array( |
|
502 | + 'relation' => 'AND', |
|
503 | + array( |
|
504 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
505 | + 'operator' => 'EXISTS', |
|
506 | + ), |
|
507 | + array( |
|
508 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
509 | + 'field' => 'slug', |
|
510 | + 'terms' => 'article', |
|
511 | + 'operator' => 'NOT IN', |
|
512 | + ), |
|
513 | + ); |
|
514 | + |
|
515 | + return $args + array( |
|
516 | + 'post_type' => self::valid_entity_post_types(), |
|
517 | + /* |
|
518 | 518 | * Ensure compatibility with Polylang. |
519 | 519 | * |
520 | 520 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -522,101 +522,101 @@ discard block |
||
522 | 522 | * |
523 | 523 | * @since 3.19.5 |
524 | 524 | */ |
525 | - 'lang' => '', |
|
526 | - 'tax_query' => $tax_query, |
|
527 | - ); |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Create a new entity. |
|
532 | - * |
|
533 | - * @param string $name The entity name. |
|
534 | - * @param string $type_uri The entity's type URI. |
|
535 | - * @param null $logo The entity logo id (or NULL if none). |
|
536 | - * @param string $status The post status, by default 'publish'. |
|
537 | - * |
|
538 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
539 | - * @since 3.9.0 |
|
540 | - */ |
|
541 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
542 | - |
|
543 | - // Create an entity for the publisher. |
|
544 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
545 | - $post_id = @wp_insert_post( |
|
546 | - array( |
|
547 | - 'post_type' => self::TYPE_NAME, |
|
548 | - 'post_title' => $name, |
|
549 | - 'post_status' => $status, |
|
550 | - 'post_content' => '', |
|
551 | - ) |
|
552 | - ); |
|
553 | - |
|
554 | - // Return the error if any. |
|
555 | - if ( is_wp_error( $post_id ) ) { |
|
556 | - return $post_id; |
|
557 | - } |
|
558 | - |
|
559 | - // Set the entity logo. |
|
560 | - if ( $logo && is_numeric( $logo ) ) { |
|
561 | - set_post_thumbnail( $post_id, $logo ); |
|
562 | - } |
|
563 | - |
|
564 | - // Set the entity type. |
|
565 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
566 | - |
|
567 | - return $post_id; |
|
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Get the entities related to the one with the specified id. By default only |
|
572 | - * published entities will be returned. |
|
573 | - * |
|
574 | - * @param int $id The post id. |
|
575 | - * @param string $post_status The target post status (default = publish). |
|
576 | - * |
|
577 | - * @return array An array of post ids. |
|
578 | - * @since 3.10.0 |
|
579 | - */ |
|
580 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
581 | - |
|
582 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
583 | - } |
|
584 | - |
|
585 | - /** |
|
586 | - * Get the list of entities. |
|
587 | - * |
|
588 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
589 | - * |
|
590 | - * @return array An array of entity posts. |
|
591 | - * @since 3.12.2 |
|
592 | - */ |
|
593 | - public function get( $params = array() ) { |
|
594 | - |
|
595 | - // Set the defaults. |
|
596 | - $defaults = array( 'post_type' => 'entity' ); |
|
597 | - |
|
598 | - // Merge the defaults with the provided parameters. |
|
599 | - $args = wp_parse_args( $params, $defaults ); |
|
600 | - |
|
601 | - // Call the `get_posts` function. |
|
602 | - return get_posts( $args ); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * The list of post type names which can be used for entities |
|
607 | - * |
|
608 | - * Criteria is that the post type is public. The list of valid post types |
|
609 | - * can be overridden with a filter. |
|
610 | - * |
|
611 | - * @return array Array containing the names of the valid post types. |
|
612 | - * @since 3.15.0 |
|
613 | - */ |
|
614 | - public static function valid_entity_post_types() { |
|
615 | - |
|
616 | - // Ignore builtins in the call to avoid getting attachments. |
|
617 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
618 | - |
|
619 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
620 | - } |
|
525 | + 'lang' => '', |
|
526 | + 'tax_query' => $tax_query, |
|
527 | + ); |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Create a new entity. |
|
532 | + * |
|
533 | + * @param string $name The entity name. |
|
534 | + * @param string $type_uri The entity's type URI. |
|
535 | + * @param null $logo The entity logo id (or NULL if none). |
|
536 | + * @param string $status The post status, by default 'publish'. |
|
537 | + * |
|
538 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
539 | + * @since 3.9.0 |
|
540 | + */ |
|
541 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
542 | + |
|
543 | + // Create an entity for the publisher. |
|
544 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
545 | + $post_id = @wp_insert_post( |
|
546 | + array( |
|
547 | + 'post_type' => self::TYPE_NAME, |
|
548 | + 'post_title' => $name, |
|
549 | + 'post_status' => $status, |
|
550 | + 'post_content' => '', |
|
551 | + ) |
|
552 | + ); |
|
553 | + |
|
554 | + // Return the error if any. |
|
555 | + if ( is_wp_error( $post_id ) ) { |
|
556 | + return $post_id; |
|
557 | + } |
|
558 | + |
|
559 | + // Set the entity logo. |
|
560 | + if ( $logo && is_numeric( $logo ) ) { |
|
561 | + set_post_thumbnail( $post_id, $logo ); |
|
562 | + } |
|
563 | + |
|
564 | + // Set the entity type. |
|
565 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
566 | + |
|
567 | + return $post_id; |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Get the entities related to the one with the specified id. By default only |
|
572 | + * published entities will be returned. |
|
573 | + * |
|
574 | + * @param int $id The post id. |
|
575 | + * @param string $post_status The target post status (default = publish). |
|
576 | + * |
|
577 | + * @return array An array of post ids. |
|
578 | + * @since 3.10.0 |
|
579 | + */ |
|
580 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
581 | + |
|
582 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
583 | + } |
|
584 | + |
|
585 | + /** |
|
586 | + * Get the list of entities. |
|
587 | + * |
|
588 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
589 | + * |
|
590 | + * @return array An array of entity posts. |
|
591 | + * @since 3.12.2 |
|
592 | + */ |
|
593 | + public function get( $params = array() ) { |
|
594 | + |
|
595 | + // Set the defaults. |
|
596 | + $defaults = array( 'post_type' => 'entity' ); |
|
597 | + |
|
598 | + // Merge the defaults with the provided parameters. |
|
599 | + $args = wp_parse_args( $params, $defaults ); |
|
600 | + |
|
601 | + // Call the `get_posts` function. |
|
602 | + return get_posts( $args ); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * The list of post type names which can be used for entities |
|
607 | + * |
|
608 | + * Criteria is that the post type is public. The list of valid post types |
|
609 | + * can be overridden with a filter. |
|
610 | + * |
|
611 | + * @return array Array containing the names of the valid post types. |
|
612 | + * @since 3.15.0 |
|
613 | + */ |
|
614 | + public static function valid_entity_post_types() { |
|
615 | + |
|
616 | + // Ignore builtins in the call to avoid getting attachments. |
|
617 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
618 | + |
|
619 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
620 | + } |
|
621 | 621 | |
622 | 622 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | * @since 3.2.0 |
82 | 82 | */ |
83 | 83 | protected function __construct() { |
84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
84 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
85 | 85 | |
86 | 86 | $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
87 | 87 | $this->relation_service = Wordlift_Relation_Service::get_instance(); |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | public static function get_instance() { |
107 | 107 | |
108 | - if ( ! isset( self::$instance ) ) { |
|
108 | + if ( ! isset(self::$instance)) { |
|
109 | 109 | self::$instance = new self(); |
110 | 110 | } |
111 | 111 | |
@@ -121,22 +121,22 @@ discard block |
||
121 | 121 | * @return bool Return true if the post is an entity otherwise false. |
122 | 122 | * @since 3.1.0 |
123 | 123 | */ |
124 | - public function is_entity( $post_id ) { |
|
124 | + public function is_entity($post_id) { |
|
125 | 125 | |
126 | 126 | // Improve performance by giving for granted that a product is an entity. |
127 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
127 | + if ('product' === get_post_type($post_id)) { |
|
128 | 128 | return true; |
129 | 129 | } |
130 | 130 | |
131 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
131 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
132 | 132 | |
133 | - if ( is_wp_error( $terms ) ) { |
|
134 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
133 | + if (is_wp_error($terms)) { |
|
134 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
135 | 135 | |
136 | 136 | return false; |
137 | 137 | } |
138 | 138 | |
139 | - if ( empty( $terms ) ) { |
|
139 | + if (empty($terms)) { |
|
140 | 140 | return false; |
141 | 141 | } |
142 | 142 | |
@@ -147,8 +147,8 @@ discard block |
||
147 | 147 | * |
148 | 148 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
149 | 149 | */ |
150 | - foreach ( $terms as $term ) { |
|
151 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
150 | + foreach ($terms as $term) { |
|
151 | + if (1 !== preg_match('~(^|-)article$~', $term->slug)) { |
|
152 | 152 | return true; |
153 | 153 | } |
154 | 154 | } |
@@ -167,20 +167,20 @@ discard block |
||
167 | 167 | * @return string Returns a classification scope (e.g. 'what'). |
168 | 168 | * @since 3.5.0 |
169 | 169 | */ |
170 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
170 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
171 | 171 | |
172 | - if ( false === $this->is_entity( $post_id ) ) { |
|
172 | + if (false === $this->is_entity($post_id)) { |
|
173 | 173 | return $default; |
174 | 174 | } |
175 | 175 | |
176 | 176 | // Retrieve the entity type |
177 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
178 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
177 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
178 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
179 | 179 | // Retrieve classification boxes configuration |
180 | 180 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
181 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
182 | - foreach ( $classification_boxes as $cb ) { |
|
183 | - if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
181 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
182 | + foreach ($classification_boxes as $cb) { |
|
183 | + if (in_array($entity_type, $cb['registeredTypes'], true)) { |
|
184 | 184 | return $cb['id']; |
185 | 185 | } |
186 | 186 | } |
@@ -195,13 +195,13 @@ discard block |
||
195 | 195 | * |
196 | 196 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
197 | 197 | */ |
198 | - public function is_used( $post_id ) { |
|
198 | + public function is_used($post_id) { |
|
199 | 199 | |
200 | - if ( false === $this->is_entity( $post_id ) ) { |
|
200 | + if (false === $this->is_entity($post_id)) { |
|
201 | 201 | return null; |
202 | 202 | } |
203 | 203 | // Retrieve the post |
204 | - $entity = get_post( $post_id ); |
|
204 | + $entity = get_post($post_id); |
|
205 | 205 | |
206 | 206 | global $wpdb; |
207 | 207 | // Retrieve Wordlift relation instances table name |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | ) |
216 | 216 | ); |
217 | 217 | // If there is at least one relation instance for the current entity, then it's used |
218 | - if ( 0 < $relation_instances ) { |
|
218 | + if (0 < $relation_instances) { |
|
219 | 219 | return true; |
220 | 220 | } |
221 | 221 | |
@@ -224,12 +224,12 @@ discard block |
||
224 | 224 | $wpdb->prepare( |
225 | 225 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
226 | 226 | $entity->ID, |
227 | - wl_get_entity_uri( $entity->ID ) |
|
227 | + wl_get_entity_uri($entity->ID) |
|
228 | 228 | ) |
229 | 229 | ); |
230 | 230 | |
231 | 231 | // If there is at least one meta that refers the current entity uri, then current entity is used |
232 | - if ( 0 < $meta_instances ) { |
|
232 | + if (0 < $meta_instances) { |
|
233 | 233 | return true; |
234 | 234 | } |
235 | 235 | |
@@ -248,9 +248,9 @@ discard block |
||
248 | 248 | * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
249 | 249 | * @since 3.2.0 |
250 | 250 | */ |
251 | - public function get_entity_post_by_uri( $uri ) { |
|
251 | + public function get_entity_post_by_uri($uri) { |
|
252 | 252 | |
253 | - return $this->entity_uri_service->get_entity( $uri ); |
|
253 | + return $this->entity_uri_service->get_entity($uri); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
@@ -266,19 +266,19 @@ discard block |
||
266 | 266 | * @param int $post_id Post ID. |
267 | 267 | * @param WP_Post $post Post object. |
268 | 268 | */ |
269 | - public function save_post( $post_id, $post ) { |
|
269 | + public function save_post($post_id, $post) { |
|
270 | 270 | // Avoid doing anything if post is autosave or a revision. |
271 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
271 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
272 | 272 | return; |
273 | 273 | } |
274 | 274 | |
275 | 275 | // We expect a numeric value here. |
276 | - if ( ! isset( $_REQUEST['post_ID'] ) || ! is_numeric( $_REQUEST['post_ID'] ) ) { |
|
276 | + if ( ! isset($_REQUEST['post_ID']) || ! is_numeric($_REQUEST['post_ID'])) { |
|
277 | 277 | return; |
278 | 278 | } |
279 | 279 | |
280 | 280 | // Get the numeric post ID from the request. |
281 | - $request_post_id = intval( $_REQUEST['post_ID'] ); |
|
281 | + $request_post_id = intval($_REQUEST['post_ID']); |
|
282 | 282 | |
283 | 283 | // We're setting the alternative label that have been provided via the UI |
284 | 284 | // (in fact we're using $_REQUEST), while save_post may be also called |
@@ -286,16 +286,16 @@ discard block |
||
286 | 286 | // the $post_id in the save_post call matches the post id set in the request. |
287 | 287 | // |
288 | 288 | // If this is not the current post being saved or if it's not an entity, return. |
289 | - if ( $request_post_id !== $post_id || ! $this->is_entity( $post_id ) ) { |
|
289 | + if ($request_post_id !== $post_id || ! $this->is_entity($post_id)) { |
|
290 | 290 | return; |
291 | 291 | } |
292 | 292 | |
293 | - if ( isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
294 | - $data = filter_var_array( $_REQUEST, array( 'wl_alternative_label' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); |
|
293 | + if (isset($_REQUEST['wl_alternative_label'])) { |
|
294 | + $data = filter_var_array($_REQUEST, array('wl_alternative_label' => array('flags' => FILTER_REQUIRE_ARRAY))); |
|
295 | 295 | // Get the alt labels from the request (or empty array). |
296 | - $alt_labels = isset( $data['wl_alternative_label'] ) ? $data['wl_alternative_label'] : array(); |
|
296 | + $alt_labels = isset($data['wl_alternative_label']) ? $data['wl_alternative_label'] : array(); |
|
297 | 297 | // This is via classic editor, so set the alternative labels. |
298 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
298 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | } |
@@ -308,48 +308,48 @@ discard block |
||
308 | 308 | * |
309 | 309 | * @since 3.2.0 |
310 | 310 | */ |
311 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
311 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
312 | 312 | |
313 | 313 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
314 | 314 | // instead of post id. |
315 | - if ( ! is_numeric( $post_id ) ) { |
|
315 | + if ( ! is_numeric($post_id)) { |
|
316 | 316 | return; |
317 | 317 | } |
318 | 318 | |
319 | 319 | // Force $alt_labels to be an array |
320 | - if ( ! is_array( $alt_labels ) ) { |
|
321 | - $alt_labels = array( $alt_labels ); |
|
320 | + if ( ! is_array($alt_labels)) { |
|
321 | + $alt_labels = array($alt_labels); |
|
322 | 322 | } |
323 | 323 | |
324 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . ' ]' ); |
|
324 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels).' ]'); |
|
325 | 325 | |
326 | 326 | // Delete all the existing alternate labels. |
327 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
327 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
328 | 328 | |
329 | 329 | // Save only unique synonymns. |
330 | - $alt_labels = array_unique( $alt_labels ); |
|
330 | + $alt_labels = array_unique($alt_labels); |
|
331 | 331 | |
332 | 332 | // Set the alternative labels. |
333 | - foreach ( $alt_labels as $alt_label ) { |
|
333 | + foreach ($alt_labels as $alt_label) { |
|
334 | 334 | |
335 | 335 | // Strip html code from synonym. |
336 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
336 | + $alt_label = wp_strip_all_tags($alt_label); |
|
337 | 337 | |
338 | - if ( ! empty( $alt_label ) ) { |
|
339 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
338 | + if ( ! empty($alt_label)) { |
|
339 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label); |
|
340 | 340 | } |
341 | 341 | } |
342 | 342 | |
343 | 343 | } |
344 | 344 | |
345 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
345 | + public function append_alternative_labels($post_id, $labels_to_append) { |
|
346 | 346 | |
347 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
347 | + $merged_labels = $this->get_alternative_labels($post_id); |
|
348 | 348 | |
349 | 349 | // Append new synonyms to the end. |
350 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
350 | + $merged_labels = array_merge($merged_labels, $labels_to_append); |
|
351 | 351 | |
352 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
352 | + $this->set_alternative_labels($post_id, $merged_labels); |
|
353 | 353 | |
354 | 354 | } |
355 | 355 | |
@@ -361,9 +361,9 @@ discard block |
||
361 | 361 | * @return mixed An array of alternative labels. |
362 | 362 | * @since 3.2.0 |
363 | 363 | */ |
364 | - public function get_alternative_labels( $post_id ) { |
|
364 | + public function get_alternative_labels($post_id) { |
|
365 | 365 | |
366 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
366 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
367 | 367 | } |
368 | 368 | |
369 | 369 | /** |
@@ -375,9 +375,9 @@ discard block |
||
375 | 375 | * @return array An array with the entity title and labels. |
376 | 376 | * @since 3.12.0 |
377 | 377 | */ |
378 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
379 | - if ( Object_Type_Enum::POST === $object_type ) { |
|
380 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
378 | + public function get_labels($post_id, $object_type = Object_Type_Enum::POST) { |
|
379 | + if (Object_Type_Enum::POST === $object_type) { |
|
380 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | // Term Reference dont have synonyms yet. |
@@ -391,46 +391,46 @@ discard block |
||
391 | 391 | * |
392 | 392 | * @since 3.2.0 |
393 | 393 | */ |
394 | - public function edit_form_before_permalink( $post ) { |
|
394 | + public function edit_form_before_permalink($post) { |
|
395 | 395 | |
396 | 396 | // If it's not an entity, return. |
397 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
397 | + if ( ! $this->is_entity($post->ID)) { |
|
398 | 398 | return; |
399 | 399 | } |
400 | 400 | |
401 | 401 | // If disabled by filter, return. |
402 | 402 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
403 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
403 | + if ( ! apply_filters('wl_feature__enable__add-synonyms', true)) { |
|
404 | 404 | return; |
405 | 405 | } |
406 | 406 | |
407 | 407 | // Print the input template. |
408 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
408 | + Wordlift_UI_Service::print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
409 | 409 | |
410 | 410 | // Print all the currently set alternative labels. |
411 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
411 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
412 | 412 | |
413 | - echo wp_kses( $this->get_alternative_label_input( $alt_label ), wp_kses_allowed_html( 'post' ) ); |
|
413 | + echo wp_kses($this->get_alternative_label_input($alt_label), wp_kses_allowed_html('post')); |
|
414 | 414 | |
415 | 415 | }; |
416 | 416 | |
417 | 417 | // Print the button. |
418 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
418 | + Wordlift_UI_Service::print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
419 | 419 | |
420 | 420 | } |
421 | 421 | |
422 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
422 | + public function get_uri($object_id, $type = Object_Type_Enum::POST) { |
|
423 | 423 | $content_service = Wordpress_Content_Service::get_instance(); |
424 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
424 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
425 | 425 | $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
426 | 426 | |
427 | - if ( ! isset( $entity_id ) || |
|
428 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
429 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
427 | + if ( ! isset($entity_id) || |
|
428 | + ( ! empty($dataset_uri) && 0 !== strpos($entity_id, $dataset_uri))) { |
|
429 | + $rel_uri = Entity_Uri_Generator::create_uri($type, $object_id); |
|
430 | 430 | try { |
431 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
432 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
433 | - } catch ( Exception $e ) { |
|
431 | + $content_service->set_entity_id(new Wordpress_Content_Id($object_id, $type), $rel_uri); |
|
432 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
433 | + } catch (Exception $e) { |
|
434 | 434 | return null; |
435 | 435 | } |
436 | 436 | } |
@@ -446,9 +446,9 @@ discard block |
||
446 | 446 | * @return string The input HTML code. |
447 | 447 | * @since 3.2.0 |
448 | 448 | */ |
449 | - private function get_alternative_label_input( $value = '' ) { |
|
449 | + private function get_alternative_label_input($value = '') { |
|
450 | 450 | |
451 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
451 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | /** |
@@ -461,8 +461,8 @@ discard block |
||
461 | 461 | global $wpdb; |
462 | 462 | |
463 | 463 | // Try to get the count from the transient. |
464 | - $count = get_transient( '_wl_entity_service__count' ); |
|
465 | - if ( false !== $count ) { |
|
464 | + $count = get_transient('_wl_entity_service__count'); |
|
465 | + if (false !== $count) { |
|
466 | 466 | return $count; |
467 | 467 | } |
468 | 468 | |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | ); |
482 | 482 | |
483 | 483 | // Store the count in cache. |
484 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
484 | + set_transient('_wl_entity_service__count', $count, 900); |
|
485 | 485 | |
486 | 486 | return $count; |
487 | 487 | } |
@@ -495,7 +495,7 @@ discard block |
||
495 | 495 | * @return array The arguments for a `get_posts` call. |
496 | 496 | * @since 3.15.0 |
497 | 497 | */ |
498 | - public static function add_criterias( $args ) { |
|
498 | + public static function add_criterias($args) { |
|
499 | 499 | |
500 | 500 | // Build an optimal tax-query. |
501 | 501 | $tax_query = array( |
@@ -538,7 +538,7 @@ discard block |
||
538 | 538 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
539 | 539 | * @since 3.9.0 |
540 | 540 | */ |
541 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
541 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
542 | 542 | |
543 | 543 | // Create an entity for the publisher. |
544 | 544 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
@@ -552,17 +552,17 @@ discard block |
||
552 | 552 | ); |
553 | 553 | |
554 | 554 | // Return the error if any. |
555 | - if ( is_wp_error( $post_id ) ) { |
|
555 | + if (is_wp_error($post_id)) { |
|
556 | 556 | return $post_id; |
557 | 557 | } |
558 | 558 | |
559 | 559 | // Set the entity logo. |
560 | - if ( $logo && is_numeric( $logo ) ) { |
|
561 | - set_post_thumbnail( $post_id, $logo ); |
|
560 | + if ($logo && is_numeric($logo)) { |
|
561 | + set_post_thumbnail($post_id, $logo); |
|
562 | 562 | } |
563 | 563 | |
564 | 564 | // Set the entity type. |
565 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
565 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
566 | 566 | |
567 | 567 | return $post_id; |
568 | 568 | } |
@@ -577,9 +577,9 @@ discard block |
||
577 | 577 | * @return array An array of post ids. |
578 | 578 | * @since 3.10.0 |
579 | 579 | */ |
580 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
580 | + public function get_related_entities($id, $post_status = 'publish') { |
|
581 | 581 | |
582 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
582 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
583 | 583 | } |
584 | 584 | |
585 | 585 | /** |
@@ -590,16 +590,16 @@ discard block |
||
590 | 590 | * @return array An array of entity posts. |
591 | 591 | * @since 3.12.2 |
592 | 592 | */ |
593 | - public function get( $params = array() ) { |
|
593 | + public function get($params = array()) { |
|
594 | 594 | |
595 | 595 | // Set the defaults. |
596 | - $defaults = array( 'post_type' => 'entity' ); |
|
596 | + $defaults = array('post_type' => 'entity'); |
|
597 | 597 | |
598 | 598 | // Merge the defaults with the provided parameters. |
599 | - $args = wp_parse_args( $params, $defaults ); |
|
599 | + $args = wp_parse_args($params, $defaults); |
|
600 | 600 | |
601 | 601 | // Call the `get_posts` function. |
602 | - return get_posts( $args ); |
|
602 | + return get_posts($args); |
|
603 | 603 | } |
604 | 604 | |
605 | 605 | /** |
@@ -614,9 +614,9 @@ discard block |
||
614 | 614 | public static function valid_entity_post_types() { |
615 | 615 | |
616 | 616 | // Ignore builtins in the call to avoid getting attachments. |
617 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
617 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
618 | 618 | |
619 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
619 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
620 | 620 | } |
621 | 621 | |
622 | 622 | } |
@@ -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 |
@@ -16,232 +16,232 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_File_Cache_Service implements Wordlift_Cache_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * The cache directory. |
|
21 | - * |
|
22 | - * @since 3.16.0 |
|
23 | - * @access private |
|
24 | - * @var string $cache_dir The root cache directory (ending with a trailing slash). |
|
25 | - */ |
|
26 | - private $cache_dir; |
|
27 | - |
|
28 | - /** |
|
29 | - * The file extension for cache files (e.g. `.wlcache`). |
|
30 | - * |
|
31 | - * @since 3.16.0 |
|
32 | - * @access private |
|
33 | - * @var string $file_extension The file extension for cache files (e.g. `.wlcache`). |
|
34 | - */ |
|
35 | - private $file_extension; |
|
36 | - |
|
37 | - /** |
|
38 | - * A {@link Wordlift_Log_Service} instance. |
|
39 | - * |
|
40 | - * @since 3.16.0 |
|
41 | - * @access private |
|
42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
43 | - */ |
|
44 | - private $log; |
|
45 | - |
|
46 | - /** |
|
47 | - * The {@link Wordlift_File_Cache_Service} registered instances. |
|
48 | - * |
|
49 | - * Each {@link Wordlift_File_Cache_Service} adds itself to the registered |
|
50 | - * instances. |
|
51 | - * |
|
52 | - * @since 3.16.3 |
|
53 | - * @access private |
|
54 | - * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances. |
|
55 | - */ |
|
56 | - private static $instances = array(); |
|
57 | - |
|
58 | - private static $instance; |
|
59 | - |
|
60 | - /** |
|
61 | - * Create a {@link Wordlift_File_Cache_Service} instance. |
|
62 | - * |
|
63 | - * The File Cache Service requires a base cache directory (to which a unique |
|
64 | - * id for the current site will be appended) and a file extension for cache |
|
65 | - * files (by default `.wlcache`) is used. |
|
66 | - * |
|
67 | - * @param string $cache_dir The base cache directory. |
|
68 | - * @param string $file_extension The file extension, by default `.wlcache`. |
|
69 | - * |
|
70 | - * @since 3.16.0 |
|
71 | - */ |
|
72 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
73 | - |
|
74 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
75 | - |
|
76 | - // Set the cache directory using the base directory provided by the caller |
|
77 | - // and appending a hash for the unique site id. |
|
78 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
79 | - $this->file_extension = $file_extension; |
|
80 | - |
|
81 | - // Create the cache dir. |
|
82 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
83 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
84 | - @mkdir( $this->cache_dir, 0755, true ); |
|
85 | - } |
|
86 | - |
|
87 | - // Add ourselves to the list of instances. |
|
88 | - self::$instances[] = $this; |
|
89 | - |
|
90 | - // Initialize the singleton and the ajax method. |
|
91 | - if ( ! isset( self::$instance ) ) { |
|
92 | - self::$instance = $this; |
|
93 | - |
|
94 | - add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
95 | - } |
|
96 | - |
|
97 | - $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
98 | - |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Get the cached response for the specified `id`. |
|
103 | - * |
|
104 | - * @param int $id The cache `id`. |
|
105 | - * |
|
106 | - * @return mixed|false The cached contents or false if the cache isn't found. |
|
107 | - * @since 3.16.0 |
|
108 | - */ |
|
109 | - public function get_cache( $id ) { |
|
110 | - |
|
111 | - // Bail out if we don't have the cache. |
|
112 | - if ( ! $this->has_cache( $id ) ) { |
|
113 | - return false; |
|
114 | - } |
|
115 | - |
|
116 | - // Get the filename. |
|
117 | - $filename = $this->get_filename( $id ); |
|
118 | - |
|
119 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
120 | - |
|
121 | - // Try to decode the contents. |
|
122 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
123 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
124 | - |
|
125 | - // Return false if decoding failed, otherwise the decoded contents. |
|
126 | - return $contents ? $contents : false; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Set the cache contents for the specified `id`. |
|
131 | - * |
|
132 | - * @param int $id The cache id. |
|
133 | - * |
|
134 | - * @return bool True if the `id` has a cache. |
|
135 | - * @since 3.16.0 |
|
136 | - */ |
|
137 | - public function has_cache( $id ) { |
|
138 | - |
|
139 | - // Get the filename. |
|
140 | - $filename = $this->get_filename( $id ); |
|
141 | - |
|
142 | - // Bail out if the file doesn't exist. |
|
143 | - return file_exists( $filename ); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @inheritdoc |
|
148 | - */ |
|
149 | - public function set_cache( $id, $contents ) { |
|
150 | - |
|
151 | - $filename = $this->get_filename( $id ); |
|
152 | - |
|
153 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
154 | - |
|
155 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
156 | - @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
157 | - |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Delete the cache for the specified `id`. |
|
162 | - * |
|
163 | - * @param int $id The cache `id`. |
|
164 | - * |
|
165 | - * @since 3.16.0 |
|
166 | - */ |
|
167 | - public function delete_cache( $id ) { |
|
168 | - |
|
169 | - $filename = $this->get_filename( $id ); |
|
170 | - |
|
171 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
19 | + /** |
|
20 | + * The cache directory. |
|
21 | + * |
|
22 | + * @since 3.16.0 |
|
23 | + * @access private |
|
24 | + * @var string $cache_dir The root cache directory (ending with a trailing slash). |
|
25 | + */ |
|
26 | + private $cache_dir; |
|
27 | + |
|
28 | + /** |
|
29 | + * The file extension for cache files (e.g. `.wlcache`). |
|
30 | + * |
|
31 | + * @since 3.16.0 |
|
32 | + * @access private |
|
33 | + * @var string $file_extension The file extension for cache files (e.g. `.wlcache`). |
|
34 | + */ |
|
35 | + private $file_extension; |
|
36 | + |
|
37 | + /** |
|
38 | + * A {@link Wordlift_Log_Service} instance. |
|
39 | + * |
|
40 | + * @since 3.16.0 |
|
41 | + * @access private |
|
42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
43 | + */ |
|
44 | + private $log; |
|
45 | + |
|
46 | + /** |
|
47 | + * The {@link Wordlift_File_Cache_Service} registered instances. |
|
48 | + * |
|
49 | + * Each {@link Wordlift_File_Cache_Service} adds itself to the registered |
|
50 | + * instances. |
|
51 | + * |
|
52 | + * @since 3.16.3 |
|
53 | + * @access private |
|
54 | + * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances. |
|
55 | + */ |
|
56 | + private static $instances = array(); |
|
57 | + |
|
58 | + private static $instance; |
|
59 | + |
|
60 | + /** |
|
61 | + * Create a {@link Wordlift_File_Cache_Service} instance. |
|
62 | + * |
|
63 | + * The File Cache Service requires a base cache directory (to which a unique |
|
64 | + * id for the current site will be appended) and a file extension for cache |
|
65 | + * files (by default `.wlcache`) is used. |
|
66 | + * |
|
67 | + * @param string $cache_dir The base cache directory. |
|
68 | + * @param string $file_extension The file extension, by default `.wlcache`. |
|
69 | + * |
|
70 | + * @since 3.16.0 |
|
71 | + */ |
|
72 | + public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
73 | + |
|
74 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
75 | + |
|
76 | + // Set the cache directory using the base directory provided by the caller |
|
77 | + // and appending a hash for the unique site id. |
|
78 | + $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
79 | + $this->file_extension = $file_extension; |
|
80 | + |
|
81 | + // Create the cache dir. |
|
82 | + if ( ! file_exists( $this->cache_dir ) ) { |
|
83 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
84 | + @mkdir( $this->cache_dir, 0755, true ); |
|
85 | + } |
|
86 | + |
|
87 | + // Add ourselves to the list of instances. |
|
88 | + self::$instances[] = $this; |
|
89 | + |
|
90 | + // Initialize the singleton and the ajax method. |
|
91 | + if ( ! isset( self::$instance ) ) { |
|
92 | + self::$instance = $this; |
|
93 | + |
|
94 | + add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
95 | + } |
|
96 | + |
|
97 | + $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
98 | + |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Get the cached response for the specified `id`. |
|
103 | + * |
|
104 | + * @param int $id The cache `id`. |
|
105 | + * |
|
106 | + * @return mixed|false The cached contents or false if the cache isn't found. |
|
107 | + * @since 3.16.0 |
|
108 | + */ |
|
109 | + public function get_cache( $id ) { |
|
110 | + |
|
111 | + // Bail out if we don't have the cache. |
|
112 | + if ( ! $this->has_cache( $id ) ) { |
|
113 | + return false; |
|
114 | + } |
|
115 | + |
|
116 | + // Get the filename. |
|
117 | + $filename = $this->get_filename( $id ); |
|
118 | + |
|
119 | + $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
120 | + |
|
121 | + // Try to decode the contents. |
|
122 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
123 | + $contents = json_decode( file_get_contents( $filename ), true ); |
|
124 | + |
|
125 | + // Return false if decoding failed, otherwise the decoded contents. |
|
126 | + return $contents ? $contents : false; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Set the cache contents for the specified `id`. |
|
131 | + * |
|
132 | + * @param int $id The cache id. |
|
133 | + * |
|
134 | + * @return bool True if the `id` has a cache. |
|
135 | + * @since 3.16.0 |
|
136 | + */ |
|
137 | + public function has_cache( $id ) { |
|
138 | + |
|
139 | + // Get the filename. |
|
140 | + $filename = $this->get_filename( $id ); |
|
141 | + |
|
142 | + // Bail out if the file doesn't exist. |
|
143 | + return file_exists( $filename ); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @inheritdoc |
|
148 | + */ |
|
149 | + public function set_cache( $id, $contents ) { |
|
150 | + |
|
151 | + $filename = $this->get_filename( $id ); |
|
152 | + |
|
153 | + $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
154 | + |
|
155 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
156 | + @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
157 | + |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Delete the cache for the specified `id`. |
|
162 | + * |
|
163 | + * @param int $id The cache `id`. |
|
164 | + * |
|
165 | + * @since 3.16.0 |
|
166 | + */ |
|
167 | + public function delete_cache( $id ) { |
|
168 | + |
|
169 | + $filename = $this->get_filename( $id ); |
|
170 | + |
|
171 | + $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
172 | 172 | |
173 | - if ( file_exists( $filename ) ) { |
|
174 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
175 | - @unlink( $filename ); |
|
176 | - } |
|
177 | - |
|
178 | - } |
|
173 | + if ( file_exists( $filename ) ) { |
|
174 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
175 | + @unlink( $filename ); |
|
176 | + } |
|
177 | + |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Flush the whole cache. |
|
182 | - * |
|
183 | - * @since 3.16.0 |
|
184 | - */ |
|
185 | - public function flush() { |
|
180 | + /** |
|
181 | + * Flush the whole cache. |
|
182 | + * |
|
183 | + * @since 3.16.0 |
|
184 | + */ |
|
185 | + public function flush() { |
|
186 | 186 | |
187 | - // Bail out if the cache dir isn't set. |
|
188 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
189 | - return; |
|
190 | - } |
|
187 | + // Bail out if the cache dir isn't set. |
|
188 | + if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
189 | + return; |
|
190 | + } |
|
191 | 191 | |
192 | - $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
192 | + $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
193 | 193 | |
194 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
195 | - $handle = @opendir( $this->cache_dir ); |
|
194 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
195 | + $handle = @opendir( $this->cache_dir ); |
|
196 | 196 | |
197 | - // Bail out if the directory can't be opened. |
|
198 | - if ( false === $handle ) { |
|
199 | - return; |
|
200 | - } |
|
201 | - |
|
202 | - // Calculate the file extension length for matching file names. |
|
203 | - $file_extension_length = strlen( $this->file_extension ); |
|
204 | - |
|
205 | - // Loop into the directory to delete files. |
|
206 | - while ( false !== ( $entry = readdir( $handle ) ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | - if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
208 | - && file_exists( $this->cache_dir . $entry ) ) { |
|
209 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
197 | + // Bail out if the directory can't be opened. |
|
198 | + if ( false === $handle ) { |
|
199 | + return; |
|
200 | + } |
|
201 | + |
|
202 | + // Calculate the file extension length for matching file names. |
|
203 | + $file_extension_length = strlen( $this->file_extension ); |
|
204 | + |
|
205 | + // Loop into the directory to delete files. |
|
206 | + while ( false !== ( $entry = readdir( $handle ) ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | + if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
208 | + && file_exists( $this->cache_dir . $entry ) ) { |
|
209 | + $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
210 | 210 | |
211 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
212 | - @unlink( $this->cache_dir . $entry ); |
|
213 | - } |
|
214 | - } |
|
211 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
212 | + @unlink( $this->cache_dir . $entry ); |
|
213 | + } |
|
214 | + } |
|
215 | 215 | |
216 | - // Finally closed the directory. |
|
217 | - closedir( $handle ); |
|
216 | + // Finally closed the directory. |
|
217 | + closedir( $handle ); |
|
218 | 218 | |
219 | - } |
|
219 | + } |
|
220 | 220 | |
221 | - public static function flush_all() { |
|
222 | - |
|
223 | - foreach ( self::$instances as $instance ) { |
|
224 | - $instance->flush(); |
|
225 | - } |
|
226 | - |
|
227 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
228 | - && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
229 | - wp_send_json_success(); |
|
230 | - } |
|
221 | + public static function flush_all() { |
|
222 | + |
|
223 | + foreach ( self::$instances as $instance ) { |
|
224 | + $instance->flush(); |
|
225 | + } |
|
226 | + |
|
227 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
228 | + && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
229 | + wp_send_json_success(); |
|
230 | + } |
|
231 | 231 | |
232 | - } |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Get the filename holding the cache contents for the specified `id`. |
|
236 | - * |
|
237 | - * @param int $id The cache `id`. |
|
238 | - * |
|
239 | - * @return string The filename. |
|
240 | - * @since 3.16.0 |
|
241 | - */ |
|
242 | - private function get_filename( $id ) { |
|
234 | + /** |
|
235 | + * Get the filename holding the cache contents for the specified `id`. |
|
236 | + * |
|
237 | + * @param int $id The cache `id`. |
|
238 | + * |
|
239 | + * @return string The filename. |
|
240 | + * @since 3.16.0 |
|
241 | + */ |
|
242 | + private function get_filename( $id ) { |
|
243 | 243 | |
244 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
245 | - } |
|
244 | + return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
245 | + } |
|
246 | 246 | |
247 | 247 | } |
@@ -69,32 +69,32 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @since 3.16.0 |
71 | 71 | */ |
72 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
72 | + public function __construct($cache_dir, $file_extension = '.wlcache') { |
|
73 | 73 | |
74 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
74 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
75 | 75 | |
76 | 76 | // Set the cache directory using the base directory provided by the caller |
77 | 77 | // and appending a hash for the unique site id. |
78 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
78 | + $this->cache_dir = trailingslashit($cache_dir).md5(get_site_url()).'/'; |
|
79 | 79 | $this->file_extension = $file_extension; |
80 | 80 | |
81 | 81 | // Create the cache dir. |
82 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
82 | + if ( ! file_exists($this->cache_dir)) { |
|
83 | 83 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
84 | - @mkdir( $this->cache_dir, 0755, true ); |
|
84 | + @mkdir($this->cache_dir, 0755, true); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | // Add ourselves to the list of instances. |
88 | 88 | self::$instances[] = $this; |
89 | 89 | |
90 | 90 | // Initialize the singleton and the ajax method. |
91 | - if ( ! isset( self::$instance ) ) { |
|
91 | + if ( ! isset(self::$instance)) { |
|
92 | 92 | self::$instance = $this; |
93 | 93 | |
94 | - add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
94 | + add_action('wp_ajax_wl_file_cache__flush_all', array('Wordlift_File_Cache_Service', 'flush_all')); |
|
95 | 95 | } |
96 | 96 | |
97 | - $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
97 | + $this->log->debug("File Cache service initialized on $this->cache_dir."); |
|
98 | 98 | |
99 | 99 | } |
100 | 100 | |
@@ -106,21 +106,21 @@ discard block |
||
106 | 106 | * @return mixed|false The cached contents or false if the cache isn't found. |
107 | 107 | * @since 3.16.0 |
108 | 108 | */ |
109 | - public function get_cache( $id ) { |
|
109 | + public function get_cache($id) { |
|
110 | 110 | |
111 | 111 | // Bail out if we don't have the cache. |
112 | - if ( ! $this->has_cache( $id ) ) { |
|
112 | + if ( ! $this->has_cache($id)) { |
|
113 | 113 | return false; |
114 | 114 | } |
115 | 115 | |
116 | 116 | // Get the filename. |
117 | - $filename = $this->get_filename( $id ); |
|
117 | + $filename = $this->get_filename($id); |
|
118 | 118 | |
119 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
119 | + $this->log->trace("Trying to get cache contents for $id from $filename..."); |
|
120 | 120 | |
121 | 121 | // Try to decode the contents. |
122 | 122 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
123 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
123 | + $contents = json_decode(file_get_contents($filename), true); |
|
124 | 124 | |
125 | 125 | // Return false if decoding failed, otherwise the decoded contents. |
126 | 126 | return $contents ? $contents : false; |
@@ -134,26 +134,26 @@ discard block |
||
134 | 134 | * @return bool True if the `id` has a cache. |
135 | 135 | * @since 3.16.0 |
136 | 136 | */ |
137 | - public function has_cache( $id ) { |
|
137 | + public function has_cache($id) { |
|
138 | 138 | |
139 | 139 | // Get the filename. |
140 | - $filename = $this->get_filename( $id ); |
|
140 | + $filename = $this->get_filename($id); |
|
141 | 141 | |
142 | 142 | // Bail out if the file doesn't exist. |
143 | - return file_exists( $filename ); |
|
143 | + return file_exists($filename); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
147 | 147 | * @inheritdoc |
148 | 148 | */ |
149 | - public function set_cache( $id, $contents ) { |
|
149 | + public function set_cache($id, $contents) { |
|
150 | 150 | |
151 | - $filename = $this->get_filename( $id ); |
|
151 | + $filename = $this->get_filename($id); |
|
152 | 152 | |
153 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
153 | + $this->log->trace("Writing cache contents for $id to $filename..."); |
|
154 | 154 | |
155 | 155 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
156 | - @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
156 | + @file_put_contents($filename, wp_json_encode($contents)); |
|
157 | 157 | |
158 | 158 | } |
159 | 159 | |
@@ -164,15 +164,15 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @since 3.16.0 |
166 | 166 | */ |
167 | - public function delete_cache( $id ) { |
|
167 | + public function delete_cache($id) { |
|
168 | 168 | |
169 | - $filename = $this->get_filename( $id ); |
|
169 | + $filename = $this->get_filename($id); |
|
170 | 170 | |
171 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
171 | + $this->log->trace("Deleting cache contents for $id, file $filename..."); |
|
172 | 172 | |
173 | - if ( file_exists( $filename ) ) { |
|
173 | + if (file_exists($filename)) { |
|
174 | 174 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
175 | - @unlink( $filename ); |
|
175 | + @unlink($filename); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | } |
@@ -185,47 +185,47 @@ discard block |
||
185 | 185 | public function flush() { |
186 | 186 | |
187 | 187 | // Bail out if the cache dir isn't set. |
188 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
188 | + if (empty($this->cache_dir) || '/' === $this->cache_dir) { |
|
189 | 189 | return; |
190 | 190 | } |
191 | 191 | |
192 | - $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
192 | + $this->log->trace("Flushing cache contents from $this->cache_dir..."); |
|
193 | 193 | |
194 | 194 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
195 | - $handle = @opendir( $this->cache_dir ); |
|
195 | + $handle = @opendir($this->cache_dir); |
|
196 | 196 | |
197 | 197 | // Bail out if the directory can't be opened. |
198 | - if ( false === $handle ) { |
|
198 | + if (false === $handle) { |
|
199 | 199 | return; |
200 | 200 | } |
201 | 201 | |
202 | 202 | // Calculate the file extension length for matching file names. |
203 | - $file_extension_length = strlen( $this->file_extension ); |
|
203 | + $file_extension_length = strlen($this->file_extension); |
|
204 | 204 | |
205 | 205 | // Loop into the directory to delete files. |
206 | - while ( false !== ( $entry = readdir( $handle ) ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | - if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
208 | - && file_exists( $this->cache_dir . $entry ) ) { |
|
209 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
206 | + while (false !== ($entry = readdir($handle))) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | + if (substr($entry, - $file_extension_length) === $this->file_extension |
|
208 | + && file_exists($this->cache_dir.$entry)) { |
|
209 | + $this->log->trace("Deleting file {$this->cache_dir}{$entry}..."); |
|
210 | 210 | |
211 | 211 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
212 | - @unlink( $this->cache_dir . $entry ); |
|
212 | + @unlink($this->cache_dir.$entry); |
|
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
216 | 216 | // Finally closed the directory. |
217 | - closedir( $handle ); |
|
217 | + closedir($handle); |
|
218 | 218 | |
219 | 219 | } |
220 | 220 | |
221 | 221 | public static function flush_all() { |
222 | 222 | |
223 | - foreach ( self::$instances as $instance ) { |
|
223 | + foreach (self::$instances as $instance) { |
|
224 | 224 | $instance->flush(); |
225 | 225 | } |
226 | 226 | |
227 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
228 | - && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
227 | + if (defined('DOING_AJAX') && DOING_AJAX |
|
228 | + && isset($_REQUEST['action']) && 'wl_file_cache__flush_all' === $_REQUEST['action']) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
229 | 229 | wp_send_json_success(); |
230 | 230 | } |
231 | 231 | |
@@ -239,9 +239,9 @@ discard block |
||
239 | 239 | * @return string The filename. |
240 | 240 | * @since 3.16.0 |
241 | 241 | */ |
242 | - private function get_filename( $id ) { |
|
242 | + private function get_filename($id) { |
|
243 | 243 | |
244 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
244 | + return $this->cache_dir.md5($id).$this->file_extension; |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | } |
@@ -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 |