| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used for any admin AJAX interactions. |
| 10 |
*/ |
| 11 |
class P_Admin_Ajax extends P_Core { |
| 12 |
|
| 13 |
/** |
| 14 |
* Add the actions required for AJAX interactions. |
| 15 |
* |
| 16 |
* @param Patchstack $core |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function __construct( $core ) { |
| 20 |
parent::__construct( $core ); |
| 21 |
if ( isset( $_POST['PatchstackNonce'] ) && current_user_can( 'manage_options' ) && wp_verify_nonce( $_POST['PatchstackNonce'], 'patchstack-nonce' ) ) { |
| 22 |
// License related actions. |
| 23 |
add_action( 'wp_ajax_patchstack_activate_license', [ $this, 'activate_license' ] ); |
| 24 |
|
| 25 |
// Auto license activator. |
| 26 |
add_action( 'wp_ajax_patchstack_activate_auto', [ $this, 'auto_activate' ] ); |
| 27 |
add_action( 'wp_ajax_patchstack_activation_status', [ $this, 'activation_status' ] ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Test and activate a new license. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function activate_license() { |
| 37 |
if ( ! isset( $_POST['key'] ) || strpos( $_POST['key'], '-' ) === false) { |
| 38 |
wp_send_json( |
| 39 |
[ |
| 40 |
'result' => 'error', |
| 41 |
'error_message' => esc_attr__('An invalid API key was provided.', 'patchstack') |
| 42 |
] |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
// Since we have the keys combined into one now, split them up here. |
| 47 |
$split = explode('-', $_POST['key']); |
| 48 |
$secretkey = $split[0]; |
| 49 |
$clientid = $split[1]; |
| 50 |
|
| 51 |
// Test the new keys. |
| 52 |
update_option( 'patchstack_api_token', '' ); |
| 53 |
$results = $this->plugin->activation->alter_license( wp_filter_nohtml_kses( $clientid ), wp_filter_nohtml_kses( $secretkey ), 'activate' ); |
| 54 |
if ( $results ) { |
| 55 |
$response = $this->plugin->api->update_license_status(); |
| 56 |
$results['response'] = $response; |
| 57 |
wp_send_json( $results ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Attempt to auto activate the license after a plugin activation, if no current license exists. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function auto_activate() { |
| 67 |
$secretToken = get_option( 'patchstack_activation_secret', '' ); |
| 68 |
|
| 69 |
// Only continue if we have a secret token. |
| 70 |
$autoActivated = false; |
| 71 |
if ( ! empty( $secretToken ) ) { |
| 72 |
$autoActivated = $this->plugin->api->send_secret_token( $secretToken ); |
| 73 |
} |
| 74 |
|
| 75 |
wp_send_json( [ |
| 76 |
'result' => $autoActivated || get_option( 'patchstack_clientid', false ) != false ? 'success' : 'error' |
| 77 |
] ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the current license activation status. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function activation_status() { |
| 86 |
wp_send_json( [ |
| 87 |
'activated' => get_option( 'patchstack_clientid', false ) != false |
| 88 |
] ); |
| 89 |
} |
| 90 |
} |
| 91 |
|