| 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 |
// Manual connection re-check (Retry link on the settings card). |
| 30 |
add_action( 'wp_ajax_patchstack_check_connection', [ $this, 'check_connection' ] ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Test and activate a new license. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function activate_license() { |
| 40 |
if ( ! isset( $_POST['key'] ) || strpos( $_POST['key'], '-' ) === false) { |
| 41 |
wp_send_json( |
| 42 |
[ |
| 43 |
'result' => 'error', |
| 44 |
'error_message' => esc_attr__('An invalid API key was provided.', 'patchstack') |
| 45 |
] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
// Since we have the keys combined into one now, split them up here. |
| 50 |
$split = explode('-', $_POST['key']); |
| 51 |
$secretkey = trim($split[0]); |
| 52 |
$clientid = trim($split[1]); |
| 53 |
|
| 54 |
// Test the new keys. |
| 55 |
update_option( 'patchstack_api_token', '' ); |
| 56 |
$results = $this->plugin->activation->alter_license( wp_filter_nohtml_kses( $clientid ), wp_filter_nohtml_kses( $secretkey ), 'activate' ); |
| 57 |
if ( $results ) { |
| 58 |
$response = $this->plugin->api->update_license_status(); |
| 59 |
$results['response'] = $response; |
| 60 |
wp_send_json( $results ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Attempt to auto activate the license after a plugin activation, if no current license exists. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function auto_activate() { |
| 70 |
$secretToken = get_option( 'patchstack_activation_secret', '' ); |
| 71 |
|
| 72 |
// Only continue if we have a secret token. |
| 73 |
$autoActivated = false; |
| 74 |
if ( ! empty( $secretToken ) ) { |
| 75 |
$autoActivated = $this->plugin->api->send_secret_token( $secretToken ); |
| 76 |
} |
| 77 |
|
| 78 |
wp_send_json( [ |
| 79 |
'result' => $autoActivated || get_option( 'patchstack_clientid', false ) != false ? 'success' : 'error' |
| 80 |
] ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get the current license activation status. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function activation_status() { |
| 89 |
wp_send_json( [ |
| 90 |
'activated' => get_option( 'patchstack_clientid', false ) != false |
| 91 |
] ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Run a connection check on demand and return the new state for the settings card row. |
| 96 |
* Wraps P_Api::update_license_status() so the existing 422-handling happens for free; a |
| 97 |
* successful request stamps patchstack_last_sync, which get_last_sync_time() reads back. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function check_connection() { |
| 102 |
$this->plugin->api->update_license_status(); |
| 103 |
|
| 104 |
$timestamp = $this->get_last_sync_time(); |
| 105 |
|
| 106 |
wp_send_json( |
| 107 |
[ |
| 108 |
'connected' => $this->is_connected(), |
| 109 |
'timestamp' => $timestamp, |
| 110 |
'label' => $this->format_relative_time( $timestamp ), |
| 111 |
] |
| 112 |
); |
| 113 |
} |
| 114 |
} |
| 115 |
|