From 046413eff8bc594b9346bae82c27cd0fbbecb727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bouc=CC=8Cek?= Date: Fri, 30 Apr 2021 14:57:48 +0200 Subject: [PATCH] Plugin: Reinit updated plugin --- src/Plugin.php | 55 ++++++++++++++++++++++++++++++++---------------- src/Settings.php | 4 ++-- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index f583033..8297016 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -42,7 +42,7 @@ public function __construct( $mainFile ) { $this->secure_key = $this->load_api_key(); $this->email = $this->load_email(); - register_activation_hook( plugin_dir_path( $this->pluginMainFile ), [ $this, 'ssc_activation_hook' ] ); + $this->reactivate_updated_plugin(); } private function init() { @@ -111,7 +111,7 @@ public function get_settings() { public function get_access() { return $this->access; } - + public function get_post_types() { $args = [ 'public' => true @@ -120,22 +120,6 @@ public function get_post_types() { return get_post_types( $args ); } - - public function ssc_activation_hook() { - if ( ! function_exists( 'curl_init' ) || ! function_exists( 'random_bytes' ) ) { - echo '

' . __( 'Plugin activation failed. Please contact your provider and ask to install PHP extensions: cUrl and Mcrypt.', - 'simpleshop-cz' ) . '

'; - - //Adding @ before will prevent XDebug output - @trigger_error( __( 'Plugin activation failed. Please contact your provider and ask to install PHP extensions: cUrl and Mcrypt.', - 'simpleshop-cz' ), E_USER_ERROR ); - } - - // Generate and save the secure key - $key = $this->generate_secure_key(); - $this->save_secure_key( $key ); - } - public function get_plugin_main_file() { return $this->pluginMainFile; } @@ -161,4 +145,39 @@ public function get_api_client( $overrideLogin = null, $overrideApiKey = null ) return $client; } + + /** + * Detects plugin update (compare current and last known plugin version) and re-activate it when version changed + * + * @link https://wordpress.stackexchange.com/a/144873 + */ + public function reactivate_updated_plugin( ) { + $current_version = SIMPLESHOP_PLUGIN_VERSION; + $previous_version = get_option('ssc_plugin_version', null); + + /** @noinspection TypeUnsafeComparisonInspection */ + if( $current_version != $previous_version) { + update_option('ssc_plugin_version', $current_version); + $this->init_plugin_activation(); + } + } + + /** + * Init plugin activation at SimpleShop API + * + * @param VyfakturujAPI|null $api_client + * + * @return array|false|mixed + */ + public function init_plugin_activation( VyfakturujAPI $api_client = null ) { + if($api_client === null && $this->has_credentials()) { + $api_client = $this->get_api_client(); + } + + if($api_client === null) { + return false; + } + + return $api_client->initWPPlugin( site_url() ); + } } diff --git a/src/Settings.php b/src/Settings.php index 89a2c52..8b35670 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -380,8 +380,8 @@ public function settings_notices( $object_id, $updated ) { $vyfakturuj_api = $this->loader->get_api_client( $api_email, $api_key ); try { - $result = $vyfakturuj_api->initWPPlugin( site_url() ); - if ( isset( $result['status'] ) && $result['status'] == 'success' ) { + $result = $this->loader->init_plugin_activation($vyfakturuj_api); + if ( isset( $result['status'] ) && $result['status'] === 'success' ) { update_option( 'ssc_valid_api_keys', 1 ); } else { update_option( 'ssc_valid_api_keys', 0 );