From a29d7c8f6747a41a6ae346bf865a3616576bc5f1 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Fri, 27 Sep 2024 14:53:05 +0530 Subject: [PATCH 1/4] Solutions: WP CLI commands and premium plugin utlities --- bootstrap.php | 3 +- composer.json | 4 + composer.lock | 303 +++++++++++++++++- includes/Data/Plugins.php | 37 ++- includes/Installer.php | 3 +- includes/Permissions.php | 2 - includes/RestApi/PluginsController.php | 2 - includes/RestApi/ThemeInstallerController.php | 3 - includes/Services/PluginInstaller.php | 79 ++++- includes/Services/PluginUninstaller.php | 1 - includes/Services/PluginUpgrader.php | 151 +++++++++ includes/Services/ThemeInstaller.php | 1 - .../TaskManagers/ThemeInstallTaskManager.php | 1 - includes/Tasks/PluginActivationTask.php | 2 +- includes/Tasks/PluginDeactivationTask.php | 2 +- includes/Tasks/PluginInstallTask.php | 2 +- includes/Tasks/PluginUninstallTask.php | 3 +- includes/Tasks/ThemeInstallTask.php | 3 +- .../Handlers/InstallerCommandHandler.php | 124 +++++++ includes/WPCLI/WPCLI.php | 22 ++ 20 files changed, 718 insertions(+), 30 deletions(-) create mode 100644 includes/Services/PluginUpgrader.php create mode 100644 includes/WPCLI/Handlers/InstallerCommandHandler.php create mode 100644 includes/WPCLI/WPCLI.php diff --git a/bootstrap.php b/bootstrap.php index a0926b2..8e3d983 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -13,7 +13,7 @@ function () { register( array( 'name' => 'installer', - 'label' => __( 'Installer', 'newfold-installer-module' ), + 'label' => __( 'Installer', 'wp-module-installer' ), 'callback' => function ( Container $container ) { if ( ! defined( 'NFD_INSTALLER_VERSION' ) ) { @@ -26,7 +26,6 @@ function () { 'isHidden' => true, ) ); - } ); diff --git a/composer.json b/composer.json index 23a4819..ab56dc6 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,10 @@ ] } }, + "require": { + "wp-cli/wp-cli": "^2.11", + "@newfold-labs/wp-module-pls": "^1.0" + }, "require-dev": { "newfold-labs/wp-php-standards": "^1.2" }, diff --git a/composer.lock b/composer.lock index ec6791f..7b220e4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,307 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c02f600861634c55bc7df8bb1be0b24b", - "packages": [], + "content-hash": "fd04132052480817c11bedd4fb96593a", + "packages": [ + { + "name": "mustache/mustache", + "version": "v2.14.2", + "source": { + "type": "git", + "url": "https://github.com/bobthecow/mustache.php.git", + "reference": "e62b7c3849d22ec55f3ec425507bf7968193a6cb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/bobthecow/mustache.php/zipball/e62b7c3849d22ec55f3ec425507bf7968193a6cb", + "reference": "e62b7c3849d22ec55f3ec425507bf7968193a6cb", + "shasum": "" + }, + "require": { + "php": ">=5.2.4" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~1.11", + "phpunit/phpunit": "~3.7|~4.0|~5.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "Mustache": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Justin Hileman", + "email": "justin@justinhileman.info", + "homepage": "http://justinhileman.com" + } + ], + "description": "A Mustache implementation in PHP.", + "homepage": "https://github.com/bobthecow/mustache.php", + "keywords": [ + "mustache", + "templating" + ], + "support": { + "issues": "https://github.com/bobthecow/mustache.php/issues", + "source": "https://github.com/bobthecow/mustache.php/tree/v2.14.2" + }, + "time": "2022-08-23T13:07:01+00:00" + }, + { + "name": "symfony/finder", + "version": "v7.1.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "d95bbf319f7d052082fb7af147e0f835a695e823" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/d95bbf319f7d052082fb7af147e0f835a695e823", + "reference": "d95bbf319f7d052082fb7af147e0f835a695e823", + "shasum": "" + }, + "require": { + "php": ">=8.2" + }, + "require-dev": { + "symfony/filesystem": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Finds files and directories via an intuitive fluent interface", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/finder/tree/v7.1.4" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-08-13T14:28:19+00:00" + }, + { + "name": "wp-cli/mustangostang-spyc", + "version": "0.6.3", + "source": { + "type": "git", + "url": "https://github.com/wp-cli/spyc.git", + "reference": "6aa0b4da69ce9e9a2c8402dab8d43cf32c581cc7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wp-cli/spyc/zipball/6aa0b4da69ce9e9a2c8402dab8d43cf32c581cc7", + "reference": "6aa0b4da69ce9e9a2c8402dab8d43cf32c581cc7", + "shasum": "" + }, + "require": { + "php": ">=5.3.1" + }, + "require-dev": { + "phpunit/phpunit": "4.3.*@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.5.x-dev" + } + }, + "autoload": { + "files": [ + "includes/functions.php" + ], + "psr-4": { + "Mustangostang\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "mustangostang", + "email": "vlad.andersen@gmail.com" + } + ], + "description": "A simple YAML loader/dumper class for PHP (WP-CLI fork)", + "homepage": "https://github.com/mustangostang/spyc/", + "support": { + "source": "https://github.com/wp-cli/spyc/tree/autoload" + }, + "time": "2017-04-25T11:26:20+00:00" + }, + { + "name": "wp-cli/php-cli-tools", + "version": "v0.11.22", + "source": { + "type": "git", + "url": "https://github.com/wp-cli/php-cli-tools.git", + "reference": "a6bb94664ca36d0962f9c2ff25591c315a550c51" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wp-cli/php-cli-tools/zipball/a6bb94664ca36d0962f9c2ff25591c315a550c51", + "reference": "a6bb94664ca36d0962f9c2ff25591c315a550c51", + "shasum": "" + }, + "require": { + "php": ">= 5.3.0" + }, + "require-dev": { + "roave/security-advisories": "dev-latest", + "wp-cli/wp-cli-tests": "^4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.11.x-dev" + } + }, + "autoload": { + "files": [ + "lib/cli/cli.php" + ], + "psr-0": { + "cli": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Daniel Bachhuber", + "email": "daniel@handbuilt.co", + "role": "Maintainer" + }, + { + "name": "James Logsdon", + "email": "jlogsdon@php.net", + "role": "Developer" + } + ], + "description": "Console utilities for PHP", + "homepage": "http://github.com/wp-cli/php-cli-tools", + "keywords": [ + "cli", + "console" + ], + "support": { + "issues": "https://github.com/wp-cli/php-cli-tools/issues", + "source": "https://github.com/wp-cli/php-cli-tools/tree/v0.11.22" + }, + "time": "2023-12-03T19:25:05+00:00" + }, + { + "name": "wp-cli/wp-cli", + "version": "v2.11.0", + "source": { + "type": "git", + "url": "https://github.com/wp-cli/wp-cli.git", + "reference": "53f0df112901fcf95099d0f501912a209429b6a9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wp-cli/wp-cli/zipball/53f0df112901fcf95099d0f501912a209429b6a9", + "reference": "53f0df112901fcf95099d0f501912a209429b6a9", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "mustache/mustache": "^2.14.1", + "php": "^5.6 || ^7.0 || ^8.0", + "symfony/finder": ">2.7", + "wp-cli/mustangostang-spyc": "^0.6.3", + "wp-cli/php-cli-tools": "~0.11.2" + }, + "require-dev": { + "roave/security-advisories": "dev-latest", + "wp-cli/db-command": "^1.3 || ^2", + "wp-cli/entity-command": "^1.2 || ^2", + "wp-cli/extension-command": "^1.1 || ^2", + "wp-cli/package-command": "^1 || ^2", + "wp-cli/wp-cli-tests": "^4.0.1" + }, + "suggest": { + "ext-readline": "Include for a better --prompt implementation", + "ext-zip": "Needed to support extraction of ZIP archives when doing downloads or updates" + }, + "bin": [ + "bin/wp", + "bin/wp.bat" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.11.x-dev" + } + }, + "autoload": { + "psr-0": { + "WP_CLI\\": "php/" + }, + "classmap": [ + "php/class-wp-cli.php", + "php/class-wp-cli-command.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "WP-CLI framework", + "homepage": "https://wp-cli.org", + "keywords": [ + "cli", + "wordpress" + ], + "support": { + "docs": "https://make.wordpress.org/cli/handbook/", + "issues": "https://github.com/wp-cli/wp-cli/issues", + "source": "https://github.com/wp-cli/wp-cli" + }, + "time": "2024-08-08T03:04:55+00:00" + } + ], "packages-dev": [ { "name": "dealerdirect/phpcodesniffer-composer-installer", diff --git a/includes/Data/Plugins.php b/includes/Data/Plugins.php index 0117c80..92d72bc 100644 --- a/includes/Data/Plugins.php +++ b/includes/Data/Plugins.php @@ -138,6 +138,21 @@ final class Plugins { 'downloads.wordpress.org' => true, 'nonapproveddomain.com' => null, ); + /** + * Holds the possible status codes for a plugin. + * + * @var array $status_codes Possible plugin status codes including: + * 'unknown' - The plugin's status cannot be determined. + * 'installed' - The plugin is installed but not activated. + * 'active' - The plugin is installed and active. + * 'not_installed' - The plugin is not installed on the system. + */ + protected static $status_codes = array( + 'unknown' => 'unknown', + 'installed' => 'installed', + 'active' => 'active', + 'not_installed' => 'not_installed', + ); /** * Returns a list of whitelisted WordPress Plugin slugs. @@ -166,6 +181,15 @@ public static function get_domains() { return self::$domains; } + /** + * Retrieves the array of plugin status codes. + * + * @return array + */ + public static function get_status_codes() { + return self::$status_codes; + } + /** * Use this return value for a faster search of slug/url/domain. * @@ -173,10 +197,11 @@ public static function get_domains() { */ public static function get() { return array( - 'wp_slugs' => self::$wp_slugs, - 'nfd_slugs' => self::$nfd_slugs, - 'urls' => self::$urls, - 'domains' => self::$domains, + 'wp_slugs' => self::$wp_slugs, + 'nfd_slugs' => self::$nfd_slugs, + 'urls' => self::$urls, + 'domains' => self::$domains, + 'status_codes' => self::$status_codes, ); } @@ -222,7 +247,7 @@ private static function check_approved( $value ) { * @return void */ public static function wc_prevent_redirect_on_activation() { - \delete_transient( '_wc_activation_redirect' ); + delete_transient( '_wc_activation_redirect' ); } /** @@ -240,7 +265,7 @@ public static function toggle_jetpack_module( $module, $active = true ) { ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( array( $module => $active ) ) ); - $response = \rest_do_request( $request ); + $response = rest_do_request( $request ); if ( 200 !== $response->status ) { return false; diff --git a/includes/Installer.php b/includes/Installer.php index adaf82d..8feee49 100644 --- a/includes/Installer.php +++ b/includes/Installer.php @@ -4,6 +4,7 @@ use NewfoldLabs\WP\Module\Installer\RestApi\RestApi; use NewfoldLabs\WP\Module\Installer\TaskManagers\TaskManager; +use NewfoldLabs\WP\Module\Installer\WPCLI\WPCLI; use NewfoldLabs\WP\ModuleLoader\Container; /** @@ -33,6 +34,6 @@ public function __construct( Container $container ) { new TaskManager(); + new WPCLI(); } - } diff --git a/includes/Permissions.php b/includes/Permissions.php index 2d92661..ffb4c65 100644 --- a/includes/Permissions.php +++ b/includes/Permissions.php @@ -18,6 +18,4 @@ final class Permissions { public static function rest_is_authorized_admin() { return \is_user_logged_in() && \current_user_can( self::ADMIN ); } - } - diff --git a/includes/RestApi/PluginsController.php b/includes/RestApi/PluginsController.php index 3d0e444..26e93aa 100644 --- a/includes/RestApi/PluginsController.php +++ b/includes/RestApi/PluginsController.php @@ -310,7 +310,5 @@ public function get_status( \WP_REST_Request $request ) { ), 200 ); - } } - diff --git a/includes/RestApi/ThemeInstallerController.php b/includes/RestApi/ThemeInstallerController.php index c72cd89..c48e622 100644 --- a/includes/RestApi/ThemeInstallerController.php +++ b/includes/RestApi/ThemeInstallerController.php @@ -67,7 +67,6 @@ public function register_routes() { ), ) ); - } /** @@ -230,7 +229,6 @@ public function get_status( \WP_REST_Request $request ) { ), 200 ); - } /** @@ -261,6 +259,5 @@ public function expedite( \WP_REST_Request $request ) { array(), 400 ); - } } diff --git a/includes/Services/PluginInstaller.php b/includes/Services/PluginInstaller.php index ad8cf69..2d6318a 100644 --- a/includes/Services/PluginInstaller.php +++ b/includes/Services/PluginInstaller.php @@ -3,6 +3,7 @@ use NewfoldLabs\WP\Module\Installer\Data\Plugins; use NewfoldLabs\WP\Module\Installer\Permissions; +use NewfoldLabs\WP\Module\PLS\Utilities\PLSUtility; /** * Class PluginInstaller @@ -151,6 +152,54 @@ 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 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 + ); + } + + // Provision a license for the premium plugin + $license_response = PLSUtility::provision_license( $plugin ); + if ( is_wp_error( $license_response ) ) { + return $license_response; + } + + // 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 and/or activate 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 ); + } + + return new \WP_REST_Response( + array( + 'message' => __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $plugin, + ), + 200 + ); + } + /** * Install the plugin from a custom ZIP. * @@ -229,7 +278,7 @@ public static function install_from_zip( $url, $activate, $language_packs = arra if ( ! empty( $language_packs ) ) { $language_packs = array_map( - static function( $item ) { + static function ( $item ) { return (object) $item; }, $language_packs @@ -237,7 +286,7 @@ static function( $item ) { $language_packs = array_filter( $language_packs, - static function( $pack ) use ( $installed_locales ) { + static function ( $pack ) use ( $installed_locales ) { return in_array( $pack->language, $installed_locales, true ); } ); @@ -490,4 +539,30 @@ public static function rest_verify_plugin_install_hash( $hash ) { return self::rest_get_plugin_install_hash() === $hash; } + /** + * Retrieves the current status of a plugin. + * + * @param string $plugin The slug or identifier of the plugin. + * + * @return string + */ + public static function get_plugin_status( $plugin ) { + $plugin_type = self::get_plugin_type( $plugin ); + $plugin_path = self::get_plugin_path( $plugin, $plugin_type ); + $plugin_status_codes = Plugins::get_status_codes(); + + if ( ! $plugin_path ) { + return $plugin_status_codes['unknown']; + } + + if ( is_plugin_active( $plugin_path ) ) { + return $plugin_status_codes['active']; + } + + if ( self::is_plugin_installed( $plugin_path ) ) { + return $plugin_status_codes['installed']; + } + + return $plugin_status_codes['not_installed']; + } } diff --git a/includes/Services/PluginUninstaller.php b/includes/Services/PluginUninstaller.php index 6b10c4d..a800a94 100644 --- a/includes/Services/PluginUninstaller.php +++ b/includes/Services/PluginUninstaller.php @@ -149,5 +149,4 @@ public static function delete_plugin( $plugin_path ) { return true; } - } diff --git a/includes/Services/PluginUpgrader.php b/includes/Services/PluginUpgrader.php new file mode 100644 index 0000000..9acae1c --- /dev/null +++ b/includes/Services/PluginUpgrader.php @@ -0,0 +1,151 @@ + $extended_slug ) { + // Call the single plugin upgrade function and get the status + $upgrade_status = self::upgrade_single_extended_plugin( $extended_slug ); + + // Store the result of the upgrade in the array + $yith_plugins_upgrade_status[ $extended_slug ] = $upgrade_status; + } + + // Return the array containing the upgrade status for each plugin + return $yith_plugins_upgrade_status; + } + + /** + * Upgrades a single extended YITH plugin to its corresponding premium version. + * + * @param string $extended_slug The slug of the extended plugin. + * + * @return array Contains the status of the upgrade process with 'upgraded' and 'message' keys. + */ + public static function upgrade_single_extended_plugin( $extended_slug ) { + // Define the list of extended YITH plugins and their corresponding premium versions. + // TODO: Replace the dummy entitlement slug 'nfd_slug_yith_paypal_payments_for_woocommerce' with actual entitlement slugs. + $yith_plugins_to_upgrade = array( + 'yith-woocommerce-ajax-search' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_ajax_product_filter' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_wishlist' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_booking' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_gift_cards' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_customize_myaccount_page' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + ); + + // Initialize status array for the plugin upgrade process + $upgrade_status = array( + 'upgraded' => false, + 'message' => '', + ); + + // Get plugin status codes for comparison + $status_codes = Plugins::get_status_codes(); + + // Check if the extended plugin exists in the list + if ( ! isset( $yith_plugins_to_upgrade[ $extended_slug ] ) ) { + $upgrade_status['message'] = 'Invalid extended plugin slug provided.'; + return $upgrade_status; + } + + // Get the corresponding premium plugin slug + $premium_slug = $yith_plugins_to_upgrade[ $extended_slug ]; + + // Get the status of the extended plugin + $extended_status = PluginInstaller::get_plugin_status( $extended_slug ); + + // Check if the extended plugin's status is unknown or not installed + if ( $status_codes['unknown'] === $extended_status ) { + $upgrade_status['message'] = 'Could not determine the status of the extended plugin.'; + return $upgrade_status; + } + + if ( $status_codes['not_installed'] === $extended_status ) { + $upgrade_status['message'] = 'Extended plugin is not installed.'; + return $upgrade_status; + } + + // Get the status of the premium version of the plugin + $premium_status = PluginInstaller::get_plugin_status( $premium_slug ); + + // Skip if the premium plugin is already active or installed + if ( $status_codes['active'] === $premium_status || $status_codes['installed'] === $premium_status ) { + $upgrade_status['message'] = 'Premium plugin already installed or active: ' . $premium_slug; + return $upgrade_status; + } + + // Provision a license for the premium version of the plugin + $license_response = PLSUtility::provision_license( $premium_slug ); + if ( is_wp_error( $license_response ) ) { + $upgrade_status['message'] = 'Failed to provision license for: ' . $premium_slug; + return $upgrade_status; + } + + // Check if the download URL is present in the license response + if ( empty( $license_response['downloadUrl'] ) ) { + $upgrade_status['message'] = 'Download URL is missing for premium plugin: ' . $premium_slug; + return $upgrade_status; + } + + // Check if the premium plugin should be activated after installation + $should_activate = ( $status_codes['active'] === $extended_status ); + + // Deactivate the extended version of the plugin if the premium plugin needs to be activated + if ( $should_activate ) { + PluginInstaller::deactivate( $extended_slug ); + } + + // Attempt to install the premium plugin using the provided download URL, and activate it if needed + $premium_install_status = PluginInstaller::install_from_zip( $license_response['downloadUrl'], $should_activate ); + if ( is_wp_error( $premium_install_status ) ) { + $upgrade_status['message'] = 'Failed to install the premium plugin: ' . $premium_slug; + if ( $should_activate ) { + PluginInstaller::activate( $extended_slug ); + } + return $upgrade_status; + } + + // Mark as successfully upgraded + $upgrade_status['upgraded'] = true; + $upgrade_status['message'] = 'Successfully provisioned and installed: ' . $premium_slug; + + // Attempt to uninstall the extended version of the plugin + $uninstall_status = PluginUninstaller::uninstall( $extended_slug ); + if ( is_wp_error( $uninstall_status ) ) { + $upgrade_status['message'] = 'Successfully installed the premium plugin, but there was an error uninstalling the extended plugin. Manual cleanup may be required.'; + return $upgrade_status; + } + + // Return the status of the upgrade process + return $upgrade_status; + } +} diff --git a/includes/Services/ThemeInstaller.php b/includes/Services/ThemeInstaller.php index 3f8e131..30c779f 100644 --- a/includes/Services/ThemeInstaller.php +++ b/includes/Services/ThemeInstaller.php @@ -112,7 +112,6 @@ public static function install_from_zip( $url, $activate, $stylesheet ) { array(), 201 ); - } /** diff --git a/includes/TaskManagers/ThemeInstallTaskManager.php b/includes/TaskManagers/ThemeInstallTaskManager.php index 7142056..4dee92c 100644 --- a/includes/TaskManagers/ThemeInstallTaskManager.php +++ b/includes/TaskManagers/ThemeInstallTaskManager.php @@ -118,7 +118,6 @@ public static function expedite( $theme_slug ) { \update_option( Options::get_option_name( self::$queue_name ), $themes ); return true; - } /** diff --git a/includes/Tasks/PluginActivationTask.php b/includes/Tasks/PluginActivationTask.php index b5665c1..918dfcb 100644 --- a/includes/Tasks/PluginActivationTask.php +++ b/includes/Tasks/PluginActivationTask.php @@ -75,7 +75,7 @@ public function get_retries() { * @return void */ public function increment_retries() { - $this->retries++; + ++$this->retries; } /** diff --git a/includes/Tasks/PluginDeactivationTask.php b/includes/Tasks/PluginDeactivationTask.php index f4b9fdd..1463795 100644 --- a/includes/Tasks/PluginDeactivationTask.php +++ b/includes/Tasks/PluginDeactivationTask.php @@ -75,7 +75,7 @@ public function get_retries() { * @return void */ public function increment_retries() { - $this->retries++; + ++$this->retries; } /** diff --git a/includes/Tasks/PluginInstallTask.php b/includes/Tasks/PluginInstallTask.php index 88e50e5..7fd82ec 100644 --- a/includes/Tasks/PluginInstallTask.php +++ b/includes/Tasks/PluginInstallTask.php @@ -93,7 +93,7 @@ public function get_retries() { * @return void */ public function increment_retries() { - $this->retries++; + ++$this->retries; } /** diff --git a/includes/Tasks/PluginUninstallTask.php b/includes/Tasks/PluginUninstallTask.php index ea65ecb..3f45e9e 100644 --- a/includes/Tasks/PluginUninstallTask.php +++ b/includes/Tasks/PluginUninstallTask.php @@ -75,7 +75,7 @@ public function get_retries() { * @return void */ public function increment_retries() { - $this->retries++; + ++$this->retries; } /** @@ -99,5 +99,4 @@ public function to_array() { 'retries' => $this->retries, ); } - } diff --git a/includes/Tasks/ThemeInstallTask.php b/includes/Tasks/ThemeInstallTask.php index 0bc4f15..57a6b7d 100644 --- a/includes/Tasks/ThemeInstallTask.php +++ b/includes/Tasks/ThemeInstallTask.php @@ -93,7 +93,7 @@ public function get_retries() { * @return void */ public function increment_retries() { - $this->retries++; + ++$this->retries; } /** @@ -121,5 +121,4 @@ public function to_array() { 'retries' => $this->retries, ); } - } diff --git a/includes/WPCLI/Handlers/InstallerCommandHandler.php b/includes/WPCLI/Handlers/InstallerCommandHandler.php new file mode 100644 index 0000000..1b78c58 --- /dev/null +++ b/includes/WPCLI/Handlers/InstallerCommandHandler.php @@ -0,0 +1,124 @@ + $status_info ) { + if ( ! $status_info['upgraded'] ) { + $all_upgrades_successful = false; + } + + $status_json = wp_json_encode( + array( + 'slug' => $plugin_slug, + 'status' => $status_info['upgraded'], + 'message' => $status_info['message'], + ) + ); + + WP_CLI::log( $status_json ); + } + + if ( $all_upgrades_successful ) { + WP_CLI::success( 'YITH plugin upgrade process completed successfully.' ); + } else { + WP_CLI::error( 'YITH plugin upgrade process completed, but some upgrades failed. Please check the logs.' ); + } + } + + /** + * Triggers the upgrade of a single extended YITH plugin. + * + * This command upgrades a specific YITH plugin from its extended version + * to its corresponding premium version and outputs the result in JSON format. + * + * ## EXAMPLES + * + * wp installer upgrade_single_extended_yith_plugin + * + * @param array $args Arguments passed from the command line. First argument is the plugin slug. + * + * @return void + */ + public function upgrade_single_yith_plugin( $args ) { + $plugin_slug = $args[0]; + + $status_info = PluginUpgrader::upgrade_single_extended_plugin( $plugin_slug ); + $status_json = wp_json_encode( + array( + 'slug' => $plugin_slug, + 'status' => $status_info['upgraded'], + 'message' => $status_info['message'], + ) + ); + WP_CLI::log( $status_json ); + + if ( $status_info['upgraded'] ) { + WP_CLI::success( 'Plugin upgrade completed successfully.' ); + } else { + WP_CLI::error( 'Plugin upgrade failed. Please check the logs for more details.' ); + } + } + + /** + * 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. + * + * ## OPTIONS + * + * + * : The slug of the premium plugin to be installed. + * + * [--activate] + * : Optional flag to activate the plugin after installation. + * + * ## EXAMPLES + * + * wp installer install_premium_plugin --activate + * wp installer install_premium_plugin + * + * @param array $args Arguments passed from the command line. First argument is the plugin slug. + * @param array $assoc_args Associative arguments (like --activate). + * + * @return void + */ + public function install_premium_plugin( $args, $assoc_args ) { + $premium_slug = $args[0]; + $activate = isset( $assoc_args['activate'] ); + + // Call the function to provision, install, and (optionally) activate the premium plugin + $status = PluginInstaller::install_premium_plugin( $premium_slug, $activate ); + + // Handle error or success response + if ( is_wp_error( $status ) ) { + WP_CLI::error( $status->get_error_message() ); + } else { + WP_CLI::success( $status->get_data()['message'] ); + } + } +} diff --git a/includes/WPCLI/WPCLI.php b/includes/WPCLI/WPCLI.php new file mode 100644 index 0000000..e9a8361 --- /dev/null +++ b/includes/WPCLI/WPCLI.php @@ -0,0 +1,22 @@ + Date: Fri, 27 Sep 2024 15:13:12 +0530 Subject: [PATCH 2/4] Update function names --- includes/Services/PluginUpgrader.php | 9 ++------- includes/WPCLI/Handlers/InstallerCommandHandler.php | 6 +++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/includes/Services/PluginUpgrader.php b/includes/Services/PluginUpgrader.php index 9acae1c..fbd0999 100644 --- a/includes/Services/PluginUpgrader.php +++ b/includes/Services/PluginUpgrader.php @@ -30,16 +30,11 @@ public static function upgrade_extended_yith_plugins() { // Array to store the status of each plugin's upgrade process $yith_plugins_upgrade_status = array(); - // Loop through each YITH plugin and use the upgrade_single_extended_plugin function for the upgrade foreach ( $yith_plugins_to_upgrade as $index => $extended_slug ) { - // Call the single plugin upgrade function and get the status - $upgrade_status = self::upgrade_single_extended_plugin( $extended_slug ); - - // Store the result of the upgrade in the array + $upgrade_status = self::upgrade_extended_yith_plugin( $extended_slug ); $yith_plugins_upgrade_status[ $extended_slug ] = $upgrade_status; } - // Return the array containing the upgrade status for each plugin return $yith_plugins_upgrade_status; } @@ -50,7 +45,7 @@ public static function upgrade_extended_yith_plugins() { * * @return array Contains the status of the upgrade process with 'upgraded' and 'message' keys. */ - public static function upgrade_single_extended_plugin( $extended_slug ) { + public static function upgrade_extended_yith_plugin( $extended_slug ) { // Define the list of extended YITH plugins and their corresponding premium versions. // TODO: Replace the dummy entitlement slug 'nfd_slug_yith_paypal_payments_for_woocommerce' with actual entitlement slugs. $yith_plugins_to_upgrade = array( diff --git a/includes/WPCLI/Handlers/InstallerCommandHandler.php b/includes/WPCLI/Handlers/InstallerCommandHandler.php index 1b78c58..7ddc5d5 100644 --- a/includes/WPCLI/Handlers/InstallerCommandHandler.php +++ b/includes/WPCLI/Handlers/InstallerCommandHandler.php @@ -57,16 +57,16 @@ public function auto_upgrade_extended_yith_plugins() { * * ## EXAMPLES * - * wp installer upgrade_single_extended_yith_plugin + * wp installer upgrade_extended_yith_plugin * * @param array $args Arguments passed from the command line. First argument is the plugin slug. * * @return void */ - public function upgrade_single_yith_plugin( $args ) { + public function upgrade_extended_yith_plugin( $args ) { $plugin_slug = $args[0]; - $status_info = PluginUpgrader::upgrade_single_extended_plugin( $plugin_slug ); + $status_info = PluginUpgrader::upgrade_extended_yith_plugin( $plugin_slug ); $status_json = wp_json_encode( array( 'slug' => $plugin_slug, From 8e94d33af707686e6503b8cfd7c356231c0721cd Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Mon, 7 Oct 2024 15:30:37 +0530 Subject: [PATCH 3/4] Add i18n support for status messages --- includes/Services/PluginUpgrader.php | 18 +++++++++--------- .../WPCLI/Handlers/InstallerCommandHandler.php | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/Services/PluginUpgrader.php b/includes/Services/PluginUpgrader.php index fbd0999..b8ee133 100644 --- a/includes/Services/PluginUpgrader.php +++ b/includes/Services/PluginUpgrader.php @@ -68,7 +68,7 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) { // Check if the extended plugin exists in the list if ( ! isset( $yith_plugins_to_upgrade[ $extended_slug ] ) ) { - $upgrade_status['message'] = 'Invalid extended plugin slug provided.'; + $upgrade_status['message'] = __( 'Invalid extended plugin slug provided.', 'wp-module-installer' ); return $upgrade_status; } @@ -80,12 +80,12 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) { // Check if the extended plugin's status is unknown or not installed if ( $status_codes['unknown'] === $extended_status ) { - $upgrade_status['message'] = 'Could not determine the status of the extended plugin.'; + $upgrade_status['message'] = __( 'Could not determine the status of the extended plugin.', 'wp-module-installer' ); return $upgrade_status; } if ( $status_codes['not_installed'] === $extended_status ) { - $upgrade_status['message'] = 'Extended plugin is not installed.'; + $upgrade_status['message'] = __( 'Extended plugin is not installed.', 'wp-module-installer' ); return $upgrade_status; } @@ -94,20 +94,20 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) { // Skip if the premium plugin is already active or installed if ( $status_codes['active'] === $premium_status || $status_codes['installed'] === $premium_status ) { - $upgrade_status['message'] = 'Premium plugin already installed or active: ' . $premium_slug; + $upgrade_status['message'] = __( 'Premium plugin already installed or active: ', 'wp-module-installer' ) . $premium_slug; return $upgrade_status; } // Provision a license for the premium version of the plugin $license_response = PLSUtility::provision_license( $premium_slug ); if ( is_wp_error( $license_response ) ) { - $upgrade_status['message'] = 'Failed to provision license for: ' . $premium_slug; + $upgrade_status['message'] = __( 'Failed to provision license for: ', 'wp-module-installer' ) . $premium_slug; return $upgrade_status; } // Check if the download URL is present in the license response if ( empty( $license_response['downloadUrl'] ) ) { - $upgrade_status['message'] = 'Download URL is missing for premium plugin: ' . $premium_slug; + $upgrade_status['message'] = __( 'Download URL is missing for premium plugin: ', 'wp-module-installer' ) . $premium_slug; return $upgrade_status; } @@ -122,7 +122,7 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) { // Attempt to install the premium plugin using the provided download URL, and activate it if needed $premium_install_status = PluginInstaller::install_from_zip( $license_response['downloadUrl'], $should_activate ); if ( is_wp_error( $premium_install_status ) ) { - $upgrade_status['message'] = 'Failed to install the premium plugin: ' . $premium_slug; + $upgrade_status['message'] = __( 'Failed to install the premium plugin: ', 'wp-module-installer' ) . $premium_slug; if ( $should_activate ) { PluginInstaller::activate( $extended_slug ); } @@ -131,12 +131,12 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) { // Mark as successfully upgraded $upgrade_status['upgraded'] = true; - $upgrade_status['message'] = 'Successfully provisioned and installed: ' . $premium_slug; + $upgrade_status['message'] = __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $premium_slug; // Attempt to uninstall the extended version of the plugin $uninstall_status = PluginUninstaller::uninstall( $extended_slug ); if ( is_wp_error( $uninstall_status ) ) { - $upgrade_status['message'] = 'Successfully installed the premium plugin, but there was an error uninstalling the extended plugin. Manual cleanup may be required.'; + $upgrade_status['message'] = __( 'Successfully installed the premium plugin, but there was an error uninstalling the extended plugin. Manual cleanup may be required.', 'wp-module-installer' ); return $upgrade_status; } diff --git a/includes/WPCLI/Handlers/InstallerCommandHandler.php b/includes/WPCLI/Handlers/InstallerCommandHandler.php index 7ddc5d5..3c21775 100644 --- a/includes/WPCLI/Handlers/InstallerCommandHandler.php +++ b/includes/WPCLI/Handlers/InstallerCommandHandler.php @@ -43,9 +43,9 @@ public function auto_upgrade_extended_yith_plugins() { } if ( $all_upgrades_successful ) { - WP_CLI::success( 'YITH plugin upgrade process completed successfully.' ); + WP_CLI::success( __( 'YITH plugin upgrade process completed successfully.', 'wp-module-installer' ) ); } else { - WP_CLI::error( 'YITH plugin upgrade process completed, but some upgrades failed. Please check the logs.' ); + WP_CLI::error( __( 'YITH plugin upgrade process completed, but some upgrades failed. Please check the logs.', 'wp-module-installer' ) ); } } From a553fe2256580c886aa4d4b2f602c11f382cf9ac Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Tue, 8 Oct 2024 13:49:18 +0530 Subject: [PATCH 4/4] Update slugs --- includes/Services/PluginUpgrader.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/Services/PluginUpgrader.php b/includes/Services/PluginUpgrader.php index b8ee133..478fc1e 100644 --- a/includes/Services/PluginUpgrader.php +++ b/includes/Services/PluginUpgrader.php @@ -50,11 +50,11 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) { // TODO: Replace the dummy entitlement slug 'nfd_slug_yith_paypal_payments_for_woocommerce' with actual entitlement slugs. $yith_plugins_to_upgrade = array( 'yith-woocommerce-ajax-search' => 'nfd_slug_yith_paypal_payments_for_woocommerce', - 'nfd_slug_yith_woocommerce_ajax_product_filter' => 'nfd_slug_yith_paypal_payments_for_woocommerce', - 'nfd_slug_yith_woocommerce_wishlist' => 'nfd_slug_yith_paypal_payments_for_woocommerce', - 'nfd_slug_yith_woocommerce_booking' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_ajax_product_filter' => 'yith-woocommerce-ajax-product-filter-premium', + 'nfd_slug_yith_woocommerce_wishlist' => 'yith-woocommerce-wishlist-premium', + 'nfd_slug_yith_woocommerce_booking' => 'yith-woocommerce-booking-premium', 'nfd_slug_yith_woocommerce_gift_cards' => 'nfd_slug_yith_paypal_payments_for_woocommerce', - 'nfd_slug_yith_woocommerce_customize_myaccount_page' => 'nfd_slug_yith_paypal_payments_for_woocommerce', + 'nfd_slug_yith_woocommerce_customize_myaccount_page' => 'yith-woocommerce-customize-myaccount-page', ); // Initialize status array for the plugin upgrade process