Total Complexity | 96 |
Total Lines | 516 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Notifications often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Notifications, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class MonsterInsights_Notifications { |
||
9 | |||
10 | /** |
||
11 | * Source of notifications content. |
||
12 | * |
||
13 | * @since {VERSION} |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | const SOURCE_URL = 'https://plugin-cdn.monsterinsights.com/notifications.json'; |
||
18 | |||
19 | /** |
||
20 | * Option value. |
||
21 | * |
||
22 | * @since {VERSION} |
||
23 | * |
||
24 | * @var bool|array |
||
25 | */ |
||
26 | public $option = false; |
||
27 | |||
28 | /** |
||
29 | * The name of the option used to store the data. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | public $option_name = 'monsterinsights_notifications'; |
||
34 | |||
35 | /** |
||
36 | * MonsterInsights_Notifications constructor. |
||
37 | */ |
||
38 | public function __construct() { |
||
39 | $this->init(); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Initialize class. |
||
44 | * |
||
45 | * @since {VERSION} |
||
46 | */ |
||
47 | public function init() { |
||
48 | |||
49 | $this->hooks(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Register hooks. |
||
54 | * |
||
55 | * @since {VERSION} |
||
56 | */ |
||
57 | public function hooks() { |
||
58 | add_action( 'wp_ajax_monsterinsights_notification_dismiss', array( $this, 'dismiss' ) ); |
||
59 | |||
60 | add_action( 'wp_ajax_monsterinsights_vue_get_notifications', array( $this, 'ajax_get_notifications' ) ); |
||
61 | |||
62 | add_action( 'monsterinsights_admin_notifications_update', array( $this, 'update' ) ); |
||
63 | |||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Check if user has access and is enabled. |
||
68 | * |
||
69 | * @return bool |
||
70 | * @since {VERSION} |
||
71 | * |
||
72 | */ |
||
73 | public function has_access() { |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get option value. |
||
86 | * |
||
87 | * @param bool $cache Reference property cache if available. |
||
88 | * |
||
89 | * @return array |
||
90 | * @since {VERSION} |
||
91 | * |
||
92 | */ |
||
93 | public function get_option( $cache = true ) { |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Fetch notifications from feed. |
||
113 | * |
||
114 | * @return array |
||
115 | * @since {VERSION} |
||
116 | * |
||
117 | */ |
||
118 | public function fetch_feed() { |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Verify notification data before it is saved. |
||
137 | * |
||
138 | * @param array $notifications Array of notifications items to verify. |
||
139 | * |
||
140 | * @return array |
||
141 | * @since {VERSION} |
||
142 | * |
||
143 | */ |
||
144 | public function verify( $notifications ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh |
||
145 | |||
146 | $data = array(); |
||
147 | |||
148 | if ( ! is_array( $notifications ) || empty( $notifications ) ) { |
||
|
|||
149 | return $data; |
||
150 | } |
||
151 | |||
152 | $option = $this->get_option(); |
||
153 | |||
154 | foreach ( $notifications as $notification ) { |
||
155 | |||
156 | // The message and license should never be empty, if they are, ignore. |
||
157 | if ( empty( $notification['content'] ) || empty( $notification['type'] ) ) { |
||
158 | continue; |
||
159 | } |
||
160 | |||
161 | // Ignore if license type does not match. |
||
162 | $license_type = MonsterInsights()->license->get_license_type() ? MonsterInsights()->license->get_license_type() : 'lite'; |
||
163 | |||
164 | if ( ! in_array( $license_type, $notification['type'] ) ) { |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | // Ignore if notification is not ready to display(based on start time). |
||
169 | if ( ! empty( $notification['start'] ) && time() < strtotime( $notification['start'] ) ) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | // Ignore if expired. |
||
174 | if ( ! empty( $notification['end'] ) && time() > strtotime( $notification['end'] ) ) { |
||
175 | continue; |
||
176 | } |
||
177 | |||
178 | // Ignore if notification has already been dismissed. |
||
179 | $notification_already_dismissed = false; |
||
180 | if ( is_array( $option['dismissed'] ) && ! empty( $option['dismissed'] ) ) { |
||
181 | foreach ( $option['dismissed'] as $dismiss_notification ) { |
||
182 | if ( $notification['id'] === $dismiss_notification['id'] ) { |
||
183 | $notification_already_dismissed = true; |
||
184 | break; |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if ( true === $notification_already_dismissed ) { |
||
190 | continue; |
||
191 | } |
||
192 | |||
193 | // Ignore if notification existed before installing MonsterInsights. |
||
194 | // Prevents bombarding the user with notifications after activation. |
||
195 | $over_time = get_option( 'monsterinsights_over_time', array() ); |
||
196 | |||
197 | if ( |
||
198 | ! empty( $over_time['installed_date'] ) && |
||
199 | ! empty( $notification['start'] ) && |
||
200 | $over_time['installed_date'] > strtotime( $notification['start'] ) |
||
201 | ) { |
||
202 | continue; |
||
203 | } |
||
204 | |||
205 | $data[] = $notification; |
||
206 | } |
||
207 | |||
208 | return $data; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Verify saved notification data for active notifications. |
||
213 | * |
||
214 | * @param array $notifications Array of notifications items to verify. |
||
215 | * |
||
216 | * @return array |
||
217 | * @since {VERSION} |
||
218 | * |
||
219 | */ |
||
220 | public function verify_active( $notifications ) { |
||
221 | |||
222 | if ( ! is_array( $notifications ) || empty( $notifications ) ) { |
||
223 | return array(); |
||
224 | } |
||
225 | |||
226 | $license_type = MonsterInsights()->license->get_license_type() ? MonsterInsights()->license->get_license_type() : 'lite'; |
||
227 | |||
228 | // Remove notifications that are not active, or if the license type not exists |
||
229 | foreach ( $notifications as $key => $notification ) { |
||
230 | if ( |
||
231 | ( ! empty( $notification['start'] ) && time() < strtotime( $notification['start'] ) ) || |
||
232 | ( ! empty( $notification['end'] ) && time() > strtotime( $notification['end'] ) ) || |
||
233 | ( ! empty( $notification['type'] ) && ! in_array( $license_type, $notification['type'] ) ) |
||
234 | ) { |
||
235 | unset( $notifications[ $key ] ); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | return $notifications; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get notification data. |
||
244 | * |
||
245 | * @return array |
||
246 | * @since {VERSION} |
||
247 | * |
||
248 | */ |
||
249 | public function get() { |
||
250 | |||
251 | if ( ! $this->has_access() ) { |
||
252 | return array(); |
||
253 | } |
||
254 | |||
255 | $option = $this->get_option(); |
||
256 | |||
257 | // Update notifications using async task. |
||
258 | if ( empty( $option['update'] ) || time() > $option['update'] + DAY_IN_SECONDS ) { |
||
259 | if ( false === wp_next_scheduled( 'monsterinsights_admin_notifications_update' ) ) { |
||
260 | wp_schedule_single_event( time(), 'monsterinsights_admin_notifications_update' ); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | $events = ! empty( $option['events'] ) ? $this->verify_active( $option['events'] ) : array(); |
||
265 | $feed = ! empty( $option['feed'] ) ? $this->verify_active( $option['feed'] ) : array(); |
||
266 | |||
267 | $notifications = array(); |
||
268 | $notifications['active'] = array_merge( $events, $feed ); |
||
269 | $notifications['active'] = $this->get_notifications_with_human_readeable_start_time( $notifications['active'] ); |
||
270 | $notifications['dismissed'] = ! empty( $option['dismissed'] ) ? $option['dismissed'] : array(); |
||
271 | $notifications['dismissed'] = $this->get_notifications_with_human_readeable_start_time( $notifications['dismissed'] ); |
||
272 | |||
273 | return $notifications; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Get notifications start time with human time difference |
||
278 | * |
||
279 | * @return array $notifications |
||
280 | * |
||
281 | * @since 7.12.3 |
||
282 | */ |
||
283 | public function get_notifications_with_human_readeable_start_time( $notifications ) { |
||
284 | if ( ! is_array( $notifications ) || empty( $notifications ) ) { |
||
285 | return; |
||
286 | } |
||
287 | |||
288 | foreach ( $notifications as $key => $notification ) { |
||
289 | if ( ! isset( $notification['start'] ) || empty( $notification['start'] ) ) { |
||
290 | continue; |
||
291 | } |
||
292 | |||
293 | // Translators: Readable time to display |
||
294 | $modified_start_time = sprintf( __( '%1$s ago', 'google-analytics-for-wordpress' ), human_time_diff( strtotime( $notification['start'] ), current_time( 'timestamp' ) ) ); |
||
295 | $notifications[ $key ]['start'] = $modified_start_time; |
||
296 | } |
||
297 | |||
298 | return $notifications; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get active notifications. |
||
303 | * |
||
304 | * @return array $notifications['active'] active notifications |
||
305 | * |
||
306 | * @since 7.12.3 |
||
307 | */ |
||
308 | public function get_active_notifications() { |
||
309 | $notifications = $this->get(); |
||
310 | return isset( $notifications['active'] ) ? $notifications['active'] : array(); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Get dismissed notifications. |
||
315 | * |
||
316 | * @return array $notifications['dismissed'] dismissed notifications |
||
317 | * |
||
318 | * @since 7.12.3 |
||
319 | */ |
||
320 | public function get_dismissed_notifications() { |
||
321 | $notifications = $this->get(); |
||
322 | return isset( $notifications['dismissed'] ) ? $notifications['dismissed'] : array(); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Get notification count. |
||
327 | * |
||
328 | * @return int |
||
329 | * @since {VERSION} |
||
330 | * |
||
331 | */ |
||
332 | public function get_count() { |
||
333 | |||
334 | return count( $this->get_active_notifications() ); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Add a manual notification event. |
||
339 | * |
||
340 | * @param array $notification Notification data. |
||
341 | * |
||
342 | * @since {VERSION} |
||
343 | * |
||
344 | */ |
||
345 | public function add( $notification ) { |
||
346 | |||
347 | if ( empty( $notification['id'] ) ) { |
||
348 | return; |
||
349 | } |
||
350 | |||
351 | $option = $this->get_option(); |
||
352 | |||
353 | if ( in_array( $notification['id'], $option['dismissed'] ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
||
354 | return; |
||
355 | } |
||
356 | |||
357 | foreach ( $option['events'] as $item ) { |
||
358 | if ( $item['id'] === $notification['id'] ) { |
||
359 | return; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | $notification = $this->verify( array( $notification ) ); |
||
364 | |||
365 | update_option( |
||
366 | $this->option_name, |
||
367 | array( |
||
368 | 'update' => $option['update'], |
||
369 | 'feed' => $option['feed'], |
||
370 | 'events' => array_merge( $notification, $option['events'] ), |
||
371 | 'dismissed' => $option['dismissed'], |
||
372 | ) |
||
373 | ); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Update notification data from feed. |
||
378 | * |
||
379 | * @since {VERSION} |
||
380 | */ |
||
381 | public function update() { |
||
382 | |||
383 | $feed = $this->fetch_feed(); |
||
384 | $option = $this->get_option(); |
||
385 | |||
386 | update_option( |
||
387 | $this->option_name, |
||
388 | array( |
||
389 | 'update' => time(), |
||
390 | 'feed' => $feed, |
||
391 | 'events' => $option['events'], |
||
392 | 'dismissed' => $option['dismissed'], |
||
393 | ) |
||
394 | ); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Dismiss notification via AJAX. |
||
399 | * |
||
400 | * @since {VERSION} |
||
401 | */ |
||
402 | public function dismiss() { |
||
403 | // Run a security check. |
||
404 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
405 | |||
406 | // Check for access and required param. |
||
407 | if ( ! $this->has_access() || empty( $_POST['id'] ) ) { |
||
408 | wp_send_json_error(); |
||
409 | } |
||
410 | |||
411 | $id = sanitize_text_field( wp_unslash( $_POST['id'] ) ); |
||
412 | $option = $this->get_option(); |
||
413 | |||
414 | // dismiss all notifications and add them to dissmiss array |
||
415 | if ( $id === 'all' ) { |
||
416 | if ( is_array( $option['feed'] ) && ! empty( $option['feed'] ) ) { |
||
417 | foreach ( $option['feed'] as $key => $notification ) { |
||
418 | array_unshift($option['dismissed'], $notification); |
||
419 | unset( $option['feed'][ $key ] ); |
||
420 | } |
||
421 | } |
||
422 | if ( is_array( $option['events'] ) && ! empty( $option['events'] ) ) { |
||
423 | foreach ( $option['events'] as $key => $notification ) { |
||
424 | array_unshift($option['dismissed'], $notification); |
||
425 | unset( $option['events'][ $key ] ); |
||
426 | } |
||
427 | } |
||
428 | } |
||
429 | |||
430 | $type = is_numeric( $id ) ? 'feed' : 'events'; |
||
431 | |||
432 | // Remove notification and add in dismissed array. |
||
433 | if ( is_array( $option[ $type ] ) && ! empty( $option[ $type ] ) ) { |
||
434 | foreach ( $option[ $type ] as $key => $notification ) { |
||
435 | if ( $notification['id'] == $id ) { // phpcs:ignore WordPress.PHP.StrictComparisons |
||
436 | // add notification to dismissed array |
||
437 | array_unshift($option['dismissed'], $notification); |
||
438 | // remove notification from feed or events |
||
439 | unset( $option[ $type ][ $key ] ); |
||
440 | break; |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 | |||
445 | update_option( $this->option_name, $option ); |
||
446 | |||
447 | wp_send_json_success(); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * This generates the markup for the notifications indicator if needed. |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | public function get_menu_count() { |
||
456 | |||
457 | if ( $this->get_count() > 0 ) { |
||
458 | return '<span class="monsterinsights-menu-notification-indicator">'. $this->get_count() .'</span>'; |
||
459 | } |
||
460 | |||
461 | return ''; |
||
462 | |||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Retrieve the notifications via an ajax call. |
||
467 | */ |
||
468 | public function ajax_get_notifications() { |
||
469 | |||
470 | // Run a security check. |
||
471 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
472 | |||
473 | $notifications_data = array( |
||
474 | 'notifications' => $this->get_active_notifications(), |
||
475 | 'dismissed' => $this->get_dismissed_notifications(), |
||
476 | 'view_url' => $this->get_view_url(), |
||
477 | 'sidebar_url' => $this->get_sidebar_url(), |
||
478 | ); |
||
479 | |||
480 | wp_send_json_success( $notifications_data ); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Get the URL for the page where users can see/read notifications. |
||
485 | * |
||
486 | * @return string |
||
487 | */ |
||
488 | public function get_view_url( ) { |
||
499 | |||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Get the notification sidebar URL for the page where users can see/read notifications. |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | public function get_sidebar_url() { |
||
524 | } |
||
525 | } |
||
526 |