/server/wordpress/wp-content/plugins/wordpress-seo/src/integrations/watchers/auto-update-watcher.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br · PHP · 204 lines · 90 code · 29 blank · 85 comment · 9 complexity · bbd64f0ebe5a62488572f72670aa6594 MD5 · raw file

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