Total Complexity | 104 |
Total Lines | 568 |
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 |
||
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 ) { |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get notification data. |
||
244 | * |
||
245 | * @return array |
||
246 | * @since {VERSION} |
||
247 | * |
||
248 | */ |
||
249 | public function get() { |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Improve format of the content of notifications before display. By default just runs wpautop. |
||
280 | * |
||
281 | * @param array $notifications The notifications to be parsed. |
||
282 | * |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function get_notifications_with_formatted_content( $notifications ) { |
||
286 | if ( ! is_array( $notifications ) || empty( $notifications ) ) { |
||
287 | return $notifications; |
||
288 | } |
||
289 | |||
290 | foreach ( $notifications as $key => $notification ) { |
||
291 | if ( ! empty( $notification['content'] ) ) { |
||
292 | $notifications[ $key ]['content'] = wpautop( $notification['content'] ); |
||
293 | $notifications[ $key ]['content'] = apply_filters( 'monsterinsights_notification_content_display', $notifications[ $key ]['content'] ); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | return $notifications; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get notifications start time with human time difference |
||
302 | * |
||
303 | * @return array $notifications |
||
304 | * |
||
305 | * @since 7.12.3 |
||
306 | */ |
||
307 | public function get_notifications_with_human_readeable_start_time( $notifications ) { |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Get active notifications. |
||
327 | * |
||
328 | * @return array $notifications['active'] active notifications |
||
329 | * |
||
330 | * @since 7.12.3 |
||
331 | */ |
||
332 | public function get_active_notifications() { |
||
333 | $notifications = $this->get(); |
||
334 | |||
335 | return isset( $notifications['active'] ) ? $notifications['active'] : array(); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Get dismissed notifications. |
||
340 | * |
||
341 | * @return array $notifications['dismissed'] dismissed notifications |
||
342 | * |
||
343 | * @since 7.12.3 |
||
344 | */ |
||
345 | public function get_dismissed_notifications() { |
||
346 | $notifications = $this->get(); |
||
347 | |||
348 | return isset( $notifications['dismissed'] ) ? $notifications['dismissed'] : array(); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Get notification count. |
||
353 | * |
||
354 | * @return int |
||
355 | * @since {VERSION} |
||
356 | * |
||
357 | */ |
||
358 | public function get_count() { |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Add a manual notification event. |
||
365 | * |
||
366 | * @param array $notification Notification data. |
||
367 | * |
||
368 | * @since {VERSION} |
||
369 | * |
||
370 | */ |
||
371 | public function add( $notification ) { |
||
372 | |||
373 | if ( empty( $notification['id'] ) ) { |
||
374 | return; |
||
375 | } |
||
376 | |||
377 | $option = $this->get_option(); |
||
378 | |||
379 | foreach ( $option['dismissed'] as $item ) { |
||
380 | if ( $item['id'] === $notification['id'] ) { |
||
381 | return; |
||
382 | } |
||
383 | } |
||
384 | |||
385 | foreach ( $option['events'] as $item ) { |
||
386 | if ( $item['id'] === $notification['id'] ) { |
||
387 | return; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | $notification = $this->verify( array( $notification ) ); |
||
392 | |||
393 | update_option( |
||
394 | $this->option_name, |
||
395 | array( |
||
396 | 'update' => $option['update'], |
||
397 | 'feed' => $option['feed'], |
||
398 | 'events' => array_merge( $notification, $option['events'] ), |
||
399 | 'dismissed' => $option['dismissed'], |
||
400 | ), |
||
401 | false |
||
402 | ); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Update notification data from feed. |
||
407 | * |
||
408 | * @param array $option (Optional) Added @since 7.13.2 |
||
409 | * |
||
410 | * @since {VERSION} |
||
411 | */ |
||
412 | public function update() { |
||
413 | |||
414 | $feed = $this->fetch_feed(); |
||
415 | $option = $this->get_option(); |
||
416 | |||
417 | update_option( |
||
418 | $this->option_name, |
||
419 | array( |
||
420 | 'update' => time(), |
||
421 | 'feed' => $feed, |
||
422 | 'events' => $option['events'], |
||
423 | 'dismissed' => array_slice( $option['dismissed'], 0, 30 ), // Limit dismissed notifications to last 30. |
||
424 | ), |
||
425 | false |
||
426 | ); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Dismiss notification via AJAX. |
||
431 | * |
||
432 | * @since {VERSION} |
||
433 | */ |
||
434 | public function dismiss() { |
||
435 | // Run a security check. |
||
436 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
437 | |||
438 | // Check for access and required param. |
||
439 | if ( ! $this->has_access() || empty( $_POST['id'] ) ) { |
||
440 | wp_send_json_error(); |
||
441 | } |
||
442 | |||
443 | $id = sanitize_text_field( wp_unslash( $_POST['id'] ) ); |
||
444 | $option = $this->get_option(); |
||
445 | |||
446 | // Dismiss all notifications and add them to dissmiss array. |
||
447 | if ( 'all' === $id ) { |
||
448 | if ( is_array( $option['feed'] ) && ! empty( $option['feed'] ) ) { |
||
449 | foreach ( $option['feed'] as $key => $notification ) { |
||
450 | array_unshift( $option['dismissed'], $notification ); |
||
451 | unset( $option['feed'][ $key ] ); |
||
452 | } |
||
453 | } |
||
454 | if ( is_array( $option['events'] ) && ! empty( $option['events'] ) ) { |
||
455 | foreach ( $option['events'] as $key => $notification ) { |
||
456 | array_unshift( $option['dismissed'], $notification ); |
||
457 | unset( $option['events'][ $key ] ); |
||
458 | } |
||
459 | } |
||
460 | } |
||
461 | |||
462 | $type = is_numeric( $id ) ? 'feed' : 'events'; |
||
463 | |||
464 | // Remove notification and add in dismissed array. |
||
465 | if ( is_array( $option[ $type ] ) && ! empty( $option[ $type ] ) ) { |
||
466 | foreach ( $option[ $type ] as $key => $notification ) { |
||
467 | if ( $notification['id'] == $id ) { // phpcs:ignore WordPress.PHP.StrictComparisons |
||
468 | // Add notification to dismissed array. |
||
469 | array_unshift( $option['dismissed'], $notification ); |
||
470 | // Remove notification from feed or events. |
||
471 | unset( $option[ $type ][ $key ] ); |
||
472 | break; |
||
473 | } |
||
474 | } |
||
475 | } |
||
476 | |||
477 | update_option( $this->option_name, $option, false ); |
||
478 | |||
479 | wp_send_json_success(); |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * This generates the markup for the notifications indicator if needed. |
||
484 | * |
||
485 | * @return string |
||
486 | */ |
||
487 | public function get_menu_count() { |
||
488 | |||
489 | if ( $this->get_count() > 0 ) { |
||
490 | return '<span class="monsterinsights-menu-notification-indicator update-plugins">' . $this->get_count() . '</span>'; |
||
491 | } |
||
492 | |||
493 | return ''; |
||
494 | |||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Retrieve the notifications via an ajax call. |
||
499 | */ |
||
500 | public function ajax_get_notifications() { |
||
501 | |||
502 | // Run a security check. |
||
503 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
504 | |||
505 | $notifications_data = array( |
||
506 | 'notifications' => $this->get_active_notifications(), |
||
507 | 'dismissed' => $this->get_dismissed_notifications(), |
||
508 | 'view_url' => $this->get_view_url( 'monsterinsights-report-overview', 'monsterinsights_reports' ), |
||
509 | 'sidebar_url' => $this->get_sidebar_url(), |
||
510 | ); |
||
511 | |||
512 | wp_send_json_success( $notifications_data ); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Get the URL for the page where users can see/read notifications. |
||
517 | * |
||
518 | * @return string |
||
519 | */ |
||
520 | public function get_view_url( $scroll_to, $page, $tab='' ) { |
||
538 | |||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Get the notification sidebar URL for the page where users can see/read notifications. |
||
543 | * |
||
544 | * @return string |
||
545 | */ |
||
546 | public function get_sidebar_url() { |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Delete the notification options. |
||
567 | */ |
||
568 | public function delete_notifications_data() { |
||
579 |