Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional logic for installing premium plugins based on their status #35

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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