From 11b46d4f779f736bc7aac60eaae2e5e72c418bec Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Thu, 12 Jan 2023 14:16:01 +1300 Subject: [PATCH] Add a new set of CLI commands to WooCommerce Beta Tester (#36339) --- .../changelog/dev-add-cli-command | 4 + .../class-wc-beta-tester-admin-menus.php | 9 +- .../includes/class-wc-beta-tester-cli.php | 89 +++++++++++++++++++ ...wc-beta-tester-live-branches-installer.php | 20 +++++ .../live-branches/components/BranchList.tsx | 6 +- .../woocommerce-beta-tester.php | 12 ++- 6 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce-beta-tester/changelog/dev-add-cli-command create mode 100644 plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-cli.php diff --git a/plugins/woocommerce-beta-tester/changelog/dev-add-cli-command b/plugins/woocommerce-beta-tester/changelog/dev-add-cli-command new file mode 100644 index 00000000000..b65eafa05a0 --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/dev-add-cli-command @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add a wp cli command for activating live branches. diff --git a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-admin-menus.php b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-admin-menus.php index 3a79dd4b01c..7aa5efad1a8 100644 --- a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-admin-menus.php +++ b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-admin-menus.php @@ -153,6 +153,13 @@ Copy and paste the system status report from **WooCommerce > System Status** in * @return string */ protected function construct_ssr() { + // This function depends on the WC core global being available. Sometimes, such as when we deactivate + // WC to install a live branches version, WC will not be available and cause a crash if we don't exit early + // here. + if ( ! class_exists( 'WC' ) ) { + return ''; + } + if ( version_compare( WC()->version, '3.6', '<' ) ) { return ''; } @@ -332,7 +339,7 @@ Copy and paste the system status report from **WooCommerce > System Status** in $items_to_remove = array( 'wc-beta-tester-settings', 'wc-beta-tester-version-picker', 'wc-beta-tester' ); if ( isset( $submenu['plugins.php'] ) ) { foreach ( $submenu['plugins.php'] as $key => $menu ) { - if ( in_array( $menu[2], $items_to_remove ) ) { + if ( in_array( $menu[2], $items_to_remove, true ) ) { unset( $submenu['plugins.php'][ $key ] ); } } diff --git a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-cli.php b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-cli.php new file mode 100644 index 00000000000..c10ae886d60 --- /dev/null +++ b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-cli.php @@ -0,0 +1,89 @@ + + * : The branch to install. + * + * ## Examples + * + * wp wc-beta-tester install update/some-branch + * + * @param array $args Arguments passed to CLI. + */ + public function install( $args ) { + $installer = new WC_Beta_Tester_Live_Branches_Installer(); + + $branch = $args[0]; + + $info = $installer->get_branch_info_from_manifest( $branch ); + + if ( ! $info ) { + WP_CLI::error( "Could not find branch $branch in manifest" ); + } else { + $install_result = $installer->install( $info->download_url, $info->branch, $info->version ); + + if ( is_wp_error( $install_result ) ) { + WP_CLI::error( $install_result->get_error_message() ); + } + + WP_CLI::success( "Installed $branch" ); + } + } + + /** + * Deactivate WooCommerce. + * + * ## Examples + * wp wc-beta-tester deactivate_woocommerce + */ + public function deactivate_woocommerce() { + $installer = new WC_Beta_Tester_Live_Branches_Installer(); + $installer->deactivate_woocommerce(); + + WP_CLI::success( 'Deactivated WooCommerce' ); + } + + /** + * Activate a live branch of the WooCommerce plugin. + * + * ## Options + * + * : The branch to activate. + * + * ## Examples + * + * wp wc-beta-tester activate update/some-branch* + * + * @param array $args Arguments passed to CLI. + */ + public function activate( $args ) { + $installer = new WC_Beta_Tester_Live_Branches_Installer(); + $branch = $args[0]; + $info = $installer->get_branch_info_from_manifest( $branch ); + + if ( ! $info ) { + WP_CLI::error( "Could not find branch $branch in manifest" ); + } else { + $installer->activate( $info->version ); + + WP_CLI::success( "Activated $branch" ); + } + } +} diff --git a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-live-branches-installer.php b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-live-branches-installer.php index 8d4d6aa6af8..42cf568a629 100644 --- a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-live-branches-installer.php +++ b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-live-branches-installer.php @@ -46,6 +46,26 @@ class WC_Beta_Tester_Live_Branches_Installer { return $wp_filesystem; } + /** + * Get the download url of a WooCommerce plugin version from the manifest. + * + * @param string $branch The name of the branch. + */ + public function get_branch_info_from_manifest( $branch ) { + $response = wp_remote_get( 'https://betadownload.jetpack.me/woocommerce-branches.json' ); + $body = wp_remote_retrieve_body( $response ); + + $obj = json_decode( $body ); + + foreach ( $obj->pr as $key => $value ) { + if ( $value->branch === $branch ) { + return $value; + } + } + + return false; + } + /** * Install a WooCommerce plugin version by download url. * diff --git a/plugins/woocommerce-beta-tester/src/live-branches/components/BranchList.tsx b/plugins/woocommerce-beta-tester/src/live-branches/components/BranchList.tsx index 4d044ea7e86..3a1b4c8f490 100644 --- a/plugins/woocommerce-beta-tester/src/live-branches/components/BranchList.tsx +++ b/plugins/woocommerce-beta-tester/src/live-branches/components/BranchList.tsx @@ -109,7 +109,7 @@ const BranchInfo = ( { branch }: { branch: Branch } ) => { const WooCommerceVersionInfo = () => { // @ts-ignore - const version = window?.wc?.WC_VERSION || 'unknown'; + const version = window?.wc?.wcSettings?.WC_VERSION || 'unknown'; return (

@@ -136,6 +136,8 @@ export const BranchList = ( { branches }: { branches: Branch[] } ) => { uninstalledBranches[ 0 ] ); + const installedBranchesExist = !! installedBranches.length; + return ( <> @@ -188,7 +190,7 @@ export const BranchList = ( { branches }: { branches: Branch[] } ) => { - { installedBranches.length && ( + { installedBranchesExist && (

Other Installed Branches

diff --git a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php index 0e4b5a711ec..633697db4d3 100644 --- a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php +++ b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php @@ -17,6 +17,12 @@ defined( 'ABSPATH' ) || exit; + +if ( defined( 'WP_CLI' ) ) { + require_once dirname( __FILE__ ) . '/includes/class-wc-beta-tester-cli.php'; + WP_CLI::add_command( 'wc-beta-tester', WC_Beta_Tester_CLI::class ); +} + // Define WC_BETA_TESTER_FILE. if ( ! defined( 'WC_BETA_TESTER_FILE' ) ) { define( 'WC_BETA_TESTER_FILE', __FILE__ ); @@ -63,7 +69,7 @@ function _wc_beta_tester_bootstrap() { } // Load admin. - require( 'plugin.php' ); + require 'plugin.php'; } add_action( 'plugins_loaded', '_wc_beta_tester_bootstrap' ); @@ -75,12 +81,12 @@ function add_extension_register_script() { $script_path = '/build/index.js'; $script_asset_path = dirname( __FILE__ ) . '/build/index.asset.php'; $script_asset = file_exists( $script_asset_path ) - ? require( $script_asset_path ) + ? require $script_asset_path : array( 'dependencies' => array(), 'version' => filemtime( $script_path ), ); - $script_url = plugins_url( $script_path, __FILE__ ); + $script_url = plugins_url( $script_path, __FILE__ ); wp_register_script( 'woocommerce-admin-test-helper',