@@ -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 |
@@ -16,185 +16,185 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Http_Api { |
18 | 18 | |
19 | - /** |
|
20 | - * A {@link Wordlift_Log_Service} instance. |
|
21 | - * |
|
22 | - * @since 3.15.3 |
|
23 | - * |
|
24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25 | - */ |
|
26 | - private $log; |
|
27 | - |
|
28 | - /** |
|
29 | - * Create a {@link Wordlift_End_Point} instance. |
|
30 | - * |
|
31 | - * @since 3.15.3 |
|
32 | - */ |
|
33 | - public function __construct() { |
|
34 | - |
|
35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36 | - |
|
37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39 | - |
|
40 | - // region SAMPLE ACTIONS. |
|
41 | - add_action( |
|
42 | - 'admin_post_wl_hello_world', |
|
43 | - array( |
|
44 | - $this, |
|
45 | - 'hello_world', |
|
46 | - ) |
|
47 | - ); |
|
48 | - add_action( |
|
49 | - 'admin_post_nopriv_wl_hello_world', |
|
50 | - array( |
|
51 | - $this, |
|
52 | - 'nopriv_hello_world', |
|
53 | - ) |
|
54 | - ); |
|
55 | - // endregion |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Add the `wl-api` rewrite end-point. |
|
60 | - * |
|
61 | - * @since 3.15.3 |
|
62 | - */ |
|
63 | - public function add_rewrite_endpoint() { |
|
64 | - |
|
65 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
66 | - $this->ensure_rewrite_rules_are_flushed(); |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Handle `template_redirect` hooks. |
|
72 | - * |
|
73 | - * @since 3.15.3 |
|
74 | - */ |
|
75 | - public function template_redirect() { |
|
76 | - |
|
77 | - global $wp_query; |
|
78 | - |
|
79 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
81 | - |
|
82 | - return; |
|
83 | - } |
|
84 | - |
|
85 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | - $this->do_action( $action ); |
|
87 | - |
|
88 | - exit; |
|
89 | - |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Do the requested action. |
|
94 | - * |
|
95 | - * @param string $action The action to execute. |
|
96 | - * |
|
97 | - * @since 3.15.3 |
|
98 | - */ |
|
99 | - private function do_action( $action ) { |
|
100 | - |
|
101 | - if ( empty( $action ) ) { |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
106 | - /** |
|
107 | - * Fires on a non-authenticated admin post request for the given action. |
|
108 | - * |
|
109 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
110 | - * request action. |
|
111 | - * |
|
112 | - * @since 2.6.0 |
|
113 | - */ |
|
114 | - do_action( "admin_post_nopriv_{$action}" ); |
|
115 | - } else { |
|
116 | - /** |
|
117 | - * Fires on an authenticated admin post request for the given action. |
|
118 | - * |
|
119 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
120 | - * request action. |
|
121 | - * |
|
122 | - * @since 2.6.0 |
|
123 | - */ |
|
124 | - do_action( "admin_post_{$action}" ); |
|
125 | - } |
|
126 | - |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Test function, anonymous. |
|
131 | - * |
|
132 | - * @since 3.15.3 |
|
133 | - */ |
|
134 | - public function nopriv_hello_world() { |
|
135 | - |
|
136 | - wp_die( 'Hello World! (from anonymous)' ); |
|
137 | - |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Test function, authenticated. |
|
142 | - * |
|
143 | - * @since 3.15.3 |
|
144 | - */ |
|
145 | - public function hello_world() { |
|
146 | - |
|
147 | - wp_die( 'Hello World! (from authenticated)' ); |
|
148 | - |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Ensure that the rewrite rules are flushed the first time. |
|
153 | - * |
|
154 | - * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
155 | - * @since 3.15.3 |
|
156 | - */ |
|
157 | - public static function ensure_rewrite_rules_are_flushed() { |
|
158 | - |
|
159 | - // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
160 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | - update_option( 'wl_http_api', 'yes' ); |
|
162 | - add_action( |
|
163 | - 'wp_loaded', |
|
164 | - function () { |
|
165 | - flush_rewrite_rules(); |
|
166 | - } |
|
167 | - ); |
|
168 | - } |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
174 | - * reinitialize the `wl-api` route. |
|
175 | - * |
|
176 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
177 | - * |
|
178 | - * @since 3.19.2 |
|
179 | - */ |
|
180 | - public static function activate() { |
|
181 | - |
|
182 | - // Force the plugin to reinitialize the rewrite rules. |
|
183 | - update_option( 'wl_http_api', 'no' ); |
|
184 | - |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Delete the option when the plugin is deactivated. |
|
189 | - * |
|
190 | - * @since 3.19.4 |
|
191 | - * |
|
192 | - * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
193 | - */ |
|
194 | - public static function deactivate() { |
|
195 | - |
|
196 | - delete_option( 'wl_http_api' ); |
|
197 | - |
|
198 | - } |
|
19 | + /** |
|
20 | + * A {@link Wordlift_Log_Service} instance. |
|
21 | + * |
|
22 | + * @since 3.15.3 |
|
23 | + * |
|
24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25 | + */ |
|
26 | + private $log; |
|
27 | + |
|
28 | + /** |
|
29 | + * Create a {@link Wordlift_End_Point} instance. |
|
30 | + * |
|
31 | + * @since 3.15.3 |
|
32 | + */ |
|
33 | + public function __construct() { |
|
34 | + |
|
35 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36 | + |
|
37 | + add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39 | + |
|
40 | + // region SAMPLE ACTIONS. |
|
41 | + add_action( |
|
42 | + 'admin_post_wl_hello_world', |
|
43 | + array( |
|
44 | + $this, |
|
45 | + 'hello_world', |
|
46 | + ) |
|
47 | + ); |
|
48 | + add_action( |
|
49 | + 'admin_post_nopriv_wl_hello_world', |
|
50 | + array( |
|
51 | + $this, |
|
52 | + 'nopriv_hello_world', |
|
53 | + ) |
|
54 | + ); |
|
55 | + // endregion |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Add the `wl-api` rewrite end-point. |
|
60 | + * |
|
61 | + * @since 3.15.3 |
|
62 | + */ |
|
63 | + public function add_rewrite_endpoint() { |
|
64 | + |
|
65 | + add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
66 | + $this->ensure_rewrite_rules_are_flushed(); |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Handle `template_redirect` hooks. |
|
72 | + * |
|
73 | + * @since 3.15.3 |
|
74 | + */ |
|
75 | + public function template_redirect() { |
|
76 | + |
|
77 | + global $wp_query; |
|
78 | + |
|
79 | + if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | + $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
81 | + |
|
82 | + return; |
|
83 | + } |
|
84 | + |
|
85 | + $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | + $this->do_action( $action ); |
|
87 | + |
|
88 | + exit; |
|
89 | + |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Do the requested action. |
|
94 | + * |
|
95 | + * @param string $action The action to execute. |
|
96 | + * |
|
97 | + * @since 3.15.3 |
|
98 | + */ |
|
99 | + private function do_action( $action ) { |
|
100 | + |
|
101 | + if ( empty( $action ) ) { |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
106 | + /** |
|
107 | + * Fires on a non-authenticated admin post request for the given action. |
|
108 | + * |
|
109 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
110 | + * request action. |
|
111 | + * |
|
112 | + * @since 2.6.0 |
|
113 | + */ |
|
114 | + do_action( "admin_post_nopriv_{$action}" ); |
|
115 | + } else { |
|
116 | + /** |
|
117 | + * Fires on an authenticated admin post request for the given action. |
|
118 | + * |
|
119 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
120 | + * request action. |
|
121 | + * |
|
122 | + * @since 2.6.0 |
|
123 | + */ |
|
124 | + do_action( "admin_post_{$action}" ); |
|
125 | + } |
|
126 | + |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Test function, anonymous. |
|
131 | + * |
|
132 | + * @since 3.15.3 |
|
133 | + */ |
|
134 | + public function nopriv_hello_world() { |
|
135 | + |
|
136 | + wp_die( 'Hello World! (from anonymous)' ); |
|
137 | + |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Test function, authenticated. |
|
142 | + * |
|
143 | + * @since 3.15.3 |
|
144 | + */ |
|
145 | + public function hello_world() { |
|
146 | + |
|
147 | + wp_die( 'Hello World! (from authenticated)' ); |
|
148 | + |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Ensure that the rewrite rules are flushed the first time. |
|
153 | + * |
|
154 | + * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
155 | + * @since 3.15.3 |
|
156 | + */ |
|
157 | + public static function ensure_rewrite_rules_are_flushed() { |
|
158 | + |
|
159 | + // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
160 | + if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | + update_option( 'wl_http_api', 'yes' ); |
|
162 | + add_action( |
|
163 | + 'wp_loaded', |
|
164 | + function () { |
|
165 | + flush_rewrite_rules(); |
|
166 | + } |
|
167 | + ); |
|
168 | + } |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
174 | + * reinitialize the `wl-api` route. |
|
175 | + * |
|
176 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
177 | + * |
|
178 | + * @since 3.19.2 |
|
179 | + */ |
|
180 | + public static function activate() { |
|
181 | + |
|
182 | + // Force the plugin to reinitialize the rewrite rules. |
|
183 | + update_option( 'wl_http_api', 'no' ); |
|
184 | + |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Delete the option when the plugin is deactivated. |
|
189 | + * |
|
190 | + * @since 3.19.4 |
|
191 | + * |
|
192 | + * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
193 | + */ |
|
194 | + public static function deactivate() { |
|
195 | + |
|
196 | + delete_option( 'wl_http_api' ); |
|
197 | + |
|
198 | + } |
|
199 | 199 | |
200 | 200 | } |
@@ -32,10 +32,10 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public function __construct() { |
34 | 34 | |
35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
35 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
36 | 36 | |
37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
37 | + add_action('init', array($this, 'add_rewrite_endpoint')); |
|
38 | + add_action('template_redirect', array($this, 'template_redirect')); |
|
39 | 39 | |
40 | 40 | // region SAMPLE ACTIONS. |
41 | 41 | add_action( |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function add_rewrite_endpoint() { |
64 | 64 | |
65 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
65 | + add_rewrite_endpoint('wl-api', EP_ROOT); |
|
66 | 66 | $this->ensure_rewrite_rules_are_flushed(); |
67 | 67 | |
68 | 68 | } |
@@ -76,14 +76,14 @@ discard block |
||
76 | 76 | |
77 | 77 | global $wp_query; |
78 | 78 | |
79 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
79 | + if ( ! isset($wp_query->query_vars['wl-api'])) { |
|
80 | + $this->log->trace('Skipping, not a `wl-api` call.'); |
|
81 | 81 | |
82 | 82 | return; |
83 | 83 | } |
84 | 84 | |
85 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | - $this->do_action( $action ); |
|
85 | + $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | + $this->do_action($action); |
|
87 | 87 | |
88 | 88 | exit; |
89 | 89 | |
@@ -96,13 +96,13 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @since 3.15.3 |
98 | 98 | */ |
99 | - private function do_action( $action ) { |
|
99 | + private function do_action($action) { |
|
100 | 100 | |
101 | - if ( empty( $action ) ) { |
|
101 | + if (empty($action)) { |
|
102 | 102 | return; |
103 | 103 | } |
104 | 104 | |
105 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
105 | + if ( ! wp_validate_auth_cookie('', 'logged_in')) { |
|
106 | 106 | /** |
107 | 107 | * Fires on a non-authenticated admin post request for the given action. |
108 | 108 | * |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @since 2.6.0 |
113 | 113 | */ |
114 | - do_action( "admin_post_nopriv_{$action}" ); |
|
114 | + do_action("admin_post_nopriv_{$action}"); |
|
115 | 115 | } else { |
116 | 116 | /** |
117 | 117 | * Fires on an authenticated admin post request for the given action. |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * |
122 | 122 | * @since 2.6.0 |
123 | 123 | */ |
124 | - do_action( "admin_post_{$action}" ); |
|
124 | + do_action("admin_post_{$action}"); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function nopriv_hello_world() { |
135 | 135 | |
136 | - wp_die( 'Hello World! (from anonymous)' ); |
|
136 | + wp_die('Hello World! (from anonymous)'); |
|
137 | 137 | |
138 | 138 | } |
139 | 139 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | */ |
145 | 145 | public function hello_world() { |
146 | 146 | |
147 | - wp_die( 'Hello World! (from authenticated)' ); |
|
147 | + wp_die('Hello World! (from authenticated)'); |
|
148 | 148 | |
149 | 149 | } |
150 | 150 | |
@@ -157,11 +157,11 @@ discard block |
||
157 | 157 | public static function ensure_rewrite_rules_are_flushed() { |
158 | 158 | |
159 | 159 | // See https://github.com/insideout10/wordlift-plugin/issues/698. |
160 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | - update_option( 'wl_http_api', 'yes' ); |
|
160 | + if ('yes' !== get_option('wl_http_api')) { |
|
161 | + update_option('wl_http_api', 'yes'); |
|
162 | 162 | add_action( |
163 | 163 | 'wp_loaded', |
164 | - function () { |
|
164 | + function() { |
|
165 | 165 | flush_rewrite_rules(); |
166 | 166 | } |
167 | 167 | ); |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | public static function activate() { |
181 | 181 | |
182 | 182 | // Force the plugin to reinitialize the rewrite rules. |
183 | - update_option( 'wl_http_api', 'no' ); |
|
183 | + update_option('wl_http_api', 'no'); |
|
184 | 184 | |
185 | 185 | } |
186 | 186 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | public static function deactivate() { |
195 | 195 | |
196 | - delete_option( 'wl_http_api' ); |
|
196 | + delete_option('wl_http_api'); |
|
197 | 197 | |
198 | 198 | } |
199 | 199 |
@@ -8,80 +8,80 @@ |
||
8 | 8 | |
9 | 9 | class Wordlift_Mapping_Ajax_Adapter { |
10 | 10 | |
11 | - /** |
|
12 | - * The {@link Wordlift_Mapping_Service} instance. |
|
13 | - * |
|
14 | - * @since 3.20.0 |
|
15 | - * @access private |
|
16 | - * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
17 | - */ |
|
18 | - private $mapping_service; |
|
11 | + /** |
|
12 | + * The {@link Wordlift_Mapping_Service} instance. |
|
13 | + * |
|
14 | + * @since 3.20.0 |
|
15 | + * @access private |
|
16 | + * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
17 | + */ |
|
18 | + private $mapping_service; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
22 | - * |
|
23 | - * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
24 | - * |
|
25 | - * @since 3.20.0 |
|
26 | - */ |
|
27 | - public function __construct( $mapping_service ) { |
|
20 | + /** |
|
21 | + * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
22 | + * |
|
23 | + * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
24 | + * |
|
25 | + * @since 3.20.0 |
|
26 | + */ |
|
27 | + public function __construct( $mapping_service ) { |
|
28 | 28 | |
29 | - $this->mapping_service = $mapping_service; |
|
29 | + $this->mapping_service = $mapping_service; |
|
30 | 30 | |
31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
31 | + add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | + add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
33 | 33 | |
34 | - } |
|
34 | + } |
|
35 | 35 | |
36 | - public function set_entity_types_for_post_type() { |
|
36 | + public function set_entity_types_for_post_type() { |
|
37 | 37 | |
38 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | - return; |
|
40 | - } |
|
38 | + if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | + return; |
|
40 | + } |
|
41 | 41 | |
42 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | - $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
42 | + $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | + $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
44 | 44 | |
45 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
45 | + $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
46 | 46 | |
47 | - wp_send_json_success(); |
|
47 | + wp_send_json_success(); |
|
48 | 48 | |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - public function update_post_type_entity_types() { |
|
51 | + public function update_post_type_entity_types() { |
|
52 | 52 | |
53 | - // If the nonce is invalid, return an error. |
|
54 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
57 | - } |
|
53 | + // If the nonce is invalid, return an error. |
|
54 | + $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | + if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
57 | + } |
|
58 | 58 | |
59 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
61 | - } |
|
59 | + if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | + wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
61 | + } |
|
62 | 62 | |
63 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
65 | - } |
|
63 | + if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | + wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
65 | + } |
|
66 | 66 | |
67 | - // Get the post type. |
|
68 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
67 | + // Get the post type. |
|
68 | + $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
69 | 69 | |
70 | - // Get the entity types URIs. |
|
71 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
70 | + // Get the entity types URIs. |
|
71 | + $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
72 | 72 | |
73 | - // Get the offset. |
|
74 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
73 | + // Get the offset. |
|
74 | + $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
75 | 75 | |
76 | - // Update and get the results. |
|
77 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
76 | + // Update and get the results. |
|
77 | + $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
78 | 78 | |
79 | - // Add our nonce to the result. |
|
80 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
79 | + // Add our nonce to the result. |
|
80 | + $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
81 | 81 | |
82 | - // Finally send the results. |
|
83 | - wp_send_json_success( $result ); |
|
82 | + // Finally send the results. |
|
83 | + wp_send_json_success( $result ); |
|
84 | 84 | |
85 | - } |
|
85 | + } |
|
86 | 86 | |
87 | 87 | } |
@@ -24,25 +24,25 @@ discard block |
||
24 | 24 | * |
25 | 25 | * @since 3.20.0 |
26 | 26 | */ |
27 | - public function __construct( $mapping_service ) { |
|
27 | + public function __construct($mapping_service) { |
|
28 | 28 | |
29 | 29 | $this->mapping_service = $mapping_service; |
30 | 30 | |
31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
31 | + add_action('wp_ajax_wl_set_entity_types_for_post_type', array($this, 'set_entity_types_for_post_type')); |
|
32 | + add_action('wp_ajax_wl_update_post_type_entity_types', array($this, 'update_post_type_entity_types')); |
|
33 | 33 | |
34 | 34 | } |
35 | 35 | |
36 | 36 | public function set_entity_types_for_post_type() { |
37 | 37 | |
38 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
38 | + if ( ! isset($_REQUEST['post_type']) || ! isset($_REQUEST['entity_types'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | - $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
42 | + $post_type = sanitize_text_field(wp_unslash($_REQUEST['post_type'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | + $entity_types = array_map('sanitize_text_field', wp_unslash((array) $_REQUEST['entity_types'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
44 | 44 | |
45 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
45 | + $this->mapping_service->set_entity_types_for_post_type($post_type, $entity_types); |
|
46 | 46 | |
47 | 47 | wp_send_json_success(); |
48 | 48 | |
@@ -51,36 +51,36 @@ discard block |
||
51 | 51 | public function update_post_type_entity_types() { |
52 | 52 | |
53 | 53 | // If the nonce is invalid, return an error. |
54 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
54 | + $nonce = isset($_REQUEST['_nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_nonce'])) : ''; |
|
55 | + if ( ! wp_verify_nonce($nonce, 'update_post_type_entity_types')) { |
|
56 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
57 | 57 | } |
58 | 58 | |
59 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
59 | + if (empty($_REQUEST['post_type'])) { |
|
60 | + wp_send_json_error(__('`post_type` is required', 'wordlift')); |
|
61 | 61 | } |
62 | 62 | |
63 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
63 | + if (empty($_REQUEST['entity_types'])) { |
|
64 | + wp_send_json_error(__('`entity_types` is required', 'wordlift')); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | // Get the post type. |
68 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
68 | + $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field(wp_unslash($_REQUEST['post_type'])) : ''; |
|
69 | 69 | |
70 | 70 | // Get the entity types URIs. |
71 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
71 | + $entity_types = isset($_REQUEST['entity_types']) ? filter_var(wp_unslash($_REQUEST['entity_types']), FILTER_REQUIRE_ARRAY) : array(); |
|
72 | 72 | |
73 | 73 | // Get the offset. |
74 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
74 | + $offset = isset($_REQUEST['offset']) ? intval($_REQUEST['offset']) : 0; |
|
75 | 75 | |
76 | 76 | // Update and get the results. |
77 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
77 | + $result = $this->mapping_service->update($post_type, $entity_types, $offset); |
|
78 | 78 | |
79 | 79 | // Add our nonce to the result. |
80 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
80 | + $result['_nonce'] = wp_create_nonce('update_post_type_entity_types'); |
|
81 | 81 | |
82 | 82 | // Finally send the results. |
83 | - wp_send_json_success( $result ); |
|
83 | + wp_send_json_success($result); |
|
84 | 84 | |
85 | 85 | } |
86 | 86 |
@@ -14,223 +14,223 @@ |
||
14 | 14 | */ |
15 | 15 | class Wordlift_Mapping_Service { |
16 | 16 | |
17 | - /** |
|
18 | - * The mapping's options. |
|
19 | - * |
|
20 | - * @since 3.20.0 |
|
21 | - * @access private |
|
22 | - * @var array $options The mapping's options. |
|
23 | - */ |
|
24 | - private $options; |
|
25 | - |
|
26 | - /** |
|
27 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
28 | - * |
|
29 | - * @since 3.20.0 |
|
30 | - * @access private |
|
31 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
32 | - */ |
|
33 | - private $entity_type_service; |
|
34 | - |
|
35 | - /** |
|
36 | - * The singleton instance. |
|
37 | - * |
|
38 | - * @since 3.20.0 |
|
39 | - * @access private |
|
40 | - * @var \Wordlift_Mapping_Service $instance The singleton instance. |
|
41 | - */ |
|
42 | - private static $instance; |
|
43 | - |
|
44 | - /** |
|
45 | - * Create a {@link Wordlift_Mapping_Service} instance. |
|
46 | - * |
|
47 | - * @since 3.20.0 |
|
48 | - * |
|
49 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
50 | - */ |
|
51 | - public function __construct( $entity_type_service ) { |
|
52 | - |
|
53 | - // Set the entity type service instance. |
|
54 | - $this->entity_type_service = $entity_type_service; |
|
55 | - |
|
56 | - // Load the options. |
|
57 | - $this->options = get_option( 'wl_mappings', array() ); |
|
58 | - |
|
59 | - // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`. |
|
60 | - add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types' ), 9 ); |
|
61 | - add_filter( |
|
62 | - 'wl_default_entity_types_for_post_type', |
|
63 | - array( |
|
64 | - $this, |
|
65 | - 'default_entity_types_for_post_type', |
|
66 | - ), |
|
67 | - 9, |
|
68 | - 2 |
|
69 | - ); |
|
70 | - |
|
71 | - // Set the singleton instance. |
|
72 | - self::$instance = $this; |
|
73 | - |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get the singleton instance. |
|
78 | - * |
|
79 | - * @since 3.20.0 |
|
80 | - * |
|
81 | - * @return \Wordlift_Mapping_Service The singleton instance. |
|
82 | - */ |
|
83 | - public static function get_instance() { |
|
84 | - |
|
85 | - return self::$instance; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Save the options. |
|
90 | - * |
|
91 | - * @since 3.20.0 |
|
92 | - */ |
|
93 | - private function save_options() { |
|
94 | - |
|
95 | - update_option( 'wl_mappings', $this->options, true ); |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Set the default entity types for a post type. |
|
101 | - * |
|
102 | - * @since 3.20.0 |
|
103 | - * |
|
104 | - * @param string $post_type Post type. |
|
105 | - * @param array $entity_types An array of entity types slugs. |
|
106 | - */ |
|
107 | - public function set_entity_types_for_post_type( $post_type, $entity_types ) { |
|
108 | - |
|
109 | - $this->options[ $post_type ] = $entity_types; |
|
110 | - $this->save_options(); |
|
111 | - |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types. |
|
116 | - * |
|
117 | - * @since 3.20.0 |
|
118 | - * |
|
119 | - * @param array $post_types The default post types. |
|
120 | - * |
|
121 | - * @return array The supported post types. |
|
122 | - */ |
|
123 | - public function valid_entity_post_types( $post_types ) { |
|
124 | - |
|
125 | - return array_merge( $post_types, array_keys( $this->options ) ); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type. |
|
130 | - * |
|
131 | - * @since 3.20.0 |
|
132 | - * |
|
133 | - * @param array $default The default entity types. |
|
134 | - * @param string $post_type The post type. |
|
135 | - * |
|
136 | - * @return array The default entity types. |
|
137 | - */ |
|
138 | - public function default_entity_types_for_post_type( $default, $post_type ) { |
|
139 | - |
|
140 | - return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Update the post type with the entity types, starting at the specified offset. |
|
145 | - * |
|
146 | - * @since 3.20.0 |
|
147 | - * |
|
148 | - * @param string $post_type The post type. |
|
149 | - * @param array $entity_types The entity types. |
|
150 | - * @param int $offset The offset (0 by default). |
|
151 | - * |
|
152 | - * @return array { |
|
153 | - * The result array. |
|
154 | - * |
|
155 | - * @type int $current The current offset. |
|
156 | - * @type int $next The next offset. |
|
157 | - * @type int $count The total element count. |
|
158 | - * } |
|
159 | - */ |
|
160 | - public function update( $post_type, $entity_types, $offset = 0 ) { |
|
161 | - |
|
162 | - $entity_type_service = $this->entity_type_service; |
|
163 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
164 | - |
|
165 | - return Wordlift_Batch_Action::process( |
|
166 | - $post_type, |
|
167 | - $offset, |
|
168 | - $tax_query, |
|
169 | - function ( $post_id ) use ( $entity_type_service, $entity_types ) { |
|
170 | - foreach ( $entity_types as $entity_type ) { |
|
171 | - $entity_type_service->set( $post_id, $entity_type, false ); |
|
172 | - } |
|
173 | - } |
|
174 | - ); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Count the number of posts that need to be assigned with the entity types. |
|
179 | - * |
|
180 | - * @since 3.20.0 |
|
181 | - * |
|
182 | - * @param string $post_type The post type. |
|
183 | - * @param array $entity_types An array of entity types. |
|
184 | - * |
|
185 | - * @return int The number of posts to be assigned with entity types. |
|
186 | - */ |
|
187 | - public function count( $post_type, $entity_types ) { |
|
188 | - |
|
189 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
190 | - |
|
191 | - return Wordlift_Batch_Action::count( $post_type, $tax_query ); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Get the taxonomy query for the specified entity types. |
|
196 | - * |
|
197 | - * @since 3.20.0 |
|
198 | - * |
|
199 | - * @param array $entity_types The entity types. |
|
200 | - * |
|
201 | - * @return array The tax query. |
|
202 | - */ |
|
203 | - private function get_tax_query( $entity_types ) { |
|
204 | - |
|
205 | - $entity_type_service = $this->entity_type_service; |
|
206 | - $entity_types_terms = array_filter( |
|
207 | - array_map( |
|
208 | - function ( $item ) use ( $entity_type_service ) { |
|
209 | - return $entity_type_service->get_term_by_uri( $item ); |
|
210 | - }, |
|
211 | - $entity_types |
|
212 | - ) |
|
213 | - ); |
|
214 | - |
|
215 | - $entity_types_terms_ids = array_map( |
|
216 | - function ( $term ) { |
|
217 | - return $term->term_id; |
|
218 | - }, |
|
219 | - $entity_types_terms |
|
220 | - ); |
|
221 | - |
|
222 | - $tax_query = array( |
|
223 | - 'tax_query' => array( |
|
224 | - array( |
|
225 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
226 | - 'field' => 'term_id', |
|
227 | - 'terms' => $entity_types_terms_ids, |
|
228 | - 'operator' => 'NOT IN', |
|
229 | - ), |
|
230 | - ), |
|
231 | - ); |
|
232 | - |
|
233 | - return $tax_query; |
|
234 | - } |
|
17 | + /** |
|
18 | + * The mapping's options. |
|
19 | + * |
|
20 | + * @since 3.20.0 |
|
21 | + * @access private |
|
22 | + * @var array $options The mapping's options. |
|
23 | + */ |
|
24 | + private $options; |
|
25 | + |
|
26 | + /** |
|
27 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
28 | + * |
|
29 | + * @since 3.20.0 |
|
30 | + * @access private |
|
31 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
32 | + */ |
|
33 | + private $entity_type_service; |
|
34 | + |
|
35 | + /** |
|
36 | + * The singleton instance. |
|
37 | + * |
|
38 | + * @since 3.20.0 |
|
39 | + * @access private |
|
40 | + * @var \Wordlift_Mapping_Service $instance The singleton instance. |
|
41 | + */ |
|
42 | + private static $instance; |
|
43 | + |
|
44 | + /** |
|
45 | + * Create a {@link Wordlift_Mapping_Service} instance. |
|
46 | + * |
|
47 | + * @since 3.20.0 |
|
48 | + * |
|
49 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
50 | + */ |
|
51 | + public function __construct( $entity_type_service ) { |
|
52 | + |
|
53 | + // Set the entity type service instance. |
|
54 | + $this->entity_type_service = $entity_type_service; |
|
55 | + |
|
56 | + // Load the options. |
|
57 | + $this->options = get_option( 'wl_mappings', array() ); |
|
58 | + |
|
59 | + // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`. |
|
60 | + add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types' ), 9 ); |
|
61 | + add_filter( |
|
62 | + 'wl_default_entity_types_for_post_type', |
|
63 | + array( |
|
64 | + $this, |
|
65 | + 'default_entity_types_for_post_type', |
|
66 | + ), |
|
67 | + 9, |
|
68 | + 2 |
|
69 | + ); |
|
70 | + |
|
71 | + // Set the singleton instance. |
|
72 | + self::$instance = $this; |
|
73 | + |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get the singleton instance. |
|
78 | + * |
|
79 | + * @since 3.20.0 |
|
80 | + * |
|
81 | + * @return \Wordlift_Mapping_Service The singleton instance. |
|
82 | + */ |
|
83 | + public static function get_instance() { |
|
84 | + |
|
85 | + return self::$instance; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Save the options. |
|
90 | + * |
|
91 | + * @since 3.20.0 |
|
92 | + */ |
|
93 | + private function save_options() { |
|
94 | + |
|
95 | + update_option( 'wl_mappings', $this->options, true ); |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Set the default entity types for a post type. |
|
101 | + * |
|
102 | + * @since 3.20.0 |
|
103 | + * |
|
104 | + * @param string $post_type Post type. |
|
105 | + * @param array $entity_types An array of entity types slugs. |
|
106 | + */ |
|
107 | + public function set_entity_types_for_post_type( $post_type, $entity_types ) { |
|
108 | + |
|
109 | + $this->options[ $post_type ] = $entity_types; |
|
110 | + $this->save_options(); |
|
111 | + |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types. |
|
116 | + * |
|
117 | + * @since 3.20.0 |
|
118 | + * |
|
119 | + * @param array $post_types The default post types. |
|
120 | + * |
|
121 | + * @return array The supported post types. |
|
122 | + */ |
|
123 | + public function valid_entity_post_types( $post_types ) { |
|
124 | + |
|
125 | + return array_merge( $post_types, array_keys( $this->options ) ); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type. |
|
130 | + * |
|
131 | + * @since 3.20.0 |
|
132 | + * |
|
133 | + * @param array $default The default entity types. |
|
134 | + * @param string $post_type The post type. |
|
135 | + * |
|
136 | + * @return array The default entity types. |
|
137 | + */ |
|
138 | + public function default_entity_types_for_post_type( $default, $post_type ) { |
|
139 | + |
|
140 | + return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Update the post type with the entity types, starting at the specified offset. |
|
145 | + * |
|
146 | + * @since 3.20.0 |
|
147 | + * |
|
148 | + * @param string $post_type The post type. |
|
149 | + * @param array $entity_types The entity types. |
|
150 | + * @param int $offset The offset (0 by default). |
|
151 | + * |
|
152 | + * @return array { |
|
153 | + * The result array. |
|
154 | + * |
|
155 | + * @type int $current The current offset. |
|
156 | + * @type int $next The next offset. |
|
157 | + * @type int $count The total element count. |
|
158 | + * } |
|
159 | + */ |
|
160 | + public function update( $post_type, $entity_types, $offset = 0 ) { |
|
161 | + |
|
162 | + $entity_type_service = $this->entity_type_service; |
|
163 | + $tax_query = $this->get_tax_query( $entity_types ); |
|
164 | + |
|
165 | + return Wordlift_Batch_Action::process( |
|
166 | + $post_type, |
|
167 | + $offset, |
|
168 | + $tax_query, |
|
169 | + function ( $post_id ) use ( $entity_type_service, $entity_types ) { |
|
170 | + foreach ( $entity_types as $entity_type ) { |
|
171 | + $entity_type_service->set( $post_id, $entity_type, false ); |
|
172 | + } |
|
173 | + } |
|
174 | + ); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Count the number of posts that need to be assigned with the entity types. |
|
179 | + * |
|
180 | + * @since 3.20.0 |
|
181 | + * |
|
182 | + * @param string $post_type The post type. |
|
183 | + * @param array $entity_types An array of entity types. |
|
184 | + * |
|
185 | + * @return int The number of posts to be assigned with entity types. |
|
186 | + */ |
|
187 | + public function count( $post_type, $entity_types ) { |
|
188 | + |
|
189 | + $tax_query = $this->get_tax_query( $entity_types ); |
|
190 | + |
|
191 | + return Wordlift_Batch_Action::count( $post_type, $tax_query ); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Get the taxonomy query for the specified entity types. |
|
196 | + * |
|
197 | + * @since 3.20.0 |
|
198 | + * |
|
199 | + * @param array $entity_types The entity types. |
|
200 | + * |
|
201 | + * @return array The tax query. |
|
202 | + */ |
|
203 | + private function get_tax_query( $entity_types ) { |
|
204 | + |
|
205 | + $entity_type_service = $this->entity_type_service; |
|
206 | + $entity_types_terms = array_filter( |
|
207 | + array_map( |
|
208 | + function ( $item ) use ( $entity_type_service ) { |
|
209 | + return $entity_type_service->get_term_by_uri( $item ); |
|
210 | + }, |
|
211 | + $entity_types |
|
212 | + ) |
|
213 | + ); |
|
214 | + |
|
215 | + $entity_types_terms_ids = array_map( |
|
216 | + function ( $term ) { |
|
217 | + return $term->term_id; |
|
218 | + }, |
|
219 | + $entity_types_terms |
|
220 | + ); |
|
221 | + |
|
222 | + $tax_query = array( |
|
223 | + 'tax_query' => array( |
|
224 | + array( |
|
225 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
226 | + 'field' => 'term_id', |
|
227 | + 'terms' => $entity_types_terms_ids, |
|
228 | + 'operator' => 'NOT IN', |
|
229 | + ), |
|
230 | + ), |
|
231 | + ); |
|
232 | + |
|
233 | + return $tax_query; |
|
234 | + } |
|
235 | 235 | |
236 | 236 | } |
@@ -48,16 +48,16 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
50 | 50 | */ |
51 | - public function __construct( $entity_type_service ) { |
|
51 | + public function __construct($entity_type_service) { |
|
52 | 52 | |
53 | 53 | // Set the entity type service instance. |
54 | 54 | $this->entity_type_service = $entity_type_service; |
55 | 55 | |
56 | 56 | // Load the options. |
57 | - $this->options = get_option( 'wl_mappings', array() ); |
|
57 | + $this->options = get_option('wl_mappings', array()); |
|
58 | 58 | |
59 | 59 | // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`. |
60 | - add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types' ), 9 ); |
|
60 | + add_filter('wl_valid_entity_post_types', array($this, 'valid_entity_post_types'), 9); |
|
61 | 61 | add_filter( |
62 | 62 | 'wl_default_entity_types_for_post_type', |
63 | 63 | array( |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | private function save_options() { |
94 | 94 | |
95 | - update_option( 'wl_mappings', $this->options, true ); |
|
95 | + update_option('wl_mappings', $this->options, true); |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
@@ -104,9 +104,9 @@ discard block |
||
104 | 104 | * @param string $post_type Post type. |
105 | 105 | * @param array $entity_types An array of entity types slugs. |
106 | 106 | */ |
107 | - public function set_entity_types_for_post_type( $post_type, $entity_types ) { |
|
107 | + public function set_entity_types_for_post_type($post_type, $entity_types) { |
|
108 | 108 | |
109 | - $this->options[ $post_type ] = $entity_types; |
|
109 | + $this->options[$post_type] = $entity_types; |
|
110 | 110 | $this->save_options(); |
111 | 111 | |
112 | 112 | } |
@@ -120,9 +120,9 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @return array The supported post types. |
122 | 122 | */ |
123 | - public function valid_entity_post_types( $post_types ) { |
|
123 | + public function valid_entity_post_types($post_types) { |
|
124 | 124 | |
125 | - return array_merge( $post_types, array_keys( $this->options ) ); |
|
125 | + return array_merge($post_types, array_keys($this->options)); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @return array The default entity types. |
137 | 137 | */ |
138 | - public function default_entity_types_for_post_type( $default, $post_type ) { |
|
138 | + public function default_entity_types_for_post_type($default, $post_type) { |
|
139 | 139 | |
140 | - return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default; |
|
140 | + return isset($this->options[$post_type]) ? $this->options[$post_type] : $default; |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | /** |
@@ -157,18 +157,18 @@ discard block |
||
157 | 157 | * @type int $count The total element count. |
158 | 158 | * } |
159 | 159 | */ |
160 | - public function update( $post_type, $entity_types, $offset = 0 ) { |
|
160 | + public function update($post_type, $entity_types, $offset = 0) { |
|
161 | 161 | |
162 | 162 | $entity_type_service = $this->entity_type_service; |
163 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
163 | + $tax_query = $this->get_tax_query($entity_types); |
|
164 | 164 | |
165 | 165 | return Wordlift_Batch_Action::process( |
166 | 166 | $post_type, |
167 | 167 | $offset, |
168 | 168 | $tax_query, |
169 | - function ( $post_id ) use ( $entity_type_service, $entity_types ) { |
|
170 | - foreach ( $entity_types as $entity_type ) { |
|
171 | - $entity_type_service->set( $post_id, $entity_type, false ); |
|
169 | + function($post_id) use ($entity_type_service, $entity_types) { |
|
170 | + foreach ($entity_types as $entity_type) { |
|
171 | + $entity_type_service->set($post_id, $entity_type, false); |
|
172 | 172 | } |
173 | 173 | } |
174 | 174 | ); |
@@ -184,11 +184,11 @@ discard block |
||
184 | 184 | * |
185 | 185 | * @return int The number of posts to be assigned with entity types. |
186 | 186 | */ |
187 | - public function count( $post_type, $entity_types ) { |
|
187 | + public function count($post_type, $entity_types) { |
|
188 | 188 | |
189 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
189 | + $tax_query = $this->get_tax_query($entity_types); |
|
190 | 190 | |
191 | - return Wordlift_Batch_Action::count( $post_type, $tax_query ); |
|
191 | + return Wordlift_Batch_Action::count($post_type, $tax_query); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -200,20 +200,20 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @return array The tax query. |
202 | 202 | */ |
203 | - private function get_tax_query( $entity_types ) { |
|
203 | + private function get_tax_query($entity_types) { |
|
204 | 204 | |
205 | 205 | $entity_type_service = $this->entity_type_service; |
206 | 206 | $entity_types_terms = array_filter( |
207 | 207 | array_map( |
208 | - function ( $item ) use ( $entity_type_service ) { |
|
209 | - return $entity_type_service->get_term_by_uri( $item ); |
|
208 | + function($item) use ($entity_type_service) { |
|
209 | + return $entity_type_service->get_term_by_uri($item); |
|
210 | 210 | }, |
211 | 211 | $entity_types |
212 | 212 | ) |
213 | 213 | ); |
214 | 214 | |
215 | 215 | $entity_types_terms_ids = array_map( |
216 | - function ( $term ) { |
|
216 | + function($term) { |
|
217 | 217 | return $term->term_id; |
218 | 218 | }, |
219 | 219 | $entity_types_terms |
@@ -7,148 +7,148 @@ |
||
7 | 7 | */ |
8 | 8 | class Wordlift_Log_Service { |
9 | 9 | |
10 | - const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
11 | - |
|
12 | - const ERROR = 4; |
|
13 | - const WARN = 3; |
|
14 | - const INFO = 2; |
|
15 | - const DEBUG = 1; |
|
16 | - const TRACE = 0; |
|
17 | - |
|
18 | - /** |
|
19 | - * The class related to the logs. |
|
20 | - * |
|
21 | - * @since 1.0.0 |
|
22 | - * @access private |
|
23 | - * @var string $class_name The class related to the logs. |
|
24 | - */ |
|
25 | - private $class_name; |
|
26 | - |
|
27 | - /** |
|
28 | - * The log levels for printing in log lines. |
|
29 | - * |
|
30 | - * @var array $levels An array of log levels. |
|
31 | - */ |
|
32 | - private static $levels = array( |
|
33 | - self::TRACE => 'TRACE', |
|
34 | - self::DEBUG => 'DEBUG', |
|
35 | - self::INFO => 'INFO', |
|
36 | - self::WARN => 'WARN', |
|
37 | - self::ERROR => 'ERROR', |
|
38 | - ); |
|
39 | - |
|
40 | - /** |
|
41 | - * A singleton instance for legacy logging. |
|
42 | - * |
|
43 | - * @since 3.10.0 |
|
44 | - * @access private |
|
45 | - * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
46 | - */ |
|
47 | - private static $instance = null; |
|
10 | + const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
11 | + |
|
12 | + const ERROR = 4; |
|
13 | + const WARN = 3; |
|
14 | + const INFO = 2; |
|
15 | + const DEBUG = 1; |
|
16 | + const TRACE = 0; |
|
17 | + |
|
18 | + /** |
|
19 | + * The class related to the logs. |
|
20 | + * |
|
21 | + * @since 1.0.0 |
|
22 | + * @access private |
|
23 | + * @var string $class_name The class related to the logs. |
|
24 | + */ |
|
25 | + private $class_name; |
|
26 | + |
|
27 | + /** |
|
28 | + * The log levels for printing in log lines. |
|
29 | + * |
|
30 | + * @var array $levels An array of log levels. |
|
31 | + */ |
|
32 | + private static $levels = array( |
|
33 | + self::TRACE => 'TRACE', |
|
34 | + self::DEBUG => 'DEBUG', |
|
35 | + self::INFO => 'INFO', |
|
36 | + self::WARN => 'WARN', |
|
37 | + self::ERROR => 'ERROR', |
|
38 | + ); |
|
39 | + |
|
40 | + /** |
|
41 | + * A singleton instance for legacy logging. |
|
42 | + * |
|
43 | + * @since 3.10.0 |
|
44 | + * @access private |
|
45 | + * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
46 | + */ |
|
47 | + private static $instance = null; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Create an instance of the Log service. |
|
51 | - * |
|
52 | - * @param string $class_name The class related to the logs. |
|
53 | - * |
|
54 | - * @since 1.0.0 |
|
55 | - */ |
|
56 | - public function __construct( $class_name ) { |
|
49 | + /** |
|
50 | + * Create an instance of the Log service. |
|
51 | + * |
|
52 | + * @param string $class_name The class related to the logs. |
|
53 | + * |
|
54 | + * @since 1.0.0 |
|
55 | + */ |
|
56 | + public function __construct( $class_name ) { |
|
57 | 57 | |
58 | - $this->class_name = $class_name; |
|
58 | + $this->class_name = $class_name; |
|
59 | 59 | |
60 | - } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get the ROOT logger. |
|
64 | - * |
|
65 | - * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
66 | - * @since 3.10.0 |
|
67 | - */ |
|
68 | - public static function get_instance() { |
|
62 | + /** |
|
63 | + * Get the ROOT logger. |
|
64 | + * |
|
65 | + * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
66 | + * @since 3.10.0 |
|
67 | + */ |
|
68 | + public static function get_instance() { |
|
69 | 69 | |
70 | - if ( ! isset( self::$instance ) ) { |
|
71 | - self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
72 | - } |
|
70 | + if ( ! isset( self::$instance ) ) { |
|
71 | + self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
72 | + } |
|
73 | 73 | |
74 | - return self::$instance; |
|
75 | - } |
|
74 | + return self::$instance; |
|
75 | + } |
|
76 | 76 | |
77 | - public static function get_logger( $class_name ) { |
|
77 | + public static function get_logger( $class_name ) { |
|
78 | 78 | |
79 | - return new Wordlift_Log_Service( $class_name ); |
|
79 | + return new Wordlift_Log_Service( $class_name ); |
|
80 | 80 | |
81 | - } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Log a message. |
|
85 | - * |
|
86 | - * @param string $level The log level. |
|
87 | - * @param string $message The message to log. |
|
88 | - * |
|
89 | - * @since 1.0.0 |
|
90 | - */ |
|
91 | - public function log( $level, $message ) { |
|
83 | + /** |
|
84 | + * Log a message. |
|
85 | + * |
|
86 | + * @param string $level The log level. |
|
87 | + * @param string $message The message to log. |
|
88 | + * |
|
89 | + * @since 1.0.0 |
|
90 | + */ |
|
91 | + public function log( $level, $message ) { |
|
92 | 92 | |
93 | - // echo( sprintf( self::MESSAGE_TEMPLATE . "\n", self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
93 | + // echo( sprintf( self::MESSAGE_TEMPLATE . "\n", self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
94 | 94 | |
95 | - // Bail out if `WL_DEBUG` isn't defined or it's false. |
|
96 | - if ( ! defined( 'WL_DEBUG' ) || false === WL_DEBUG ) { |
|
97 | - return; |
|
98 | - } |
|
95 | + // Bail out if `WL_DEBUG` isn't defined or it's false. |
|
96 | + if ( ! defined( 'WL_DEBUG' ) || false === WL_DEBUG ) { |
|
97 | + return; |
|
98 | + } |
|
99 | 99 | |
100 | - // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
|
101 | - if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
102 | - && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
103 | - return; |
|
104 | - } |
|
100 | + // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
|
101 | + if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
102 | + && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
103 | + return; |
|
104 | + } |
|
105 | 105 | |
106 | - // Bail out if the log message is below the minimum log level. |
|
107 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
108 | - return; |
|
109 | - } |
|
106 | + // Bail out if the log message is below the minimum log level. |
|
107 | + if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
108 | + return; |
|
109 | + } |
|
110 | 110 | |
111 | - // Bail out if there's a filter and we don't match it. |
|
112 | - $class_name = wp_slash( $this->class_name ); |
|
113 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$class_name($|,)/", WL_LOG_FILTER ) ) { |
|
114 | - return; |
|
115 | - } |
|
111 | + // Bail out if there's a filter and we don't match it. |
|
112 | + $class_name = wp_slash( $this->class_name ); |
|
113 | + if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$class_name($|,)/", WL_LOG_FILTER ) ) { |
|
114 | + return; |
|
115 | + } |
|
116 | 116 | |
117 | - // Finally log the message. |
|
118 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
119 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
117 | + // Finally log the message. |
|
118 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
119 | + error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
120 | 120 | |
121 | - } |
|
121 | + } |
|
122 | 122 | |
123 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
124 | - public function error( $message, $exception = null ) { |
|
123 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
124 | + public function error( $message, $exception = null ) { |
|
125 | 125 | |
126 | - $this->log( self::ERROR, $message ); |
|
126 | + $this->log( self::ERROR, $message ); |
|
127 | 127 | |
128 | - } |
|
128 | + } |
|
129 | 129 | |
130 | - public function warn( $message ) { |
|
130 | + public function warn( $message ) { |
|
131 | 131 | |
132 | - $this->log( self::WARN, $message ); |
|
132 | + $this->log( self::WARN, $message ); |
|
133 | 133 | |
134 | - } |
|
134 | + } |
|
135 | 135 | |
136 | - public function info( $message ) { |
|
136 | + public function info( $message ) { |
|
137 | 137 | |
138 | - $this->log( self::INFO, $message ); |
|
138 | + $this->log( self::INFO, $message ); |
|
139 | 139 | |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - public function debug( $message ) { |
|
142 | + public function debug( $message ) { |
|
143 | 143 | |
144 | - $this->log( self::DEBUG, $message ); |
|
144 | + $this->log( self::DEBUG, $message ); |
|
145 | 145 | |
146 | - } |
|
146 | + } |
|
147 | 147 | |
148 | - public function trace( $message ) { |
|
148 | + public function trace( $message ) { |
|
149 | 149 | |
150 | - $this->log( self::TRACE, $message ); |
|
150 | + $this->log( self::TRACE, $message ); |
|
151 | 151 | |
152 | - } |
|
152 | + } |
|
153 | 153 | |
154 | 154 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @since 1.0.0 |
55 | 55 | */ |
56 | - public function __construct( $class_name ) { |
|
56 | + public function __construct($class_name) { |
|
57 | 57 | |
58 | 58 | $this->class_name = $class_name; |
59 | 59 | |
@@ -67,16 +67,16 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public static function get_instance() { |
69 | 69 | |
70 | - if ( ! isset( self::$instance ) ) { |
|
71 | - self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
70 | + if ( ! isset(self::$instance)) { |
|
71 | + self::$instance = new Wordlift_Log_Service('ROOT'); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | return self::$instance; |
75 | 75 | } |
76 | 76 | |
77 | - public static function get_logger( $class_name ) { |
|
77 | + public static function get_logger($class_name) { |
|
78 | 78 | |
79 | - return new Wordlift_Log_Service( $class_name ); |
|
79 | + return new Wordlift_Log_Service($class_name); |
|
80 | 80 | |
81 | 81 | } |
82 | 82 | |
@@ -88,66 +88,66 @@ discard block |
||
88 | 88 | * |
89 | 89 | * @since 1.0.0 |
90 | 90 | */ |
91 | - public function log( $level, $message ) { |
|
91 | + public function log($level, $message) { |
|
92 | 92 | |
93 | 93 | // echo( sprintf( self::MESSAGE_TEMPLATE . "\n", self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
94 | 94 | |
95 | 95 | // Bail out if `WL_DEBUG` isn't defined or it's false. |
96 | - if ( ! defined( 'WL_DEBUG' ) || false === WL_DEBUG ) { |
|
96 | + if ( ! defined('WL_DEBUG') || false === WL_DEBUG) { |
|
97 | 97 | return; |
98 | 98 | } |
99 | 99 | |
100 | 100 | // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
101 | - if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
102 | - && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
101 | + if ( ! defined('WL_LOG_LEVEL') && $level < self::ERROR |
|
102 | + && ( ! defined('WP_DEBUG') || false === WP_DEBUG)) { |
|
103 | 103 | return; |
104 | 104 | } |
105 | 105 | |
106 | 106 | // Bail out if the log message is below the minimum log level. |
107 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
107 | + if (defined('WL_LOG_LEVEL') && $level < intval(WL_LOG_LEVEL)) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | |
111 | 111 | // Bail out if there's a filter and we don't match it. |
112 | - $class_name = wp_slash( $this->class_name ); |
|
113 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$class_name($|,)/", WL_LOG_FILTER ) ) { |
|
112 | + $class_name = wp_slash($this->class_name); |
|
113 | + if (defined('WL_LOG_FILTER') && 1 !== preg_match("/(^|,)$class_name($|,)/", WL_LOG_FILTER)) { |
|
114 | 114 | return; |
115 | 115 | } |
116 | 116 | |
117 | 117 | // Finally log the message. |
118 | 118 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
119 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
119 | + error_log(sprintf(self::MESSAGE_TEMPLATE, self::$levels[$level], $this->class_name, is_array($message) ? implode(', ', $message) : $message)); |
|
120 | 120 | |
121 | 121 | } |
122 | 122 | |
123 | 123 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
124 | - public function error( $message, $exception = null ) { |
|
124 | + public function error($message, $exception = null) { |
|
125 | 125 | |
126 | - $this->log( self::ERROR, $message ); |
|
126 | + $this->log(self::ERROR, $message); |
|
127 | 127 | |
128 | 128 | } |
129 | 129 | |
130 | - public function warn( $message ) { |
|
130 | + public function warn($message) { |
|
131 | 131 | |
132 | - $this->log( self::WARN, $message ); |
|
132 | + $this->log(self::WARN, $message); |
|
133 | 133 | |
134 | 134 | } |
135 | 135 | |
136 | - public function info( $message ) { |
|
136 | + public function info($message) { |
|
137 | 137 | |
138 | - $this->log( self::INFO, $message ); |
|
138 | + $this->log(self::INFO, $message); |
|
139 | 139 | |
140 | 140 | } |
141 | 141 | |
142 | - public function debug( $message ) { |
|
142 | + public function debug($message) { |
|
143 | 143 | |
144 | - $this->log( self::DEBUG, $message ); |
|
144 | + $this->log(self::DEBUG, $message); |
|
145 | 145 | |
146 | 146 | } |
147 | 147 | |
148 | - public function trace( $message ) { |
|
148 | + public function trace($message) { |
|
149 | 149 | |
150 | - $this->log( self::TRACE, $message ); |
|
150 | + $this->log(self::TRACE, $message); |
|
151 | 151 | |
152 | 152 | } |
153 | 153 |
@@ -16,55 +16,55 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Publisher_Ajax_Adapter { |
18 | 18 | |
19 | - /** |
|
20 | - * The {@link Wordlift_Publisher_Service} instance. |
|
21 | - * |
|
22 | - * @since 3.11.0 |
|
23 | - * @access private |
|
24 | - * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
25 | - */ |
|
26 | - private $publisher_service; |
|
19 | + /** |
|
20 | + * The {@link Wordlift_Publisher_Service} instance. |
|
21 | + * |
|
22 | + * @since 3.11.0 |
|
23 | + * @access private |
|
24 | + * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
25 | + */ |
|
26 | + private $publisher_service; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
30 | - * |
|
31 | - * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
32 | - * |
|
33 | - * @since 3.11.0 |
|
34 | - */ |
|
35 | - public function __construct( $publisher_service ) { |
|
28 | + /** |
|
29 | + * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
30 | + * |
|
31 | + * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
32 | + * |
|
33 | + * @since 3.11.0 |
|
34 | + */ |
|
35 | + public function __construct( $publisher_service ) { |
|
36 | 36 | |
37 | - $this->publisher_service = $publisher_service; |
|
37 | + $this->publisher_service = $publisher_service; |
|
38 | 38 | |
39 | - } |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
43 | - * action. |
|
44 | - * |
|
45 | - * @since 3.11.0 |
|
46 | - */ |
|
47 | - public function publisher() { |
|
41 | + /** |
|
42 | + * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
43 | + * action. |
|
44 | + * |
|
45 | + * @since 3.11.0 |
|
46 | + */ |
|
47 | + public function publisher() { |
|
48 | 48 | |
49 | - // Ensure we don't have garbage before us. |
|
50 | - ob_clean(); |
|
49 | + // Ensure we don't have garbage before us. |
|
50 | + ob_clean(); |
|
51 | 51 | |
52 | - // Check if the current user can `manage_options`. |
|
53 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
54 | - wp_send_json_error( 'Access denied.' ); |
|
55 | - } |
|
52 | + // Check if the current user can `manage_options`. |
|
53 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
54 | + wp_send_json_error( 'Access denied.' ); |
|
55 | + } |
|
56 | 56 | |
57 | - // No actual search parameter was passed, bail out. |
|
58 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | - wp_send_json_error( 'The q parameter is required.' ); |
|
60 | - } |
|
57 | + // No actual search parameter was passed, bail out. |
|
58 | + if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | + wp_send_json_error( 'The q parameter is required.' ); |
|
60 | + } |
|
61 | 61 | |
62 | - // Get the response. |
|
63 | - $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
62 | + // Get the response. |
|
63 | + $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
64 | 64 | |
65 | - // Finally output the response. |
|
66 | - wp_send_json_success( $response ); |
|
65 | + // Finally output the response. |
|
66 | + wp_send_json_success( $response ); |
|
67 | 67 | |
68 | - } |
|
68 | + } |
|
69 | 69 | |
70 | 70 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @since 3.11.0 |
34 | 34 | */ |
35 | - public function __construct( $publisher_service ) { |
|
35 | + public function __construct($publisher_service) { |
|
36 | 36 | |
37 | 37 | $this->publisher_service = $publisher_service; |
38 | 38 | |
@@ -50,20 +50,20 @@ discard block |
||
50 | 50 | ob_clean(); |
51 | 51 | |
52 | 52 | // Check if the current user can `manage_options`. |
53 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
54 | - wp_send_json_error( 'Access denied.' ); |
|
53 | + if ( ! current_user_can('manage_options')) { |
|
54 | + wp_send_json_error('Access denied.'); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | // No actual search parameter was passed, bail out. |
58 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | - wp_send_json_error( 'The q parameter is required.' ); |
|
58 | + if ( ! isset($_POST['q']) || empty($_POST['q'])) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | + wp_send_json_error('The q parameter is required.'); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | // Get the response. |
63 | - $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
63 | + $response = $this->publisher_service->query(sanitize_text_field(wp_unslash((string) $_POST['q']))); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
64 | 64 | |
65 | 65 | // Finally output the response. |
66 | - wp_send_json_success( $response ); |
|
66 | + wp_send_json_success($response); |
|
67 | 67 | |
68 | 68 | } |
69 | 69 |
@@ -7,172 +7,172 @@ |
||
7 | 7 | */ |
8 | 8 | class Wordlift_Topic_Taxonomy_Service { |
9 | 9 | |
10 | - /** |
|
11 | - * The Log service. |
|
12 | - * |
|
13 | - * @since 3.5.0 |
|
14 | - * @access private |
|
15 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
16 | - */ |
|
17 | - private $log_service; |
|
18 | - |
|
19 | - /** |
|
20 | - * Taxonomy name. |
|
21 | - * |
|
22 | - * @since 3.5.0 |
|
23 | - */ |
|
24 | - const TAXONOMY_NAME = 'wl_topic'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Taxonomy object type. |
|
28 | - * |
|
29 | - * @since 3.5.0 |
|
30 | - */ |
|
31 | - const TAXONOMY_OBJECT_TYPE = 'post'; |
|
32 | - |
|
33 | - /** |
|
34 | - * Taxonomy slug. |
|
35 | - * |
|
36 | - * @since 3.5.0 |
|
37 | - */ |
|
38 | - const TAXONOMY_SLUG = 'wl_topic'; |
|
39 | - |
|
40 | - /** |
|
41 | - * A singleton instance of the Wordlift_Topic_Taxonomy_Service service. |
|
42 | - * |
|
43 | - * @since 3.5.0 |
|
44 | - * @access private |
|
45 | - * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service. |
|
46 | - */ |
|
47 | - private static $instance; |
|
48 | - |
|
49 | - /** |
|
50 | - * Create a Wordlift_Topic_Taxonomy_Service instance. |
|
51 | - * |
|
52 | - * @since 3.5.0 |
|
53 | - */ |
|
54 | - public function __construct() { |
|
55 | - |
|
56 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
57 | - |
|
58 | - // Set the singleton instance. |
|
59 | - self::$instance = $this; |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Get the singleton instance of the Entity service. |
|
65 | - * |
|
66 | - * @since 3.5.0 |
|
67 | - * @return Wordlift_Topic_Taxonomy_Service |
|
68 | - */ |
|
69 | - public static function get_instance() { |
|
70 | - |
|
71 | - return self::$instance; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Just register the topic taxonomy. |
|
76 | - * |
|
77 | - * @since 3.5.0 |
|
78 | - */ |
|
79 | - public function init() { |
|
80 | - |
|
81 | - // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
|
82 | - $labels = array( |
|
83 | - 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
84 | - 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
85 | - 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
86 | - 'all_items' => __( 'All Topics', 'wordlift' ), |
|
87 | - 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
88 | - 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
89 | - 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
90 | - 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
91 | - 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
92 | - 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
93 | - 'menu_name' => __( 'Topics', 'wordlift' ), |
|
94 | - ); |
|
95 | - |
|
96 | - $capabilities = array( |
|
97 | - 'manage_terms' => null, |
|
98 | - 'edit_terms' => null, |
|
99 | - 'delete_terms' => null, |
|
100 | - 'assign_terms' => 'edit_posts', |
|
101 | - ); |
|
102 | - |
|
103 | - $args = array( |
|
104 | - 'labels' => $labels, |
|
105 | - 'capabilities' => $capabilities, |
|
106 | - 'hierarchical' => true, |
|
107 | - 'show_admin_column' => false, |
|
108 | - 'show_ui' => false, |
|
109 | - 'rewrite' => array( |
|
110 | - 'slug' => self::TAXONOMY_SLUG, |
|
111 | - ), |
|
112 | - ); |
|
113 | - |
|
114 | - // Register taxonomy |
|
115 | - register_taxonomy( |
|
116 | - self::TAXONOMY_NAME, |
|
117 | - self::TAXONOMY_OBJECT_TYPE, |
|
118 | - $args |
|
119 | - ); |
|
120 | - |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Get or create a taxonomy term from a given entity topic. |
|
125 | - * |
|
126 | - * @since 3.5.0 |
|
127 | - */ |
|
128 | - public function get_or_create_term_from_topic_entity( $topic ) { |
|
129 | - |
|
130 | - // Define taxonomy term slug |
|
131 | - $term_slug = sanitize_title( $topic->post_title ); |
|
132 | - // Look for an existing taxonomy term with a given slug |
|
133 | - $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
134 | - if ( $term ) { |
|
135 | - return (int) $term->term_id; |
|
136 | - } |
|
137 | - // Otherwise create a new term and return it |
|
138 | - $result = wp_insert_term( |
|
139 | - $topic->post_title, |
|
140 | - self::TAXONOMY_NAME, |
|
141 | - array( |
|
142 | - 'slug' => $term_slug, |
|
143 | - 'description' => $topic->post_content, |
|
144 | - ) |
|
145 | - ); |
|
146 | - |
|
147 | - return (int) $result['term_id']; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Set a topic for a given post. |
|
152 | - * |
|
153 | - * @since 3.5.0 |
|
154 | - */ |
|
155 | - public function set_topic_for( $post_id, $topic_id ) { |
|
156 | - // Retrieve the topic entity post |
|
157 | - $topic_entity_post = get_post( $topic_id ); |
|
158 | - // If current topic does not exist in db false is returned |
|
159 | - if ( null === $topic_entity_post ) { |
|
160 | - return false; |
|
161 | - } |
|
162 | - // Create the proper taxonomy term if needed |
|
163 | - $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
164 | - // Link the term to the current post |
|
165 | - wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
166 | - return true; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Unlink any topic for a given post. |
|
171 | - * |
|
172 | - * @since 3.5.0 |
|
173 | - */ |
|
174 | - public function unlink_topic_for( $post_id ) { |
|
175 | - wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
176 | - } |
|
10 | + /** |
|
11 | + * The Log service. |
|
12 | + * |
|
13 | + * @since 3.5.0 |
|
14 | + * @access private |
|
15 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
16 | + */ |
|
17 | + private $log_service; |
|
18 | + |
|
19 | + /** |
|
20 | + * Taxonomy name. |
|
21 | + * |
|
22 | + * @since 3.5.0 |
|
23 | + */ |
|
24 | + const TAXONOMY_NAME = 'wl_topic'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Taxonomy object type. |
|
28 | + * |
|
29 | + * @since 3.5.0 |
|
30 | + */ |
|
31 | + const TAXONOMY_OBJECT_TYPE = 'post'; |
|
32 | + |
|
33 | + /** |
|
34 | + * Taxonomy slug. |
|
35 | + * |
|
36 | + * @since 3.5.0 |
|
37 | + */ |
|
38 | + const TAXONOMY_SLUG = 'wl_topic'; |
|
39 | + |
|
40 | + /** |
|
41 | + * A singleton instance of the Wordlift_Topic_Taxonomy_Service service. |
|
42 | + * |
|
43 | + * @since 3.5.0 |
|
44 | + * @access private |
|
45 | + * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service. |
|
46 | + */ |
|
47 | + private static $instance; |
|
48 | + |
|
49 | + /** |
|
50 | + * Create a Wordlift_Topic_Taxonomy_Service instance. |
|
51 | + * |
|
52 | + * @since 3.5.0 |
|
53 | + */ |
|
54 | + public function __construct() { |
|
55 | + |
|
56 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
57 | + |
|
58 | + // Set the singleton instance. |
|
59 | + self::$instance = $this; |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Get the singleton instance of the Entity service. |
|
65 | + * |
|
66 | + * @since 3.5.0 |
|
67 | + * @return Wordlift_Topic_Taxonomy_Service |
|
68 | + */ |
|
69 | + public static function get_instance() { |
|
70 | + |
|
71 | + return self::$instance; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Just register the topic taxonomy. |
|
76 | + * |
|
77 | + * @since 3.5.0 |
|
78 | + */ |
|
79 | + public function init() { |
|
80 | + |
|
81 | + // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
|
82 | + $labels = array( |
|
83 | + 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
84 | + 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
85 | + 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
86 | + 'all_items' => __( 'All Topics', 'wordlift' ), |
|
87 | + 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
88 | + 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
89 | + 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
90 | + 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
91 | + 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
92 | + 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
93 | + 'menu_name' => __( 'Topics', 'wordlift' ), |
|
94 | + ); |
|
95 | + |
|
96 | + $capabilities = array( |
|
97 | + 'manage_terms' => null, |
|
98 | + 'edit_terms' => null, |
|
99 | + 'delete_terms' => null, |
|
100 | + 'assign_terms' => 'edit_posts', |
|
101 | + ); |
|
102 | + |
|
103 | + $args = array( |
|
104 | + 'labels' => $labels, |
|
105 | + 'capabilities' => $capabilities, |
|
106 | + 'hierarchical' => true, |
|
107 | + 'show_admin_column' => false, |
|
108 | + 'show_ui' => false, |
|
109 | + 'rewrite' => array( |
|
110 | + 'slug' => self::TAXONOMY_SLUG, |
|
111 | + ), |
|
112 | + ); |
|
113 | + |
|
114 | + // Register taxonomy |
|
115 | + register_taxonomy( |
|
116 | + self::TAXONOMY_NAME, |
|
117 | + self::TAXONOMY_OBJECT_TYPE, |
|
118 | + $args |
|
119 | + ); |
|
120 | + |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Get or create a taxonomy term from a given entity topic. |
|
125 | + * |
|
126 | + * @since 3.5.0 |
|
127 | + */ |
|
128 | + public function get_or_create_term_from_topic_entity( $topic ) { |
|
129 | + |
|
130 | + // Define taxonomy term slug |
|
131 | + $term_slug = sanitize_title( $topic->post_title ); |
|
132 | + // Look for an existing taxonomy term with a given slug |
|
133 | + $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
134 | + if ( $term ) { |
|
135 | + return (int) $term->term_id; |
|
136 | + } |
|
137 | + // Otherwise create a new term and return it |
|
138 | + $result = wp_insert_term( |
|
139 | + $topic->post_title, |
|
140 | + self::TAXONOMY_NAME, |
|
141 | + array( |
|
142 | + 'slug' => $term_slug, |
|
143 | + 'description' => $topic->post_content, |
|
144 | + ) |
|
145 | + ); |
|
146 | + |
|
147 | + return (int) $result['term_id']; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Set a topic for a given post. |
|
152 | + * |
|
153 | + * @since 3.5.0 |
|
154 | + */ |
|
155 | + public function set_topic_for( $post_id, $topic_id ) { |
|
156 | + // Retrieve the topic entity post |
|
157 | + $topic_entity_post = get_post( $topic_id ); |
|
158 | + // If current topic does not exist in db false is returned |
|
159 | + if ( null === $topic_entity_post ) { |
|
160 | + return false; |
|
161 | + } |
|
162 | + // Create the proper taxonomy term if needed |
|
163 | + $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
164 | + // Link the term to the current post |
|
165 | + wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
166 | + return true; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Unlink any topic for a given post. |
|
171 | + * |
|
172 | + * @since 3.5.0 |
|
173 | + */ |
|
174 | + public function unlink_topic_for( $post_id ) { |
|
175 | + wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
176 | + } |
|
177 | 177 | |
178 | 178 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function __construct() { |
55 | 55 | |
56 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
56 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
57 | 57 | |
58 | 58 | // Set the singleton instance. |
59 | 59 | self::$instance = $this; |
@@ -80,17 +80,17 @@ discard block |
||
80 | 80 | |
81 | 81 | // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
82 | 82 | $labels = array( |
83 | - 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
84 | - 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
85 | - 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
86 | - 'all_items' => __( 'All Topics', 'wordlift' ), |
|
87 | - 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
88 | - 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
89 | - 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
90 | - 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
91 | - 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
92 | - 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
93 | - 'menu_name' => __( 'Topics', 'wordlift' ), |
|
83 | + 'name' => _x('Topics', 'taxonomy general name', 'wordlift'), |
|
84 | + 'singular_name' => _x('Topic', 'taxonomy singular name', 'wordlift'), |
|
85 | + 'search_items' => __('Search Topics', 'wordlift'), |
|
86 | + 'all_items' => __('All Topics', 'wordlift'), |
|
87 | + 'parent_item' => __('Parent Topic', 'wordlift'), |
|
88 | + 'parent_item_colon' => __('Parent Topic:', 'wordlift'), |
|
89 | + 'edit_item' => __('Edit Topic', 'wordlift'), |
|
90 | + 'update_item' => __('Update Topic', 'wordlift'), |
|
91 | + 'add_new_item' => __('Add New Topic', 'wordlift'), |
|
92 | + 'new_item_name' => __('New Topic', 'wordlift'), |
|
93 | + 'menu_name' => __('Topics', 'wordlift'), |
|
94 | 94 | ); |
95 | 95 | |
96 | 96 | $capabilities = array( |
@@ -125,13 +125,13 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @since 3.5.0 |
127 | 127 | */ |
128 | - public function get_or_create_term_from_topic_entity( $topic ) { |
|
128 | + public function get_or_create_term_from_topic_entity($topic) { |
|
129 | 129 | |
130 | 130 | // Define taxonomy term slug |
131 | - $term_slug = sanitize_title( $topic->post_title ); |
|
131 | + $term_slug = sanitize_title($topic->post_title); |
|
132 | 132 | // Look for an existing taxonomy term with a given slug |
133 | - $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
134 | - if ( $term ) { |
|
133 | + $term = get_term_by('slug', $term_slug, self::TAXONOMY_NAME); |
|
134 | + if ($term) { |
|
135 | 135 | return (int) $term->term_id; |
136 | 136 | } |
137 | 137 | // Otherwise create a new term and return it |
@@ -152,17 +152,17 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @since 3.5.0 |
154 | 154 | */ |
155 | - public function set_topic_for( $post_id, $topic_id ) { |
|
155 | + public function set_topic_for($post_id, $topic_id) { |
|
156 | 156 | // Retrieve the topic entity post |
157 | - $topic_entity_post = get_post( $topic_id ); |
|
157 | + $topic_entity_post = get_post($topic_id); |
|
158 | 158 | // If current topic does not exist in db false is returned |
159 | - if ( null === $topic_entity_post ) { |
|
159 | + if (null === $topic_entity_post) { |
|
160 | 160 | return false; |
161 | 161 | } |
162 | 162 | // Create the proper taxonomy term if needed |
163 | - $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
163 | + $term_id = $this->get_or_create_term_from_topic_entity($topic_entity_post); |
|
164 | 164 | // Link the term to the current post |
165 | - wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
165 | + wp_set_post_terms($post_id, $term_id, self::TAXONOMY_NAME, false); |
|
166 | 166 | return true; |
167 | 167 | } |
168 | 168 | |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | * |
172 | 172 | * @since 3.5.0 |
173 | 173 | */ |
174 | - public function unlink_topic_for( $post_id ) { |
|
175 | - wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
174 | + public function unlink_topic_for($post_id) { |
|
175 | + wp_delete_object_term_relationships($post_id, self::TAXONOMY_NAME); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | } |
@@ -14,369 +14,369 @@ |
||
14 | 14 | */ |
15 | 15 | class Wordlift_Rating_Service { |
16 | 16 | |
17 | - /** |
|
18 | - * Entity rating max. |
|
19 | - * |
|
20 | - * @since 3.3.0 |
|
21 | - */ |
|
22 | - const RATING_MAX = 7; |
|
23 | - |
|
24 | - /** |
|
25 | - * Entity rating score meta key. |
|
26 | - * |
|
27 | - * @since 3.3.0 |
|
28 | - */ |
|
29 | - const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Entity rating warnings meta key. |
|
33 | - * |
|
34 | - * @since 3.3.0 |
|
35 | - */ |
|
36 | - const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
37 | - |
|
38 | - /** |
|
39 | - * Entity warning has related post identifier. |
|
40 | - * |
|
41 | - * @since 3.3.0 |
|
42 | - */ |
|
43 | - const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
44 | - |
|
45 | - /** |
|
46 | - * Entity warning has content post identifier. |
|
47 | - * |
|
48 | - * @since 3.3.0 |
|
49 | - */ |
|
50 | - const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
51 | - |
|
52 | - /** |
|
53 | - * Entity warning has related entities identifier. |
|
54 | - * |
|
55 | - * @since 3.3.0 |
|
56 | - */ |
|
57 | - const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
58 | - |
|
59 | - /** |
|
60 | - * Entity warning is published identifier. |
|
61 | - * |
|
62 | - * @since 3.3.0 |
|
63 | - */ |
|
64 | - const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
65 | - |
|
66 | - /** |
|
67 | - * Entity warning has thumbnail identifier. |
|
68 | - * |
|
69 | - * @since 3.3.0 |
|
70 | - */ |
|
71 | - const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
72 | - |
|
73 | - /** |
|
74 | - * Entity warning has same as identifier. |
|
75 | - * |
|
76 | - * @since 3.3.0 |
|
77 | - */ |
|
78 | - const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
79 | - |
|
80 | - /** |
|
81 | - * Entity warning has completed metadata identifier. |
|
82 | - * |
|
83 | - * @since 3.3.0 |
|
84 | - */ |
|
85 | - const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
86 | - |
|
87 | - /** |
|
88 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
89 | - * |
|
90 | - * @since 3.10.0 |
|
91 | - * @access private |
|
92 | - * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
93 | - */ |
|
94 | - private $entity_type_service; |
|
95 | - |
|
96 | - /** |
|
97 | - * The Notice service. |
|
98 | - * |
|
99 | - * @since 3.3.0 |
|
100 | - * @access private |
|
101 | - * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
102 | - */ |
|
103 | - private $notice_service; |
|
104 | - |
|
105 | - /** |
|
106 | - * A {@link Wordlift_Log_Service} instance. |
|
107 | - * |
|
108 | - * @since 3.10.0 |
|
109 | - * @access private |
|
110 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
111 | - */ |
|
112 | - private $log; |
|
113 | - |
|
114 | - /** |
|
115 | - * Create a {@link Wordlift_Rating_Service} instance. |
|
116 | - * |
|
117 | - * @since 3.10.0 |
|
118 | - */ |
|
119 | - protected function __construct() { |
|
120 | - |
|
121 | - $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
122 | - $this->notice_service = Wordlift_Notice_Service::get_instance(); |
|
123 | - |
|
124 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
125 | - |
|
126 | - } |
|
127 | - |
|
128 | - private static $instance; |
|
129 | - |
|
130 | - public static function get_instance() { |
|
131 | - if ( ! isset( self::$instance ) ) { |
|
132 | - self::$instance = new self(); |
|
133 | - } |
|
134 | - |
|
135 | - return self::$instance; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Set rating for a given entity. |
|
140 | - * |
|
141 | - * @param int $post_id The entity post id. |
|
142 | - * |
|
143 | - * @return array An array representing the rating obj. |
|
144 | - * @since 3.3.0 |
|
145 | - */ |
|
146 | - public function set_rating_for( $post_id ) { |
|
147 | - |
|
148 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
149 | - if ( ! apply_filters( 'wl_feature__enable__entity-rating', true ) ) { |
|
150 | - return; |
|
151 | - } |
|
152 | - |
|
153 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
154 | - return; |
|
155 | - } |
|
156 | - |
|
157 | - // Calculate rating for the given post. |
|
158 | - $rating = $this->calculate_rating_for( $post_id ); |
|
159 | - |
|
160 | - // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
161 | - // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
162 | - // |
|
163 | - // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
164 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
165 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
166 | - |
|
167 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
168 | - |
|
169 | - // Finally returns the rating |
|
170 | - return $rating; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Get or calculate rating for a given entity |
|
175 | - * |
|
176 | - * @param int $post_id The entity post id. |
|
177 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
178 | - * |
|
179 | - * @return array An array representing the rating obj. |
|
180 | - * @since 3.3.0 |
|
181 | - */ |
|
182 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
183 | - |
|
184 | - // If forced reload is required or rating is missing. |
|
185 | - if ( $force_reload ) { |
|
186 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
187 | - |
|
188 | - return $this->set_rating_for( $post_id ); |
|
189 | - } |
|
190 | - |
|
191 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
192 | - |
|
193 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
194 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
195 | - |
|
196 | - return $this->set_rating_for( $post_id ); |
|
197 | - } |
|
198 | - |
|
199 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
200 | - |
|
201 | - // Finally return score and warnings |
|
202 | - return array( |
|
203 | - 'raw_score' => $current_raw_score, |
|
204 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
205 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
206 | - 'warnings' => $current_warnings, |
|
207 | - ); |
|
208 | - |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Calculate rating for a given entity. |
|
213 | - * |
|
214 | - * Rating depends from following criteria: |
|
215 | - * |
|
216 | - * 1. Is the current entity related to at least 1 post? |
|
217 | - * 2. Is the current entity content post not empty? |
|
218 | - * 3. Is the current entity related to at least 1 entity? |
|
219 | - * 4. Is the entity published? |
|
220 | - * 5. There is a a thumbnail associated to the entity? |
|
221 | - * 6. Has the entity a sameas defined? |
|
222 | - * 7. Are all schema.org required metadata compiled? |
|
223 | - * |
|
224 | - * Each positive check means +1 in terms of rating score. |
|
225 | - * |
|
226 | - * @param int $post_id The entity post id. |
|
227 | - * |
|
228 | - * @return array An array representing the rating obj. |
|
229 | - * @since 3.3.0 |
|
230 | - */ |
|
231 | - private function calculate_rating_for( $post_id ) { |
|
232 | - |
|
233 | - // If it's not an entity, return. |
|
234 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
235 | - return array(); |
|
236 | - } |
|
237 | - // Retrieve the post object. |
|
238 | - $post = get_post( $post_id ); |
|
239 | - |
|
240 | - // Rating value. |
|
241 | - $score = 0; |
|
242 | - |
|
243 | - // Store warning messages. |
|
244 | - $warnings = array(); |
|
245 | - |
|
246 | - // Is the current entity related to at least 1 post? |
|
247 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
248 | - $score ++ : |
|
249 | - array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
250 | - |
|
251 | - // Is the post content not empty? |
|
252 | - ( ! empty( $post->post_content ) ) ? |
|
253 | - $score ++ : |
|
254 | - array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
255 | - |
|
256 | - // Is the current entity related to at least 1 entity? |
|
257 | - // Was the current entity already disambiguated? |
|
258 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
259 | - $score ++ : |
|
260 | - array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
261 | - |
|
262 | - // Is the entity published? |
|
263 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
264 | - $score ++ : |
|
265 | - array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
266 | - |
|
267 | - // Has a thumbnail? |
|
268 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
269 | - $score ++ : |
|
270 | - array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
271 | - |
|
272 | - // Get all post meta keys for the current post |
|
273 | - global $wpdb; |
|
274 | - |
|
275 | - // Check intersection between available meta keys and expected ones |
|
276 | - // arrays to detect missing values. |
|
277 | - $available_meta_keys = $wpdb->get_col( |
|
278 | - $wpdb->prepare( |
|
279 | - "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", |
|
280 | - $post->ID |
|
281 | - ) |
|
282 | - ); |
|
283 | - |
|
284 | - // If each expected key is contained in available keys array ... |
|
285 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true ) ) ? |
|
286 | - $score ++ : |
|
287 | - array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
288 | - |
|
289 | - $schema = $this->entity_type_service->get( $post_id ); |
|
290 | - |
|
291 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
292 | - array() : |
|
293 | - array_keys( $schema['custom_fields'] ); |
|
294 | - |
|
295 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
296 | - // If each expected key is contained in available keys array ... |
|
297 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
298 | - $score ++ : |
|
299 | - array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
300 | - |
|
301 | - // Finally return score and warnings |
|
302 | - return array( |
|
303 | - 'raw_score' => $score, |
|
304 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
305 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
306 | - 'warnings' => $warnings, |
|
307 | - ); |
|
308 | - |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Get as rating as input and convert in a traffic-light rating |
|
313 | - * |
|
314 | - * @param int $score The rating score for a given entity. |
|
315 | - * |
|
316 | - * @return string The input HTML code. |
|
317 | - * @since 3.3.0 |
|
318 | - */ |
|
319 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
320 | - // RATING_MAX : $score = 3 : x |
|
321 | - // See http://php.net/manual/en/function.round.php |
|
322 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
323 | - |
|
324 | - // If rating is 0, return 1, otherwise return rating |
|
325 | - return ( 0 === $rating ) ? 1 : $rating; |
|
326 | - |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * Get as rating as input and convert in a traffic-light rating |
|
331 | - * |
|
332 | - * @param int $score The rating score for a given entity. |
|
333 | - * |
|
334 | - * @return string The input HTML code. |
|
335 | - * @since 3.3.0 |
|
336 | - */ |
|
337 | - public function convert_raw_score_to_percentage( $score ) { |
|
338 | - |
|
339 | - // RATING_MAX : $score = 100 : x |
|
340 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Add admin notices for the current entity depending on the current rating. |
|
345 | - * |
|
346 | - * @since 3.3.0 |
|
347 | - */ |
|
348 | - public function in_admin_header() { |
|
349 | - |
|
350 | - // Return safely if get_current_screen() is not defined (yet) |
|
351 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
352 | - return; |
|
353 | - } |
|
354 | - |
|
355 | - $screen = get_current_screen(); |
|
356 | - // If there is any valid screen nothing to do |
|
357 | - if ( null === $screen ) { |
|
358 | - return; |
|
359 | - } |
|
360 | - |
|
361 | - // If you're not in the entity post edit page, return. |
|
362 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
363 | - return; |
|
364 | - } |
|
365 | - // Retrieve the current global post |
|
366 | - global $post; |
|
367 | - // If it's not an entity, return. |
|
368 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post->ID ) ) { |
|
369 | - return; |
|
370 | - } |
|
371 | - // Retrieve an updated rating for the current entity |
|
372 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
373 | - |
|
374 | - // If there is at least 1 warning |
|
375 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
376 | - // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
377 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
378 | - } |
|
379 | - |
|
380 | - } |
|
17 | + /** |
|
18 | + * Entity rating max. |
|
19 | + * |
|
20 | + * @since 3.3.0 |
|
21 | + */ |
|
22 | + const RATING_MAX = 7; |
|
23 | + |
|
24 | + /** |
|
25 | + * Entity rating score meta key. |
|
26 | + * |
|
27 | + * @since 3.3.0 |
|
28 | + */ |
|
29 | + const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Entity rating warnings meta key. |
|
33 | + * |
|
34 | + * @since 3.3.0 |
|
35 | + */ |
|
36 | + const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
37 | + |
|
38 | + /** |
|
39 | + * Entity warning has related post identifier. |
|
40 | + * |
|
41 | + * @since 3.3.0 |
|
42 | + */ |
|
43 | + const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
44 | + |
|
45 | + /** |
|
46 | + * Entity warning has content post identifier. |
|
47 | + * |
|
48 | + * @since 3.3.0 |
|
49 | + */ |
|
50 | + const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
51 | + |
|
52 | + /** |
|
53 | + * Entity warning has related entities identifier. |
|
54 | + * |
|
55 | + * @since 3.3.0 |
|
56 | + */ |
|
57 | + const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
58 | + |
|
59 | + /** |
|
60 | + * Entity warning is published identifier. |
|
61 | + * |
|
62 | + * @since 3.3.0 |
|
63 | + */ |
|
64 | + const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
65 | + |
|
66 | + /** |
|
67 | + * Entity warning has thumbnail identifier. |
|
68 | + * |
|
69 | + * @since 3.3.0 |
|
70 | + */ |
|
71 | + const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
72 | + |
|
73 | + /** |
|
74 | + * Entity warning has same as identifier. |
|
75 | + * |
|
76 | + * @since 3.3.0 |
|
77 | + */ |
|
78 | + const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
79 | + |
|
80 | + /** |
|
81 | + * Entity warning has completed metadata identifier. |
|
82 | + * |
|
83 | + * @since 3.3.0 |
|
84 | + */ |
|
85 | + const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
86 | + |
|
87 | + /** |
|
88 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
89 | + * |
|
90 | + * @since 3.10.0 |
|
91 | + * @access private |
|
92 | + * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
93 | + */ |
|
94 | + private $entity_type_service; |
|
95 | + |
|
96 | + /** |
|
97 | + * The Notice service. |
|
98 | + * |
|
99 | + * @since 3.3.0 |
|
100 | + * @access private |
|
101 | + * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
102 | + */ |
|
103 | + private $notice_service; |
|
104 | + |
|
105 | + /** |
|
106 | + * A {@link Wordlift_Log_Service} instance. |
|
107 | + * |
|
108 | + * @since 3.10.0 |
|
109 | + * @access private |
|
110 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
111 | + */ |
|
112 | + private $log; |
|
113 | + |
|
114 | + /** |
|
115 | + * Create a {@link Wordlift_Rating_Service} instance. |
|
116 | + * |
|
117 | + * @since 3.10.0 |
|
118 | + */ |
|
119 | + protected function __construct() { |
|
120 | + |
|
121 | + $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
122 | + $this->notice_service = Wordlift_Notice_Service::get_instance(); |
|
123 | + |
|
124 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
125 | + |
|
126 | + } |
|
127 | + |
|
128 | + private static $instance; |
|
129 | + |
|
130 | + public static function get_instance() { |
|
131 | + if ( ! isset( self::$instance ) ) { |
|
132 | + self::$instance = new self(); |
|
133 | + } |
|
134 | + |
|
135 | + return self::$instance; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Set rating for a given entity. |
|
140 | + * |
|
141 | + * @param int $post_id The entity post id. |
|
142 | + * |
|
143 | + * @return array An array representing the rating obj. |
|
144 | + * @since 3.3.0 |
|
145 | + */ |
|
146 | + public function set_rating_for( $post_id ) { |
|
147 | + |
|
148 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
149 | + if ( ! apply_filters( 'wl_feature__enable__entity-rating', true ) ) { |
|
150 | + return; |
|
151 | + } |
|
152 | + |
|
153 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
154 | + return; |
|
155 | + } |
|
156 | + |
|
157 | + // Calculate rating for the given post. |
|
158 | + $rating = $this->calculate_rating_for( $post_id ); |
|
159 | + |
|
160 | + // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
161 | + // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
162 | + // |
|
163 | + // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
164 | + update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
165 | + update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
166 | + |
|
167 | + $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
168 | + |
|
169 | + // Finally returns the rating |
|
170 | + return $rating; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Get or calculate rating for a given entity |
|
175 | + * |
|
176 | + * @param int $post_id The entity post id. |
|
177 | + * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
178 | + * |
|
179 | + * @return array An array representing the rating obj. |
|
180 | + * @since 3.3.0 |
|
181 | + */ |
|
182 | + public function get_rating_for( $post_id, $force_reload = false ) { |
|
183 | + |
|
184 | + // If forced reload is required or rating is missing. |
|
185 | + if ( $force_reload ) { |
|
186 | + $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
187 | + |
|
188 | + return $this->set_rating_for( $post_id ); |
|
189 | + } |
|
190 | + |
|
191 | + $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
192 | + |
|
193 | + if ( ! is_numeric( $current_raw_score ) ) { |
|
194 | + $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
195 | + |
|
196 | + return $this->set_rating_for( $post_id ); |
|
197 | + } |
|
198 | + |
|
199 | + $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
200 | + |
|
201 | + // Finally return score and warnings |
|
202 | + return array( |
|
203 | + 'raw_score' => $current_raw_score, |
|
204 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
205 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
206 | + 'warnings' => $current_warnings, |
|
207 | + ); |
|
208 | + |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Calculate rating for a given entity. |
|
213 | + * |
|
214 | + * Rating depends from following criteria: |
|
215 | + * |
|
216 | + * 1. Is the current entity related to at least 1 post? |
|
217 | + * 2. Is the current entity content post not empty? |
|
218 | + * 3. Is the current entity related to at least 1 entity? |
|
219 | + * 4. Is the entity published? |
|
220 | + * 5. There is a a thumbnail associated to the entity? |
|
221 | + * 6. Has the entity a sameas defined? |
|
222 | + * 7. Are all schema.org required metadata compiled? |
|
223 | + * |
|
224 | + * Each positive check means +1 in terms of rating score. |
|
225 | + * |
|
226 | + * @param int $post_id The entity post id. |
|
227 | + * |
|
228 | + * @return array An array representing the rating obj. |
|
229 | + * @since 3.3.0 |
|
230 | + */ |
|
231 | + private function calculate_rating_for( $post_id ) { |
|
232 | + |
|
233 | + // If it's not an entity, return. |
|
234 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
235 | + return array(); |
|
236 | + } |
|
237 | + // Retrieve the post object. |
|
238 | + $post = get_post( $post_id ); |
|
239 | + |
|
240 | + // Rating value. |
|
241 | + $score = 0; |
|
242 | + |
|
243 | + // Store warning messages. |
|
244 | + $warnings = array(); |
|
245 | + |
|
246 | + // Is the current entity related to at least 1 post? |
|
247 | + ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
248 | + $score ++ : |
|
249 | + array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
250 | + |
|
251 | + // Is the post content not empty? |
|
252 | + ( ! empty( $post->post_content ) ) ? |
|
253 | + $score ++ : |
|
254 | + array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
255 | + |
|
256 | + // Is the current entity related to at least 1 entity? |
|
257 | + // Was the current entity already disambiguated? |
|
258 | + ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
259 | + $score ++ : |
|
260 | + array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
261 | + |
|
262 | + // Is the entity published? |
|
263 | + ( 'publish' === get_post_status( $post->ID ) ) ? |
|
264 | + $score ++ : |
|
265 | + array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
266 | + |
|
267 | + // Has a thumbnail? |
|
268 | + ( has_post_thumbnail( $post->ID ) ) ? |
|
269 | + $score ++ : |
|
270 | + array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
271 | + |
|
272 | + // Get all post meta keys for the current post |
|
273 | + global $wpdb; |
|
274 | + |
|
275 | + // Check intersection between available meta keys and expected ones |
|
276 | + // arrays to detect missing values. |
|
277 | + $available_meta_keys = $wpdb->get_col( |
|
278 | + $wpdb->prepare( |
|
279 | + "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", |
|
280 | + $post->ID |
|
281 | + ) |
|
282 | + ); |
|
283 | + |
|
284 | + // If each expected key is contained in available keys array ... |
|
285 | + ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true ) ) ? |
|
286 | + $score ++ : |
|
287 | + array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
288 | + |
|
289 | + $schema = $this->entity_type_service->get( $post_id ); |
|
290 | + |
|
291 | + $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
292 | + array() : |
|
293 | + array_keys( $schema['custom_fields'] ); |
|
294 | + |
|
295 | + $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
296 | + // If each expected key is contained in available keys array ... |
|
297 | + ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
298 | + $score ++ : |
|
299 | + array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
300 | + |
|
301 | + // Finally return score and warnings |
|
302 | + return array( |
|
303 | + 'raw_score' => $score, |
|
304 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
305 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
306 | + 'warnings' => $warnings, |
|
307 | + ); |
|
308 | + |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Get as rating as input and convert in a traffic-light rating |
|
313 | + * |
|
314 | + * @param int $score The rating score for a given entity. |
|
315 | + * |
|
316 | + * @return string The input HTML code. |
|
317 | + * @since 3.3.0 |
|
318 | + */ |
|
319 | + private function convert_raw_score_to_traffic_light( $score ) { |
|
320 | + // RATING_MAX : $score = 3 : x |
|
321 | + // See http://php.net/manual/en/function.round.php |
|
322 | + $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
323 | + |
|
324 | + // If rating is 0, return 1, otherwise return rating |
|
325 | + return ( 0 === $rating ) ? 1 : $rating; |
|
326 | + |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * Get as rating as input and convert in a traffic-light rating |
|
331 | + * |
|
332 | + * @param int $score The rating score for a given entity. |
|
333 | + * |
|
334 | + * @return string The input HTML code. |
|
335 | + * @since 3.3.0 |
|
336 | + */ |
|
337 | + public function convert_raw_score_to_percentage( $score ) { |
|
338 | + |
|
339 | + // RATING_MAX : $score = 100 : x |
|
340 | + return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Add admin notices for the current entity depending on the current rating. |
|
345 | + * |
|
346 | + * @since 3.3.0 |
|
347 | + */ |
|
348 | + public function in_admin_header() { |
|
349 | + |
|
350 | + // Return safely if get_current_screen() is not defined (yet) |
|
351 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
352 | + return; |
|
353 | + } |
|
354 | + |
|
355 | + $screen = get_current_screen(); |
|
356 | + // If there is any valid screen nothing to do |
|
357 | + if ( null === $screen ) { |
|
358 | + return; |
|
359 | + } |
|
360 | + |
|
361 | + // If you're not in the entity post edit page, return. |
|
362 | + if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
363 | + return; |
|
364 | + } |
|
365 | + // Retrieve the current global post |
|
366 | + global $post; |
|
367 | + // If it's not an entity, return. |
|
368 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post->ID ) ) { |
|
369 | + return; |
|
370 | + } |
|
371 | + // Retrieve an updated rating for the current entity |
|
372 | + $rating = $this->get_rating_for( $post->ID, true ); |
|
373 | + |
|
374 | + // If there is at least 1 warning |
|
375 | + if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
376 | + // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
377 | + $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
378 | + } |
|
379 | + |
|
380 | + } |
|
381 | 381 | |
382 | 382 | } |
@@ -121,14 +121,14 @@ discard block |
||
121 | 121 | $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
122 | 122 | $this->notice_service = Wordlift_Notice_Service::get_instance(); |
123 | 123 | |
124 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
124 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rating_Service'); |
|
125 | 125 | |
126 | 126 | } |
127 | 127 | |
128 | 128 | private static $instance; |
129 | 129 | |
130 | 130 | public static function get_instance() { |
131 | - if ( ! isset( self::$instance ) ) { |
|
131 | + if ( ! isset(self::$instance)) { |
|
132 | 132 | self::$instance = new self(); |
133 | 133 | } |
134 | 134 | |
@@ -143,28 +143,28 @@ discard block |
||
143 | 143 | * @return array An array representing the rating obj. |
144 | 144 | * @since 3.3.0 |
145 | 145 | */ |
146 | - public function set_rating_for( $post_id ) { |
|
146 | + public function set_rating_for($post_id) { |
|
147 | 147 | |
148 | 148 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
149 | - if ( ! apply_filters( 'wl_feature__enable__entity-rating', true ) ) { |
|
149 | + if ( ! apply_filters('wl_feature__enable__entity-rating', true)) { |
|
150 | 150 | return; |
151 | 151 | } |
152 | 152 | |
153 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
153 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
154 | 154 | return; |
155 | 155 | } |
156 | 156 | |
157 | 157 | // Calculate rating for the given post. |
158 | - $rating = $this->calculate_rating_for( $post_id ); |
|
158 | + $rating = $this->calculate_rating_for($post_id); |
|
159 | 159 | |
160 | 160 | // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
161 | 161 | // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
162 | 162 | // |
163 | 163 | // See https://codex.wordpress.org/Function_Reference/update_post_meta |
164 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
165 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
164 | + update_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score']); |
|
165 | + update_post_meta($post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings']); |
|
166 | 166 | |
167 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
167 | + $this->log->trace(sprintf("Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'])); |
|
168 | 168 | |
169 | 169 | // Finally returns the rating |
170 | 170 | return $rating; |
@@ -179,30 +179,30 @@ discard block |
||
179 | 179 | * @return array An array representing the rating obj. |
180 | 180 | * @since 3.3.0 |
181 | 181 | */ |
182 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
182 | + public function get_rating_for($post_id, $force_reload = false) { |
|
183 | 183 | |
184 | 184 | // If forced reload is required or rating is missing. |
185 | - if ( $force_reload ) { |
|
186 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
185 | + if ($force_reload) { |
|
186 | + $this->log->trace("Force rating reload [ post_id :: $post_id ]"); |
|
187 | 187 | |
188 | - return $this->set_rating_for( $post_id ); |
|
188 | + return $this->set_rating_for($post_id); |
|
189 | 189 | } |
190 | 190 | |
191 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
191 | + $current_raw_score = get_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, true); |
|
192 | 192 | |
193 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
194 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
193 | + if ( ! is_numeric($current_raw_score)) { |
|
194 | + $this->log->trace("Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]"); |
|
195 | 195 | |
196 | - return $this->set_rating_for( $post_id ); |
|
196 | + return $this->set_rating_for($post_id); |
|
197 | 197 | } |
198 | 198 | |
199 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
199 | + $current_warnings = get_post_meta($post_id, self::RATING_WARNINGS_META_KEY, true); |
|
200 | 200 | |
201 | 201 | // Finally return score and warnings |
202 | 202 | return array( |
203 | 203 | 'raw_score' => $current_raw_score, |
204 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
205 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
204 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($current_raw_score), |
|
205 | + 'percentage_score' => $this->convert_raw_score_to_percentage($current_raw_score), |
|
206 | 206 | 'warnings' => $current_warnings, |
207 | 207 | ); |
208 | 208 | |
@@ -228,14 +228,14 @@ discard block |
||
228 | 228 | * @return array An array representing the rating obj. |
229 | 229 | * @since 3.3.0 |
230 | 230 | */ |
231 | - private function calculate_rating_for( $post_id ) { |
|
231 | + private function calculate_rating_for($post_id) { |
|
232 | 232 | |
233 | 233 | // If it's not an entity, return. |
234 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
234 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
235 | 235 | return array(); |
236 | 236 | } |
237 | 237 | // Retrieve the post object. |
238 | - $post = get_post( $post_id ); |
|
238 | + $post = get_post($post_id); |
|
239 | 239 | |
240 | 240 | // Rating value. |
241 | 241 | $score = 0; |
@@ -244,30 +244,25 @@ discard block |
||
244 | 244 | $warnings = array(); |
245 | 245 | |
246 | 246 | // Is the current entity related to at least 1 post? |
247 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
248 | - $score ++ : |
|
249 | - array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
247 | + (0 < count(wl_core_get_related_post_ids($post->ID))) ? |
|
248 | + $score++ : array_push($warnings, __('There are no related posts for the current entity.', 'wordlift')); |
|
250 | 249 | |
251 | 250 | // Is the post content not empty? |
252 | - ( ! empty( $post->post_content ) ) ? |
|
253 | - $score ++ : |
|
254 | - array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
251 | + ( ! empty($post->post_content)) ? |
|
252 | + $score++ : array_push($warnings, __('This entity has not description.', 'wordlift')); |
|
255 | 253 | |
256 | 254 | // Is the current entity related to at least 1 entity? |
257 | 255 | // Was the current entity already disambiguated? |
258 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
259 | - $score ++ : |
|
260 | - array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
256 | + (0 < count(wl_core_get_related_entity_ids($post->ID))) ? |
|
257 | + $score++ : array_push($warnings, __('There are no related entities for the current entity.', 'wordlift')); |
|
261 | 258 | |
262 | 259 | // Is the entity published? |
263 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
264 | - $score ++ : |
|
265 | - array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
260 | + ('publish' === get_post_status($post->ID)) ? |
|
261 | + $score++ : array_push($warnings, __('This entity is not published. It will not appear within analysis results.', 'wordlift')); |
|
266 | 262 | |
267 | 263 | // Has a thumbnail? |
268 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
269 | - $score ++ : |
|
270 | - array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
264 | + (has_post_thumbnail($post->ID)) ? |
|
265 | + $score++ : array_push($warnings, __('This entity has no featured image yet.', 'wordlift')); |
|
271 | 266 | |
272 | 267 | // Get all post meta keys for the current post |
273 | 268 | global $wpdb; |
@@ -282,27 +277,24 @@ discard block |
||
282 | 277 | ); |
283 | 278 | |
284 | 279 | // If each expected key is contained in available keys array ... |
285 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true ) ) ? |
|
286 | - $score ++ : |
|
287 | - array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
280 | + (in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true)) ? |
|
281 | + $score++ : array_push($warnings, __('There are no sameAs configured for this entity.', 'wordlift')); |
|
288 | 282 | |
289 | - $schema = $this->entity_type_service->get( $post_id ); |
|
283 | + $schema = $this->entity_type_service->get($post_id); |
|
290 | 284 | |
291 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
292 | - array() : |
|
293 | - array_keys( $schema['custom_fields'] ); |
|
285 | + $expected_meta_keys = (null === $schema['custom_fields']) ? |
|
286 | + array() : array_keys($schema['custom_fields']); |
|
294 | 287 | |
295 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
288 | + $intersection = array_intersect($expected_meta_keys, $available_meta_keys); |
|
296 | 289 | // If each expected key is contained in available keys array ... |
297 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
298 | - $score ++ : |
|
299 | - array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
290 | + (count($intersection) === count($expected_meta_keys)) ? |
|
291 | + $score++ : array_push($warnings, __('Schema.org metadata for this entity are not completed.', 'wordlift')); |
|
300 | 292 | |
301 | 293 | // Finally return score and warnings |
302 | 294 | return array( |
303 | 295 | 'raw_score' => $score, |
304 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
305 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
296 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), |
|
297 | + 'percentage_score' => $this->convert_raw_score_to_percentage($score), |
|
306 | 298 | 'warnings' => $warnings, |
307 | 299 | ); |
308 | 300 | |
@@ -316,13 +308,13 @@ discard block |
||
316 | 308 | * @return string The input HTML code. |
317 | 309 | * @since 3.3.0 |
318 | 310 | */ |
319 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
311 | + private function convert_raw_score_to_traffic_light($score) { |
|
320 | 312 | // RATING_MAX : $score = 3 : x |
321 | 313 | // See http://php.net/manual/en/function.round.php |
322 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
314 | + $rating = round(($score * 3) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
323 | 315 | |
324 | 316 | // If rating is 0, return 1, otherwise return rating |
325 | - return ( 0 === $rating ) ? 1 : $rating; |
|
317 | + return (0 === $rating) ? 1 : $rating; |
|
326 | 318 | |
327 | 319 | } |
328 | 320 | |
@@ -334,10 +326,10 @@ discard block |
||
334 | 326 | * @return string The input HTML code. |
335 | 327 | * @since 3.3.0 |
336 | 328 | */ |
337 | - public function convert_raw_score_to_percentage( $score ) { |
|
329 | + public function convert_raw_score_to_percentage($score) { |
|
338 | 330 | |
339 | 331 | // RATING_MAX : $score = 100 : x |
340 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
332 | + return round(($score * 100) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
341 | 333 | } |
342 | 334 | |
343 | 335 | /** |
@@ -348,33 +340,33 @@ discard block |
||
348 | 340 | public function in_admin_header() { |
349 | 341 | |
350 | 342 | // Return safely if get_current_screen() is not defined (yet) |
351 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
343 | + if (false === function_exists('get_current_screen')) { |
|
352 | 344 | return; |
353 | 345 | } |
354 | 346 | |
355 | 347 | $screen = get_current_screen(); |
356 | 348 | // If there is any valid screen nothing to do |
357 | - if ( null === $screen ) { |
|
349 | + if (null === $screen) { |
|
358 | 350 | return; |
359 | 351 | } |
360 | 352 | |
361 | 353 | // If you're not in the entity post edit page, return. |
362 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
354 | + if (Wordlift_Entity_Service::TYPE_NAME !== $screen->id) { |
|
363 | 355 | return; |
364 | 356 | } |
365 | 357 | // Retrieve the current global post |
366 | 358 | global $post; |
367 | 359 | // If it's not an entity, return. |
368 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post->ID ) ) { |
|
360 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity($post->ID)) { |
|
369 | 361 | return; |
370 | 362 | } |
371 | 363 | // Retrieve an updated rating for the current entity |
372 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
364 | + $rating = $this->get_rating_for($post->ID, true); |
|
373 | 365 | |
374 | 366 | // If there is at least 1 warning |
375 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
367 | + if (isset($rating['warnings']) && 0 < count($rating['warnings'])) { |
|
376 | 368 | // TODO - Pass Wordlift_Notice_Service trough the service constructor |
377 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
369 | + $this->notice_service->add_suggestion($rating['warnings']); |
|
378 | 370 | } |
379 | 371 | |
380 | 372 | } |
@@ -13,492 +13,492 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class Wordlift_Plugin_WP_Background_Process extends Wordlift_Plugin_WP_Async_Request { |
15 | 15 | |
16 | - /** |
|
17 | - * Action |
|
18 | - * |
|
19 | - * (default value: 'background_process') |
|
20 | - * |
|
21 | - * @var string |
|
22 | - * @access protected |
|
23 | - */ |
|
24 | - protected $action = 'background_process'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Start time of current process. |
|
28 | - * |
|
29 | - * (default value: 0) |
|
30 | - * |
|
31 | - * @var int |
|
32 | - * @access protected |
|
33 | - */ |
|
34 | - protected $start_time = 0; |
|
35 | - |
|
36 | - /** |
|
37 | - * Cron_hook_identifier |
|
38 | - * |
|
39 | - * @var mixed |
|
40 | - * @access protected |
|
41 | - */ |
|
42 | - protected $cron_hook_identifier; |
|
43 | - |
|
44 | - /** |
|
45 | - * Cron_interval_identifier |
|
46 | - * |
|
47 | - * @var mixed |
|
48 | - * @access protected |
|
49 | - */ |
|
50 | - protected $cron_interval_identifier; |
|
51 | - |
|
52 | - /** |
|
53 | - * Initiate new background process |
|
54 | - */ |
|
55 | - public function __construct() { |
|
56 | - parent::__construct(); |
|
57 | - |
|
58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
60 | - |
|
61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Dispatch |
|
67 | - * |
|
68 | - * @access public |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function dispatch() { |
|
72 | - // Schedule the cron healthcheck. |
|
73 | - $this->schedule_event(); |
|
74 | - |
|
75 | - // Perform remote post. |
|
76 | - return parent::dispatch(); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Push to queue |
|
81 | - * |
|
82 | - * @param mixed $data Data. |
|
83 | - * |
|
84 | - * @return $this |
|
85 | - */ |
|
86 | - public function push_to_queue( $data ) { |
|
87 | - $this->data[] = $data; |
|
88 | - |
|
89 | - return $this; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Save queue |
|
94 | - * |
|
95 | - * @return $this |
|
96 | - */ |
|
97 | - public function save() { |
|
98 | - $key = $this->generate_key(); |
|
99 | - |
|
100 | - if ( ! empty( $this->data ) ) { |
|
101 | - update_site_option( $key, $this->data ); |
|
102 | - } |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Update queue |
|
109 | - * |
|
110 | - * @param string $key Key. |
|
111 | - * @param array $data Data. |
|
112 | - * |
|
113 | - * @return $this |
|
114 | - */ |
|
115 | - public function update( $key, $data ) { |
|
116 | - if ( ! empty( $data ) ) { |
|
117 | - update_site_option( $key, $data ); |
|
118 | - } |
|
119 | - |
|
120 | - return $this; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Delete queue |
|
125 | - * |
|
126 | - * @param string $key Key. |
|
127 | - * |
|
128 | - * @return $this |
|
129 | - */ |
|
130 | - public function delete( $key ) { |
|
131 | - delete_site_option( $key ); |
|
132 | - |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Generate key |
|
138 | - * |
|
139 | - * Generates a unique key based on microtime. Queue items are |
|
140 | - * given a unique key so that they can be merged upon save. |
|
141 | - * |
|
142 | - * @param int $length Length. |
|
143 | - * |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - protected function generate_key( $length = 64 ) { |
|
147 | - $unique = md5( microtime() . rand() ); |
|
148 | - $prepend = $this->identifier . '_batch_'; |
|
149 | - |
|
150 | - return substr( $prepend . $unique, 0, $length ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Maybe process queue |
|
155 | - * |
|
156 | - * Checks whether data exists within the queue and that |
|
157 | - * the process is not already running. |
|
158 | - */ |
|
159 | - public function maybe_handle() { |
|
160 | - // Don't lock up other requests while processing |
|
161 | - session_write_close(); |
|
162 | - |
|
163 | - if ( $this->is_process_running() ) { |
|
164 | - // Background process already running. |
|
165 | - wp_die(); |
|
166 | - } |
|
167 | - |
|
168 | - if ( $this->is_queue_empty() ) { |
|
169 | - // No data to process. |
|
170 | - wp_die(); |
|
171 | - } |
|
172 | - |
|
173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
174 | - |
|
175 | - $this->handle(); |
|
176 | - |
|
177 | - wp_die(); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Is queue empty |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - protected function is_queue_empty() { |
|
186 | - global $wpdb; |
|
187 | - |
|
188 | - $table = $wpdb->options; |
|
189 | - $column = 'option_name'; |
|
190 | - |
|
191 | - if ( is_multisite() ) { |
|
192 | - $table = $wpdb->sitemeta; |
|
193 | - $column = 'meta_key'; |
|
194 | - } |
|
195 | - |
|
196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
197 | - |
|
198 | - $count = $wpdb->get_var( |
|
199 | - $wpdb->prepare( |
|
200 | - "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
201 | - $key |
|
202 | - ) |
|
203 | - ); |
|
204 | - |
|
205 | - return ( $count > 0 ) ? false : true; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Is process running |
|
210 | - * |
|
211 | - * Check whether the current process is already running |
|
212 | - * in a background process. |
|
213 | - */ |
|
214 | - protected function is_process_running() { |
|
215 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
216 | - // Process already running. |
|
217 | - return true; |
|
218 | - } |
|
219 | - |
|
220 | - return false; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Lock process |
|
225 | - * |
|
226 | - * Lock the process so that multiple instances can't run simultaneously. |
|
227 | - * Override if applicable, but the duration should be greater than that |
|
228 | - * defined in the time_exceeded() method. |
|
229 | - */ |
|
230 | - protected function lock_process() { |
|
231 | - $this->start_time = time(); // Set start time of current process. |
|
232 | - |
|
233 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
234 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
235 | - |
|
236 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Unlock process |
|
241 | - * |
|
242 | - * Unlock the process so that other instances can spawn. |
|
243 | - * |
|
244 | - * @return $this |
|
245 | - */ |
|
246 | - protected function unlock_process() { |
|
247 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
248 | - |
|
249 | - return $this; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get batch |
|
254 | - * |
|
255 | - * @return stdClass Return the first batch from the queue |
|
256 | - */ |
|
257 | - protected function get_batch() { |
|
258 | - global $wpdb; |
|
259 | - |
|
260 | - $table = $wpdb->options; |
|
261 | - $column = 'option_name'; |
|
262 | - $key_column = 'option_id'; |
|
263 | - $value_column = 'option_value'; |
|
264 | - |
|
265 | - if ( is_multisite() ) { |
|
266 | - $table = $wpdb->sitemeta; |
|
267 | - $column = 'meta_key'; |
|
268 | - $key_column = 'meta_id'; |
|
269 | - $value_column = 'meta_value'; |
|
270 | - } |
|
271 | - |
|
272 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
273 | - |
|
274 | - $query = $wpdb->get_row( |
|
275 | - $wpdb->prepare( |
|
276 | - "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
277 | - $key |
|
278 | - ) |
|
279 | - ); |
|
280 | - |
|
281 | - $batch = new stdClass(); |
|
282 | - $batch->key = $query->$column; |
|
283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
284 | - |
|
285 | - return $batch; |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Handle |
|
290 | - * |
|
291 | - * Pass each queue item to the task handler, while remaining |
|
292 | - * within server memory and time limit constraints. |
|
293 | - */ |
|
294 | - protected function handle() { |
|
295 | - $this->lock_process(); |
|
296 | - |
|
297 | - do { |
|
298 | - $batch = $this->get_batch(); |
|
299 | - |
|
300 | - foreach ( $batch->data as $key => $value ) { |
|
301 | - $task = $this->task( $value ); |
|
302 | - |
|
303 | - if ( false !== $task ) { |
|
304 | - $batch->data[ $key ] = $task; |
|
305 | - } else { |
|
306 | - unset( $batch->data[ $key ] ); |
|
307 | - } |
|
308 | - |
|
309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
310 | - // Batch limits reached. |
|
311 | - break; |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - // Update or delete current batch. |
|
316 | - if ( ! empty( $batch->data ) ) { |
|
317 | - $this->update( $batch->key, $batch->data ); |
|
318 | - } else { |
|
319 | - $this->delete( $batch->key ); |
|
320 | - } |
|
321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
322 | - |
|
323 | - $this->unlock_process(); |
|
324 | - |
|
325 | - // Start next batch or complete process. |
|
326 | - if ( ! $this->is_queue_empty() ) { |
|
327 | - $this->dispatch(); |
|
328 | - } else { |
|
329 | - $this->complete(); |
|
330 | - } |
|
331 | - |
|
332 | - wp_die(); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Memory exceeded |
|
337 | - * |
|
338 | - * Ensures the batch process never exceeds 90% |
|
339 | - * of the maximum WordPress memory. |
|
340 | - * |
|
341 | - * @return bool |
|
342 | - */ |
|
343 | - protected function memory_exceeded() { |
|
344 | - $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
345 | - $current_memory = memory_get_usage( true ); |
|
346 | - $return = false; |
|
347 | - |
|
348 | - if ( $current_memory >= $memory_limit ) { |
|
349 | - $return = true; |
|
350 | - } |
|
351 | - |
|
352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Get memory limit |
|
357 | - * |
|
358 | - * @return int |
|
359 | - */ |
|
360 | - protected function get_memory_limit() { |
|
361 | - if ( function_exists( 'ini_get' ) ) { |
|
362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
363 | - } else { |
|
364 | - // Sensible default. |
|
365 | - $memory_limit = '128M'; |
|
366 | - } |
|
367 | - |
|
368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
369 | - // Unlimited, set to 32GB. |
|
370 | - $memory_limit = '32000M'; |
|
371 | - } |
|
372 | - |
|
373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Time exceeded. |
|
378 | - * |
|
379 | - * Ensures the batch never exceeds a sensible time limit. |
|
380 | - * A timeout limit of 30s is common on shared hosting. |
|
381 | - * |
|
382 | - * @return bool |
|
383 | - */ |
|
384 | - protected function time_exceeded() { |
|
385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
386 | - $return = false; |
|
387 | - |
|
388 | - if ( time() >= $finish ) { |
|
389 | - $return = true; |
|
390 | - } |
|
391 | - |
|
392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Complete. |
|
397 | - * |
|
398 | - * Override if applicable, but ensure that the below actions are |
|
399 | - * performed, or, call parent::complete(). |
|
400 | - */ |
|
401 | - protected function complete() { |
|
402 | - // Unschedule the cron healthcheck. |
|
403 | - $this->clear_scheduled_event(); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Schedule cron healthcheck |
|
408 | - * |
|
409 | - * @access public |
|
410 | - * |
|
411 | - * @param mixed $schedules Schedules. |
|
412 | - * |
|
413 | - * @return mixed |
|
414 | - */ |
|
415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
417 | - |
|
418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
420 | - } |
|
421 | - |
|
422 | - // Adds every 5 minutes to the existing schedules. |
|
423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
424 | - 'interval' => MINUTE_IN_SECONDS * $interval, |
|
425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
426 | - ); |
|
427 | - |
|
428 | - return $schedules; |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Handle cron healthcheck |
|
433 | - * |
|
434 | - * Restart the background process if not already running |
|
435 | - * and data exists in the queue. |
|
436 | - */ |
|
437 | - public function handle_cron_healthcheck() { |
|
438 | - if ( $this->is_process_running() ) { |
|
439 | - // Background process already running. |
|
440 | - exit; |
|
441 | - } |
|
442 | - |
|
443 | - if ( $this->is_queue_empty() ) { |
|
444 | - // No data to process. |
|
445 | - $this->clear_scheduled_event(); |
|
446 | - exit; |
|
447 | - } |
|
448 | - |
|
449 | - $this->handle(); |
|
450 | - |
|
451 | - exit; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Schedule event |
|
456 | - */ |
|
457 | - protected function schedule_event() { |
|
458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
460 | - } |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Clear scheduled event |
|
465 | - */ |
|
466 | - protected function clear_scheduled_event() { |
|
467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
468 | - |
|
469 | - if ( $timestamp ) { |
|
470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
471 | - } |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * Cancel Process |
|
476 | - * |
|
477 | - * Stop processing queue items, clear cronjob and delete batch. |
|
478 | - */ |
|
479 | - public function cancel_process() { |
|
480 | - if ( ! $this->is_queue_empty() ) { |
|
481 | - $batch = $this->get_batch(); |
|
482 | - |
|
483 | - $this->delete( $batch->key ); |
|
484 | - |
|
485 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
486 | - } |
|
487 | - |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * Task |
|
492 | - * |
|
493 | - * Override this method to perform any actions required on each |
|
494 | - * queue item. Return the modified item for further processing |
|
495 | - * in the next pass through. Or, return false to remove the |
|
496 | - * item from the queue. |
|
497 | - * |
|
498 | - * @param mixed $item Queue item to iterate over. |
|
499 | - * |
|
500 | - * @return mixed |
|
501 | - */ |
|
502 | - abstract protected function task( $item ); |
|
16 | + /** |
|
17 | + * Action |
|
18 | + * |
|
19 | + * (default value: 'background_process') |
|
20 | + * |
|
21 | + * @var string |
|
22 | + * @access protected |
|
23 | + */ |
|
24 | + protected $action = 'background_process'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Start time of current process. |
|
28 | + * |
|
29 | + * (default value: 0) |
|
30 | + * |
|
31 | + * @var int |
|
32 | + * @access protected |
|
33 | + */ |
|
34 | + protected $start_time = 0; |
|
35 | + |
|
36 | + /** |
|
37 | + * Cron_hook_identifier |
|
38 | + * |
|
39 | + * @var mixed |
|
40 | + * @access protected |
|
41 | + */ |
|
42 | + protected $cron_hook_identifier; |
|
43 | + |
|
44 | + /** |
|
45 | + * Cron_interval_identifier |
|
46 | + * |
|
47 | + * @var mixed |
|
48 | + * @access protected |
|
49 | + */ |
|
50 | + protected $cron_interval_identifier; |
|
51 | + |
|
52 | + /** |
|
53 | + * Initiate new background process |
|
54 | + */ |
|
55 | + public function __construct() { |
|
56 | + parent::__construct(); |
|
57 | + |
|
58 | + $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
59 | + $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
60 | + |
|
61 | + add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
62 | + add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Dispatch |
|
67 | + * |
|
68 | + * @access public |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function dispatch() { |
|
72 | + // Schedule the cron healthcheck. |
|
73 | + $this->schedule_event(); |
|
74 | + |
|
75 | + // Perform remote post. |
|
76 | + return parent::dispatch(); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Push to queue |
|
81 | + * |
|
82 | + * @param mixed $data Data. |
|
83 | + * |
|
84 | + * @return $this |
|
85 | + */ |
|
86 | + public function push_to_queue( $data ) { |
|
87 | + $this->data[] = $data; |
|
88 | + |
|
89 | + return $this; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Save queue |
|
94 | + * |
|
95 | + * @return $this |
|
96 | + */ |
|
97 | + public function save() { |
|
98 | + $key = $this->generate_key(); |
|
99 | + |
|
100 | + if ( ! empty( $this->data ) ) { |
|
101 | + update_site_option( $key, $this->data ); |
|
102 | + } |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Update queue |
|
109 | + * |
|
110 | + * @param string $key Key. |
|
111 | + * @param array $data Data. |
|
112 | + * |
|
113 | + * @return $this |
|
114 | + */ |
|
115 | + public function update( $key, $data ) { |
|
116 | + if ( ! empty( $data ) ) { |
|
117 | + update_site_option( $key, $data ); |
|
118 | + } |
|
119 | + |
|
120 | + return $this; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Delete queue |
|
125 | + * |
|
126 | + * @param string $key Key. |
|
127 | + * |
|
128 | + * @return $this |
|
129 | + */ |
|
130 | + public function delete( $key ) { |
|
131 | + delete_site_option( $key ); |
|
132 | + |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Generate key |
|
138 | + * |
|
139 | + * Generates a unique key based on microtime. Queue items are |
|
140 | + * given a unique key so that they can be merged upon save. |
|
141 | + * |
|
142 | + * @param int $length Length. |
|
143 | + * |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + protected function generate_key( $length = 64 ) { |
|
147 | + $unique = md5( microtime() . rand() ); |
|
148 | + $prepend = $this->identifier . '_batch_'; |
|
149 | + |
|
150 | + return substr( $prepend . $unique, 0, $length ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Maybe process queue |
|
155 | + * |
|
156 | + * Checks whether data exists within the queue and that |
|
157 | + * the process is not already running. |
|
158 | + */ |
|
159 | + public function maybe_handle() { |
|
160 | + // Don't lock up other requests while processing |
|
161 | + session_write_close(); |
|
162 | + |
|
163 | + if ( $this->is_process_running() ) { |
|
164 | + // Background process already running. |
|
165 | + wp_die(); |
|
166 | + } |
|
167 | + |
|
168 | + if ( $this->is_queue_empty() ) { |
|
169 | + // No data to process. |
|
170 | + wp_die(); |
|
171 | + } |
|
172 | + |
|
173 | + check_ajax_referer( $this->identifier, 'nonce' ); |
|
174 | + |
|
175 | + $this->handle(); |
|
176 | + |
|
177 | + wp_die(); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Is queue empty |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + protected function is_queue_empty() { |
|
186 | + global $wpdb; |
|
187 | + |
|
188 | + $table = $wpdb->options; |
|
189 | + $column = 'option_name'; |
|
190 | + |
|
191 | + if ( is_multisite() ) { |
|
192 | + $table = $wpdb->sitemeta; |
|
193 | + $column = 'meta_key'; |
|
194 | + } |
|
195 | + |
|
196 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
197 | + |
|
198 | + $count = $wpdb->get_var( |
|
199 | + $wpdb->prepare( |
|
200 | + "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
201 | + $key |
|
202 | + ) |
|
203 | + ); |
|
204 | + |
|
205 | + return ( $count > 0 ) ? false : true; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Is process running |
|
210 | + * |
|
211 | + * Check whether the current process is already running |
|
212 | + * in a background process. |
|
213 | + */ |
|
214 | + protected function is_process_running() { |
|
215 | + if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
216 | + // Process already running. |
|
217 | + return true; |
|
218 | + } |
|
219 | + |
|
220 | + return false; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Lock process |
|
225 | + * |
|
226 | + * Lock the process so that multiple instances can't run simultaneously. |
|
227 | + * Override if applicable, but the duration should be greater than that |
|
228 | + * defined in the time_exceeded() method. |
|
229 | + */ |
|
230 | + protected function lock_process() { |
|
231 | + $this->start_time = time(); // Set start time of current process. |
|
232 | + |
|
233 | + $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
234 | + $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
235 | + |
|
236 | + set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Unlock process |
|
241 | + * |
|
242 | + * Unlock the process so that other instances can spawn. |
|
243 | + * |
|
244 | + * @return $this |
|
245 | + */ |
|
246 | + protected function unlock_process() { |
|
247 | + delete_site_transient( $this->identifier . '_process_lock' ); |
|
248 | + |
|
249 | + return $this; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get batch |
|
254 | + * |
|
255 | + * @return stdClass Return the first batch from the queue |
|
256 | + */ |
|
257 | + protected function get_batch() { |
|
258 | + global $wpdb; |
|
259 | + |
|
260 | + $table = $wpdb->options; |
|
261 | + $column = 'option_name'; |
|
262 | + $key_column = 'option_id'; |
|
263 | + $value_column = 'option_value'; |
|
264 | + |
|
265 | + if ( is_multisite() ) { |
|
266 | + $table = $wpdb->sitemeta; |
|
267 | + $column = 'meta_key'; |
|
268 | + $key_column = 'meta_id'; |
|
269 | + $value_column = 'meta_value'; |
|
270 | + } |
|
271 | + |
|
272 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
273 | + |
|
274 | + $query = $wpdb->get_row( |
|
275 | + $wpdb->prepare( |
|
276 | + "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
277 | + $key |
|
278 | + ) |
|
279 | + ); |
|
280 | + |
|
281 | + $batch = new stdClass(); |
|
282 | + $batch->key = $query->$column; |
|
283 | + $batch->data = maybe_unserialize( $query->$value_column ); |
|
284 | + |
|
285 | + return $batch; |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Handle |
|
290 | + * |
|
291 | + * Pass each queue item to the task handler, while remaining |
|
292 | + * within server memory and time limit constraints. |
|
293 | + */ |
|
294 | + protected function handle() { |
|
295 | + $this->lock_process(); |
|
296 | + |
|
297 | + do { |
|
298 | + $batch = $this->get_batch(); |
|
299 | + |
|
300 | + foreach ( $batch->data as $key => $value ) { |
|
301 | + $task = $this->task( $value ); |
|
302 | + |
|
303 | + if ( false !== $task ) { |
|
304 | + $batch->data[ $key ] = $task; |
|
305 | + } else { |
|
306 | + unset( $batch->data[ $key ] ); |
|
307 | + } |
|
308 | + |
|
309 | + if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
310 | + // Batch limits reached. |
|
311 | + break; |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + // Update or delete current batch. |
|
316 | + if ( ! empty( $batch->data ) ) { |
|
317 | + $this->update( $batch->key, $batch->data ); |
|
318 | + } else { |
|
319 | + $this->delete( $batch->key ); |
|
320 | + } |
|
321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
322 | + |
|
323 | + $this->unlock_process(); |
|
324 | + |
|
325 | + // Start next batch or complete process. |
|
326 | + if ( ! $this->is_queue_empty() ) { |
|
327 | + $this->dispatch(); |
|
328 | + } else { |
|
329 | + $this->complete(); |
|
330 | + } |
|
331 | + |
|
332 | + wp_die(); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Memory exceeded |
|
337 | + * |
|
338 | + * Ensures the batch process never exceeds 90% |
|
339 | + * of the maximum WordPress memory. |
|
340 | + * |
|
341 | + * @return bool |
|
342 | + */ |
|
343 | + protected function memory_exceeded() { |
|
344 | + $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
345 | + $current_memory = memory_get_usage( true ); |
|
346 | + $return = false; |
|
347 | + |
|
348 | + if ( $current_memory >= $memory_limit ) { |
|
349 | + $return = true; |
|
350 | + } |
|
351 | + |
|
352 | + return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Get memory limit |
|
357 | + * |
|
358 | + * @return int |
|
359 | + */ |
|
360 | + protected function get_memory_limit() { |
|
361 | + if ( function_exists( 'ini_get' ) ) { |
|
362 | + $memory_limit = ini_get( 'memory_limit' ); |
|
363 | + } else { |
|
364 | + // Sensible default. |
|
365 | + $memory_limit = '128M'; |
|
366 | + } |
|
367 | + |
|
368 | + if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
369 | + // Unlimited, set to 32GB. |
|
370 | + $memory_limit = '32000M'; |
|
371 | + } |
|
372 | + |
|
373 | + return wp_convert_hr_to_bytes( $memory_limit ); |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Time exceeded. |
|
378 | + * |
|
379 | + * Ensures the batch never exceeds a sensible time limit. |
|
380 | + * A timeout limit of 30s is common on shared hosting. |
|
381 | + * |
|
382 | + * @return bool |
|
383 | + */ |
|
384 | + protected function time_exceeded() { |
|
385 | + $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
386 | + $return = false; |
|
387 | + |
|
388 | + if ( time() >= $finish ) { |
|
389 | + $return = true; |
|
390 | + } |
|
391 | + |
|
392 | + return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Complete. |
|
397 | + * |
|
398 | + * Override if applicable, but ensure that the below actions are |
|
399 | + * performed, or, call parent::complete(). |
|
400 | + */ |
|
401 | + protected function complete() { |
|
402 | + // Unschedule the cron healthcheck. |
|
403 | + $this->clear_scheduled_event(); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Schedule cron healthcheck |
|
408 | + * |
|
409 | + * @access public |
|
410 | + * |
|
411 | + * @param mixed $schedules Schedules. |
|
412 | + * |
|
413 | + * @return mixed |
|
414 | + */ |
|
415 | + public function schedule_cron_healthcheck( $schedules ) { |
|
416 | + $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
417 | + |
|
418 | + if ( property_exists( $this, 'cron_interval' ) ) { |
|
419 | + $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
420 | + } |
|
421 | + |
|
422 | + // Adds every 5 minutes to the existing schedules. |
|
423 | + $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
424 | + 'interval' => MINUTE_IN_SECONDS * $interval, |
|
425 | + 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
426 | + ); |
|
427 | + |
|
428 | + return $schedules; |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Handle cron healthcheck |
|
433 | + * |
|
434 | + * Restart the background process if not already running |
|
435 | + * and data exists in the queue. |
|
436 | + */ |
|
437 | + public function handle_cron_healthcheck() { |
|
438 | + if ( $this->is_process_running() ) { |
|
439 | + // Background process already running. |
|
440 | + exit; |
|
441 | + } |
|
442 | + |
|
443 | + if ( $this->is_queue_empty() ) { |
|
444 | + // No data to process. |
|
445 | + $this->clear_scheduled_event(); |
|
446 | + exit; |
|
447 | + } |
|
448 | + |
|
449 | + $this->handle(); |
|
450 | + |
|
451 | + exit; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Schedule event |
|
456 | + */ |
|
457 | + protected function schedule_event() { |
|
458 | + if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
459 | + wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
460 | + } |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Clear scheduled event |
|
465 | + */ |
|
466 | + protected function clear_scheduled_event() { |
|
467 | + $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
468 | + |
|
469 | + if ( $timestamp ) { |
|
470 | + wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
471 | + } |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * Cancel Process |
|
476 | + * |
|
477 | + * Stop processing queue items, clear cronjob and delete batch. |
|
478 | + */ |
|
479 | + public function cancel_process() { |
|
480 | + if ( ! $this->is_queue_empty() ) { |
|
481 | + $batch = $this->get_batch(); |
|
482 | + |
|
483 | + $this->delete( $batch->key ); |
|
484 | + |
|
485 | + wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
486 | + } |
|
487 | + |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * Task |
|
492 | + * |
|
493 | + * Override this method to perform any actions required on each |
|
494 | + * queue item. Return the modified item for further processing |
|
495 | + * in the next pass through. Or, return false to remove the |
|
496 | + * item from the queue. |
|
497 | + * |
|
498 | + * @param mixed $item Queue item to iterate over. |
|
499 | + * |
|
500 | + * @return mixed |
|
501 | + */ |
|
502 | + abstract protected function task( $item ); |
|
503 | 503 | |
504 | 504 | } |
@@ -55,11 +55,11 @@ discard block |
||
55 | 55 | public function __construct() { |
56 | 56 | parent::__construct(); |
57 | 57 | |
58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
58 | + $this->cron_hook_identifier = $this->identifier.'_cron'; |
|
59 | + $this->cron_interval_identifier = $this->identifier.'_cron_interval'; |
|
60 | 60 | |
61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
61 | + add_action($this->cron_hook_identifier, array($this, 'handle_cron_healthcheck')); |
|
62 | + add_filter('cron_schedules', array($this, 'schedule_cron_healthcheck')); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * |
84 | 84 | * @return $this |
85 | 85 | */ |
86 | - public function push_to_queue( $data ) { |
|
86 | + public function push_to_queue($data) { |
|
87 | 87 | $this->data[] = $data; |
88 | 88 | |
89 | 89 | return $this; |
@@ -97,8 +97,8 @@ discard block |
||
97 | 97 | public function save() { |
98 | 98 | $key = $this->generate_key(); |
99 | 99 | |
100 | - if ( ! empty( $this->data ) ) { |
|
101 | - update_site_option( $key, $this->data ); |
|
100 | + if ( ! empty($this->data)) { |
|
101 | + update_site_option($key, $this->data); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | return $this; |
@@ -112,9 +112,9 @@ discard block |
||
112 | 112 | * |
113 | 113 | * @return $this |
114 | 114 | */ |
115 | - public function update( $key, $data ) { |
|
116 | - if ( ! empty( $data ) ) { |
|
117 | - update_site_option( $key, $data ); |
|
115 | + public function update($key, $data) { |
|
116 | + if ( ! empty($data)) { |
|
117 | + update_site_option($key, $data); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | return $this; |
@@ -127,8 +127,8 @@ discard block |
||
127 | 127 | * |
128 | 128 | * @return $this |
129 | 129 | */ |
130 | - public function delete( $key ) { |
|
131 | - delete_site_option( $key ); |
|
130 | + public function delete($key) { |
|
131 | + delete_site_option($key); |
|
132 | 132 | |
133 | 133 | return $this; |
134 | 134 | } |
@@ -143,11 +143,11 @@ discard block |
||
143 | 143 | * |
144 | 144 | * @return string |
145 | 145 | */ |
146 | - protected function generate_key( $length = 64 ) { |
|
147 | - $unique = md5( microtime() . rand() ); |
|
148 | - $prepend = $this->identifier . '_batch_'; |
|
146 | + protected function generate_key($length = 64) { |
|
147 | + $unique = md5(microtime().rand()); |
|
148 | + $prepend = $this->identifier.'_batch_'; |
|
149 | 149 | |
150 | - return substr( $prepend . $unique, 0, $length ); |
|
150 | + return substr($prepend.$unique, 0, $length); |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
@@ -160,17 +160,17 @@ discard block |
||
160 | 160 | // Don't lock up other requests while processing |
161 | 161 | session_write_close(); |
162 | 162 | |
163 | - if ( $this->is_process_running() ) { |
|
163 | + if ($this->is_process_running()) { |
|
164 | 164 | // Background process already running. |
165 | 165 | wp_die(); |
166 | 166 | } |
167 | 167 | |
168 | - if ( $this->is_queue_empty() ) { |
|
168 | + if ($this->is_queue_empty()) { |
|
169 | 169 | // No data to process. |
170 | 170 | wp_die(); |
171 | 171 | } |
172 | 172 | |
173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
173 | + check_ajax_referer($this->identifier, 'nonce'); |
|
174 | 174 | |
175 | 175 | $this->handle(); |
176 | 176 | |
@@ -188,12 +188,12 @@ discard block |
||
188 | 188 | $table = $wpdb->options; |
189 | 189 | $column = 'option_name'; |
190 | 190 | |
191 | - if ( is_multisite() ) { |
|
191 | + if (is_multisite()) { |
|
192 | 192 | $table = $wpdb->sitemeta; |
193 | 193 | $column = 'meta_key'; |
194 | 194 | } |
195 | 195 | |
196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
196 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
197 | 197 | |
198 | 198 | $count = $wpdb->get_var( |
199 | 199 | $wpdb->prepare( |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | ) |
203 | 203 | ); |
204 | 204 | |
205 | - return ( $count > 0 ) ? false : true; |
|
205 | + return ($count > 0) ? false : true; |
|
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | * in a background process. |
213 | 213 | */ |
214 | 214 | protected function is_process_running() { |
215 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
215 | + if (get_site_transient($this->identifier.'_process_lock')) { |
|
216 | 216 | // Process already running. |
217 | 217 | return true; |
218 | 218 | } |
@@ -230,10 +230,10 @@ discard block |
||
230 | 230 | protected function lock_process() { |
231 | 231 | $this->start_time = time(); // Set start time of current process. |
232 | 232 | |
233 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
234 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
233 | + $lock_duration = (property_exists($this, 'queue_lock_time')) ? $this->queue_lock_time : 60; // 1 minute |
|
234 | + $lock_duration = apply_filters($this->identifier.'_queue_lock_time', $lock_duration); |
|
235 | 235 | |
236 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
236 | + set_site_transient($this->identifier.'_process_lock', microtime(), $lock_duration); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | * @return $this |
245 | 245 | */ |
246 | 246 | protected function unlock_process() { |
247 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
247 | + delete_site_transient($this->identifier.'_process_lock'); |
|
248 | 248 | |
249 | 249 | return $this; |
250 | 250 | } |
@@ -262,14 +262,14 @@ discard block |
||
262 | 262 | $key_column = 'option_id'; |
263 | 263 | $value_column = 'option_value'; |
264 | 264 | |
265 | - if ( is_multisite() ) { |
|
265 | + if (is_multisite()) { |
|
266 | 266 | $table = $wpdb->sitemeta; |
267 | 267 | $column = 'meta_key'; |
268 | 268 | $key_column = 'meta_id'; |
269 | 269 | $value_column = 'meta_value'; |
270 | 270 | } |
271 | 271 | |
272 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
272 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
273 | 273 | |
274 | 274 | $query = $wpdb->get_row( |
275 | 275 | $wpdb->prepare( |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | |
281 | 281 | $batch = new stdClass(); |
282 | 282 | $batch->key = $query->$column; |
283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
283 | + $batch->data = maybe_unserialize($query->$value_column); |
|
284 | 284 | |
285 | 285 | return $batch; |
286 | 286 | } |
@@ -297,33 +297,33 @@ discard block |
||
297 | 297 | do { |
298 | 298 | $batch = $this->get_batch(); |
299 | 299 | |
300 | - foreach ( $batch->data as $key => $value ) { |
|
301 | - $task = $this->task( $value ); |
|
300 | + foreach ($batch->data as $key => $value) { |
|
301 | + $task = $this->task($value); |
|
302 | 302 | |
303 | - if ( false !== $task ) { |
|
304 | - $batch->data[ $key ] = $task; |
|
303 | + if (false !== $task) { |
|
304 | + $batch->data[$key] = $task; |
|
305 | 305 | } else { |
306 | - unset( $batch->data[ $key ] ); |
|
306 | + unset($batch->data[$key]); |
|
307 | 307 | } |
308 | 308 | |
309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
309 | + if ($this->time_exceeded() || $this->memory_exceeded()) { |
|
310 | 310 | // Batch limits reached. |
311 | 311 | break; |
312 | 312 | } |
313 | 313 | } |
314 | 314 | |
315 | 315 | // Update or delete current batch. |
316 | - if ( ! empty( $batch->data ) ) { |
|
317 | - $this->update( $batch->key, $batch->data ); |
|
316 | + if ( ! empty($batch->data)) { |
|
317 | + $this->update($batch->key, $batch->data); |
|
318 | 318 | } else { |
319 | - $this->delete( $batch->key ); |
|
319 | + $this->delete($batch->key); |
|
320 | 320 | } |
321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty()); |
|
322 | 322 | |
323 | 323 | $this->unlock_process(); |
324 | 324 | |
325 | 325 | // Start next batch or complete process. |
326 | - if ( ! $this->is_queue_empty() ) { |
|
326 | + if ( ! $this->is_queue_empty()) { |
|
327 | 327 | $this->dispatch(); |
328 | 328 | } else { |
329 | 329 | $this->complete(); |
@@ -342,14 +342,14 @@ discard block |
||
342 | 342 | */ |
343 | 343 | protected function memory_exceeded() { |
344 | 344 | $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
345 | - $current_memory = memory_get_usage( true ); |
|
345 | + $current_memory = memory_get_usage(true); |
|
346 | 346 | $return = false; |
347 | 347 | |
348 | - if ( $current_memory >= $memory_limit ) { |
|
348 | + if ($current_memory >= $memory_limit) { |
|
349 | 349 | $return = true; |
350 | 350 | } |
351 | 351 | |
352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
352 | + return apply_filters($this->identifier.'_memory_exceeded', $return); |
|
353 | 353 | } |
354 | 354 | |
355 | 355 | /** |
@@ -358,19 +358,19 @@ discard block |
||
358 | 358 | * @return int |
359 | 359 | */ |
360 | 360 | protected function get_memory_limit() { |
361 | - if ( function_exists( 'ini_get' ) ) { |
|
362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
361 | + if (function_exists('ini_get')) { |
|
362 | + $memory_limit = ini_get('memory_limit'); |
|
363 | 363 | } else { |
364 | 364 | // Sensible default. |
365 | 365 | $memory_limit = '128M'; |
366 | 366 | } |
367 | 367 | |
368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
368 | + if ( ! $memory_limit || - 1 === intval($memory_limit)) { |
|
369 | 369 | // Unlimited, set to 32GB. |
370 | 370 | $memory_limit = '32000M'; |
371 | 371 | } |
372 | 372 | |
373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
373 | + return wp_convert_hr_to_bytes($memory_limit); |
|
374 | 374 | } |
375 | 375 | |
376 | 376 | /** |
@@ -382,14 +382,14 @@ discard block |
||
382 | 382 | * @return bool |
383 | 383 | */ |
384 | 384 | protected function time_exceeded() { |
385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
385 | + $finish = $this->start_time + apply_filters($this->identifier.'_default_time_limit', 20); // 20 seconds |
|
386 | 386 | $return = false; |
387 | 387 | |
388 | - if ( time() >= $finish ) { |
|
388 | + if (time() >= $finish) { |
|
389 | 389 | $return = true; |
390 | 390 | } |
391 | 391 | |
392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
392 | + return apply_filters($this->identifier.'_time_exceeded', $return); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | /** |
@@ -412,17 +412,17 @@ discard block |
||
412 | 412 | * |
413 | 413 | * @return mixed |
414 | 414 | */ |
415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
415 | + public function schedule_cron_healthcheck($schedules) { |
|
416 | + $interval = apply_filters($this->identifier.'_cron_interval', 5); |
|
417 | 417 | |
418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
418 | + if (property_exists($this, 'cron_interval')) { |
|
419 | + $interval = apply_filters($this->identifier.'_cron_interval', $this->cron_interval); |
|
420 | 420 | } |
421 | 421 | |
422 | 422 | // Adds every 5 minutes to the existing schedules. |
423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
423 | + $schedules[$this->identifier.'_cron_interval'] = array( |
|
424 | 424 | 'interval' => MINUTE_IN_SECONDS * $interval, |
425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
425 | + 'display' => sprintf(__('Every %d Minutes'), $interval), |
|
426 | 426 | ); |
427 | 427 | |
428 | 428 | return $schedules; |
@@ -435,12 +435,12 @@ discard block |
||
435 | 435 | * and data exists in the queue. |
436 | 436 | */ |
437 | 437 | public function handle_cron_healthcheck() { |
438 | - if ( $this->is_process_running() ) { |
|
438 | + if ($this->is_process_running()) { |
|
439 | 439 | // Background process already running. |
440 | 440 | exit; |
441 | 441 | } |
442 | 442 | |
443 | - if ( $this->is_queue_empty() ) { |
|
443 | + if ($this->is_queue_empty()) { |
|
444 | 444 | // No data to process. |
445 | 445 | $this->clear_scheduled_event(); |
446 | 446 | exit; |
@@ -455,8 +455,8 @@ discard block |
||
455 | 455 | * Schedule event |
456 | 456 | */ |
457 | 457 | protected function schedule_event() { |
458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
458 | + if ( ! wp_next_scheduled($this->cron_hook_identifier)) { |
|
459 | + wp_schedule_event(time(), $this->cron_interval_identifier, $this->cron_hook_identifier); |
|
460 | 460 | } |
461 | 461 | } |
462 | 462 | |
@@ -464,10 +464,10 @@ discard block |
||
464 | 464 | * Clear scheduled event |
465 | 465 | */ |
466 | 466 | protected function clear_scheduled_event() { |
467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
467 | + $timestamp = wp_next_scheduled($this->cron_hook_identifier); |
|
468 | 468 | |
469 | - if ( $timestamp ) { |
|
470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
469 | + if ($timestamp) { |
|
470 | + wp_unschedule_event($timestamp, $this->cron_hook_identifier); |
|
471 | 471 | } |
472 | 472 | } |
473 | 473 | |
@@ -477,12 +477,12 @@ discard block |
||
477 | 477 | * Stop processing queue items, clear cronjob and delete batch. |
478 | 478 | */ |
479 | 479 | public function cancel_process() { |
480 | - if ( ! $this->is_queue_empty() ) { |
|
480 | + if ( ! $this->is_queue_empty()) { |
|
481 | 481 | $batch = $this->get_batch(); |
482 | 482 | |
483 | - $this->delete( $batch->key ); |
|
483 | + $this->delete($batch->key); |
|
484 | 484 | |
485 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
485 | + wp_clear_scheduled_hook($this->cron_hook_identifier); |
|
486 | 486 | } |
487 | 487 | |
488 | 488 | } |
@@ -499,6 +499,6 @@ discard block |
||
499 | 499 | * |
500 | 500 | * @return mixed |
501 | 501 | */ |
502 | - abstract protected function task( $item ); |
|
502 | + abstract protected function task($item); |
|
503 | 503 | |
504 | 504 | } |