| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update Checker |
| 4 |
* |
| 5 |
* @namespace UsabilityDynamics |
| 6 |
* |
| 7 |
*/ |
| 8 |
namespace UsabilityDynamics\UD_API { |
| 9 |
|
| 10 |
if( !class_exists( 'UsabilityDynamics\UD_API\Update_Checker' ) ) { |
| 11 |
|
| 12 |
/** |
| 13 |
* |
| 14 |
* @author: peshkov@UD |
| 15 |
*/ |
| 16 |
class Update_Checker { |
| 17 |
|
| 18 |
/** |
| 19 |
* |
| 20 |
*/ |
| 21 |
public static $version = '1.0.0'; |
| 22 |
|
| 23 |
/** |
| 24 |
* URL to access the Update API Manager. |
| 25 |
*/ |
| 26 |
private $upgrade_url; |
| 27 |
|
| 28 |
/** |
| 29 |
* Changelog |
| 30 |
*/ |
| 31 |
private $changelog; |
| 32 |
|
| 33 |
/** |
| 34 |
* Instance type ( plugin or theme ) |
| 35 |
*/ |
| 36 |
private $type; |
| 37 |
|
| 38 |
/** |
| 39 |
* same as plugin slug. if a theme use a theme name like 'twentyeleven' |
| 40 |
*/ |
| 41 |
private $name; |
| 42 |
|
| 43 |
/** |
| 44 |
* Path to plugin/theme file |
| 45 |
*/ |
| 46 |
private $file; |
| 47 |
|
| 48 |
/** |
| 49 |
* Software Title |
| 50 |
*/ |
| 51 |
private $product_id; |
| 52 |
|
| 53 |
/** |
| 54 |
* API License Key |
| 55 |
*/ |
| 56 |
private $api_key; |
| 57 |
|
| 58 |
/** |
| 59 |
* License Email |
| 60 |
*/ |
| 61 |
private $activation_email; |
| 62 |
|
| 63 |
/** |
| 64 |
* URL to renew a license |
| 65 |
*/ |
| 66 |
private $renew_license_url; |
| 67 |
|
| 68 |
/** |
| 69 |
* Instance ID (unique to each blog activation) |
| 70 |
*/ |
| 71 |
private $instance; |
| 72 |
|
| 73 |
/** |
| 74 |
* blog domain name |
| 75 |
*/ |
| 76 |
private $blog; |
| 77 |
|
| 78 |
/** |
| 79 |
* |
| 80 |
*/ |
| 81 |
private $software_version; |
| 82 |
|
| 83 |
/** |
| 84 |
* 'theme' or 'plugin' |
| 85 |
*/ |
| 86 |
private $plugin_or_theme; |
| 87 |
|
| 88 |
/** |
| 89 |
* localization for translation |
| 90 |
*/ |
| 91 |
private $text_domain; |
| 92 |
|
| 93 |
/** |
| 94 |
* Used to send any extra information. |
| 95 |
*/ |
| 96 |
private $extra; |
| 97 |
|
| 98 |
/** |
| 99 |
* Errors |
| 100 |
*/ |
| 101 |
public $errors; |
| 102 |
|
| 103 |
/** |
| 104 |
* Error handler. |
| 105 |
* |
| 106 |
*/ |
| 107 |
public $errors_callback; |
| 108 |
|
| 109 |
/** |
| 110 |
* Constructor. |
| 111 |
* |
| 112 |
* @access public |
| 113 |
* @since 1.0.0 |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function __construct( $args, $errors_callback = false ) { |
| 117 |
//** API data */ |
| 118 |
$this->upgrade_url = isset( $args[ 'upgrade_url' ] ) ? $args[ 'upgrade_url' ] : false; |
| 119 |
$this->type = isset( $args[ 'type' ] ) ? $args[ 'type' ] : false; |
| 120 |
$this->name = isset( $args[ 'name' ] ) ? $args[ 'name' ] : false; |
| 121 |
$this->file = isset( $args[ 'file' ] ) ? $args[ 'file' ] : false; |
| 122 |
$this->product_id = isset( $args[ 'product_id' ] ) ? $args[ 'product_id' ] : false; |
| 123 |
$this->api_key = isset( $args[ 'api_key' ] ) ? $args[ 'api_key' ] : false; |
| 124 |
$this->activation_email = isset( $args[ 'activation_email' ] ) ? $args[ 'activation_email' ] : false; |
| 125 |
$this->renew_license_url = isset( $args[ 'renew_license_url' ] ) ? $args[ 'renew_license_url' ] : false; |
| 126 |
$this->instance = isset( $args[ 'instance' ] ) ? $args[ 'instance' ] : false; |
| 127 |
$this->software_version = isset( $args[ 'software_version' ] ) ? $args[ 'software_version' ] : false; |
| 128 |
$this->text_domain = isset( $args[ 'text_domain' ] ) ? $args[ 'text_domain' ] : false; |
| 129 |
$this->extra = isset( $args[ 'extra' ] ) ? $args[ 'extra' ] : false; |
| 130 |
$this->changelog = isset( $args[ 'changelog' ] ) ? $args[ 'changelog' ] : false; |
| 131 |
|
| 132 |
/** |
| 133 |
* Some web hosts have security policies that block the : (colon) and // (slashes) in http://, |
| 134 |
* so only the host portion of the URL can be sent. For example the host portion might be |
| 135 |
* www.example.com or example.com. http://www.example.com includes the scheme http, |
| 136 |
* and the host www.example.com. |
| 137 |
* Sending only the host also eliminates issues when a client site changes from http to https, |
| 138 |
* but their activation still uses the original scheme. |
| 139 |
* To send only the host, use a line like the one below: |
| 140 |
*/ |
| 141 |
$this->blog = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); |
| 142 |
|
| 143 |
$this->errors_callback = $errors_callback; |
| 144 |
|
| 145 |
/** |
| 146 |
* More info: |
| 147 |
* function set_site_transient moved from wp-includes/functions.php |
| 148 |
* to wp-includes/option.php in WordPress 3.4 |
| 149 |
* |
| 150 |
* set_site_transient() contains the pre_set_site_transient_{$transient} filter |
| 151 |
* {$transient} is either update_plugins or update_themes |
| 152 |
* |
| 153 |
* Transient data for plugins and themes exist in the Options table: |
| 154 |
* _site_transient_update_themes |
| 155 |
* _site_transient_update_plugins |
| 156 |
*/ |
| 157 |
|
| 158 |
/** |
| 159 |
* Plugin Updates |
| 160 |
*/ |
| 161 |
if( $this->type == 'plugin' ) { |
| 162 |
//** Check For Plugin Updates */ |
| 163 |
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_check' ) ); |
| 164 |
//** Check For Plugin/Theme Information to display on the update details page */ |
| 165 |
add_filter( 'plugins_api', array( $this, 'request' ), 10, 3 ); |
| 166 |
} |
| 167 |
/** |
| 168 |
* Theme Updates |
| 169 |
*/ |
| 170 |
elseif ( $this->type == 'theme' ) { |
| 171 |
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'update_check' ) ); |
| 172 |
} |
| 173 |
|
| 174 |
add_action( 'wp_ajax_ud_api_dismiss', array( $this, 'dismiss_notices' ) ); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Upgrade API URL |
| 180 |
* |
| 181 |
*/ |
| 182 |
private function create_upgrade_api_url( $args ) { |
| 183 |
$upgrade_url = add_query_arg( 'wc-api', 'upgrade-api', $this->upgrade_url ); |
| 184 |
return $upgrade_url . '&' . http_build_query( $args ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Check for updates against the remote server. |
| 189 |
* |
| 190 |
* @access public |
| 191 |
* @since 1.0.0 |
| 192 |
* @param object $transient |
| 193 |
* @return object $transient |
| 194 |
*/ |
| 195 |
public function update_check( $transient ) { |
| 196 |
|
| 197 |
//** Check if the transient contains the 'checked' information */ |
| 198 |
//** If no, just return its value without hacking it */ |
| 199 |
if ( empty( $transient->checked ) ) { |
| 200 |
return $transient; |
| 201 |
} |
| 202 |
|
| 203 |
$args = array( |
| 204 |
'request' => 'pluginupdatecheck', |
| 205 |
'plugin_name' => $this->name, |
| 206 |
//'version' => $transient->checked[$this->name], |
| 207 |
'version' => $this->software_version, |
| 208 |
'product_id' => $this->product_id, |
| 209 |
'api_key' => $this->api_key, |
| 210 |
'activation_email' => $this->activation_email, |
| 211 |
'instance' => $this->instance, |
| 212 |
'domain' => $this->blog, |
| 213 |
'software_version' => $this->software_version, |
| 214 |
'extra' => $this->extra, |
| 215 |
); |
| 216 |
|
| 217 |
//** Check for a plugin update */ |
| 218 |
$response = $this->plugin_information( $args ); |
| 219 |
//** Displays an admin error message in the WordPress dashboard */ |
| 220 |
$this->check_response_for_errors( $response ); |
| 221 |
|
| 222 |
//** Set version variables */ |
| 223 |
if ( isset( $response ) && is_object( $response ) && $response !== false ) { |
| 224 |
//** New plugin version from the API */ |
| 225 |
$new_ver = (string)$response->new_version; |
| 226 |
//** Current installed plugin version */ |
| 227 |
$curr_ver = (string)$this->software_version; |
| 228 |
//$curr_ver = (string)$transient->checked[$this->name]; |
| 229 |
} |
| 230 |
|
| 231 |
//** If there is a new version, modify the transient to reflect an update is available */ |
| 232 |
if ( isset( $new_ver ) && isset( $curr_ver ) ) { |
| 233 |
if ( $response !== false && version_compare( $new_ver, $curr_ver, '>' ) ) { |
| 234 |
if( $this->type == 'plugin' ) { |
| 235 |
if( isset( $response->slug ) ) { |
| 236 |
$response->slug = sanitize_title( $response->slug ); |
| 237 |
} |
| 238 |
$transient->response[$this->file] = $response; |
| 239 |
} else { |
| 240 |
$theme = basename( dirname( $this->file ) ); |
| 241 |
$response = (array)$response; |
| 242 |
if( empty( $response[ 'url' ] ) ) { |
| 243 |
$response[ 'url' ] = !empty( $this->changelog ) ? $this->changelog : 'https://www.usabilitydynamics.com'; |
| 244 |
} |
| 245 |
$transient->response[$theme] = (array)$response; |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
//echo "<pre>"; print_r( $this ); echo "</pre>"; die(); |
| 252 |
//echo "<pre>"; print_r( $transient ); echo "</pre>"; die(); |
| 253 |
|
| 254 |
return $transient; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Sends and receives data to and from the server API |
| 259 |
* |
| 260 |
* @access public |
| 261 |
* @since 1.0.0 |
| 262 |
* @return object $response |
| 263 |
*/ |
| 264 |
public function plugin_information( $args ) { |
| 265 |
$target_url = $this->create_upgrade_api_url( $args ); |
| 266 |
|
| 267 |
//** Check licenses keys once per one hour! */ |
| 268 |
$response = get_transient( md5( $target_url ) ); |
| 269 |
if ( false === $response || empty( $response ) ) { |
| 270 |
//** Add nocache hack. We must be sure we do not get CACHE result. peshkov@UD */ |
| 271 |
$_target_url = $target_url . '&' . http_build_query( array( 'nocache' => rand( 10000, 99999 ) ) ); |
| 272 |
$request = wp_remote_get( $_target_url, array( 'decompress' => false, 'sslverify' => false ) ); |
| 273 |
if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
$response = unserialize( wp_remote_retrieve_body( $request ) ); |
| 277 |
|
| 278 |
if( is_object( $response ) ) { |
| 279 |
set_transient( md5( $target_url ), json_encode($response), HOUR_IN_SECONDS ); |
| 280 |
} |
| 281 |
} else { |
| 282 |
$r = json_decode( $response, true ); |
| 283 |
$response = new \stdClass(); |
| 284 |
foreach( $r as $k => $v ){ |
| 285 |
$response->{$k} = $v; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
//echo "<pre>"; print_r( $response ); echo "</pre>"; |
| 290 |
if ( is_object( $response ) ) { |
| 291 |
return $response; |
| 292 |
} else { |
| 293 |
return false; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Generic request helper. |
| 299 |
* |
| 300 |
* @access public |
| 301 |
* @since 1.0.0 |
| 302 |
* @param array $args |
| 303 |
* @return object $response or boolean false |
| 304 |
*/ |
| 305 |
public function request( $false, $action, $args ) { |
| 306 |
|
| 307 |
//** Check if this plugins API is about this plugin */ |
| 308 |
if ( isset( $args->slug ) ) { |
| 309 |
//** Check if this plugins API is about this plugin */ |
| 310 |
if ( sanitize_key( $args->slug ) != sanitize_key( $this->name ) ) { |
| 311 |
return $false; |
| 312 |
} |
| 313 |
} else { |
| 314 |
return $false; |
| 315 |
} |
| 316 |
|
| 317 |
$args = array( |
| 318 |
'request' => 'plugininformation', |
| 319 |
'plugin_name' => $this->name, |
| 320 |
//'version' => $version->checked[$this->name], |
| 321 |
'version' => $this->software_version, |
| 322 |
'product_id' => $this->product_id, |
| 323 |
'api_key' => $this->api_key, |
| 324 |
'activation_email' => $this->activation_email, |
| 325 |
'instance' => $this->instance, |
| 326 |
'domain' => $this->blog, |
| 327 |
'software_version' => $this->software_version, |
| 328 |
'extra' => $this->extra, |
| 329 |
); |
| 330 |
|
| 331 |
$response = $this->plugin_information( $args ); |
| 332 |
|
| 333 |
//** If everything is okay return the $response */ |
| 334 |
if ( isset( $response ) && is_object( $response ) && $response !== false ) { |
| 335 |
return $response; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Displays an admin error message in the WordPress dashboard |
| 341 |
* @param array $response |
| 342 |
* @return string |
| 343 |
*/ |
| 344 |
public function check_response_for_errors( $response ) { |
| 345 |
|
| 346 |
$this->errors = array(); |
| 347 |
|
| 348 |
if ( ! empty( $response ) ) { |
| 349 |
|
| 350 |
$plugins = get_plugins(); |
| 351 |
$name = isset( $plugins[$this->name] ) ? $plugins[$this->name]['Name'] : $this->name; |
| 352 |
|
| 353 |
if ( isset( $response->errors['no_key'] ) && $response->errors['no_key'] == 'no_key' && isset( $response->errors['no_subscription'] ) && $response->errors['no_subscription'] == 'no_subscription' ) { |
| 354 |
|
| 355 |
$no_key_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_no_key', '' ); |
| 356 |
$show_no_key_error = $this->check_dismiss_time( $no_key_dismissed ); |
| 357 |
if( $show_no_key_error ) { |
| 358 |
$this->errors[] = sprintf( __( 'A license key for %s could not be found. Maybe you forgot to enter a license key when setting up %s, or the key was deactivated in your account. You can reactivate or purchase a license key from your account <a href="%s" target="_blank">Licences</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_no_key" href="#">dismiss</a>.', $this->text_domain ), $name, $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 359 |
} |
| 360 |
|
| 361 |
$no_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_no_subscription', '' ); |
| 362 |
$show_no_subscription_error = $this->check_dismiss_time( $no_subscription_dismissed ); |
| 363 |
if( $show_no_subscription_error ) { |
| 364 |
$this->errors[] = sprintf( __( 'A subscription for %s could not be found. You can purchase a subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_no_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 365 |
} |
| 366 |
|
| 367 |
} else if ( isset( $response->errors['exp_license'] ) && $response->errors['exp_license'] == 'exp_license' ) { |
| 368 |
|
| 369 |
$exp_license_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_exp_license', '' ); |
| 370 |
$show_exp_license_error = $this->check_dismiss_time( $exp_license_dismissed ); |
| 371 |
if( $show_exp_license_error ) { |
| 372 |
$this->errors[] = sprintf( __( 'The license key for %s has expired. You can reactivate or get a license key from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_exp_license" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 373 |
} |
| 374 |
|
| 375 |
} else if ( isset( $response->errors['hold_subscription'] ) && $response->errors['hold_subscription'] == 'hold_subscription' ) { |
| 376 |
|
| 377 |
$hold_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_hold_subscription', '' ); |
| 378 |
$show_hold_subscription_error = $this->check_dismiss_time( $hold_subscription_dismissed ); |
| 379 |
if( $show_hold_subscription_error ) { |
| 380 |
$this->errors[] = sprintf( __( 'The subscription for %s is on-hold. You can reactivate the subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_hold_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 381 |
} |
| 382 |
|
| 383 |
} else if ( isset( $response->errors['cancelled_subscription'] ) && $response->errors['cancelled_subscription'] == 'cancelled_subscription' ) { |
| 384 |
|
| 385 |
$cancelled_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_cancelled_subscription', '' ); |
| 386 |
$show_cancelled_subscription_error = $this->check_dismiss_time( $cancelled_subscription_dismissed ); |
| 387 |
if( $show_cancelled_subscription_error ) { |
| 388 |
$this->errors[] = sprintf( __( 'The subscription for %s has been cancelled. You can renew the subscription from your account <a href="%s" target="_blank">dashboard</a>. A new license key will be emailed to you after your order has been completed. <a class="dismiss-error dismiss" data-key="dismissed_error_%s_cancelled_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 389 |
} |
| 390 |
|
| 391 |
} else if ( isset( $response->errors['exp_subscription'] ) && $response->errors['exp_subscription'] == 'exp_subscription' ) { |
| 392 |
|
| 393 |
$exp_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_exp_subscription', '' ); |
| 394 |
$show_exp_subscription_error = $this->check_dismiss_time( $exp_subscription_dismissed ); |
| 395 |
if( $show_exp_subscription_error ) { |
| 396 |
$this->errors[] = sprintf( __( 'The subscription for %s has expired. You can reactivate the subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_exp_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ) ; |
| 397 |
} |
| 398 |
|
| 399 |
} else if ( isset( $response->errors['suspended_subscription'] ) && $response->errors['suspended_subscription'] == 'suspended_subscription' ) { |
| 400 |
|
| 401 |
$suspended_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_suspended_subscription', '' ); |
| 402 |
$show_suspended_subscription_error = $this->check_dismiss_time( $suspended_subscription_dismissed ); |
| 403 |
if( $show_suspended_subscription_error ) { |
| 404 |
$this->errors[] = sprintf( __( 'The subscription for %s has been suspended. You can reactivate the subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_suspended_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ) ; |
| 405 |
} |
| 406 |
|
| 407 |
} else if ( isset( $response->errors['pending_subscription'] ) && $response->errors['pending_subscription'] == 'pending_subscription' ) { |
| 408 |
|
| 409 |
$pending_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_pending_subscription', '' ); |
| 410 |
$show_pending_subscription_error = $this->check_dismiss_time( $pending_subscription_dismissed ); |
| 411 |
if( $show_pending_subscription_error ) { |
| 412 |
$this->errors[] = sprintf( __( 'The subscription for %s is still pending. You can check on the status of the subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_pending_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ) ; |
| 413 |
} |
| 414 |
|
| 415 |
} else if ( isset( $response->errors['trash_subscription'] ) && $response->errors['trash_subscription'] == 'trash_subscription' ) { |
| 416 |
|
| 417 |
$trash_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_trash_subscription', '' ); |
| 418 |
$show_trash_subscription_error = $this->check_dismiss_time( $trash_subscription_dismissed ); |
| 419 |
if( $show_trash_subscription_error ) { |
| 420 |
$this->errors[] = sprintf( __( 'The subscription for %s has been placed in the trash and will be deleted soon. You can get a new subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_trash_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ) ; |
| 421 |
} |
| 422 |
|
| 423 |
} else if ( isset( $response->errors['no_subscription'] ) && $response->errors['no_subscription'] == 'no_subscription' ) { |
| 424 |
|
| 425 |
$no_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_no_subscription', '' ); |
| 426 |
$show_no_subscription_error = $this->check_dismiss_time( $no_subscription_dismissed ); |
| 427 |
if( $show_no_subscription_error ) { |
| 428 |
$this->errors[] = sprintf( __( 'A subscription for %s could not be found. You can get a subscription from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_no_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 429 |
} |
| 430 |
|
| 431 |
} else if ( isset( $response->errors['no_activation'] ) && $response->errors['no_activation'] == 'no_activation' ) { |
| 432 |
|
| 433 |
$no_activation_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_no_activation', '' ); |
| 434 |
$show_no_activation_error = $this->check_dismiss_time( $no_activation_dismissed ); |
| 435 |
if( $show_no_activation_error ) { |
| 436 |
$this->errors[] = sprintf( __( '%s has not been activated. Go to the settings page and enter the license key and license email to activate %s. <a class="dismiss-error dismiss" data-key="dismissed_error_%s_no_activation" href="#">dismiss</a>.', $this->text_domain ), $name, $name, sanitize_key( $name ) ) ; |
| 437 |
} |
| 438 |
|
| 439 |
} else if ( isset( $response->errors['no_key'] ) && $response->errors['no_key'] == 'no_key' ) { |
| 440 |
|
| 441 |
$no_key_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_no_key', '' ); |
| 442 |
$show_no_key_error = $this->check_dismiss_time( $no_key_dismissed ); |
| 443 |
if( $show_no_key_error ) { |
| 444 |
$this->errors[] = sprintf( __( 'A license key for %s could not be found. Maybe you forgot to enter a license key when setting up %s, or the key was deactivated in your account. You can reactivate or get a license key from your account <a href="%s" target="_blank">Licences</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_no_key" href="#">dismiss</a>.', $this->text_domain ), $name, $name, $this->renew_license_url, sanitize_key( $name ) ); |
| 445 |
} |
| 446 |
|
| 447 |
} else if ( isset( $response->errors['download_revoked'] ) && $response->errors['download_revoked'] == 'download_revoked' ) { |
| 448 |
|
| 449 |
$download_revoked_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_download_revoked', '' ); |
| 450 |
$show_download_revoked_error = $this->check_dismiss_time( $download_revoked_dismissed ); |
| 451 |
if( $show_download_revoked_error ) { |
| 452 |
$this->errors[] = sprintf( __( 'Download permission for %s has been revoked possibly due to a license key or subscription expiring. You can reactivate or get a license key from your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_download_revoked" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ) ; |
| 453 |
} |
| 454 |
|
| 455 |
} else if ( isset( $response->errors['switched_subscription'] ) && $response->errors['switched_subscription'] == 'switched_subscription' ) { |
| 456 |
|
| 457 |
$switched_subscription_dismissed = get_option( 'dismissed_error_' . sanitize_key( $name ) . '_switched_subscription', '' ); |
| 458 |
$show_switched_subscription_error = $this->check_dismiss_time( $switched_subscription_dismissed ); |
| 459 |
if( $show_switched_subscription_error ) { |
| 460 |
$this->errors[] = sprintf( __( 'You changed the subscription for %s, so you will need to enter your new API License Key in the settings page. The License Key should have arrived in your email inbox, if not you can get it by logging into your account <a href="%s" target="_blank">dashboard</a> | <a class="dismiss-error dismiss" data-key="dismissed_error_%s_switched_subscription" href="#">dismiss</a>.', $this->text_domain ), $name, $this->renew_license_url, sanitize_key( $name ) ) ; |
| 461 |
} |
| 462 |
|
| 463 |
} |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
if( !empty( $this->errors ) ) { |
| 468 |
add_action('admin_notices', array( $this, 'print_errors') ); |
| 469 |
} |
| 470 |
|
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Maybe print admin notices |
| 475 |
*/ |
| 476 |
public function print_errors() { |
| 477 |
if( !empty( $this->errors ) && is_array( $this->errors ) ) { |
| 478 |
foreach( $this->errors as $error ) { |
| 479 |
echo '<div id="message" class="error"><p>' . $error . '</p></div>'; |
| 480 |
} |
| 481 |
$this->print_scripts(); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* print script for ajax |
| 487 |
*/ |
| 488 |
public function print_scripts() { |
| 489 |
ob_start(); |
| 490 |
?> |
| 491 |
<script type="text/javascript"> |
| 492 |
jQuery( document ).ready( function () { |
| 493 |
|
| 494 |
jQuery( '.error' ).on( 'click', '.dismiss', function(e){ |
| 495 |
e.preventDefault(); |
| 496 |
|
| 497 |
var _this = jQuery( this ); |
| 498 |
|
| 499 |
var data = { |
| 500 |
action: 'ud_api_dismiss', |
| 501 |
key: _this.data('key'), |
| 502 |
} |
| 503 |
|
| 504 |
jQuery.post( "<?php echo admin_url( 'admin-ajax.php' ); ?>", data, function ( result_data ) { |
| 505 |
if( result_data.success == '1' ) { |
| 506 |
_this.closest('.error').remove(); |
| 507 |
} else if ( result_data.success == '0' ) { |
| 508 |
console.error(result_data.error); |
| 509 |
} |
| 510 |
}, "json" ); |
| 511 |
|
| 512 |
}); |
| 513 |
|
| 514 |
} ); |
| 515 |
</script> |
| 516 |
<?php |
| 517 |
echo ob_get_clean(); |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Check dismiss notice timestamp if greater than 24 hrs |
| 522 |
* |
| 523 |
* @param string $time |
| 524 |
* |
| 525 |
* @return bool |
| 526 |
*/ |
| 527 |
public function check_dismiss_time( $time = '' ) { |
| 528 |
if( empty( $time ) ) { |
| 529 |
return true; |
| 530 |
} |
| 531 |
$current_time = time(); |
| 532 |
$diff = $current_time - 86400; |
| 533 |
if ( $diff > (int)$time ) { |
| 534 |
return true; |
| 535 |
} |
| 536 |
return false; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* dismiss the notice ajax callback |
| 541 |
* @throws \Exception |
| 542 |
*/ |
| 543 |
public function dismiss_notices(){ |
| 544 |
$response = array( |
| 545 |
'success' => '0', |
| 546 |
'error' => __( 'There was an error in request.', $this->text_domain ), |
| 547 |
); |
| 548 |
$error = false; |
| 549 |
|
| 550 |
if( empty($_POST['key']) ) { |
| 551 |
$response['error'] = __( 'Invalid key', $this->text_domain ); |
| 552 |
$error = true; |
| 553 |
} |
| 554 |
|
| 555 |
if ( ! $error && update_option( ( $_POST['key'] ), time() ) ) { |
| 556 |
$response['success'] = '1'; |
| 557 |
} |
| 558 |
|
| 559 |
wp_send_json( $response ); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
} |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
} |
| 568 |
|