| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Represents the upsell notice. |
| 10 |
*/ |
| 11 |
class WPSEO_Product_Upsell_Notice { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds the name of the user meta key. |
| 15 |
* |
| 16 |
* The value of this database field holds whether the user has dismissed this notice or not. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public const USER_META_DISMISSED = 'wpseo-remove-upsell-notice'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the option name. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public const OPTION_NAME = 'wpseo'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the options. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $options; |
| 35 |
|
| 36 |
/** |
| 37 |
* Sets the options, because they always have to be there on instance. |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
$this->options = $this->get_options(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Checks if the notice should be added or removed. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function initialize() { |
| 49 |
$this->remove_notification(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Sets the upgrade notice. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function set_upgrade_notice() { |
| 58 |
|
| 59 |
if ( $this->has_first_activated_on() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$this->set_first_activated_on(); |
| 64 |
$this->add_notification(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Listener for the upsell notice. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function dismiss_notice_listener() { |
| 73 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are validating a nonce here. |
| 74 |
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'dismiss-5star-upsell' ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$dismiss_upsell = isset( $_GET['yoast_dismiss'] ) && is_string( $_GET['yoast_dismiss'] ) ? sanitize_text_field( wp_unslash( $_GET['yoast_dismiss'] ) ) : ''; |
| 79 |
|
| 80 |
if ( $dismiss_upsell !== 'upsell' ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$this->dismiss_notice(); |
| 85 |
|
| 86 |
if ( wp_safe_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) ) ) { |
| 87 |
exit(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* When the notice should be shown. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
protected function should_add_notification() { |
| 97 |
return ( $this->options['first_activated_on'] < strtotime( '-2weeks' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Checks if the options has a first activated on date value. |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
protected function has_first_activated_on() { |
| 106 |
return $this->options['first_activated_on'] !== false; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Sets the first activated on. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
protected function set_first_activated_on() { |
| 115 |
$this->options['first_activated_on'] = strtotime( '-2weeks' ); |
| 116 |
|
| 117 |
$this->save_options(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Adds a notification to the notification center. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
protected function add_notification() { |
| 126 |
$notification_center = Yoast_Notification_Center::get(); |
| 127 |
$notification_center->add_notification( $this->get_notification() ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Removes a notification to the notification center. |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
protected function remove_notification() { |
| 136 |
$notification_center = Yoast_Notification_Center::get(); |
| 137 |
$notification_center->remove_notification( $this->get_notification() ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns a premium upsell section if using the free plugin. |
| 142 |
* |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
protected function get_premium_upsell_section() { |
| 146 |
if ( ! YoastSEO()->helpers->product->is_premium() ) { |
| 147 |
return sprintf( |
| 148 |
/* translators: %1$s expands anchor to premium plugin page, %2$s expands to </a> */ |
| 149 |
__( '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' ), |
| 150 |
"<a href='" . WPSEO_Shortlinker::get( 'https://yoa.st/premium-notification' ) . "'>", |
| 151 |
'</a>', |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
return ''; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Gets the notification value. |
| 160 |
* |
| 161 |
* @return Yoast_Notification |
| 162 |
*/ |
| 163 |
protected function get_notification() { |
| 164 |
$message = sprintf( |
| 165 |
/* 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. */ |
| 166 |
__( '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' ), |
| 167 |
'Yoast SEO', |
| 168 |
'<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/rate-yoast-seo' ) . '">', |
| 169 |
'</a>', |
| 170 |
) . "\n\n"; |
| 171 |
|
| 172 |
$message .= sprintf( |
| 173 |
/* translators: %1$s is a link start tag to the bugreport guidelines on the Yoast help center, %2$s is the link closing tag. */ |
| 174 |
__( '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' ), |
| 175 |
'<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/bugreport' ) . '">', |
| 176 |
'</a>', |
| 177 |
) . "\n\n"; |
| 178 |
|
| 179 |
$message .= $this->get_premium_upsell_section() . "\n\n"; |
| 180 |
|
| 181 |
$message .= '<a class="button" href="' . wp_nonce_url( admin_url( '?page=' . WPSEO_Admin::PAGE_IDENTIFIER . '&yoast_dismiss=upsell' ), 'dismiss-5star-upsell' ) . '">' . __( 'Please don\'t show me this notification anymore', 'wordpress-seo' ) . '</a>'; |
| 182 |
|
| 183 |
$notification = new Yoast_Notification( |
| 184 |
$message, |
| 185 |
[ |
| 186 |
'type' => Yoast_Notification::WARNING, |
| 187 |
'id' => 'wpseo-upsell-notice', |
| 188 |
'capabilities' => 'wpseo_manage_options', |
| 189 |
'priority' => 0.8, |
| 190 |
], |
| 191 |
); |
| 192 |
|
| 193 |
return $notification; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Dismisses the notice. |
| 198 |
* |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
protected function is_notice_dismissed() { |
| 202 |
return get_user_meta( get_current_user_id(), self::USER_META_DISMISSED, true ) === '1'; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Dismisses the notice. |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
protected function dismiss_notice() { |
| 211 |
update_user_meta( get_current_user_id(), self::USER_META_DISMISSED, true ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns the set options. |
| 216 |
* |
| 217 |
* @return mixed |
| 218 |
*/ |
| 219 |
protected function get_options() { |
| 220 |
return get_option( self::OPTION_NAME ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Saves the options to the database. |
| 225 |
* |
| 226 |
* @return void |
| 227 |
*/ |
| 228 |
protected function save_options() { |
| 229 |
update_option( self::OPTION_NAME, $this->options ); |
| 230 |
} |
| 231 |
} |
| 232 |
|