@@ -19,258 +19,258 @@ |
||
19 | 19 | */ |
20 | 20 | class Wordlift_Post_Adapter { |
21 | 21 | |
22 | - /** |
|
23 | - * The post id to which the adopter relates. |
|
24 | - * |
|
25 | - * @since 3.14.0 |
|
26 | - * |
|
27 | - * @var integer $post_id . |
|
28 | - */ |
|
29 | - private $post_id; |
|
30 | - |
|
31 | - const TYPE_ENTITY_LINK = 0; |
|
32 | - const TYPE_TERM_LINK = 1; |
|
33 | - |
|
34 | - /** |
|
35 | - * Create the {@link Wordlift_Post_Adatpter} instance. |
|
36 | - * |
|
37 | - * @param integer $post_id the post ID of the post the adopter relates to. |
|
38 | - * |
|
39 | - * @since 3.14.0 |
|
40 | - */ |
|
41 | - public function __construct( $post_id ) { |
|
42 | - |
|
43 | - $this->post_id = $post_id; |
|
44 | - |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Get the word count of the post content. |
|
49 | - * |
|
50 | - * The count is calculated over the post content after stripping shortcodes and html tags. |
|
51 | - * |
|
52 | - * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
53 | - * @since 3.14.0 |
|
54 | - */ |
|
55 | - public function word_count() { |
|
56 | - |
|
57 | - $post = get_post( $this->post_id ); |
|
58 | - |
|
59 | - /* |
|
22 | + /** |
|
23 | + * The post id to which the adopter relates. |
|
24 | + * |
|
25 | + * @since 3.14.0 |
|
26 | + * |
|
27 | + * @var integer $post_id . |
|
28 | + */ |
|
29 | + private $post_id; |
|
30 | + |
|
31 | + const TYPE_ENTITY_LINK = 0; |
|
32 | + const TYPE_TERM_LINK = 1; |
|
33 | + |
|
34 | + /** |
|
35 | + * Create the {@link Wordlift_Post_Adatpter} instance. |
|
36 | + * |
|
37 | + * @param integer $post_id the post ID of the post the adopter relates to. |
|
38 | + * |
|
39 | + * @since 3.14.0 |
|
40 | + */ |
|
41 | + public function __construct( $post_id ) { |
|
42 | + |
|
43 | + $this->post_id = $post_id; |
|
44 | + |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Get the word count of the post content. |
|
49 | + * |
|
50 | + * The count is calculated over the post content after stripping shortcodes and html tags. |
|
51 | + * |
|
52 | + * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
53 | + * @since 3.14.0 |
|
54 | + */ |
|
55 | + public function word_count() { |
|
56 | + |
|
57 | + $post = get_post( $this->post_id ); |
|
58 | + |
|
59 | + /* |
|
60 | 60 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
61 | 61 | * because the content is written elsewhere. |
62 | 62 | * |
63 | 63 | * @since 3.20.0 |
64 | 64 | */ |
65 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
66 | - |
|
67 | - return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Count words in the string, taking into account UTF-8 characters. |
|
72 | - * |
|
73 | - * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
74 | - * |
|
75 | - * @since 3.20.0 |
|
76 | - * |
|
77 | - * @param string $str The target string. |
|
78 | - * |
|
79 | - * @return int The number of words. |
|
80 | - */ |
|
81 | - private static function str_word_count_utf8( $str ) { |
|
82 | - |
|
83 | - $words = preg_split( '~[^\p{L}\p{N}\']+~u', $str ); |
|
84 | - |
|
85 | - return $words ? count( $words ) : 0; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
90 | - * |
|
91 | - * @param int $post_id Post ID. |
|
92 | - * |
|
93 | - * @return string The post permalink. |
|
94 | - * @since 3.20.0 |
|
95 | - */ |
|
96 | - public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
97 | - |
|
98 | - $object_link_service = Object_Link_Provider::get_instance(); |
|
99 | - $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
100 | - |
|
101 | - /** |
|
102 | - * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
103 | - * |
|
104 | - * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
105 | - * post permalink if we find them. |
|
106 | - */ |
|
107 | - global $wp_version; |
|
108 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
109 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
114 | - * when the production environment is copied over from a staging environment with staging |
|
115 | - * URLs. |
|
116 | - * |
|
117 | - * @param string $permalink_url The default permalink. |
|
118 | - * @param int $post_id The post id. |
|
119 | - * |
|
120 | - * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
121 | - * @since 3.20.0 |
|
122 | - * |
|
123 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
124 | - */ |
|
125 | - |
|
126 | - $uris = $object_link_service->get_same_as_uris( |
|
127 | - $post_id, |
|
128 | - $object_type |
|
129 | - ); |
|
130 | - |
|
131 | - // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
132 | - $terms = array(); |
|
133 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
134 | - // Try to find one term matching the entity. |
|
135 | - $terms = get_terms( |
|
136 | - array( |
|
137 | - 'number' => 1, |
|
138 | - 'hide_empty' => false, |
|
139 | - 'update_term_meta_cache' => false, |
|
140 | - 'meta_key' => '_wl_entity_id', |
|
141 | - 'meta_value' => $uris, |
|
142 | - 'meta_compare' => 'IN', |
|
143 | - ) |
|
144 | - ); |
|
145 | - } |
|
146 | - |
|
147 | - $type = self::TYPE_ENTITY_LINK; |
|
148 | - $term = null; |
|
149 | - // If found use the term link, otherwise the permalink. |
|
150 | - if ( 1 === count( $terms ) ) { |
|
151 | - $term = current( $terms ); |
|
152 | - $permalink = get_term_link( $term ); |
|
153 | - $type = self::TYPE_TERM_LINK; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Apply the `wl_production_permalink` filter. |
|
158 | - * |
|
159 | - * @param string $permalink The permalink. |
|
160 | - * @param int $post_id The post id. |
|
161 | - * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
162 | - * @param WP_Term $term The term if type is term link, otherwise null. |
|
163 | - * |
|
164 | - * @since 3.23.0 add the permalink type and term parameters. |
|
165 | - */ |
|
166 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get comma separated tags to be used as keywords |
|
171 | - * |
|
172 | - * @return string|void Comma separated tags |
|
173 | - * |
|
174 | - * @since 3.27.2 |
|
175 | - */ |
|
176 | - public function keywords() { |
|
177 | - $tags = get_the_tags( $this->post_id ); |
|
178 | - |
|
179 | - if ( empty( $tags ) ) { |
|
180 | - return; |
|
181 | - } |
|
182 | - |
|
183 | - return implode( |
|
184 | - ',', |
|
185 | - array_map( |
|
186 | - function ( $tag ) { |
|
187 | - return $tag->name; |
|
188 | - }, |
|
189 | - $tags |
|
190 | - ) |
|
191 | - ); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Get comma separated categories to be used as article section |
|
196 | - * |
|
197 | - * @return string[] Comma separated categories |
|
198 | - * |
|
199 | - * @since 3.27.2 |
|
200 | - */ |
|
201 | - public function article_section() { |
|
202 | - $categories = get_the_category( $this->post_id ); |
|
203 | - |
|
204 | - return wp_list_pluck( $categories, 'cat_name' ); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Get comment count |
|
209 | - * |
|
210 | - * @return string|void Comment count |
|
211 | - * |
|
212 | - * @since 3.27.2 |
|
213 | - */ |
|
214 | - public function comment_count() { |
|
215 | - return get_comments_number( $this->post_id ); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Get Language |
|
220 | - * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
221 | - * |
|
222 | - * @return string|void Language code (locale) |
|
223 | - * |
|
224 | - * @since 3.27.2 |
|
225 | - */ |
|
226 | - public function locale() { |
|
227 | - $language = 'en-US'; |
|
228 | - |
|
229 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
230 | - // WPML handling |
|
231 | - // WPML: Updated function signature. |
|
232 | - // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
|
233 | - $post_language = wpml_get_language_information( null, $this->post_id ); |
|
234 | - if ( ! $post_language instanceof WP_Error ) { |
|
235 | - $language = $post_language['locale']; |
|
236 | - } |
|
237 | - } elseif ( function_exists( 'pll_get_post_language' ) ) { |
|
238 | - // Polylang handling |
|
239 | - $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
240 | - } else { |
|
241 | - $language = get_locale(); |
|
242 | - } |
|
243 | - |
|
244 | - return str_replace( '_', '-', $language ); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Add mentions to an article. |
|
249 | - * To add mentions we just push the referenced entities to mentions. |
|
250 | - * |
|
251 | - * @param $post_id int |
|
252 | - * @param $references int[] |
|
253 | - */ |
|
254 | - public function add_references( $post_id, &$references ) { |
|
255 | - $tags = get_the_tags( $post_id ); |
|
256 | - |
|
257 | - if ( $tags && ! is_wp_error( $tags ) ) { |
|
258 | - // Loop through the tags and push it to references. |
|
259 | - foreach ( $tags as $tag ) { |
|
260 | - /** |
|
261 | - * @var $tag WP_Term |
|
262 | - */ |
|
263 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
264 | - foreach ( $entity_uris as $uri ) { |
|
265 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
266 | - if ( $referenced_entity instanceof WP_Post ) { |
|
267 | - // push the referenced entities to references. |
|
268 | - $references[] = $referenced_entity->ID; |
|
269 | - } |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - } |
|
65 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
66 | + |
|
67 | + return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Count words in the string, taking into account UTF-8 characters. |
|
72 | + * |
|
73 | + * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
74 | + * |
|
75 | + * @since 3.20.0 |
|
76 | + * |
|
77 | + * @param string $str The target string. |
|
78 | + * |
|
79 | + * @return int The number of words. |
|
80 | + */ |
|
81 | + private static function str_word_count_utf8( $str ) { |
|
82 | + |
|
83 | + $words = preg_split( '~[^\p{L}\p{N}\']+~u', $str ); |
|
84 | + |
|
85 | + return $words ? count( $words ) : 0; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
90 | + * |
|
91 | + * @param int $post_id Post ID. |
|
92 | + * |
|
93 | + * @return string The post permalink. |
|
94 | + * @since 3.20.0 |
|
95 | + */ |
|
96 | + public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
97 | + |
|
98 | + $object_link_service = Object_Link_Provider::get_instance(); |
|
99 | + $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
100 | + |
|
101 | + /** |
|
102 | + * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
103 | + * |
|
104 | + * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
105 | + * post permalink if we find them. |
|
106 | + */ |
|
107 | + global $wp_version; |
|
108 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
109 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
114 | + * when the production environment is copied over from a staging environment with staging |
|
115 | + * URLs. |
|
116 | + * |
|
117 | + * @param string $permalink_url The default permalink. |
|
118 | + * @param int $post_id The post id. |
|
119 | + * |
|
120 | + * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
121 | + * @since 3.20.0 |
|
122 | + * |
|
123 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
124 | + */ |
|
125 | + |
|
126 | + $uris = $object_link_service->get_same_as_uris( |
|
127 | + $post_id, |
|
128 | + $object_type |
|
129 | + ); |
|
130 | + |
|
131 | + // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
132 | + $terms = array(); |
|
133 | + if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
134 | + // Try to find one term matching the entity. |
|
135 | + $terms = get_terms( |
|
136 | + array( |
|
137 | + 'number' => 1, |
|
138 | + 'hide_empty' => false, |
|
139 | + 'update_term_meta_cache' => false, |
|
140 | + 'meta_key' => '_wl_entity_id', |
|
141 | + 'meta_value' => $uris, |
|
142 | + 'meta_compare' => 'IN', |
|
143 | + ) |
|
144 | + ); |
|
145 | + } |
|
146 | + |
|
147 | + $type = self::TYPE_ENTITY_LINK; |
|
148 | + $term = null; |
|
149 | + // If found use the term link, otherwise the permalink. |
|
150 | + if ( 1 === count( $terms ) ) { |
|
151 | + $term = current( $terms ); |
|
152 | + $permalink = get_term_link( $term ); |
|
153 | + $type = self::TYPE_TERM_LINK; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Apply the `wl_production_permalink` filter. |
|
158 | + * |
|
159 | + * @param string $permalink The permalink. |
|
160 | + * @param int $post_id The post id. |
|
161 | + * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
162 | + * @param WP_Term $term The term if type is term link, otherwise null. |
|
163 | + * |
|
164 | + * @since 3.23.0 add the permalink type and term parameters. |
|
165 | + */ |
|
166 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get comma separated tags to be used as keywords |
|
171 | + * |
|
172 | + * @return string|void Comma separated tags |
|
173 | + * |
|
174 | + * @since 3.27.2 |
|
175 | + */ |
|
176 | + public function keywords() { |
|
177 | + $tags = get_the_tags( $this->post_id ); |
|
178 | + |
|
179 | + if ( empty( $tags ) ) { |
|
180 | + return; |
|
181 | + } |
|
182 | + |
|
183 | + return implode( |
|
184 | + ',', |
|
185 | + array_map( |
|
186 | + function ( $tag ) { |
|
187 | + return $tag->name; |
|
188 | + }, |
|
189 | + $tags |
|
190 | + ) |
|
191 | + ); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Get comma separated categories to be used as article section |
|
196 | + * |
|
197 | + * @return string[] Comma separated categories |
|
198 | + * |
|
199 | + * @since 3.27.2 |
|
200 | + */ |
|
201 | + public function article_section() { |
|
202 | + $categories = get_the_category( $this->post_id ); |
|
203 | + |
|
204 | + return wp_list_pluck( $categories, 'cat_name' ); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Get comment count |
|
209 | + * |
|
210 | + * @return string|void Comment count |
|
211 | + * |
|
212 | + * @since 3.27.2 |
|
213 | + */ |
|
214 | + public function comment_count() { |
|
215 | + return get_comments_number( $this->post_id ); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Get Language |
|
220 | + * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
221 | + * |
|
222 | + * @return string|void Language code (locale) |
|
223 | + * |
|
224 | + * @since 3.27.2 |
|
225 | + */ |
|
226 | + public function locale() { |
|
227 | + $language = 'en-US'; |
|
228 | + |
|
229 | + if ( function_exists( 'wpml_get_language_information' ) ) { |
|
230 | + // WPML handling |
|
231 | + // WPML: Updated function signature. |
|
232 | + // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
|
233 | + $post_language = wpml_get_language_information( null, $this->post_id ); |
|
234 | + if ( ! $post_language instanceof WP_Error ) { |
|
235 | + $language = $post_language['locale']; |
|
236 | + } |
|
237 | + } elseif ( function_exists( 'pll_get_post_language' ) ) { |
|
238 | + // Polylang handling |
|
239 | + $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
240 | + } else { |
|
241 | + $language = get_locale(); |
|
242 | + } |
|
243 | + |
|
244 | + return str_replace( '_', '-', $language ); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Add mentions to an article. |
|
249 | + * To add mentions we just push the referenced entities to mentions. |
|
250 | + * |
|
251 | + * @param $post_id int |
|
252 | + * @param $references int[] |
|
253 | + */ |
|
254 | + public function add_references( $post_id, &$references ) { |
|
255 | + $tags = get_the_tags( $post_id ); |
|
256 | + |
|
257 | + if ( $tags && ! is_wp_error( $tags ) ) { |
|
258 | + // Loop through the tags and push it to references. |
|
259 | + foreach ( $tags as $tag ) { |
|
260 | + /** |
|
261 | + * @var $tag WP_Term |
|
262 | + */ |
|
263 | + $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
264 | + foreach ( $entity_uris as $uri ) { |
|
265 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
266 | + if ( $referenced_entity instanceof WP_Post ) { |
|
267 | + // push the referenced entities to references. |
|
268 | + $references[] = $referenced_entity->ID; |
|
269 | + } |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + } |
|
275 | 275 | |
276 | 276 | } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @since 3.14.0 |
40 | 40 | */ |
41 | - public function __construct( $post_id ) { |
|
41 | + public function __construct($post_id) { |
|
42 | 42 | |
43 | 43 | $this->post_id = $post_id; |
44 | 44 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function word_count() { |
56 | 56 | |
57 | - $post = get_post( $this->post_id ); |
|
57 | + $post = get_post($this->post_id); |
|
58 | 58 | |
59 | 59 | /* |
60 | 60 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
@@ -62,9 +62,9 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @since 3.20.0 |
64 | 64 | */ |
65 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
65 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
66 | 66 | |
67 | - return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
67 | + return self::str_word_count_utf8(wp_strip_all_tags(strip_shortcodes($post_content))); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -78,11 +78,11 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return int The number of words. |
80 | 80 | */ |
81 | - private static function str_word_count_utf8( $str ) { |
|
81 | + private static function str_word_count_utf8($str) { |
|
82 | 82 | |
83 | - $words = preg_split( '~[^\p{L}\p{N}\']+~u', $str ); |
|
83 | + $words = preg_split('~[^\p{L}\p{N}\']+~u', $str); |
|
84 | 84 | |
85 | - return $words ? count( $words ) : 0; |
|
85 | + return $words ? count($words) : 0; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | * @return string The post permalink. |
94 | 94 | * @since 3.20.0 |
95 | 95 | */ |
96 | - public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
96 | + public static function get_production_permalink($post_id, $object_type = Object_Type_Enum::POST) { |
|
97 | 97 | |
98 | 98 | $object_link_service = Object_Link_Provider::get_instance(); |
99 | - $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
99 | + $permalink = $object_link_service->get_permalink($post_id, $object_type); |
|
100 | 100 | |
101 | 101 | /** |
102 | 102 | * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
@@ -105,8 +105,8 @@ discard block |
||
105 | 105 | * post permalink if we find them. |
106 | 106 | */ |
107 | 107 | global $wp_version; |
108 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
109 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
108 | + if (version_compare($wp_version, '4.5', '<')) { |
|
109 | + return apply_filters('wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | /** |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | |
131 | 131 | // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
132 | 132 | $terms = array(); |
133 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
133 | + if (defined('WL_ENABLE_TERM_LINKING') && WL_ENABLE_TERM_LINKING) { |
|
134 | 134 | // Try to find one term matching the entity. |
135 | 135 | $terms = get_terms( |
136 | 136 | array( |
@@ -147,9 +147,9 @@ discard block |
||
147 | 147 | $type = self::TYPE_ENTITY_LINK; |
148 | 148 | $term = null; |
149 | 149 | // If found use the term link, otherwise the permalink. |
150 | - if ( 1 === count( $terms ) ) { |
|
151 | - $term = current( $terms ); |
|
152 | - $permalink = get_term_link( $term ); |
|
150 | + if (1 === count($terms)) { |
|
151 | + $term = current($terms); |
|
152 | + $permalink = get_term_link($term); |
|
153 | 153 | $type = self::TYPE_TERM_LINK; |
154 | 154 | } |
155 | 155 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * |
164 | 164 | * @since 3.23.0 add the permalink type and term parameters. |
165 | 165 | */ |
166 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
166 | + return apply_filters('wl_production_permalink', $permalink, $post_id, $type, $term); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -174,16 +174,16 @@ discard block |
||
174 | 174 | * @since 3.27.2 |
175 | 175 | */ |
176 | 176 | public function keywords() { |
177 | - $tags = get_the_tags( $this->post_id ); |
|
177 | + $tags = get_the_tags($this->post_id); |
|
178 | 178 | |
179 | - if ( empty( $tags ) ) { |
|
179 | + if (empty($tags)) { |
|
180 | 180 | return; |
181 | 181 | } |
182 | 182 | |
183 | 183 | return implode( |
184 | 184 | ',', |
185 | 185 | array_map( |
186 | - function ( $tag ) { |
|
186 | + function($tag) { |
|
187 | 187 | return $tag->name; |
188 | 188 | }, |
189 | 189 | $tags |
@@ -199,9 +199,9 @@ discard block |
||
199 | 199 | * @since 3.27.2 |
200 | 200 | */ |
201 | 201 | public function article_section() { |
202 | - $categories = get_the_category( $this->post_id ); |
|
202 | + $categories = get_the_category($this->post_id); |
|
203 | 203 | |
204 | - return wp_list_pluck( $categories, 'cat_name' ); |
|
204 | + return wp_list_pluck($categories, 'cat_name'); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | /** |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | * @since 3.27.2 |
213 | 213 | */ |
214 | 214 | public function comment_count() { |
215 | - return get_comments_number( $this->post_id ); |
|
215 | + return get_comments_number($this->post_id); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -226,22 +226,22 @@ discard block |
||
226 | 226 | public function locale() { |
227 | 227 | $language = 'en-US'; |
228 | 228 | |
229 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
229 | + if (function_exists('wpml_get_language_information')) { |
|
230 | 230 | // WPML handling |
231 | 231 | // WPML: Updated function signature. |
232 | 232 | // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
233 | - $post_language = wpml_get_language_information( null, $this->post_id ); |
|
234 | - if ( ! $post_language instanceof WP_Error ) { |
|
233 | + $post_language = wpml_get_language_information(null, $this->post_id); |
|
234 | + if ( ! $post_language instanceof WP_Error) { |
|
235 | 235 | $language = $post_language['locale']; |
236 | 236 | } |
237 | - } elseif ( function_exists( 'pll_get_post_language' ) ) { |
|
237 | + } elseif (function_exists('pll_get_post_language')) { |
|
238 | 238 | // Polylang handling |
239 | - $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
239 | + $language = pll_get_post_language($this->post_id, 'locale'); |
|
240 | 240 | } else { |
241 | 241 | $language = get_locale(); |
242 | 242 | } |
243 | 243 | |
244 | - return str_replace( '_', '-', $language ); |
|
244 | + return str_replace('_', '-', $language); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -251,19 +251,19 @@ discard block |
||
251 | 251 | * @param $post_id int |
252 | 252 | * @param $references int[] |
253 | 253 | */ |
254 | - public function add_references( $post_id, &$references ) { |
|
255 | - $tags = get_the_tags( $post_id ); |
|
254 | + public function add_references($post_id, &$references) { |
|
255 | + $tags = get_the_tags($post_id); |
|
256 | 256 | |
257 | - if ( $tags && ! is_wp_error( $tags ) ) { |
|
257 | + if ($tags && ! is_wp_error($tags)) { |
|
258 | 258 | // Loop through the tags and push it to references. |
259 | - foreach ( $tags as $tag ) { |
|
259 | + foreach ($tags as $tag) { |
|
260 | 260 | /** |
261 | 261 | * @var $tag WP_Term |
262 | 262 | */ |
263 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
264 | - foreach ( $entity_uris as $uri ) { |
|
265 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
266 | - if ( $referenced_entity instanceof WP_Post ) { |
|
263 | + $entity_uris = get_term_meta($tag->term_id, '_wl_entity_id'); |
|
264 | + foreach ($entity_uris as $uri) { |
|
265 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity($uri); |
|
266 | + if ($referenced_entity instanceof WP_Post) { |
|
267 | 267 | // push the referenced entities to references. |
268 | 268 | $references[] = $referenced_entity->ID; |
269 | 269 | } |
@@ -20,100 +20,100 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class Wordlift_Entity_Uri_Service { |
22 | 22 | |
23 | - /** |
|
24 | - * A {@link Wordlift_Log_Service} instance. |
|
25 | - * |
|
26 | - * @since 3.16.3 |
|
27 | - * @access private |
|
28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29 | - */ |
|
30 | - private $log; |
|
31 | - |
|
32 | - /** |
|
33 | - * An array of URIs to post ID valid for the current request. |
|
34 | - * |
|
35 | - * @since 3.16.3 |
|
36 | - * @access private |
|
37 | - * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
38 | - */ |
|
39 | - protected $uri_to_post; |
|
40 | - |
|
41 | - /** |
|
42 | - * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
43 | - * |
|
44 | - * @since 3.16.3 |
|
45 | - */ |
|
46 | - protected function __construct() { |
|
47 | - |
|
48 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
49 | - |
|
50 | - // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
|
51 | - add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
52 | - add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 ); |
|
53 | - |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Holds the {@link Wordlift_Entity_Uri_Service} instance. |
|
58 | - * |
|
59 | - * @since 3.21.5 |
|
60 | - * @access private |
|
61 | - * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton. |
|
62 | - */ |
|
63 | - private static $instance = null; |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the singleton. |
|
67 | - * |
|
68 | - * @return Wordlift_Entity_Uri_Service The singleton instance. |
|
69 | - * @since 3.21.5 |
|
70 | - */ |
|
71 | - public static function get_instance() { |
|
72 | - |
|
73 | - if ( ! isset( self::$instance ) ) { |
|
74 | - $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' ); |
|
75 | - self::$instance = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service ); |
|
76 | - |
|
77 | - } |
|
78 | - |
|
79 | - return self::$instance; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Try to find a post when the content service doesn't find it. |
|
84 | - * |
|
85 | - * @param WP_Post|null $post |
|
86 | - * @param string $uri |
|
87 | - * |
|
88 | - * @return false|int |
|
89 | - */ |
|
90 | - public function content_service__post__not_found( $post, $uri ) { |
|
91 | - return $this->get_post_id_from_url( $uri ); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Preload the provided URIs in the local cache. |
|
96 | - * |
|
97 | - * This function will populate the local `$uri_to_post` array by running a |
|
98 | - * single query with all the URIs and returning the mappings in the array. |
|
99 | - * |
|
100 | - * @param array $uris An array of URIs. |
|
101 | - * |
|
102 | - * @since 3.16.3 |
|
103 | - */ |
|
104 | - public function preload_uris( $uris ) { |
|
105 | - |
|
106 | - // Bail out if there are no URIs. |
|
107 | - if ( 0 === count( $uris ) ) { |
|
108 | - return; |
|
109 | - } |
|
110 | - |
|
111 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
112 | - |
|
113 | - global $wpdb; |
|
114 | - $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
115 | - $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
116 | - $sql = " |
|
23 | + /** |
|
24 | + * A {@link Wordlift_Log_Service} instance. |
|
25 | + * |
|
26 | + * @since 3.16.3 |
|
27 | + * @access private |
|
28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29 | + */ |
|
30 | + private $log; |
|
31 | + |
|
32 | + /** |
|
33 | + * An array of URIs to post ID valid for the current request. |
|
34 | + * |
|
35 | + * @since 3.16.3 |
|
36 | + * @access private |
|
37 | + * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
38 | + */ |
|
39 | + protected $uri_to_post; |
|
40 | + |
|
41 | + /** |
|
42 | + * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
43 | + * |
|
44 | + * @since 3.16.3 |
|
45 | + */ |
|
46 | + protected function __construct() { |
|
47 | + |
|
48 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
49 | + |
|
50 | + // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
|
51 | + add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
52 | + add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 ); |
|
53 | + |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Holds the {@link Wordlift_Entity_Uri_Service} instance. |
|
58 | + * |
|
59 | + * @since 3.21.5 |
|
60 | + * @access private |
|
61 | + * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton. |
|
62 | + */ |
|
63 | + private static $instance = null; |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the singleton. |
|
67 | + * |
|
68 | + * @return Wordlift_Entity_Uri_Service The singleton instance. |
|
69 | + * @since 3.21.5 |
|
70 | + */ |
|
71 | + public static function get_instance() { |
|
72 | + |
|
73 | + if ( ! isset( self::$instance ) ) { |
|
74 | + $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' ); |
|
75 | + self::$instance = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service ); |
|
76 | + |
|
77 | + } |
|
78 | + |
|
79 | + return self::$instance; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Try to find a post when the content service doesn't find it. |
|
84 | + * |
|
85 | + * @param WP_Post|null $post |
|
86 | + * @param string $uri |
|
87 | + * |
|
88 | + * @return false|int |
|
89 | + */ |
|
90 | + public function content_service__post__not_found( $post, $uri ) { |
|
91 | + return $this->get_post_id_from_url( $uri ); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Preload the provided URIs in the local cache. |
|
96 | + * |
|
97 | + * This function will populate the local `$uri_to_post` array by running a |
|
98 | + * single query with all the URIs and returning the mappings in the array. |
|
99 | + * |
|
100 | + * @param array $uris An array of URIs. |
|
101 | + * |
|
102 | + * @since 3.16.3 |
|
103 | + */ |
|
104 | + public function preload_uris( $uris ) { |
|
105 | + |
|
106 | + // Bail out if there are no URIs. |
|
107 | + if ( 0 === count( $uris ) ) { |
|
108 | + return; |
|
109 | + } |
|
110 | + |
|
111 | + $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
112 | + |
|
113 | + global $wpdb; |
|
114 | + $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
115 | + $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
116 | + $sql = " |
|
117 | 117 | SELECT ID FROM $wpdb->posts p |
118 | 118 | INNER JOIN $wpdb->postmeta pm |
119 | 119 | ON pm.post_id = p.ID |
@@ -123,150 +123,150 @@ discard block |
||
123 | 123 | AND p.post_status IN ( 'publish', 'draft', 'private', 'future' ) |
124 | 124 | "; |
125 | 125 | |
126 | - // Get the posts. |
|
127 | - $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
128 | - |
|
129 | - // Populate the array. We reinitialize the array on purpose because |
|
130 | - // we don't want these data to long live. |
|
131 | - $this->uri_to_post = array_reduce( |
|
132 | - $posts, |
|
133 | - function ( $carry, $item ) { |
|
134 | - $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
135 | - |
|
136 | - $uri = Wordpress_Content_Service::get_instance() |
|
137 | - ->get_entity_id( Wordpress_Content_Id::create_post( $item ) ); |
|
138 | - |
|
139 | - if ( isset( $uri ) ) { |
|
140 | - $uris[] = $uri; |
|
141 | - } |
|
142 | - |
|
143 | - return $carry |
|
144 | - // Get the URI related to the post and fill them with the item id. |
|
145 | - + array_fill_keys( $uris, $item ); |
|
146 | - }, |
|
147 | - array() |
|
148 | - ); |
|
149 | - |
|
150 | - // Add the not found URIs. |
|
151 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
152 | - |
|
153 | - $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' ); |
|
154 | - |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Reset the URI to post local cache. |
|
159 | - * |
|
160 | - * @since 3.16.3 |
|
161 | - */ |
|
162 | - public function reset_uris() { |
|
163 | - |
|
164 | - $this->uri_to_post = array(); |
|
165 | - |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
170 | - * |
|
171 | - * @param string $uri The entity URI. |
|
172 | - * |
|
173 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
174 | - * @since 3.2.0 |
|
175 | - */ |
|
176 | - public function get_entity( $uri ) { |
|
177 | - |
|
178 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
179 | - |
|
180 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
181 | - |
|
182 | - // Return null if the content isn't found or isn't a post. |
|
183 | - if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
184 | - return null; |
|
185 | - } |
|
186 | - |
|
187 | - return $content->get_bag(); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Determines whether a given uri is an internal uri or not. |
|
192 | - * |
|
193 | - * @param string $uri An uri. |
|
194 | - * |
|
195 | - * @return true if the uri internal to the current dataset otherwise false. |
|
196 | - * @since 3.16.3 |
|
197 | - */ |
|
198 | - public function is_internal( $uri ) { |
|
199 | - |
|
200 | - return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) ); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`. |
|
205 | - * |
|
206 | - * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need |
|
207 | - * to provide full compatibility with WordPress 4.4+. |
|
208 | - * |
|
209 | - * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. |
|
210 | - * |
|
211 | - * @return WP_HTTP_Response The result to send to the client. |
|
212 | - * |
|
213 | - * @since 3.23.0 |
|
214 | - */ |
|
215 | - public function rest_post_dispatch( $result ) { |
|
216 | - |
|
217 | - // Get a reference to the actual data. |
|
218 | - $data = &$result->data; |
|
219 | - |
|
220 | - // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
|
221 | - if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
222 | - || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
223 | - return $result; |
|
224 | - } |
|
225 | - |
|
226 | - // Add the `wl:entity_url`. |
|
227 | - $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
228 | - |
|
229 | - return $result; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Helper function to fetch post_id from a WordPress URL |
|
234 | - * Primarily used when dataset is not enabled |
|
235 | - * |
|
236 | - * @param $url |
|
237 | - * |
|
238 | - * @return int Post ID | bool false |
|
239 | - */ |
|
240 | - public function get_post_id_from_url( $url ) { |
|
241 | - |
|
242 | - // Try url_to_postid |
|
243 | - $post_id = url_to_postid( htmlspecialchars_decode( $url ) ); |
|
244 | - if ( 0 !== $post_id ) { |
|
245 | - return $post_id; |
|
246 | - } |
|
247 | - |
|
248 | - $parsed_url = wp_parse_url( $url ); |
|
249 | - |
|
250 | - if ( ! isset( $parsed_url['query'] ) ) { |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - parse_str( $parsed_url['query'], $parsed_query ); |
|
255 | - |
|
256 | - // Try to parse WooCommerce non-pretty product URL |
|
257 | - if ( $parsed_query['product'] ) { |
|
258 | - $posts = get_posts( |
|
259 | - array( |
|
260 | - 'name' => $parsed_query['product'], |
|
261 | - 'post_type' => 'product', |
|
262 | - ) |
|
263 | - ); |
|
264 | - if ( count( $posts ) > 0 ) { |
|
265 | - return $posts[0]->ID; |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - return false; |
|
270 | - } |
|
126 | + // Get the posts. |
|
127 | + $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
128 | + |
|
129 | + // Populate the array. We reinitialize the array on purpose because |
|
130 | + // we don't want these data to long live. |
|
131 | + $this->uri_to_post = array_reduce( |
|
132 | + $posts, |
|
133 | + function ( $carry, $item ) { |
|
134 | + $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
135 | + |
|
136 | + $uri = Wordpress_Content_Service::get_instance() |
|
137 | + ->get_entity_id( Wordpress_Content_Id::create_post( $item ) ); |
|
138 | + |
|
139 | + if ( isset( $uri ) ) { |
|
140 | + $uris[] = $uri; |
|
141 | + } |
|
142 | + |
|
143 | + return $carry |
|
144 | + // Get the URI related to the post and fill them with the item id. |
|
145 | + + array_fill_keys( $uris, $item ); |
|
146 | + }, |
|
147 | + array() |
|
148 | + ); |
|
149 | + |
|
150 | + // Add the not found URIs. |
|
151 | + $this->uri_to_post += array_fill_keys( $uris, null ); |
|
152 | + |
|
153 | + $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' ); |
|
154 | + |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Reset the URI to post local cache. |
|
159 | + * |
|
160 | + * @since 3.16.3 |
|
161 | + */ |
|
162 | + public function reset_uris() { |
|
163 | + |
|
164 | + $this->uri_to_post = array(); |
|
165 | + |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
170 | + * |
|
171 | + * @param string $uri The entity URI. |
|
172 | + * |
|
173 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
174 | + * @since 3.2.0 |
|
175 | + */ |
|
176 | + public function get_entity( $uri ) { |
|
177 | + |
|
178 | + $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
179 | + |
|
180 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
181 | + |
|
182 | + // Return null if the content isn't found or isn't a post. |
|
183 | + if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
184 | + return null; |
|
185 | + } |
|
186 | + |
|
187 | + return $content->get_bag(); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Determines whether a given uri is an internal uri or not. |
|
192 | + * |
|
193 | + * @param string $uri An uri. |
|
194 | + * |
|
195 | + * @return true if the uri internal to the current dataset otherwise false. |
|
196 | + * @since 3.16.3 |
|
197 | + */ |
|
198 | + public function is_internal( $uri ) { |
|
199 | + |
|
200 | + return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) ); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`. |
|
205 | + * |
|
206 | + * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need |
|
207 | + * to provide full compatibility with WordPress 4.4+. |
|
208 | + * |
|
209 | + * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. |
|
210 | + * |
|
211 | + * @return WP_HTTP_Response The result to send to the client. |
|
212 | + * |
|
213 | + * @since 3.23.0 |
|
214 | + */ |
|
215 | + public function rest_post_dispatch( $result ) { |
|
216 | + |
|
217 | + // Get a reference to the actual data. |
|
218 | + $data = &$result->data; |
|
219 | + |
|
220 | + // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
|
221 | + if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
222 | + || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
223 | + return $result; |
|
224 | + } |
|
225 | + |
|
226 | + // Add the `wl:entity_url`. |
|
227 | + $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
228 | + |
|
229 | + return $result; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Helper function to fetch post_id from a WordPress URL |
|
234 | + * Primarily used when dataset is not enabled |
|
235 | + * |
|
236 | + * @param $url |
|
237 | + * |
|
238 | + * @return int Post ID | bool false |
|
239 | + */ |
|
240 | + public function get_post_id_from_url( $url ) { |
|
241 | + |
|
242 | + // Try url_to_postid |
|
243 | + $post_id = url_to_postid( htmlspecialchars_decode( $url ) ); |
|
244 | + if ( 0 !== $post_id ) { |
|
245 | + return $post_id; |
|
246 | + } |
|
247 | + |
|
248 | + $parsed_url = wp_parse_url( $url ); |
|
249 | + |
|
250 | + if ( ! isset( $parsed_url['query'] ) ) { |
|
251 | + return false; |
|
252 | + } |
|
253 | + |
|
254 | + parse_str( $parsed_url['query'], $parsed_query ); |
|
255 | + |
|
256 | + // Try to parse WooCommerce non-pretty product URL |
|
257 | + if ( $parsed_query['product'] ) { |
|
258 | + $posts = get_posts( |
|
259 | + array( |
|
260 | + 'name' => $parsed_query['product'], |
|
261 | + 'post_type' => 'product', |
|
262 | + ) |
|
263 | + ); |
|
264 | + if ( count( $posts ) > 0 ) { |
|
265 | + return $posts[0]->ID; |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + return false; |
|
270 | + } |
|
271 | 271 | |
272 | 272 | } |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | */ |
46 | 46 | protected function __construct() { |
47 | 47 | |
48 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
48 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
49 | 49 | |
50 | 50 | // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
51 | - add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
52 | - add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 ); |
|
51 | + add_filter('rest_post_dispatch', array($this, 'rest_post_dispatch')); |
|
52 | + add_filter('wl_content_service__post__not_found', array($this, 'content_service__post__not_found'), 10, 2); |
|
53 | 53 | |
54 | 54 | } |
55 | 55 | |
@@ -70,9 +70,9 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public static function get_instance() { |
72 | 72 | |
73 | - if ( ! isset( self::$instance ) ) { |
|
74 | - $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' ); |
|
75 | - self::$instance = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service ); |
|
73 | + if ( ! isset(self::$instance)) { |
|
74 | + $entity_uri_cache_service = new Wordlift_File_Cache_Service(WL_TEMP_DIR.'entity_uri/'); |
|
75 | + self::$instance = new Wordlift_Cached_Entity_Uri_Service($entity_uri_cache_service); |
|
76 | 76 | |
77 | 77 | } |
78 | 78 | |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @return false|int |
89 | 89 | */ |
90 | - public function content_service__post__not_found( $post, $uri ) { |
|
91 | - return $this->get_post_id_from_url( $uri ); |
|
90 | + public function content_service__post__not_found($post, $uri) { |
|
91 | + return $this->get_post_id_from_url($uri); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -101,18 +101,18 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @since 3.16.3 |
103 | 103 | */ |
104 | - public function preload_uris( $uris ) { |
|
104 | + public function preload_uris($uris) { |
|
105 | 105 | |
106 | 106 | // Bail out if there are no URIs. |
107 | - if ( 0 === count( $uris ) ) { |
|
107 | + if (0 === count($uris)) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | |
111 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
111 | + $this->log->trace('Preloading '.count($uris).' URI(s)...'); |
|
112 | 112 | |
113 | 113 | global $wpdb; |
114 | - $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
115 | - $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
114 | + $in_post_types = implode("','", array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types())); |
|
115 | + $in_entity_uris = implode("','", array_map('esc_sql', $uris)); |
|
116 | 116 | $sql = " |
117 | 117 | SELECT ID FROM $wpdb->posts p |
118 | 118 | INNER JOIN $wpdb->postmeta pm |
@@ -124,33 +124,33 @@ discard block |
||
124 | 124 | "; |
125 | 125 | |
126 | 126 | // Get the posts. |
127 | - $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
127 | + $posts = $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
128 | 128 | |
129 | 129 | // Populate the array. We reinitialize the array on purpose because |
130 | 130 | // we don't want these data to long live. |
131 | 131 | $this->uri_to_post = array_reduce( |
132 | 132 | $posts, |
133 | - function ( $carry, $item ) { |
|
134 | - $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
133 | + function($carry, $item) { |
|
134 | + $uris = get_post_meta($item, Wordlift_Schema_Service::FIELD_SAME_AS); |
|
135 | 135 | |
136 | 136 | $uri = Wordpress_Content_Service::get_instance() |
137 | - ->get_entity_id( Wordpress_Content_Id::create_post( $item ) ); |
|
137 | + ->get_entity_id(Wordpress_Content_Id::create_post($item)); |
|
138 | 138 | |
139 | - if ( isset( $uri ) ) { |
|
139 | + if (isset($uri)) { |
|
140 | 140 | $uris[] = $uri; |
141 | 141 | } |
142 | 142 | |
143 | 143 | return $carry |
144 | 144 | // Get the URI related to the post and fill them with the item id. |
145 | - + array_fill_keys( $uris, $item ); |
|
145 | + + array_fill_keys($uris, $item); |
|
146 | 146 | }, |
147 | 147 | array() |
148 | 148 | ); |
149 | 149 | |
150 | 150 | // Add the not found URIs. |
151 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
151 | + $this->uri_to_post += array_fill_keys($uris, null); |
|
152 | 152 | |
153 | - $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' ); |
|
153 | + $this->log->debug(count($this->uri_to_post).' URI(s) preloaded.'); |
|
154 | 154 | |
155 | 155 | } |
156 | 156 | |
@@ -173,14 +173,14 @@ discard block |
||
173 | 173 | * @return WP_Post|null A WP_Post instance or null if not found. |
174 | 174 | * @since 3.2.0 |
175 | 175 | */ |
176 | - public function get_entity( $uri ) { |
|
176 | + public function get_entity($uri) { |
|
177 | 177 | |
178 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
178 | + $this->log->trace("Getting an entity post for URI $uri..."); |
|
179 | 179 | |
180 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
180 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri); |
|
181 | 181 | |
182 | 182 | // Return null if the content isn't found or isn't a post. |
183 | - if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
183 | + if ( ! isset($content) || ! is_a($content->get_bag(), '\WP_Post')) { |
|
184 | 184 | return null; |
185 | 185 | } |
186 | 186 | |
@@ -195,9 +195,9 @@ discard block |
||
195 | 195 | * @return true if the uri internal to the current dataset otherwise false. |
196 | 196 | * @since 3.16.3 |
197 | 197 | */ |
198 | - public function is_internal( $uri ) { |
|
198 | + public function is_internal($uri) { |
|
199 | 199 | |
200 | - return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) ); |
|
200 | + return (0 === strrpos($uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri())); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | /** |
@@ -212,19 +212,19 @@ discard block |
||
212 | 212 | * |
213 | 213 | * @since 3.23.0 |
214 | 214 | */ |
215 | - public function rest_post_dispatch( $result ) { |
|
215 | + public function rest_post_dispatch($result) { |
|
216 | 216 | |
217 | 217 | // Get a reference to the actual data. |
218 | 218 | $data = &$result->data; |
219 | 219 | |
220 | 220 | // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
221 | - if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
222 | - || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
221 | + if ( ! is_array($data) || ! isset($data['id']) || ! isset($data['type']) |
|
222 | + || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($data['type'])) { |
|
223 | 223 | return $result; |
224 | 224 | } |
225 | 225 | |
226 | 226 | // Add the `wl:entity_url`. |
227 | - $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
227 | + $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri($data['id']); |
|
228 | 228 | |
229 | 229 | return $result; |
230 | 230 | } |
@@ -237,31 +237,31 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return int Post ID | bool false |
239 | 239 | */ |
240 | - public function get_post_id_from_url( $url ) { |
|
240 | + public function get_post_id_from_url($url) { |
|
241 | 241 | |
242 | 242 | // Try url_to_postid |
243 | - $post_id = url_to_postid( htmlspecialchars_decode( $url ) ); |
|
244 | - if ( 0 !== $post_id ) { |
|
243 | + $post_id = url_to_postid(htmlspecialchars_decode($url)); |
|
244 | + if (0 !== $post_id) { |
|
245 | 245 | return $post_id; |
246 | 246 | } |
247 | 247 | |
248 | - $parsed_url = wp_parse_url( $url ); |
|
248 | + $parsed_url = wp_parse_url($url); |
|
249 | 249 | |
250 | - if ( ! isset( $parsed_url['query'] ) ) { |
|
250 | + if ( ! isset($parsed_url['query'])) { |
|
251 | 251 | return false; |
252 | 252 | } |
253 | 253 | |
254 | - parse_str( $parsed_url['query'], $parsed_query ); |
|
254 | + parse_str($parsed_url['query'], $parsed_query); |
|
255 | 255 | |
256 | 256 | // Try to parse WooCommerce non-pretty product URL |
257 | - if ( $parsed_query['product'] ) { |
|
257 | + if ($parsed_query['product']) { |
|
258 | 258 | $posts = get_posts( |
259 | 259 | array( |
260 | 260 | 'name' => $parsed_query['product'], |
261 | 261 | 'post_type' => 'product', |
262 | 262 | ) |
263 | 263 | ); |
264 | - if ( count( $posts ) > 0 ) { |
|
264 | + if (count($posts) > 0) { |
|
265 | 265 | return $posts[0]->ID; |
266 | 266 | } |
267 | 267 | } |
@@ -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 |
@@ -15,67 +15,67 @@ |
||
15 | 15 | */ |
16 | 16 | class Wordlift_Website_Jsonld_Converter extends Wordlift_Post_To_Jsonld_Converter { |
17 | 17 | |
18 | - /** |
|
19 | - * Convert the home/blog page to a JSON-LD array. |
|
20 | - * |
|
21 | - * @return array A JSON-LD array. |
|
22 | - * @since 3.14.0 |
|
23 | - */ |
|
24 | - public function create_schema() { |
|
18 | + /** |
|
19 | + * Convert the home/blog page to a JSON-LD array. |
|
20 | + * |
|
21 | + * @return array A JSON-LD array. |
|
22 | + * @since 3.14.0 |
|
23 | + */ |
|
24 | + public function create_schema() { |
|
25 | 25 | |
26 | - // Create new jsonld. |
|
27 | - $home_url = home_url( '/' ); |
|
26 | + // Create new jsonld. |
|
27 | + $home_url = home_url( '/' ); |
|
28 | 28 | |
29 | - $jsonld = array( |
|
30 | - '@context' => 'http://schema.org', |
|
31 | - '@type' => 'WebSite', |
|
32 | - '@id' => "$home_url#website", |
|
33 | - 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ), |
|
34 | - 'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ), |
|
35 | - 'url' => $home_url, |
|
36 | - ); |
|
29 | + $jsonld = array( |
|
30 | + '@context' => 'http://schema.org', |
|
31 | + '@type' => 'WebSite', |
|
32 | + '@id' => "$home_url#website", |
|
33 | + 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ), |
|
34 | + 'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ), |
|
35 | + 'url' => $home_url, |
|
36 | + ); |
|
37 | 37 | |
38 | - // Add publisher information. |
|
39 | - $this->set_publisher( $jsonld ); |
|
38 | + // Add publisher information. |
|
39 | + $this->set_publisher( $jsonld ); |
|
40 | 40 | |
41 | - // Add search action. |
|
42 | - $this->set_search_action( $jsonld ); |
|
41 | + // Add search action. |
|
42 | + $this->set_search_action( $jsonld ); |
|
43 | 43 | |
44 | - /** |
|
45 | - * Call the `wl_website_jsonld` filter. |
|
46 | - * |
|
47 | - * @param array $jsonld The JSON-LD structure. |
|
48 | - * |
|
49 | - * @since 3.14.0 |
|
50 | - * |
|
51 | - * @api |
|
52 | - */ |
|
53 | - return apply_filters( 'wl_website_jsonld', $jsonld ); |
|
54 | - } |
|
44 | + /** |
|
45 | + * Call the `wl_website_jsonld` filter. |
|
46 | + * |
|
47 | + * @param array $jsonld The JSON-LD structure. |
|
48 | + * |
|
49 | + * @since 3.14.0 |
|
50 | + * |
|
51 | + * @api |
|
52 | + */ |
|
53 | + return apply_filters( 'wl_website_jsonld', $jsonld ); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Add SearchAction part to the schema |
|
58 | - * |
|
59 | - * @param array $params The parameters array. |
|
60 | - * |
|
61 | - * @since 3.14.0 |
|
62 | - */ |
|
63 | - private function set_search_action( &$params ) { |
|
64 | - /** |
|
65 | - * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL. |
|
66 | - * |
|
67 | - * @since 3.14.0 |
|
68 | - * @api string $search_url The search URL for this site with a `{search_term_string}` variable. |
|
69 | - */ |
|
70 | - $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' ); |
|
56 | + /** |
|
57 | + * Add SearchAction part to the schema |
|
58 | + * |
|
59 | + * @param array $params The parameters array. |
|
60 | + * |
|
61 | + * @since 3.14.0 |
|
62 | + */ |
|
63 | + private function set_search_action( &$params ) { |
|
64 | + /** |
|
65 | + * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL. |
|
66 | + * |
|
67 | + * @since 3.14.0 |
|
68 | + * @api string $search_url The search URL for this site with a `{search_term_string}` variable. |
|
69 | + */ |
|
70 | + $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' ); |
|
71 | 71 | |
72 | - // Add search action |
|
73 | - $params['potentialAction'] = array( |
|
74 | - '@type' => 'SearchAction', |
|
75 | - 'target' => $search_url, |
|
76 | - 'query-input' => 'required name=search_term_string', |
|
77 | - ); |
|
72 | + // Add search action |
|
73 | + $params['potentialAction'] = array( |
|
74 | + '@type' => 'SearchAction', |
|
75 | + 'target' => $search_url, |
|
76 | + 'query-input' => 'required name=search_term_string', |
|
77 | + ); |
|
78 | 78 | |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | 81 | } |
@@ -24,22 +24,22 @@ discard block |
||
24 | 24 | public function create_schema() { |
25 | 25 | |
26 | 26 | // Create new jsonld. |
27 | - $home_url = home_url( '/' ); |
|
27 | + $home_url = home_url('/'); |
|
28 | 28 | |
29 | 29 | $jsonld = array( |
30 | 30 | '@context' => 'http://schema.org', |
31 | 31 | '@type' => 'WebSite', |
32 | 32 | '@id' => "$home_url#website", |
33 | - 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ), |
|
34 | - 'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ), |
|
33 | + 'name' => html_entity_decode(get_bloginfo('name'), ENT_QUOTES), |
|
34 | + 'alternateName' => html_entity_decode(get_bloginfo('description'), ENT_QUOTES), |
|
35 | 35 | 'url' => $home_url, |
36 | 36 | ); |
37 | 37 | |
38 | 38 | // Add publisher information. |
39 | - $this->set_publisher( $jsonld ); |
|
39 | + $this->set_publisher($jsonld); |
|
40 | 40 | |
41 | 41 | // Add search action. |
42 | - $this->set_search_action( $jsonld ); |
|
42 | + $this->set_search_action($jsonld); |
|
43 | 43 | |
44 | 44 | /** |
45 | 45 | * Call the `wl_website_jsonld` filter. |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @api |
52 | 52 | */ |
53 | - return apply_filters( 'wl_website_jsonld', $jsonld ); |
|
53 | + return apply_filters('wl_website_jsonld', $jsonld); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @since 3.14.0 |
62 | 62 | */ |
63 | - private function set_search_action( &$params ) { |
|
63 | + private function set_search_action(&$params) { |
|
64 | 64 | /** |
65 | 65 | * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL. |
66 | 66 | * |
67 | 67 | * @since 3.14.0 |
68 | 68 | * @api string $search_url The search URL for this site with a `{search_term_string}` variable. |
69 | 69 | */ |
70 | - $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' ); |
|
70 | + $search_url = apply_filters('wl_jsonld_search_url', home_url('/').'?s={search_term_string}'); |
|
71 | 71 | |
72 | 72 | // Add search action |
73 | 73 | $params['potentialAction'] = array( |
@@ -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 | } |
@@ -2,53 +2,53 @@ |
||
2 | 2 | |
3 | 3 | class Wordlift_Timeline_Widget extends WP_Widget { |
4 | 4 | |
5 | - /** |
|
6 | - * Sets up the widgets name etc |
|
7 | - */ |
|
8 | - public function __construct() { |
|
9 | - // widget actual processes |
|
10 | - parent::__construct( |
|
11 | - 'wl_timeline_widget', // Base ID |
|
12 | - __( 'WordLift Timeline Widget', 'wordlift' ), // Name |
|
13 | - array( 'description' => __( 'Displays entities of type event using an interactive timeline.', 'wordlift' ) ) // Args |
|
14 | - ); |
|
15 | - } |
|
5 | + /** |
|
6 | + * Sets up the widgets name etc |
|
7 | + */ |
|
8 | + public function __construct() { |
|
9 | + // widget actual processes |
|
10 | + parent::__construct( |
|
11 | + 'wl_timeline_widget', // Base ID |
|
12 | + __( 'WordLift Timeline Widget', 'wordlift' ), // Name |
|
13 | + array( 'description' => __( 'Displays entities of type event using an interactive timeline.', 'wordlift' ) ) // Args |
|
14 | + ); |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * Outputs the content of the widget |
|
19 | - * |
|
20 | - * @param array $args |
|
21 | - * @param array $instance |
|
22 | - */ |
|
23 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
24 | - public function widget( $args, $instance ) { |
|
25 | - // outputs the content of the widget |
|
26 | - echo do_shortcode( '[wl_timeline global=true]' ); |
|
27 | - } |
|
17 | + /** |
|
18 | + * Outputs the content of the widget |
|
19 | + * |
|
20 | + * @param array $args |
|
21 | + * @param array $instance |
|
22 | + */ |
|
23 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
24 | + public function widget( $args, $instance ) { |
|
25 | + // outputs the content of the widget |
|
26 | + echo do_shortcode( '[wl_timeline global=true]' ); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Outputs the options form on admin |
|
31 | - * |
|
32 | - * @param array $instance The widget options |
|
33 | - */ |
|
34 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
35 | - public function form( $instance ) { |
|
36 | - // outputs the options form on admin |
|
37 | - } |
|
29 | + /** |
|
30 | + * Outputs the options form on admin |
|
31 | + * |
|
32 | + * @param array $instance The widget options |
|
33 | + */ |
|
34 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
35 | + public function form( $instance ) { |
|
36 | + // outputs the options form on admin |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Processing widget options on save |
|
41 | - * |
|
42 | - * @param array $new_instance The new options |
|
43 | - * @param array $old_instance The previous options |
|
44 | - */ |
|
45 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
46 | - public function update( $new_instance, $old_instance ) { |
|
47 | - // processes widget options to be saved |
|
48 | - } |
|
39 | + /** |
|
40 | + * Processing widget options on save |
|
41 | + * |
|
42 | + * @param array $new_instance The new options |
|
43 | + * @param array $old_instance The previous options |
|
44 | + */ |
|
45 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
46 | + public function update( $new_instance, $old_instance ) { |
|
47 | + // processes widget options to be saved |
|
48 | + } |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | function wl_register_timeline_widget() { |
52 | 52 | |
53 | - register_widget( 'WordLift_Timeline_Widget' ); |
|
53 | + register_widget( 'WordLift_Timeline_Widget' ); |
|
54 | 54 | } |
@@ -9,8 +9,8 @@ discard block |
||
9 | 9 | // widget actual processes |
10 | 10 | parent::__construct( |
11 | 11 | 'wl_timeline_widget', // Base ID |
12 | - __( 'WordLift Timeline Widget', 'wordlift' ), // Name |
|
13 | - array( 'description' => __( 'Displays entities of type event using an interactive timeline.', 'wordlift' ) ) // Args |
|
12 | + __('WordLift Timeline Widget', 'wordlift'), // Name |
|
13 | + array('description' => __('Displays entities of type event using an interactive timeline.', 'wordlift')) // Args |
|
14 | 14 | ); |
15 | 15 | } |
16 | 16 | |
@@ -21,9 +21,9 @@ discard block |
||
21 | 21 | * @param array $instance |
22 | 22 | */ |
23 | 23 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
24 | - public function widget( $args, $instance ) { |
|
24 | + public function widget($args, $instance) { |
|
25 | 25 | // outputs the content of the widget |
26 | - echo do_shortcode( '[wl_timeline global=true]' ); |
|
26 | + echo do_shortcode('[wl_timeline global=true]'); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * @param array $instance The widget options |
33 | 33 | */ |
34 | 34 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
35 | - public function form( $instance ) { |
|
35 | + public function form($instance) { |
|
36 | 36 | // outputs the options form on admin |
37 | 37 | } |
38 | 38 | |
@@ -43,12 +43,12 @@ discard block |
||
43 | 43 | * @param array $old_instance The previous options |
44 | 44 | */ |
45 | 45 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
46 | - public function update( $new_instance, $old_instance ) { |
|
46 | + public function update($new_instance, $old_instance) { |
|
47 | 47 | // processes widget options to be saved |
48 | 48 | } |
49 | 49 | } |
50 | 50 | |
51 | 51 | function wl_register_timeline_widget() { |
52 | 52 | |
53 | - register_widget( 'WordLift_Timeline_Widget' ); |
|
53 | + register_widget('WordLift_Timeline_Widget'); |
|
54 | 54 | } |