Skip to content

Commit

Permalink
Merge branch 'solutions' into improve/scripts-with-UI
Browse files Browse the repository at this point in the history
* solutions: (31 commits)
  Update build
  Fix free plugin checks
  Update DataAttrListener.php
  Revert "Missed Changes"
  Missed Changes
  Update build
  Add free plugin installation
  Production build
  Fix version numbers
  Fix lint
  Update PLS data attributes
  Bump version
  Ran build
  Clean up code
  Remove blank line
  Simplify logic
  Update code formatting
  Update build
  Prevent the icon from shrinking
  Code formatting and use createInterpolateElement to handle translation and HTML safely
  ...

# Conflicts:
#	build/1.2.0-beta.3/dataAttrListener.asset.php
#	build/1.2.0-beta.3/dataAttrListener.js
#	build/1.2.0-beta.3/installer.asset.php
#	build/1.2.0-beta.3/installer.css
#	build/1.2.0-beta.3/installer.js
#	package-lock.json
#	src/Installer/components/Modal/index.jsx
#	src/Scripts/dataAttrListener.js
  • Loading branch information
circlecube committed Oct 17, 2024
2 parents 6f77937 + 8d513d6 commit d3f08ae
Show file tree
Hide file tree
Showing 17 changed files with 2,539 additions and 797 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20
2 changes: 1 addition & 1 deletion build/1.2.0-beta.3/dataAttrListener.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-dom-ready'), 'version' => 'bc22db3f938d48526ac3');
<?php return array('dependencies' => array('wp-dom-ready'), 'version' => 'd688a7c45dc137ed4ceb');
2 changes: 1 addition & 1 deletion build/1.2.0-beta.3/dataAttrListener.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/1.2.0-beta.3/installer.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '0524aceb308f50d52f01');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '20589e8ef523f5b2ed7b');
2 changes: 1 addition & 1 deletion build/1.2.0-beta.3/installer.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/1.2.0-beta.3/installer.js

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions includes/RestApi/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ public function get_install_plugin_args() {
'type' => 'boolean',
'default' => false,
),
'provider' => array(
'type' => 'string',
'default' => '',
),
);
}

/**
* Get args for the uninstall route.
*
* @return array
*/
/**
* Get args for the uninstall route.
*
* @return array
*/
public function get_uninstall_plugin_args() {
return array(
'plugin' => array(
Expand Down Expand Up @@ -183,10 +187,11 @@ public function install( \WP_REST_Request $request ) {
$queue = $request->get_param( 'queue' );
$priority = $request->get_param( 'priority' );
$premium = $request->get_param( 'premium' );
$provider = $request->get_param( 'provider' );

// Checks if the plugin is premium and uses the corresponding function for it.
if ( true === $premium ) {
return PluginInstaller::install_premium_plugin( $plugin, $activate );
return PluginInstaller::install_premium_plugin( $plugin, $provider, $activate );
}

// Checks if a plugin with the given slug and activation criteria already exists.
Expand Down
35 changes: 20 additions & 15 deletions includes/Services/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,24 @@ public static function install_from_wordpress( $plugin, $activate ) {
* Provisions a license and installs or activates a premium plugin.
*
* @param string $plugin The slug of the premium plugin.
* @param string $provider The provider name for the premium plugin.
* @param boolean $activate Whether to activate the plugin after installation.
*
* @return \WP_Error|\WP_REST_Response
*/
public static function install_premium_plugin( $plugin, $activate ) {
$status_codes = Plugins::get_status_codes();

$premium_status = self::get_plugin_status( $plugin );

// Check if the premium plugin is already installed or active
if ( $status_codes['active'] === $premium_status || $status_codes['installed'] === $premium_status ) {
return new \WP_REST_Response(
array(
'message' => __( 'Premium plugin already installed or active: ', 'wp-module-installer' ) . $plugin,
),
200
public static function install_premium_plugin( $plugin, $provider, $activate ) {
// Ensure plugin and provider are not empty
if ( empty( $plugin ) || empty( $provider ) ) {
return new \WP_Error(
'nfd_installer_error',
__( 'Plugin slug and provider name cannot be empty.', 'wp-module-installer' )
);
}

$pls_utility = new PLSUtility();

// Provision a license for the premium plugin
$license_response = PLSUtility::provision_license( $plugin );
$license_response = $pls_utility->provision_license( $plugin, $provider );
if ( is_wp_error( $license_response ) ) {
return $license_response;
}
Expand All @@ -186,15 +183,23 @@ 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: ', 'wp-module-installer' ) . $plugin,
),
200
);
Expand Down
51 changes: 51 additions & 0 deletions includes/WPAdmin/Listeners/DataAttrListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners;

use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;

/**
* Manages all the data-* listening related functionalities for the module.
*/
class DataAttrListener {
/**
* Constructor for the DataAttrListener class.
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_data_attr_listener' ) );
}

/**
* Enqueues the data-* attribute listener script.
*
* @return void
*/
public function enqueue_data_attr_listener() {
$asset_file = NFD_INSTALLER_BUILD_DIR . '/dataAttrListener.asset.php';

if ( is_readable( $asset_file ) ) {
$asset = include $asset_file;

wp_register_script(
'nfd-installer-data-attr-listener',
NFD_INSTALLER_BUILD_URL . '/dataAttrListener.js',
array_merge( $asset['dependencies'], array() ),
$asset['version'],
true
);

wp_add_inline_script(
'nfd-installer-data-attr-listener',
'var nfdInstaller =' . wp_json_encode(
value: array(
'restUrl' => \get_home_url() . '/index.php?rest_route=',
'pluginInstallHash' => PluginInstaller::rest_get_plugin_install_hash(),
)
) . ';',
'before'
);

wp_enqueue_script( 'nfd-installer-data-attr-listener' );
}
}
}
74 changes: 38 additions & 36 deletions 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,52 +14,22 @@ class InstallerListener {
* Constructor for the Installer class.
*/
public function __construct() {
add_action( 'newfold/installer/enqueue_scripts', array( $this, 'enqueue_installer_scripts' ) );
}
// Hook to enqueue installer scripts
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_installer_script' ) );

/**
* Enqueues all the installer scripts that are required.
*
* @return void
*/
public function enqueue_installer_scripts() {
$this->enqueue_data_attr_listener();
$this->enqueue_installer_react_script();
// Hook to listen to premium plugin activation
$this->listen_for_premium_plugin_activation();
}

/**
* Enqueues the data-* attribute listener script.
* Enqueues the installer script.
*
* @return void
*/
public function enqueue_data_attr_listener() {
$asset_file = NFD_INSTALLER_BUILD_DIR . '/dataAttrListener.asset.php';

if ( is_readable( $asset_file ) ) {
$asset = include $asset_file;

wp_register_script(
'nfd-installer-data-attr-listener',
NFD_INSTALLER_BUILD_URL . '/dataAttrListener.js',
array_merge( $asset['dependencies'], array() ),
$asset['version'],
true
);

wp_enqueue_script( 'nfd-installer-data-attr-listener' );
}
}

/**
* Enqueues the installer react scripts.
*
* @return void
*/
public function enqueue_installer_react_script() {
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 @@ -91,4 +62,35 @@ public function enqueue_installer_react_script() {
wp_enqueue_style( 'nfd-installer-enqueue' );
}
}

/**
* Listens for premium plugin activation using activated_plugin hook.
*
* @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_storage_map();

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

// Hook into activated_plugin action to trigger license activation after plugin activation
add_action(
'activated_plugin',
function ( $plugin, $network_wide ) use ( $pls_utility, $license_data_store ) {
foreach ( $license_data_store as $plugin_slug => $license_data ) {
if ( isset( $license_data['basename'] ) && $license_data['basename'] === $plugin ) {
$pls_utility->activate_license( $plugin_slug );
break;
}
}
},
10,
2
);
}
}
2 changes: 2 additions & 0 deletions includes/WPAdmin/WPAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NewfoldLabs\WP\Module\Installer\WPAdmin;

use NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners\DataAttrListener;
use NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners\InstallerListener;

/**
Expand All @@ -12,6 +13,7 @@ class WPAdmin {
* Constructor for the WPAdmin class.
*/
public function __construct() {
new DataAttrListener();
new InstallerListener();
}
}
20 changes: 15 additions & 5 deletions includes/WPCLI/Handlers/InstallerCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,24 @@ public function upgrade_extended_yith_plugin( $args ) {
/**
* Triggers the installation and activation of a premium plugin.
*
* This command provisions a license, installs the premium plugin, and optionally activates it based on the
* activation parameter passed. It outputs the status of the process.
* This command provisions a license, installs the premium plugin, and optionally activates it
* based on the activation parameter passed. It outputs the status of the process.
*
* ## OPTIONS
*
* <premium_slug>
* : The slug of the premium plugin to be installed.
*
* <provider>
* : The name of the provider for the premium plugin.
*
* [--activate]
* : Optional flag to activate the plugin after installation.
*
* ## EXAMPLES
*
* wp installer install_premium_plugin <premium_slug> --activate
* wp installer install_premium_plugin <premium_slug>
* wp installer install_premium_plugin <premium_slug> <provider> --activate
* wp installer install_premium_plugin <premium_slug> <provider>
*
* @param array $args Arguments passed from the command line. First argument is the plugin slug.
* @param array $assoc_args Associative arguments (like --activate).
Expand All @@ -109,10 +112,17 @@ public function upgrade_extended_yith_plugin( $args ) {
*/
public function install_premium_plugin( $args, $assoc_args ) {
$premium_slug = $args[0];
$provider = $args[1];
$activate = isset( $assoc_args['activate'] );

// Ensure both the plugin slug and provider are not empty
if ( empty( $premium_slug ) || empty( $provider ) ) {
WP_CLI::error( __( 'Both plugin slug and provider name are required.', 'wp-module-installer' ) );
return;
}

// Call the function to provision, install, and (optionally) activate the premium plugin
$status = PluginInstaller::install_premium_plugin( $premium_slug, $activate );
$status = PluginInstaller::install_premium_plugin( $premium_slug, $provider, $activate );

// Handle error or success response
if ( is_wp_error( $status ) ) {
Expand Down
Loading

0 comments on commit d3f08ae

Please sign in to comment.