| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Update; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use FloatingButton\Dashboard\DashboardHelper; |
| 8 |
use FloatingButton\Dashboard\Link; |
| 9 |
use FloatingButton\WOW_Plugin; |
| 10 |
|
| 11 |
class Checker { |
| 12 |
public function __construct() { |
| 13 |
add_filter( WOW_Plugin::PREFIX . '_admin_filter_file', [ $this, 'filter_file' ], 10, 2 ); |
| 14 |
add_action( 'admin_init', array( $this, 'plugin_updater' ), 0 ); |
| 15 |
add_action( 'admin_init', array( $this, 'register_option' ) ); |
| 16 |
add_action( 'admin_init', array( $this, 'activate_license' ) ); |
| 17 |
|
| 18 |
add_action( 'admin_init', array( $this, 'deactivate_license' ) ); |
| 19 |
|
| 20 |
add_action( 'admin_notices', [ $this, 'admin_notices' ] ); |
| 21 |
|
| 22 |
register_deactivation_hook( WOW_Plugin::file(), array( $this, 'deactivate_plugin' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
public function filter_file( $file, $current ) { |
| 27 |
|
| 28 |
if ( ( $current === 'list' || $current === 'settings' ) && ! self::key() ) { |
| 29 |
return DashboardHelper::get_file( 'license', 'pages' ); |
| 30 |
} |
| 31 |
|
| 32 |
return $file; |
| 33 |
} |
| 34 |
|
| 35 |
public static function key(): bool { |
| 36 |
|
| 37 |
$license = get_option( 'wow_license_key_' . WOW_Plugin::PREFIX ); |
| 38 |
$status = get_option( 'wow_license_status_' . WOW_Plugin::PREFIX ); |
| 39 |
|
| 40 |
return ! empty( $license ) && $status === 'valid'; |
| 41 |
} |
| 42 |
|
| 43 |
public function plugin_updater(): void { |
| 44 |
|
| 45 |
$doing_cron = defined( 'DOING_CRON' ) && DOING_CRON; |
| 46 |
if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$license_key = trim( get_option( 'wow_license_key_' . WOW_Plugin::PREFIX ) ); |
| 51 |
|
| 52 |
$edd_updater = new Plugin_Updater( |
| 53 |
WOW_Plugin::info('store'), |
| 54 |
WOW_Plugin::file(), |
| 55 |
array( |
| 56 |
'version' => WOW_Plugin::info('version'), |
| 57 |
'license' => $license_key, |
| 58 |
'item_id' => WOW_Plugin::info('item_id'), |
| 59 |
'author' => WOW_Plugin::info('author'), |
| 60 |
'beta' => false, |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
public function register_option(): void { |
| 69 |
register_setting( 'wow_license_' . WOW_Plugin::PREFIX, 'wow_license_key_' . WOW_Plugin::PREFIX, [ |
| 70 |
$this, |
| 71 |
'sanitize_license' |
| 72 |
] ); |
| 73 |
} |
| 74 |
|
| 75 |
public function sanitize_license( $new ) { |
| 76 |
$old = get_option( 'wow_license_key_' . WOW_Plugin::PREFIX ); |
| 77 |
if ( $old && $old !== $new ) { |
| 78 |
delete_option( 'wow_license_status_' . WOW_Plugin::PREFIX ); |
| 79 |
} |
| 80 |
|
| 81 |
return $new; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public function activate_license() { |
| 86 |
|
| 87 |
$action = WOW_Plugin::PREFIX . '_license_activation'; |
| 88 |
|
| 89 |
if ( ! isset( $_POST[ $action ] ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! check_admin_referer( WOW_Plugin::PREFIX . '_nonce', $action ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
$license = ! empty( $_POST[ 'wow_license_key_' . WOW_Plugin::PREFIX ] ) ? sanitize_text_field( $_POST[ 'wow_license_key_' . WOW_Plugin::PREFIX ] ) : ''; |
| 99 |
|
| 100 |
if ( ! $license ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$license = trim( $license ); |
| 105 |
|
| 106 |
|
| 107 |
$api_params = array( |
| 108 |
'edd_action' => 'activate_license', |
| 109 |
'license' => $license, |
| 110 |
'item_id' => WOW_Plugin::info('item_id'), |
| 111 |
'item_name' => rawurlencode( WOW_Plugin::info('name') ), // the name of our product in EDD |
| 112 |
'url' => home_url(), |
| 113 |
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production', |
| 114 |
); |
| 115 |
|
| 116 |
$response = wp_remote_post( WOW_Plugin::info('store'), |
| 117 |
[ |
| 118 |
'timeout' => 60, |
| 119 |
'sslverify' => false, |
| 120 |
'body' => $api_params, |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
// make sure the response came back okay |
| 125 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 126 |
|
| 127 |
if ( is_wp_error( $response ) ) { |
| 128 |
$message = $response->get_error_message(); |
| 129 |
} else { |
| 130 |
$message = __( 'An error occurred, please try again.', 'floating-button' ); |
| 131 |
} |
| 132 |
} else { |
| 133 |
|
| 134 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 135 |
|
| 136 |
if ( false === $license_data->success ) { |
| 137 |
|
| 138 |
switch ( $license_data->error ) { |
| 139 |
|
| 140 |
case 'expired': |
| 141 |
$message = sprintf( |
| 142 |
/* translators: the license key expiration date */ |
| 143 |
__( 'Your license key expired on %s.', 'floating-button' ), |
| 144 |
date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) ) |
| 145 |
); |
| 146 |
break; |
| 147 |
|
| 148 |
case 'disabled': |
| 149 |
case 'revoked': |
| 150 |
$message = __( 'Your license key has been disabled.', 'floating-button' ); |
| 151 |
break; |
| 152 |
|
| 153 |
case 'missing': |
| 154 |
$message = __( 'Invalid license.', 'floating-button' ); |
| 155 |
break; |
| 156 |
|
| 157 |
case 'invalid': |
| 158 |
case 'site_inactive': |
| 159 |
$message = __( 'Your license is not active for this URL.', 'floating-button' ); |
| 160 |
break; |
| 161 |
|
| 162 |
case 'item_name_mismatch': |
| 163 |
/* translators: the plugin name */ |
| 164 |
$message = sprintf( __( 'This appears to be an invalid license key for %s.', 'floating-button' ), WOW_Plugin::info('name') ); |
| 165 |
break; |
| 166 |
|
| 167 |
case 'no_activations_left': |
| 168 |
$message = __( 'Your license key has reached its activation limit.', 'floating-button' ); |
| 169 |
break; |
| 170 |
|
| 171 |
default: |
| 172 |
$message = __( 'An error occurred, please try again.', 'floating-button' ); |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
// Check if anything passed on a message constituting a failure |
| 180 |
if ( ! empty( $message ) ) { |
| 181 |
$redirect = Link::create( [ |
| 182 |
'tab' => 'license', |
| 183 |
'sl_activation' => 'false', |
| 184 |
'prefix' => WOW_Plugin::PREFIX, |
| 185 |
'messanger' => rawurlencode( $message ) |
| 186 |
] ); |
| 187 |
|
| 188 |
|
| 189 |
wp_safe_redirect( $redirect ); |
| 190 |
exit(); |
| 191 |
} |
| 192 |
|
| 193 |
// $license_data->license will be either "valid" or "invalid" |
| 194 |
if ( 'valid' === $license_data->license ) { |
| 195 |
update_option( 'wow_license_key_' . WOW_Plugin::PREFIX, $license ); |
| 196 |
} |
| 197 |
update_option( 'wow_license_status_' . WOW_Plugin::PREFIX, $license_data->license ); |
| 198 |
wp_safe_redirect( Link::create( [ 'tab' => 'list' ] ) ); |
| 199 |
exit(); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
public function deactivate_license() { |
| 204 |
$action = WOW_Plugin::PREFIX . '_license_deactivated'; |
| 205 |
|
| 206 |
if ( ! isset( $_POST[ $action ] ) ) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
if ( ! check_admin_referer( WOW_Plugin::PREFIX . '_nonce', $action ) ) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
$license = ! empty( $_POST[ 'wow_license_key_' . WOW_Plugin::PREFIX ] ) ? sanitize_text_field( $_POST[ 'wow_license_key_' . WOW_Plugin::PREFIX ] ) : ''; |
| 215 |
|
| 216 |
|
| 217 |
if ( ! $license ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
$license = trim( $license ); |
| 222 |
|
| 223 |
$api_params = array( |
| 224 |
'edd_action' => 'deactivate_license', |
| 225 |
'license' => $license, |
| 226 |
'item_id' => WOW_Plugin::info('item_id'), |
| 227 |
'item_name' => rawurlencode( WOW_Plugin::info('name') ), |
| 228 |
'url' => home_url(), |
| 229 |
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production', |
| 230 |
); |
| 231 |
|
| 232 |
$response = wp_remote_post( |
| 233 |
WOW_Plugin::info('store'), |
| 234 |
array( |
| 235 |
'timeout' => 60, |
| 236 |
'sslverify' => false, |
| 237 |
'body' => $api_params, |
| 238 |
) |
| 239 |
); |
| 240 |
|
| 241 |
|
| 242 |
// make sure the response came back okay |
| 243 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 244 |
|
| 245 |
if ( is_wp_error( $response ) ) { |
| 246 |
$message = $response->get_error_message(); |
| 247 |
} else { |
| 248 |
$message = __( 'An error occurred, please try again.', 'floating-button' ); |
| 249 |
} |
| 250 |
|
| 251 |
$redirect = Link::create( [ |
| 252 |
'tab' => 'license', |
| 253 |
'sl_activation' => 'false', |
| 254 |
'prefix' => WOW_Plugin::PREFIX, |
| 255 |
'messanger' => rawurlencode( $message ) |
| 256 |
] ); |
| 257 |
|
| 258 |
wp_safe_redirect( $redirect ); |
| 259 |
exit(); |
| 260 |
} |
| 261 |
|
| 262 |
// decode the license data |
| 263 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 264 |
|
| 265 |
// $license_data->license will be either "deactivated" or "failed" |
| 266 |
if ( 'deactivated' === $license_data->license || 'failed' === $license_data->license ) { |
| 267 |
delete_option( 'wow_license_status_' . WOW_Plugin::PREFIX, ); |
| 268 |
} |
| 269 |
|
| 270 |
wp_safe_redirect( Link::create( [ 'tab' => 'license' ] ) ); |
| 271 |
exit(); |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
public function deactivate_plugin(): void { |
| 276 |
|
| 277 |
$license = trim( get_option( 'wow_license_key_' . WOW_Plugin::PREFIX ) ); |
| 278 |
|
| 279 |
$api_params = array( |
| 280 |
'edd_action' => 'deactivate_license', |
| 281 |
'license' => $license, |
| 282 |
'item_id' => WOW_Plugin::info('item_id'), |
| 283 |
'item_name' => rawurlencode( WOW_Plugin::info('name') ), |
| 284 |
'url' => home_url(), |
| 285 |
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production', |
| 286 |
); |
| 287 |
|
| 288 |
$response = wp_remote_post( |
| 289 |
WOW_Plugin::info('store'), |
| 290 |
array( |
| 291 |
'timeout' => 60, |
| 292 |
'sslverify' => false, |
| 293 |
'body' => $api_params, |
| 294 |
) |
| 295 |
); |
| 296 |
|
| 297 |
|
| 298 |
// decode the license data |
| 299 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 300 |
|
| 301 |
// $license_data->license will be either "deactivated" or "failed" |
| 302 |
if ( $license_data->license == 'deactivated' || 'failed' === $license_data->license ) { |
| 303 |
delete_option( 'wow_license_status_' . WOW_Plugin::PREFIX, ); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
public function admin_notices(): void { |
| 308 |
if ( isset( $_GET['sl_activation'] ) && ! empty( $_GET['messanger'] ) && WOW_Plugin::PREFIX === $_GET['prefix'] ) { |
| 309 |
|
| 310 |
switch ( $_GET['sl_activation'] ) { |
| 311 |
|
| 312 |
case 'false': |
| 313 |
$message = urldecode( $_GET['messanger'] ); |
| 314 |
?> |
| 315 |
<div class="error"> |
| 316 |
<p><?php echo wp_kses_post( $message ); ?></p> |
| 317 |
</div> |
| 318 |
<?php |
| 319 |
break; |
| 320 |
|
| 321 |
case 'true': |
| 322 |
default: |
| 323 |
// Developers can put a custom success message here for when activation is successful if they way. |
| 324 |
break; |
| 325 |
|
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
} |