class-product-upsell-notice.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  12. * Holds the name of the user meta key.
  13. *
  14. * The value of this database field holds whether the user has dismissed this notice or not.
  15. *
  16. * @var string
  17. */
  18. const USER_META_DISMISSED = 'wpseo-remove-upsell-notice';
  19. /**
  20. * Holds the option name.
  21. *
  22. * @var string
  23. */
  24. const OPTION_NAME = 'wpseo';
  25. /**
  26. * Holds the options.
  27. *
  28. * @var array
  29. */
  30. protected $options;
  31. /**
  32. * Sets the options, because they always have to be there on instance.
  33. */
  34. public function __construct() {
  35. $this->options = $this->get_options();
  36. }
  37. /**
  38. * Checks if the notice should be added or removed.
  39. */
  40. public function initialize() {
  41. $this->remove_notification();
  42. }
  43. /**
  44. * Sets the upgrade notice.
  45. */
  46. public function set_upgrade_notice() {
  47. if ( $this->has_first_activated_on() ) {
  48. return;
  49. }
  50. $this->set_first_activated_on();
  51. $this->add_notification();
  52. }
  53. /**
  54. * Listener for the upsell notice.
  55. */
  56. public function dismiss_notice_listener() {
  57. if ( filter_input( INPUT_GET, 'yoast_dismiss' ) !== 'upsell' ) {
  58. return;
  59. }
  60. $this->dismiss_notice();
  61. wp_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
  62. exit;
  63. }
  64. /**
  65. * When the notice should be shown.
  66. *
  67. * @return bool
  68. */
  69. protected function should_add_notification() {
  70. return ( $this->options['first_activated_on'] < strtotime( '-2weeks' ) );
  71. }
  72. /**
  73. * Checks if the options has a first activated on date value.
  74. */
  75. protected function has_first_activated_on() {
  76. return $this->options['first_activated_on'] !== false;
  77. }
  78. /**
  79. * Sets the first activated on.
  80. */
  81. protected function set_first_activated_on() {
  82. $this->options['first_activated_on'] = strtotime( '-2weeks' );
  83. $this->save_options();
  84. }
  85. /**
  86. * Adds a notification to the notification center.
  87. */
  88. protected function add_notification() {
  89. $notification_center = Yoast_Notification_Center::get();
  90. $notification_center->add_notification( $this->get_notification() );
  91. }
  92. /**
  93. * Removes a notification to the notification center.
  94. */
  95. protected function remove_notification() {
  96. $notification_center = Yoast_Notification_Center::get();
  97. $notification_center->remove_notification( $this->get_notification() );
  98. }
  99. /**
  100. * Returns a premium upsell section if using the free plugin.
  101. *
  102. * @return string
  103. */
  104. protected function get_premium_upsell_section() {
  105. $features = new WPSEO_Features();
  106. if ( $features->is_free() ) {
  107. return sprintf(
  108. /* translators: %1$s expands anchor to premium plugin page, %2$s expands to </a> */
  109. __( '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 keyphrases. It also comes with 24/7 personal support.', 'wordpress-seo' ),
  110. "<a href='" . WPSEO_Shortlinker::get( 'https://yoa.st/premium-notification' ) . "'>",
  111. '</a>'
  112. );
  113. }
  114. return '';
  115. }
  116. /**
  117. * Gets the notification value.
  118. *
  119. * @return Yoast_Notification
  120. */
  121. protected function get_notification() {
  122. $message = sprintf(
  123. /* 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. */
  124. __( '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' ),
  125. 'Yoast SEO',
  126. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/rate-yoast-seo' ) . '">',
  127. '</a>'
  128. ) . "\n\n";
  129. $message .= sprintf(
  130. /* translators: %1$s is a link start tag to the bugreport guidelines on the Yoast knowledge base, %2$s is the link closing tag. */
  131. __( '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' ),
  132. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/bugreport' ) . '">',
  133. '</a>'
  134. ) . "\n\n";
  135. $message .= $this->get_premium_upsell_section() . "\n\n";
  136. $message .= '<a class="button" href="' . admin_url( '?page=' . WPSEO_Admin::PAGE_IDENTIFIER . '&yoast_dismiss=upsell' ) . '">' . __( 'Please don\'t show me this notification anymore', 'wordpress-seo' ) . '</a>';
  137. $notification = new Yoast_Notification(
  138. $message,
  139. [
  140. 'type' => Yoast_Notification::WARNING,
  141. 'id' => 'wpseo-upsell-notice',
  142. 'capabilities' => 'wpseo_manage_options',
  143. 'priority' => 0.8,
  144. ]
  145. );
  146. return $notification;
  147. }
  148. /**
  149. * Dismisses the notice.
  150. *
  151. * @return string
  152. */
  153. protected function is_notice_dismissed() {
  154. return get_user_meta( get_current_user_id(), self::USER_META_DISMISSED, true ) === '1';
  155. }
  156. /**
  157. * Dismisses the notice.
  158. */
  159. protected function dismiss_notice() {
  160. update_user_meta( get_current_user_id(), self::USER_META_DISMISSED, true );
  161. }
  162. /**
  163. * Returns the set options.
  164. *
  165. * @return mixed|void
  166. */
  167. protected function get_options() {
  168. return get_option( self::OPTION_NAME );
  169. }
  170. /**
  171. * Saves the options to the database.
  172. */
  173. protected function save_options() {
  174. update_option( self::OPTION_NAME, $this->options );
  175. }
  176. }