diff --git a/README.md b/README.md index 8404f73..04579a7 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,6 @@ add_action('init', function(){ 'menu_slug' => $client->slug . '-manage-license', 'icon_url' => '', 'position' => null, - 'parent_slug' => '', 'activated_redirect' => admin_url( 'admin.php?page=my-plugin-page' ), // should you want to redirect on activation of license. 'deactivated_redirect' => admin_url( 'admin.php?page=my-plugin-deactivation-page' ), // should you want to redirect on detactivation of license. ] @@ -109,8 +108,6 @@ add_action('init', function(){ }); ``` -Make sure you call this function directly, never use any action hook to call this function. - > For plugins example code that needs to be used on your main plugin file. > For themes example code that needs to be used on your themes `functions.php` file. @@ -148,9 +145,9 @@ $client->set_textdomain( 'your-project-textdomain' ); ``` ## Example Plugin -[surecart-plugin-license.zip](https://d.pr/f/6ZyfAS) +[surecart-plugin-license.zip](https://d.pr/f/6MPcya) ## Example Theme -[surecart-theme-license.zip](https://d.pr/f/RK8NFS) +[surecart-theme-license.zip](https://d.pr/f/mYc39R) For full setup and functionality details, see the official documentation: [Licensing Setup and Functionality](https://surecart.com/docs/licensing-setup-and-functionality/). diff --git a/src/Client.php b/src/Client.php index fd2ad0b..935508a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,7 +12,7 @@ class Client { * * @var string */ - public $version = '1.2.1'; + public $version = '1.2.2'; /** * Name of the plugin diff --git a/src/License.php b/src/License.php index 6a69214..b6c6144 100644 --- a/src/License.php +++ b/src/License.php @@ -57,6 +57,9 @@ public function retrieve( $license_key ) { * @throws \Exception If something goes wrong. */ public function activate( $key = '' ) { + $settings = $this->client->settings(); + $previous = $settings->get_options(); // restored if this attempt fails. + $activation_id = ''; try { // validate the license and store it. $license = $this->validate( $key, true ); @@ -65,21 +68,30 @@ public function activate( $key = '' ) { if ( is_wp_error( $activation ) ) { throw new \Exception( $activation->get_error_message() ); } - $this->client->settings()->activation_id = $activation->id; + $activation_id = $activation->id; + $settings->activation_id = $activation_id; // validate the release. $this->validate_release(); } catch ( \Exception $e ) { - // undo activation. - $activation = $this->client->activation()->get(); - if ( $activation ) { - $this->client->activation()->delete(); + // undo the activation this attempt created. + $undone = $activation_id ? $this->client->activation()->delete( $activation_id ) : true; + if ( is_wp_error( $undone ) && 'not_found' !== $undone->get_error_code() ) { + // keep only its id, so Deactivate License can remove it. + $settings->clear_options(); + $settings->activation_id = $activation_id; + $this->client->updater()->clear_cache(); + return new \WP_Error( 'error', $e->getMessage() . ' ' . $this->client->__( 'The activation could not be undone. Click Deactivate License to remove it.' ) ); } - // on error, clear options. - $this->client->settings()->clear_options(); + + // restore the previous options, so a failed or interrupted request never loses a saved key. + $settings->set_options( $previous ); + // return \WP_Error. return new \WP_Error( 'error', $e->getMessage() ); } + $this->client->updater()->clear_cache(); + return true; } @@ -101,12 +113,14 @@ public function deactivate( $activation_id = '' ) { // it has been deleted remotely. if ( 'not_found' === $deactivated->get_error_code() ) { $this->client->settings()->clear_options(); + $this->client->updater()->clear_cache(); return true; } return $deactivated; } $this->client->settings()->clear_options(); + $this->client->updater()->clear_cache(); return true; } @@ -148,6 +162,11 @@ public function get_current_release( $expires_in = 900 ) { * @throws \Exception If the license is not valid. */ public function validate( $key, $store = false ) { + // without a key, the request would hit the licenses collection. + if ( '' === trim( (string) $key ) ) { + throw new \Exception( $this->client->__( 'Please enter a license key.' ) ); + } + // get license. $license = $this->retrieve( sanitize_text_field( $key ) ); if ( is_wp_error( $license ) ) { diff --git a/src/Settings.php b/src/Settings.php index 2d6d04f..5924986 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -165,13 +165,24 @@ public function get_options() { return (array) get_option( $this->option_key, array() ); } + /** + * Replace all options. + * + * @param array $options The options. + * + * @return bool + */ + public function set_options( $options ) { + return update_option( $this->option_key, (array) $options ); + } + /** * Clear out the options. * * @return bool */ public function clear_options() { - return update_option( $this->option_key, array() ); + return $this->set_options( array() ); } /** @@ -222,27 +233,20 @@ public function settings_output() {