class-configuration-notifier.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Notifiers
  6. */
  7. /**
  8. * Represents the logic for showing the notification.
  9. */
  10. class WPSEO_Configuration_Notifier implements WPSEO_Listener {
  11. /**
  12. * Option name use to determine whether the notice has been dismissed.
  13. *
  14. * @var string
  15. */
  16. const META_NAME = 'wpseo-dismiss-configuration-notice';
  17. /**
  18. * Default value.
  19. *
  20. * @var string
  21. */
  22. const META_VALUE = 'yes';
  23. /**
  24. * Should the notification be shown.
  25. *
  26. * @var bool
  27. */
  28. protected $show_notification;
  29. /**
  30. * Constructs the object by setting the show notification property based the given options.
  31. */
  32. public function __construct() {
  33. $this->show_notification = WPSEO_Options::get( 'show_onboarding_notice', false );
  34. }
  35. /**
  36. * Returns the content of the notification.
  37. *
  38. * @return string A string with the notification HTML, or empty string when no notification is needed.
  39. */
  40. public function notify() {
  41. if ( ! $this->show_notification() ) {
  42. return $this->re_run_notification();
  43. }
  44. return $this->first_time_notification();
  45. }
  46. /**
  47. * Listens to an argument in the request URL. When triggered just set the notification to dismissed.
  48. *
  49. * @return void
  50. */
  51. public function listen() {
  52. if ( ! $this->show_notification() || ! $this->dismissal_is_triggered() ) {
  53. return;
  54. }
  55. $this->set_dismissed();
  56. }
  57. /**
  58. * Checks if the dismissal should be triggered.
  59. *
  60. * @return bool True when action has been triggered.
  61. */
  62. protected function dismissal_is_triggered() {
  63. return filter_input( INPUT_GET, 'dismiss_get_started' ) === '1';
  64. }
  65. /**
  66. * Checks if the current user has dismissed the notification.
  67. *
  68. * @return bool True when the notification has been dismissed.
  69. */
  70. protected function is_dismissed() {
  71. return get_user_meta( get_current_user_id(), self::META_NAME, true ) === self::META_VALUE;
  72. }
  73. /**
  74. * Sets the dismissed state for the current user.
  75. *
  76. * @return void
  77. */
  78. protected function set_dismissed() {
  79. update_user_meta( get_current_user_id(), self::META_NAME, self::META_VALUE );
  80. }
  81. /**
  82. * Checks if the notification should be shown.
  83. *
  84. * @return bool True when notification should be shown.
  85. */
  86. protected function show_notification() {
  87. return $this->show_notification && ! $this->is_dismissed();
  88. }
  89. /**
  90. * Returns the notification to re-run the config wizard.
  91. *
  92. * @return string The notification.
  93. */
  94. private function re_run_notification() {
  95. $content = sprintf(
  96. /* translators: %1$s expands to Yoast SEO, %2$s is a link start tag to the Onboarding Wizard, %3$s is the link closing tag. */
  97. esc_html__( 'If you want to double-check your %1$s settings, or change something, you can always %2$sreopen the configuration wizard%3$s.', 'wordpress-seo' ),
  98. 'Yoast SEO',
  99. '<a href="' . esc_url( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) ) . '">',
  100. '</a>'
  101. );
  102. return $this->notification( __( 'SEO settings configured', 'wordpress-seo' ), $content );
  103. }
  104. /**
  105. * Returns the notification to start the config wizard for the first time.
  106. *
  107. * @return string The notification.
  108. */
  109. private function first_time_notification() {
  110. $content = sprintf(
  111. /* translators: %1$s expands to Yoast SEO, %2$s is a link start tag to the Onboarding Wizard, %3$s is the link closing tag. */
  112. esc_html__( 'Get started quickly with the %1$s %2$sconfiguration wizard%3$s!', 'wordpress-seo' ),
  113. 'Yoast SEO',
  114. '<a href="' . esc_url( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) ) . '">',
  115. '</a>'
  116. );
  117. return $this->notification( __( 'First-time SEO configuration', 'wordpress-seo' ), $content, true );
  118. }
  119. /**
  120. * Returns a styled notification.
  121. *
  122. * @param string $title Title for the notification.
  123. * @param string $content Content for the notification.
  124. * @param bool $show_dismissal Whether to show the dismiss button or not.
  125. *
  126. * @return string The styled notification.
  127. */
  128. private function notification( $title, $content, $show_dismissal = false ) {
  129. $notification = '<div class="yoast-container yoast-container__configuration-wizard">';
  130. $notification .= sprintf(
  131. '<img src="%1$s" height="%2$s" width="%3$d" alt="" />',
  132. esc_url( plugin_dir_url( WPSEO_FILE ) . 'images/new-to-configuration-notice.svg' ),
  133. 60,
  134. 60
  135. );
  136. $notification .= '<div class="yoast-container__configuration-wizard--content">';
  137. $notification .= sprintf(
  138. '<h3>%s<span class="dashicons dashicons-yes"></span></h3>',
  139. esc_html( $title )
  140. );
  141. $notification .= '<p>';
  142. $notification .= $content;
  143. $notification .= '</p>';
  144. $notification .= '</div>';
  145. if ( $show_dismissal ) {
  146. $notification .= sprintf(
  147. '<a href="%1$s" style="" class="button dismiss yoast-container__configuration-wizard--dismiss"><span class="screen-reader-text">%2$s</span><span class="dashicons dashicons-no-alt"></span></a>',
  148. esc_url( admin_url( 'admin.php?page=wpseo_dashboard&amp;dismiss_get_started=1' ) ),
  149. esc_html__( 'Dismiss this item.', 'wordpress-seo' )
  150. );
  151. }
  152. $notification .= '</div>';
  153. return $notification;
  154. }
  155. }