/wp-content/plugins/wordpress-seo/admin/notifiers/class-configuration-notifier.php

https://bitbucket.org/carloskikea/helpet · PHP · 156 lines · 75 code · 18 blank · 63 comment · 5 complexity · 5c5e221b1d579f21cdabe22a49a465bd MD5 · raw file

  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. const META_NAME = 'wpseo-dismiss-configuration-notice';
  12. const META_VALUE = 'yes';
  13. /** @var bool */
  14. protected $show_notification;
  15. /**
  16. * Constructs the object by setting the show notification property based the given options.
  17. */
  18. public function __construct() {
  19. $this->show_notification = WPSEO_Options::get( 'show_onboarding_notice', false );
  20. }
  21. /**
  22. * Returns the content of the notification.
  23. *
  24. * @return string A string with the notification HTML, or empty string when no notification is needed.
  25. */
  26. public function notify() {
  27. if ( ! $this->show_notification() ) {
  28. return $this->re_run_notification();
  29. }
  30. return $this->first_time_notification();
  31. }
  32. /**
  33. * Listens to an argument in the request URL. When triggered just set the notification to dismissed.
  34. *
  35. * @return void
  36. */
  37. public function listen() {
  38. if ( ! $this->show_notification() || ! $this->dismissal_is_triggered() ) {
  39. return;
  40. }
  41. $this->set_dismissed();
  42. }
  43. /**
  44. * Checks if the dismissal should be triggered.
  45. *
  46. * @return bool True when action has been triggered.
  47. */
  48. protected function dismissal_is_triggered() {
  49. return filter_input( INPUT_GET, 'dismiss_get_started' ) === '1';
  50. }
  51. /**
  52. * Checks if the current user has dismissed the notification.
  53. *
  54. * @return bool True when the notification has been dismissed.
  55. */
  56. protected function is_dismissed() {
  57. return get_user_meta( get_current_user_id(), self::META_NAME, true ) === self::META_VALUE;
  58. }
  59. /**
  60. * Sets the dismissed state for the current user.
  61. *
  62. * @return void
  63. */
  64. protected function set_dismissed() {
  65. update_user_meta( get_current_user_id(), self::META_NAME, self::META_VALUE );
  66. }
  67. /**
  68. * Checks if the notification should be shown.
  69. *
  70. * @return bool True when notification should be shown.
  71. */
  72. protected function show_notification() {
  73. return $this->show_notification && ! $this->is_dismissed();
  74. }
  75. /**
  76. * Returns the notification to re-run the config wizard.
  77. *
  78. * @return string The notification.
  79. */
  80. private function re_run_notification() {
  81. $content = sprintf(
  82. /* 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. */
  83. esc_html__( 'Want to make sure your %1$s settings are still OK? %2$sOpen the configuration wizard again%3$s to validate them.', 'wordpress-seo' ),
  84. 'Yoast SEO',
  85. '<a href="' . esc_url( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) ) . '">',
  86. '</a>'
  87. );
  88. return $this->notification( __( 'Check SEO configuration', 'wordpress-seo' ), $content );
  89. }
  90. /**
  91. * Returns the notification to start the config wizard for the first time.
  92. *
  93. * @return string The notification.
  94. */
  95. private function first_time_notification() {
  96. $content = sprintf(
  97. /* 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. */
  98. esc_html__( 'Get started quickly with the %1$s %2$sconfiguration wizard%3$s!', 'wordpress-seo' ),
  99. 'Yoast SEO',
  100. '<a href="' . esc_url( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) ) . '">',
  101. '</a>'
  102. );
  103. return $this->notification( __( 'First-time SEO configuration', 'wordpress-seo' ), $content, true );
  104. }
  105. /**
  106. * Returns a styled notification.
  107. *
  108. * @param string $title Title for the notification.
  109. * @param string $content Content for the notification.
  110. * @param bool $show_dismissal Whether to show the dismiss button or not.
  111. *
  112. * @return string The styled notification.
  113. */
  114. private function notification( $title, $content, $show_dismissal = false ) {
  115. $notification = '<div class="yoast-container yoast-container__configuration-wizard">';
  116. $notification .= sprintf(
  117. '<img src="%1$s" height="%2$s" width="%3$d" />',
  118. esc_url( plugin_dir_url( WPSEO_FILE ) . 'images/new-to-configuration-notice.svg' ),
  119. 60,
  120. 60
  121. );
  122. $notification .= '<div class="yoast-container__configuration-wizard--content">';
  123. $notification .= '<h3>' . esc_html( $title ) . '</h3>';
  124. $notification .= '<p>';
  125. $notification .= $content;
  126. $notification .= '</p>';
  127. $notification .= '</div>';
  128. if ( $show_dismissal ) {
  129. $notification .= sprintf(
  130. '<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>',
  131. esc_url( admin_url( 'admin.php?page=wpseo_dashboard&amp;dismiss_get_started=1' ) ),
  132. esc_html__( 'Dismiss this item.', 'wordpress-seo' )
  133. );
  134. }
  135. $notification .= '</div>';
  136. return $notification;
  137. }
  138. }