Skip to content

Commit

Permalink
Add Support for Premium Plugin Activations
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Oct 14, 2024
1 parent 2f855de commit cffa06c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
18 changes: 15 additions & 3 deletions includes/Services/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ public static function install_premium_plugin( $plugin, $activate ) {
);
}

// Create an instance of PLSUtility for handling license operations
$pls_utility = new PLSUtility();

// Provision a license for the premium plugin
$license_response = PLSUtility::provision_license( $plugin );
$license_response = $pls_utility->provision_license( $plugin );
if ( is_wp_error( $license_response ) ) {
return $license_response;
}
Expand All @@ -186,20 +189,29 @@ public static function install_premium_plugin( $plugin, $activate ) {
return new \WP_Error( 'nfd_installer_error', __( 'Download URL is missing for premium plugin: ', 'wp-module-installer' ) . $plugin );
}

// Attempt to install and/or activate the premium plugin using the provided download URL
// Attempt to install the premium plugin using the provided download URL
$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 );
}

// If activation is requested, activate the license
if ( $activate ) {
$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 new \WP_REST_Response(
array(
'message' => __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $plugin,
'message' => __( 'Successfully provisioned, installed, and activated: ', 'wp-module-installer' ) . $plugin,
),
200
);
}


/**
* Install the plugin from a custom ZIP.
*
Expand Down
39 changes: 38 additions & 1 deletion includes/WPAdmin/Listeners/InstallerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners;

use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;
use NewfoldLabs\WP\Module\PLS\Utilities\PLSUtility;

/**
* Manages all the installer enqueue related functionalities for the module.
Expand All @@ -13,7 +14,11 @@ class InstallerListener {
* Constructor for the Installer class.
*/
public function __construct() {
// Hook to enqueue installer scripts
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_installer_script' ) );

// Hook to listen to premium plugin activation
$this->listen_for_premium_plugin_activation();
}

/**
Expand All @@ -25,7 +30,6 @@ public function enqueue_installer_script() {
$asset_file = NFD_INSTALLER_BUILD_DIR . '/installer.asset.php';

if ( is_readable( $asset_file ) ) {

$asset = include $asset_file;

wp_register_script(
Expand Down Expand Up @@ -58,4 +62,37 @@ public function enqueue_installer_script() {
wp_enqueue_style( 'nfd-installer-enqueue' );
}
}

/**
* Listens for premium plugin activation.
*
* @return void
*/
private function listen_for_premium_plugin_activation() {
$pls_utility = new PLSUtility();

// Retrieve the license data (decrypted) from the option
$license_data_store = $pls_utility->retrieve_license_data();

if ( ! $license_data_store || empty( $license_data_store ) ) {
return;
}

// Loop through each plugin in the license data and hook to its activation using basename
foreach ( $license_data_store as $plugin_slug => $license_data ) {
// Ensure that the basename is present in the license data
if ( isset( $license_data['basename'] ) ) {
$basename = $license_data['basename'];

// Hook into plugin activation using the basename
add_action(
'activate_' . $basename,
function () use ( $plugin_slug, $pls_utility ) {
// Call the activate_license function when this plugin is activated
$pls_utility->activate_license( $plugin_slug );
}
);
}
}
}
}

0 comments on commit cffa06c

Please sign in to comment.