Skip to content

Commit

Permalink
Merge pull request #35 from newfold-labs/add/activation-checks
Browse files Browse the repository at this point in the history
Conditional logic for installing premium plugins based on their status
  • Loading branch information
arunshenoy99 authored Oct 22, 2024
2 parents 8a0f103 + d4df2e0 commit d9ef7e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
44 changes: 41 additions & 3 deletions includes/Services/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,55 @@ public static function install_premium_plugin( $plugin, $provider, $activate ) {

$pls_utility = new PLSUtility();

// Provision a license for the premium plugin
// Provision a license for the premium plugin, this returns basename and download URL
$license_response = $pls_utility->provision_license( $plugin, $provider );
if ( is_wp_error( $license_response ) ) {
return $license_response;
}

// Get the plugin basename from the license response
$plugin_basename = ! empty( $license_response['basename'] ) ? $license_response['basename'] : false;

// Check if the plugin is already installed
if ( $plugin_basename && self::is_plugin_installed( $plugin_basename ) ) {
// Check if the plugin is active
if ( is_plugin_active( $plugin_basename ) ) {
// If plugin is already installed and active, return success
return new \WP_REST_Response(
array(
'message' => __( 'Plugin is already installed and active: ', 'wp-module-installer' ) . $plugin,
),
200
);
}

// Activate the plugin if it's installed but not active
$activate_plugin_response = activate_plugin( $plugin_basename );
if ( is_wp_error( $activate_plugin_response ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to activate the plugin: ', 'wp-module-installer' ) . $plugin );
}

// Activate the license
$activation_response = $pls_utility->activate_license( $plugin );
if ( is_wp_error( $activation_response ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to activate the license for the premium plugin: ', 'wp-module-installer' ) . $plugin );
}

// Return success response
return new \WP_REST_Response(
array(
'message' => __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $plugin,
),
200
);
}

// Check if the download URL is present in the license response
if ( empty( $license_response['downloadUrl'] ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Download URL is missing for premium plugin: ', 'wp-module-installer' ) . $plugin );
}

// Attempt to install the premium plugin using the provided download URL
// Plugin is not installed, proceed with installation
$install_status = self::install_from_zip( $license_response['downloadUrl'], $activate );
if ( is_wp_error( $install_status ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to install or activate the premium plugin: ', 'wp-module-installer' ) . $plugin );
Expand All @@ -197,9 +234,10 @@ public static function install_premium_plugin( $plugin, $provider, $activate ) {
}
}

// Return success response
return new \WP_REST_Response(
array(
'message' => __( 'Successfully provisioned: ', 'wp-module-installer' ) . $plugin,
'message' => __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $plugin,
),
200
);
Expand Down
2 changes: 2 additions & 0 deletions src/Installer/components/Modal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const Modal = ( {
},
} );
setPluginStatus( 'completed' );
showModal( false );
window.location.href = redirectUrl;
} catch ( e ) {
setPluginStatus( 'failed' );
Expand All @@ -120,6 +121,7 @@ const Modal = ( {
},
} );
setPluginStatus( 'completed' );
showModal( false );
window.location.href = redirectUrl;
} catch ( e ) {
setPluginStatus( 'failed' );
Expand Down

0 comments on commit d9ef7e3

Please sign in to comment.