Skip to content

Commit

Permalink
Plugin: Reinit updated plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubboucek committed May 26, 2021
1 parent 18e4dd3 commit 046413e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
55 changes: 37 additions & 18 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -111,7 +111,7 @@ public function get_settings() {
public function get_access() {
return $this->access;
}

public function get_post_types() {
$args = [
'public' => true
Expand All @@ -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 '<h3>' . __( 'Plugin activation failed. Please contact your provider and ask to install PHP extensions: cUrl and Mcrypt.',
'simpleshop-cz' ) . '</h3>';

//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;
}
Expand All @@ -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() );
}
}
4 changes: 2 additions & 2 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 046413e

Please sign in to comment.