| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Notifiers |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Abstract class representing a dismissible notification. |
| 10 |
*/ |
| 11 |
abstract class WPSEO_Dismissible_Notification implements WPSEO_Listener, WPSEO_Notification_Handler { |
| 12 |
|
| 13 |
/** |
| 14 |
* The identifier for the notification. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $notification_identifier = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* Retrieves instance of a notification. |
| 22 |
* |
| 23 |
* @return Yoast_Notification The notification. |
| 24 |
*/ |
| 25 |
abstract protected function get_notification(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Listens to an argument in the request URL and triggers an action. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function listen() { |
| 33 |
if ( $this->get_listener_value() !== $this->notification_identifier ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$this->dismiss(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Adds the notification if applicable, otherwise removes it. |
| 42 |
* |
| 43 |
* @param Yoast_Notification_Center $notification_center The notification center object. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function handle( Yoast_Notification_Center $notification_center ) { |
| 48 |
if ( $this->is_applicable() ) { |
| 49 |
$notification = $this->get_notification(); |
| 50 |
$notification_center->add_notification( $notification ); |
| 51 |
|
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$notification_center->remove_notification_by_id( 'wpseo-' . $this->notification_identifier ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Listens to an argument in the request URL and triggers an action. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
protected function dismiss() { |
| 64 |
$this->set_dismissal_state(); |
| 65 |
$this->redirect_to_dashboard(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Checks if a notice is applicable. |
| 70 |
* |
| 71 |
* @return bool Whether a notice should be shown or not. |
| 72 |
*/ |
| 73 |
protected function is_applicable() { |
| 74 |
return $this->is_notice_dismissed() === false; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Checks whether the notification has been dismissed. |
| 79 |
* |
| 80 |
* @codeCoverageIgnore |
| 81 |
* |
| 82 |
* @return bool True when notification is dismissed. |
| 83 |
*/ |
| 84 |
protected function is_notice_dismissed() { |
| 85 |
return get_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true ) === '1'; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Retrieves the value where listener is listening for. |
| 90 |
* |
| 91 |
* @codeCoverageIgnore |
| 92 |
* |
| 93 |
* @return string The listener value. |
| 94 |
*/ |
| 95 |
protected function get_listener_value() { |
| 96 |
return filter_input( INPUT_GET, 'yoast_dismiss' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Dismisses the notification. |
| 101 |
* |
| 102 |
* @codeCoverageIgnore |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
protected function set_dismissal_state() { |
| 107 |
update_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Redirects the user back to the dashboard. |
| 112 |
* |
| 113 |
* @codeCoverageIgnore |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
protected function redirect_to_dashboard() { |
| 118 |
wp_safe_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) ); |
| 119 |
exit; |
| 120 |
} |
| 121 |
} |
| 122 |
|