class-product-upsell-notice.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents the upsell notice.
  9. */
  10. class WPSEO_Product_Upsell_Notice {
  11. const USER_META_DISMISSED = 'wpseo-remove-upsell-notice';
  12. const OPTION_NAME = 'wpseo';
  13. /** @var array */
  14. protected $options;
  15. /**
  16. * Sets the options, because they always have to be there on instance.
  17. */
  18. public function __construct() {
  19. $this->options = $this->get_options();
  20. }
  21. /**
  22. * Checks if the notice should be added or removed.
  23. */
  24. public function initialize() {
  25. if ( $this->is_notice_dismissed() ) {
  26. $this->remove_notification();
  27. return;
  28. }
  29. if ( $this->should_add_notification() ) {
  30. $this->add_notification();
  31. }
  32. }
  33. /**
  34. * Sets the upgrade notice.
  35. */
  36. public function set_upgrade_notice() {
  37. if ( $this->has_first_activated_on() ) {
  38. return;
  39. }
  40. $this->set_first_activated_on();
  41. $this->add_notification();
  42. }
  43. /**
  44. * Listener for the upsell notice.
  45. */
  46. public function dismiss_notice_listener() {
  47. if ( filter_input( INPUT_GET, 'yoast_dismiss' ) !== 'upsell' ) {
  48. return;
  49. }
  50. $this->dismiss_notice();
  51. wp_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
  52. exit;
  53. }
  54. /**
  55. * When the notice should be shown.
  56. *
  57. * @return bool
  58. */
  59. protected function should_add_notification() {
  60. return ( $this->options['first_activated_on'] < strtotime( '-2weeks' ) );
  61. }
  62. /**
  63. * Checks if the options has a first activated on date value.
  64. */
  65. protected function has_first_activated_on() {
  66. return $this->options['first_activated_on'] !== false;
  67. }
  68. /**
  69. * Sets the first activated on.
  70. */
  71. protected function set_first_activated_on() {
  72. $this->options['first_activated_on'] = strtotime( '-2weeks' );
  73. $this->save_options();
  74. }
  75. /**
  76. * Adds a notification to the notification center.
  77. */
  78. protected function add_notification() {
  79. $notification_center = Yoast_Notification_Center::get();
  80. $notification_center->add_notification( $this->get_notification() );
  81. }
  82. /**
  83. * Adds a notification to the notification center.
  84. */
  85. protected function remove_notification() {
  86. $notification_center = Yoast_Notification_Center::get();
  87. $notification_center->remove_notification( $this->get_notification() );
  88. }
  89. /**
  90. * Returns a premium upsell section if using the free plugin.
  91. *
  92. * @return string
  93. */
  94. protected function get_premium_upsell_section() {
  95. $features = new WPSEO_Features();
  96. if ( $features->is_free() ) {
  97. return sprintf(
  98. /* translators: %1$s expands anchor to premium plugin page, %2$s expands to </a> */
  99. __( 'By the way, did you know we also have a %1$sPremium plugin%2$s? It offers advanced features, like a redirect manager and support for multiple keywords. It also comes with 24/7 personal support.', 'wordpress-seo' ),
  100. "<a href='" . WPSEO_Shortlinker::get( 'https://yoa.st/premium-notification' ) . "'>",
  101. '</a>'
  102. );
  103. }
  104. return '';
  105. }
  106. /**
  107. * Gets the notification value.
  108. *
  109. * @return Yoast_Notification
  110. */
  111. protected function get_notification() {
  112. $message = sprintf(
  113. /* translators: %1$s expands to Yoast SEO, %2$s is a link start tag to the plugin page on WordPress.org, %3$s is the link closing tag. */
  114. __( 'We\'ve noticed you\'ve been using %1$s for some time now; we hope you love it! We\'d be thrilled if you could %2$sgive us a 5 stars rating on WordPress.org%3$s!', 'wordpress-seo' ),
  115. 'Yoast SEO',
  116. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/rate-yoast-seo' ) . '">',
  117. '</a>'
  118. ) . "\n\n";
  119. $message .= sprintf(
  120. /* translators: %1$s is a link start tag to the bugreport guidelines on the Yoast knowledge base, %2$s is the link closing tag. */
  121. __( 'If you are experiencing issues, %1$splease file a bug report%2$s and we\'ll do our best to help you out.', 'wordpress-seo' ),
  122. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/bugreport' ) . '">',
  123. '</a>'
  124. ) . "\n\n";
  125. $message .= $this->get_premium_upsell_section() . "\n\n";
  126. $message .= sprintf(
  127. /* translators: %1$s is the notification dismissal link start tag, %2$s is the link closing tag. */
  128. __( '%1$sPlease don\'t show me this notification anymore%2$s', 'wordpress-seo' ),
  129. '<a class="button" href="' . admin_url( '?page=' . WPSEO_Admin::PAGE_IDENTIFIER . '&yoast_dismiss=upsell' ) . '">',
  130. '</a>'
  131. );
  132. $notification = new Yoast_Notification(
  133. $message,
  134. array(
  135. 'type' => Yoast_Notification::WARNING,
  136. 'id' => 'wpseo-upsell-notice',
  137. 'capabilities' => 'wpseo_manage_options',
  138. 'priority' => 0.8,
  139. )
  140. );
  141. return $notification;
  142. }
  143. /**
  144. * Dismisses the notice.
  145. *
  146. * @return string
  147. */
  148. protected function is_notice_dismissed() {
  149. return get_user_meta( get_current_user_id(), self::USER_META_DISMISSED, true ) === '1';
  150. }
  151. /**
  152. * Dismisses the notice.
  153. */
  154. protected function dismiss_notice() {
  155. update_user_meta( get_current_user_id(), self::USER_META_DISMISSED, true );
  156. }
  157. /**
  158. * Returns the set options
  159. *
  160. * @return mixed|void
  161. */
  162. protected function get_options() {
  163. return get_option( self::OPTION_NAME );
  164. }
  165. /**
  166. * Saves the options to the database.
  167. */
  168. protected function save_options() {
  169. update_option( self::OPTION_NAME, $this->options );
  170. }
  171. }