| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 6 |
use Yoast\WP\SEO\Helpers\Notification_Helper; |
| 7 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 |
use Yoast\WP\SEO\Presenters\Admin\Auto_Update_Notification_Presenter; |
| 9 |
use Yoast_Notification; |
| 10 |
use Yoast_Notification_Center; |
| 11 |
|
| 12 |
/** |
| 13 |
* Shows a notification for users who have WordPress auto updates enabled but not Yoast SEO auto updates. |
| 14 |
*/ |
| 15 |
class Auto_Update_Watcher implements Integration_Interface { |
| 16 |
|
| 17 |
use No_Conditionals; |
| 18 |
|
| 19 |
/** |
| 20 |
* The notification ID. |
| 21 |
*/ |
| 22 |
const NOTIFICATION_ID = 'wpseo-auto-update'; |
| 23 |
|
| 24 |
/** |
| 25 |
* The Yoast notification center. |
| 26 |
* |
| 27 |
* @var Yoast_Notification_Center |
| 28 |
*/ |
| 29 |
protected $notification_center; |
| 30 |
|
| 31 |
/** |
| 32 |
* The notification helper. |
| 33 |
* |
| 34 |
* @var Notification_Helper |
| 35 |
*/ |
| 36 |
protected $notification_helper; |
| 37 |
|
| 38 |
/** |
| 39 |
* Auto_Update constructor. |
| 40 |
* |
| 41 |
* @param Yoast_Notification_Center $notification_center The notification center. |
| 42 |
* @param Notification_Helper $notification_helper The notification helper. |
| 43 |
*/ |
| 44 |
public function __construct( |
| 45 |
Yoast_Notification_Center $notification_center, |
| 46 |
Notification_Helper $notification_helper |
| 47 |
) { |
| 48 |
$this->notification_center = $notification_center; |
| 49 |
$this->notification_helper = $notification_helper; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Initializes the integration. |
| 54 |
* |
| 55 |
* On admin_init, it is checked whether the notification to auto-update Yoast SEO needs to be shown or removed. |
| 56 |
* This is also done when major WP core updates are being enabled or disabled, |
| 57 |
* and when automatic updates for Yoast SEO are being enabled or disabled. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function register_hooks() { |
| 62 |
\add_action( 'admin_init', [ $this, 'auto_update_notification_not_if_dismissed' ] ); |
| 63 |
\add_action( 'update_site_option_auto_update_core_major', [ $this, 'auto_update_notification_even_if_dismissed' ] ); |
| 64 |
\add_action( 'update_site_option_auto_update_plugins', [ $this, 'auto_update_notification_not_if_dismissed' ] ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Handles the Yoast SEO auto-update notification when the user toggles the auto-update setting for WordPress Core. |
| 69 |
* |
| 70 |
* If it should be shown, this will be done even if the notification has been dismissed in the past. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function auto_update_notification_even_if_dismissed() { |
| 75 |
if ( ! $this->should_show_notification() ) { |
| 76 |
$this->save_dismissal_status(); |
| 77 |
$this->maybe_remove_notification(); |
| 78 |
|
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$this->maybe_create_notification(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Handles the Yoast SEO auto-update notification on all admin pages, |
| 87 |
* as well as when the user toggles the Yoast SEO auto-update setting. |
| 88 |
* |
| 89 |
* If it should be shown, this will only be done if the notification has not been dismissed in the past. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function auto_update_notification_not_if_dismissed() { |
| 94 |
if ( ! $this->should_show_notification() ) { |
| 95 |
$this->save_dismissal_status(); |
| 96 |
$this->maybe_remove_notification(); |
| 97 |
|
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$this->maybe_create_notification_if_not_dismissed(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Checks whether the Yoast SEO auto-update notification should be shown. |
| 106 |
* |
| 107 |
* @return bool Whether the notification should be shown. |
| 108 |
*/ |
| 109 |
protected function should_show_notification() { |
| 110 |
$core_updates_enabled = \get_site_option( 'auto_update_core_major' ) === 'enabled'; |
| 111 |
$yoast_updates_enabled = $this->yoast_auto_updates_enabled(); |
| 112 |
|
| 113 |
return $core_updates_enabled && ! $yoast_updates_enabled; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Saves the dismissal status of the notification in an option in wp_usermeta, if the notification gets dismissed. |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
protected function save_dismissal_status() { |
| 122 |
// This option exists if the notification has been dismissed at some point. |
| 123 |
$notification_dismissed = \get_user_option( 'wp_' . self::NOTIFICATION_ID ); |
| 124 |
|
| 125 |
// We wish to have its value in a different option, so we can still access it even when the notification gets removed. |
| 126 |
if ( $notification_dismissed && ! \get_user_option( 'wp_' . self::NOTIFICATION_ID . '_dismissed' ) ) { |
| 127 |
\update_user_option( \get_current_user_id(), self::NOTIFICATION_ID . '_dismissed', true ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Removes the notification from the notification center, if it exists. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
protected function maybe_remove_notification() { |
| 137 |
$this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Creates the notification if it doesn't exist already. |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
protected function maybe_create_notification() { |
| 146 |
if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) { |
| 147 |
$notification = $this->notification(); |
| 148 |
$this->notification_helper->restore_notification( $notification ); |
| 149 |
$this->notification_center->add_notification( $notification ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Creates the notification when Yoast SEO auto-updates are enabled, if it hasn't been dismissed in the past. |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
protected function maybe_create_notification_if_not_dismissed() { |
| 159 |
$notification_dismissed = \get_user_option( 'wp_' . self::NOTIFICATION_ID . '_dismissed' ) === '1'; |
| 160 |
$yoast_updates_enabled = $this->yoast_auto_updates_enabled(); |
| 161 |
|
| 162 |
if ( $notification_dismissed && ! $yoast_updates_enabled ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$this->maybe_create_notification(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Checks whether auto-updates are enabled for Yoast SEO. |
| 171 |
* |
| 172 |
* @return bool True if they are enabled, false if not. |
| 173 |
*/ |
| 174 |
protected function yoast_auto_updates_enabled() { |
| 175 |
$plugins_to_auto_update = \get_site_option( 'auto_update_plugins' ); |
| 176 |
|
| 177 |
// If no plugins are set to be automatically updated, it means that Yoast SEO isn't either. |
| 178 |
if ( ! $plugins_to_auto_update ) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
// Check if the Yoast SEO plugin file is in the array of plugins for which auto-updates are enabled. |
| 183 |
return \in_array( 'wordpress-seo/wp-seo.php', $plugins_to_auto_update, true ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns an instance of the notification. |
| 188 |
* |
| 189 |
* @return Yoast_Notification The notification to show. |
| 190 |
*/ |
| 191 |
protected function notification() { |
| 192 |
$presenter = new Auto_Update_Notification_Presenter(); |
| 193 |
|
| 194 |
return new Yoast_Notification( |
| 195 |
$presenter->present(), |
| 196 |
[ |
| 197 |
'type' => Yoast_Notification::WARNING, |
| 198 |
'id' => self::NOTIFICATION_ID, |
| 199 |
'capabilities' => 'wpseo_manage_options', |
| 200 |
'priority' => 0.8, |
| 201 |
] |
| 202 |
); |
| 203 |
} |
| 204 |
} |
| 205 |
|