diff --git a/includes/Services/PluginInstaller.php b/includes/Services/PluginInstaller.php index 2d6318a..6c79715 100644 --- a/includes/Services/PluginInstaller.php +++ b/includes/Services/PluginInstaller.php @@ -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; } @@ -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. * diff --git a/includes/WPAdmin/Listeners/InstallerListener.php b/includes/WPAdmin/Listeners/InstallerListener.php index a369ec5..67835f9 100644 --- a/includes/WPAdmin/Listeners/InstallerListener.php +++ b/includes/WPAdmin/Listeners/InstallerListener.php @@ -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. @@ -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(); } /** @@ -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( @@ -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 ); + } + ); + } + } + } }