From 2800be9b7bae98b3d43247948671e63a96edb234 Mon Sep 17 00:00:00 2001 From: Rommel Castro Date: Mon, 17 Apr 2023 22:05:10 -0600 Subject: [PATCH] fix phpcs errors --- .../class-wc-wccom-site-installer.php | 30 +++--- .../wccom-site/class-wc-wccom-site.php | 30 +++--- ...ass-wc-wccom-site-installation-manager.php | 88 +++++++-------- ...-wccom-site-installation-state-storage.php | 17 +-- ...class-wc-wccom-site-installation-state.php | 35 +++--- ...te-installation-step-activate-product.php} | 23 ++-- ...te-installation-step-download-product.php} | 17 +-- ...te-installation-step-get-product-info.php} | 25 +++-- ...m-site-installation-step-move-product.php} | 17 ++- ...site-installation-step-unpack-product.php} | 20 ++-- .../interface-installaton-step.php | 9 +- ...-rest-wccom-site-installer-error-codes.php | 100 +++++++++--------- ...ass-wc-rest-wccom-site-installer-error.php | 2 +- ...est-wccom-site-installer-controller-v2.php | 72 ++++++------- ...c-rest-wccom-site-installer-controller.php | 46 ++++---- 15 files changed, 287 insertions(+), 244 deletions(-) rename plugins/woocommerce/includes/wccom-site/installation/installation-steps/{class-installation-step-activate-product.php => class-wc-wccom-site-installation-step-activate-product.php} (84%) rename plugins/woocommerce/includes/wccom-site/installation/installation-steps/{class-installation-step-download-product.php => class-wc-wccom-site-installation-step-download-product.php} (66%) rename plugins/woocommerce/includes/wccom-site/installation/installation-steps/{class-installation-step-product-info.php => class-wc-wccom-site-installation-step-get-product-info.php} (79%) rename plugins/woocommerce/includes/wccom-site/installation/installation-steps/{class-installation-step-move-product.php => class-wc-wccom-site-installation-step-move-product.php} (75%) rename plugins/woocommerce/includes/wccom-site/installation/installation-steps/{class-installation-step-unpack-product.php => class-wc-wccom-site-installation-step-unpack-product.php} (54%) diff --git a/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site-installer.php b/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site-installer.php index e4234383913..90dd1ee2cc7 100644 --- a/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site-installer.php +++ b/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site-installer.php @@ -63,7 +63,7 @@ class WC_WCCOM_Site_Installer { 'activate_product', ); - private static $wp_upgrader; + private static $wp_upgrader; /** * Get the product install state. @@ -569,21 +569,21 @@ class WC_WCCOM_Site_Installer { return false; } - public static function get_wp_upgrader() { - if (!empty(self::$wp_upgrader)) { - return self::$wp_upgrader; - } + public static function get_wp_upgrader() { + if ( ! empty( self::$wp_upgrader ) ) { + return self::$wp_upgrader; + } - require_once ABSPATH . 'wp-admin/includes/file.php'; - require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; - require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; - require_once ABSPATH . 'wp-admin/includes/plugin.php'; + require_once ABSPATH . 'wp-admin/includes/file.php'; + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; + require_once ABSPATH . 'wp-admin/includes/plugin.php'; - WP_Filesystem(); - self::$wp_upgrader = new WP_Upgrader( new Automatic_Upgrader_Skin() ); - self::$wp_upgrader->init(); - wp_clean_plugins_cache(); + WP_Filesystem(); + self::$wp_upgrader = new WP_Upgrader( new Automatic_Upgrader_Skin() ); + self::$wp_upgrader->init(); + wp_clean_plugins_cache(); - return self::$wp_upgrader; - } + return self::$wp_upgrader; + } } diff --git a/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site.php b/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site.php index 981b2f799a5..9835b3bbb5b 100644 --- a/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site.php +++ b/plugins/woocommerce/includes/wccom-site/class-wc-wccom-site.php @@ -80,7 +80,7 @@ class WC_WCCOM_Site { add_filter( self::AUTH_ERROR_FILTER_NAME, function() { - return new Installer_Error( Installer_Error_Codes::NO_SIGNATURE ); + return new Installer_Error( Installer_Error_Codes::NO_SIGNATURE ); } ); return false; @@ -93,7 +93,7 @@ class WC_WCCOM_Site { add_filter( self::AUTH_ERROR_FILTER_NAME, function() { - return new Installer_Error( Installer_Error_Codes::SITE_NOT_CONNECTED ); + return new Installer_Error( Installer_Error_Codes::SITE_NOT_CONNECTED ); } ); return false; @@ -103,7 +103,7 @@ class WC_WCCOM_Site { add_filter( self::AUTH_ERROR_FILTER_NAME, function() { - return new Installer_Error( Installer_Error_Codes::INVALID_TOKEN ); + return new Installer_Error( Installer_Error_Codes::INVALID_TOKEN ); } ); return false; @@ -115,7 +115,7 @@ class WC_WCCOM_Site { add_filter( self::AUTH_ERROR_FILTER_NAME, function() { - return new Installer_Error( Installer_Error_Codes::REQUEST_VERIFICATION_FAILED ); + return new Installer_Error( Installer_Error_Codes::REQUEST_VERIFICATION_FAILED ); } ); return false; @@ -126,7 +126,7 @@ class WC_WCCOM_Site { add_filter( self::AUTH_ERROR_FILTER_NAME, function() { - return new Installer_Error( Installer_Error_Codes::USER_NOT_FOUND ); + return new Installer_Error( Installer_Error_Codes::USER_NOT_FOUND ); } ); return false; @@ -224,18 +224,18 @@ class WC_WCCOM_Site { require_once WC_ABSPATH . 'includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php'; require_once WC_ABSPATH . 'includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller-v2.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/class-wc-wccom-site-installation-state.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/class-wc-wccom-site-installation-state-storage.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/class-wc-wccom-site-installation-manager.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/class-wc-wccom-site-installation-state.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/class-wc-wccom-site-installation-state-storage.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/class-wc-wccom-site-installation-manager.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/interface-installaton-step.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-installation-step-product-info.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-installation-step-download-product.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-installation-step-unpack-product.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-installation-step-move-product.php'; - require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-installation-step-activate-product.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/interface-installaton-step.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-get-product-info.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-download-product.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-unpack-product.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-move-product.php'; + require_once WC_ABSPATH . 'includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-activate-product.php'; - $namespaces['wccom-site/v1'] = array( + $namespaces['wccom-site/v1'] = array( 'installer' => 'WC_REST_WCCOM_Site_Installer_Controller', ); diff --git a/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-manager.php b/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-manager.php index 566b2b27625..893146adb11 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-manager.php +++ b/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-manager.php @@ -1,4 +1,9 @@ product_id = $product_id; - $this->idempotency_key = $idempotency_key; + public function __construct( int $product_id, string $idempotency_key ) { + $this->product_id = $product_id; + $this->idempotency_key = $idempotency_key; } - public function run_installation(string $run_until_step) : bool { + public function run_installation( string $run_until_step ): bool { $state = WC_WCCOM_Site_Installation_State_Storage::get_state( $this->product_id ); if ( $state && $state->get_idempotency_key() !== $this->idempotency_key ) { - throw new Installer_Error(Installer_Error_Codes::IDEMPOTENCY_KEY_MISMATCH); + throw new Installer_Error( Installer_Error_Codes::IDEMPOTENCY_KEY_MISMATCH ); } if ( ! $state ) { @@ -33,104 +38,101 @@ class WC_WCCOM_Site_Installation_Manager { $this->can_run_installation( $run_until_step, $state ); - $next_step = $this->get_next_step($state); + $next_step = $this->get_next_step( $state ); $installation_steps = $this->get_installation_steps( $next_step, $run_until_step ); - array_walk($installation_steps, function($step_name) use ($state) { - $this->run_step($step_name, $state); - }); + array_walk( $installation_steps, function ( $step_name ) use ( $state ) { + $this->run_step( $step_name, $state ); + } ); return true; } - public function reset_installation() : bool { - $state = WC_WCCOM_Site_Installation_State_Storage::get_state( $this->product_id ); + public function reset_installation(): bool { + $state = WC_WCCOM_Site_Installation_State_Storage::get_state( $this->product_id ); - if ( ! $state ) { + if ( ! $state ) { throw new Installer_Error( Installer_Error_Codes::NO_INITIATED_INSTALLATION_FOUND ); } - if ( $state->get_idempotency_key() !== $this->idempotency_key ) { - throw new Installer_Error(Installer_Error_Codes::IDEMPOTENCY_KEY_MISMATCH); - } + if ( $state->get_idempotency_key() !== $this->idempotency_key ) { + throw new Installer_Error( Installer_Error_Codes::IDEMPOTENCY_KEY_MISMATCH ); + } $result = WC_WCCOM_Site_Installation_State_Storage::delete_state( $state ); - if ( ! $result) { - throw new Installer_Error(Installer_Error_Codes::FAILED_TO_RESET_INSTALLATION_STATE); + if ( ! $result ) { + throw new Installer_Error( Installer_Error_Codes::FAILED_TO_RESET_INSTALLATION_STATE ); } return true; - } + } protected function can_run_installation( $run_until_step, $state ) { // if ( $this->already_installed( $product_id )) { // throw new Installer_Error(Installer_Error_Codes::PLUGIN_ALREADY_INSTALLED_MESSAGE ); // } - if ( $state->get_last_step_status() === \WC_WCCOM_Site_Installation_State::STEP_STATUS_IN_PROGRESS) { - throw new Installer_Error(Installer_Error_Codes::INSTALLATION_ALREADY_RUNNING ); + if ( $state->get_last_step_status() === \WC_WCCOM_Site_Installation_State::STEP_STATUS_IN_PROGRESS ) { + throw new Installer_Error( Installer_Error_Codes::INSTALLATION_ALREADY_RUNNING ); } - if ( $state->get_last_step_status() === \WC_WCCOM_Site_Installation_State::STEP_STATUS_FAILED) { - throw new Installer_Error(Installer_Error_Codes::INSTALLATION_FAILED ); + if ( $state->get_last_step_status() === \WC_WCCOM_Site_Installation_State::STEP_STATUS_FAILED ) { + throw new Installer_Error( Installer_Error_Codes::INSTALLATION_FAILED ); } if ( $state->get_last_step_name() === self::STEPS[ count( self::STEPS ) - 1 ] ) { - throw new Installer_Error(Installer_Error_Codes::ALL_INSTALLATION_STEPS_RUN ); + throw new Installer_Error( Installer_Error_Codes::ALL_INSTALLATION_STEPS_RUN ); } - if (array_search($state->get_last_step_name(), self::STEPS) >= array_search($run_until_step, self::STEPS)) { - throw new Installer_Error(Installer_Error_Codes::REQUESTED_STEP_ALREADY_RUN ); + if ( array_search( $state->get_last_step_name(), self::STEPS ) >= array_search( $run_until_step, + self::STEPS ) ) { + throw new Installer_Error( Installer_Error_Codes::REQUESTED_STEP_ALREADY_RUN ); } if ( ! is_writable( WP_CONTENT_DIR ) ) { - throw new Installer_Error(Installer_Error_Codes::FILESYSTEM_REQUIREMENTS_NOT_MET ); + throw new Installer_Error( Installer_Error_Codes::FILESYSTEM_REQUIREMENTS_NOT_MET ); } } - protected function get_next_step($state) : string { + protected function get_next_step( $state ): string { $last_executed_step = $state->get_last_step_name(); - if (! $last_executed_step) { + if ( ! $last_executed_step ) { return self::STEPS[0]; } - $last_executed_step_index = array_search($last_executed_step, self::STEPS); + $last_executed_step_index = array_search( $last_executed_step, self::STEPS ); return self::STEPS[ $last_executed_step_index + 1 ]; } - protected function get_installation_steps(string $start_step, string $end_step) { - $start_step_offset = array_search($start_step, self::STEPS); - $end_step_index = array_search($end_step, self::STEPS); - $length = $end_step_index - $start_step_offset + 1; + protected function get_installation_steps( string $start_step, string $end_step ) { + $start_step_offset = array_search( $start_step, self::STEPS ); + $end_step_index = array_search( $end_step, self::STEPS ); + $length = $end_step_index - $start_step_offset + 1; - return array_slice(self::STEPS, $start_step_offset, $length); + return array_slice( self::STEPS, $start_step_offset, $length ); } - protected function run_step($step_name, $state ) { + protected function run_step( $step_name, $state ) { $state->initiate_step( $step_name ); WC_WCCOM_Site_Installation_State_Storage::save_state( $state ); try { - $class_name = "WC_WCCOM_Site_Installation_Step_$step_name"; + $class_name = "WC_WCCOM_Site_Installation_Step_$step_name"; $current_step = new $class_name( $state ); $current_step->run(); - - } catch(Installer_Error $exception) { - + } catch ( Installer_Error $exception ) { $state->capture_failure( $step_name, $exception->get_error_code() ); WC_WCCOM_Site_Installation_State_Storage::save_state( $state ); throw $exception; - - } catch (Throwable $error) { - + } catch ( Throwable $error ) { $state->capture_failure( $step_name, Installer_Error_Codes::UNEXPECTED_ERROR ); WC_WCCOM_Site_Installation_State_Storage::save_state( $state ); - throw new Installer_Error(Installer_Error_Codes::UNEXPECTED_ERROR, $error->getMessage() ); + throw new Installer_Error( Installer_Error_Codes::UNEXPECTED_ERROR, $error->getMessage() ); } $state->complete_step( $step_name ); diff --git a/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state-storage.php b/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state-storage.php index 544ed655aa4..4eaaa50a2cd 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state-storage.php +++ b/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state-storage.php @@ -1,4 +1,9 @@ get_product_id() ); + public static function delete_state( WC_WCCOM_Site_Installation_State $state ) : bool { + $storage_key = self::get_storage_key( $state->get_product_id() ); - return delete_option($storage_key); - } + return delete_option($storage_key); + } protected static function get_storage_key( $product_id ) : string { return sprintf('wccom-product-installation-state-%d', $product_id); diff --git a/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state.php b/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state.php index 7ba39896f56..7c1f0a094ea 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state.php +++ b/plugins/woocommerce/includes/wccom-site/installation/class-wc-wccom-site-installation-state.php @@ -1,4 +1,9 @@ product_id = $product_id; } - public static function initiate_existing( $product_id, $idempotency_key, $last_step_name, $last_step_status, $last_step_error, $started_date ) { - $instance = new self( $product_id ); - $instance->idempotency_key = $idempotency_key; - $instance->last_step_name = $last_step_name; - $instance->last_step_status = $last_step_status; - $instance->last_step_error = $last_step_error; - $instance->started_date = $started_date; + public static function initiate_existing( $product_id, $idempotency_key, $last_step_name, $last_step_status, $last_step_error, $started_date ) { + $instance = new self( $product_id ); + $instance->idempotency_key = $idempotency_key; + $instance->last_step_name = $last_step_name; + $instance->last_step_status = $last_step_status; + $instance->last_step_error = $last_step_error; + $instance->started_date = $started_date; - return $instance; - } + return $instance; + } - public static function initiate_new( $product_id, $idempotency_key ) { - $instance = new self( $product_id ); - $instance->idempotency_key = $idempotency_key; + public static function initiate_new( $product_id, $idempotency_key ) { + $instance = new self( $product_id ); + $instance->idempotency_key = $idempotency_key; $instance->started_date = time(); - return $instance; - } + return $instance; + } public function get_product_id() { return $this->product_id; @@ -138,4 +143,4 @@ class WC_WCCOM_Site_Installation_State { public function get_started_date() { return $this->started_date; } -} \ No newline at end of file +} diff --git a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-activate-product.php b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-activate-product.php similarity index 84% rename from plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-activate-product.php rename to plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-activate-product.php index 16c952ac25b..d3188433190 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-activate-product.php +++ b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-activate-product.php @@ -1,4 +1,9 @@ state = $state; } - public function run( ) { + public function run() { $product_id = $this->state->get_product_id(); if ( 'plugin' === $this->state->get_product_type() ) { @@ -28,7 +33,7 @@ class WC_WCCOM_Site_Installation_Step_Activate_Product implements WC_WCCOM_Site_ $filename = false; // If product is WP.org one, find out its filename. - $dir_name = $this->get_wporg_product_dir_name( ); + $dir_name = $this->get_wporg_product_dir_name(); if ( false !== $dir_name ) { $filename = \WC_WCCOM_Site_Installer::get_wporg_plugin_main_file( $dir_name ); } @@ -50,7 +55,7 @@ class WC_WCCOM_Site_Installation_Step_Activate_Product implements WC_WCCOM_Site_ $result = activate_plugin( $filename ); - if (is_wp_error($result)) { + if ( is_wp_error( $result ) ) { return new Installer_Error( Installer_Error_Codes::PLUGIN_ACTIVATION_ERROR, $result->get_error_message() ); } } @@ -61,7 +66,7 @@ class WC_WCCOM_Site_Installation_Step_Activate_Product implements WC_WCCOM_Site_ $theme_slug = false; // If product is WP.org theme, find out its slug. - $dir_name = $this->get_wporg_product_dir_name( ); + $dir_name = $this->get_wporg_product_dir_name(); if ( false !== $dir_name ) { $theme_slug = basename( $dir_name ); } @@ -84,18 +89,18 @@ class WC_WCCOM_Site_Installation_Step_Activate_Product implements WC_WCCOM_Site_ switch_theme( $theme_slug ); } - private function get_wporg_product_dir_name( ) { - if ( empty($this->state->get_installed_path() ) ) { + private function get_wporg_product_dir_name() { + if ( empty( $this->state->get_installed_path() ) ) { return false; } // Check whether product was downloaded from WordPress.org. $download_url = $this->state->get_download_url(); - $parsed_url = wp_parse_url( $download_url ); + $parsed_url = wp_parse_url( $download_url ); if ( ! empty( $parsed_url['host'] ) && 'downloads.wordpress.org' !== $parsed_url['host'] ) { return false; } return basename( $this->state->get_installed_path() ); } -} \ No newline at end of file +} diff --git a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-download-product.php b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-download-product.php similarity index 66% rename from plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-download-product.php rename to plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-download-product.php index 698de0936e7..5e6e410238b 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-download-product.php +++ b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-download-product.php @@ -1,4 +1,9 @@ state = $state; } - public function run( ) { + public function run() { $upgrader = WC_WCCOM_Site_Installer::get_wp_upgrader(); $download_path = $upgrader->download_package( $this->state->get_download_url() ); - if (empty($download_path)) { - throw new Installer_Error( Installer_Error_Codes::MISSING_DOWNLOAD_PATH); + if ( empty( $download_path ) ) { + throw new Installer_Error( Installer_Error_Codes::MISSING_DOWNLOAD_PATH ); } - $this->state->set_download_path($download_path); + $this->state->set_download_path( $download_path ); return $this->state; } -} \ No newline at end of file +} diff --git a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-product-info.php b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-get-product-info.php similarity index 79% rename from plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-product-info.php rename to plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-get-product-info.php index 90ffaff9c5f..7d532d16320 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-product-info.php +++ b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-get-product-info.php @@ -1,4 +1,9 @@ state = $state; } - public function run() { + public function run() { $product_id = $this->state->get_product_id(); // Get product info from woocommerce.com. @@ -25,36 +30,36 @@ class WC_WCCOM_Site_Installation_Step_Get_Product_Info implements WC_WCCOM_Site_ ); if ( 200 !== wp_remote_retrieve_response_code( $request ) ) { - throw new Installer_Error( Installer_Error_Codes::FAILED_GETTING_PRODUCT_INFO ); + throw new Installer_Error( Installer_Error_Codes::FAILED_GETTING_PRODUCT_INFO ); } $result = json_decode( wp_remote_retrieve_body( $request ), true ); - if (!isset($result['_product_type'], $result['name'])) { + if ( ! isset( $result['_product_type'], $result['name'] ) ) { throw new Installer_Error( Installer_Error_Codes::INVALID_PRODUCT_INFO_RESPONSE ); } - if ( ! empty( $result['_wporg_product'] )) { + if ( ! empty( $result['_wporg_product'] ) ) { $download_url = $this->get_wporg_download_url( $result ); } else { $download_url = $this->get_wccom_download_url( $product_id ); } $this->state->set_product_type( $result['_product_type'] ); - $this->state->set_product_name ($result['name'] ); - $this->state->set_download_url ($download_url ); + $this->state->set_product_name( $result['name'] ); + $this->state->set_download_url( $download_url ); return $this->state; } - protected function get_wporg_download_url ( $data ) { + protected function get_wporg_download_url( $data ) { if ( empty( $data['_wporg_product'] ) ) { return null; } - if( empty( $data['download_link'] ) ) { + if ( empty( $data['download_link'] ) ) { throw new Installer_Error( Installer_Error_Codes::WPORG_PRODUCT_MISSING_DOWNLOAD_LINK ); } @@ -78,4 +83,4 @@ class WC_WCCOM_Site_Installation_Step_Get_Product_Info implements WC_WCCOM_Site_ return $updates[ $product_id ]['package']; } -} \ No newline at end of file +} diff --git a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-move-product.php b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-move-product.php similarity index 75% rename from plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-move-product.php rename to plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-move-product.php index 3bca73ff428..633d12e1e24 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-move-product.php +++ b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-move-product.php @@ -1,13 +1,18 @@ state = $state; } - public function run( ) { + public function run() { $upgrader = WC_WCCOM_Site_Installer::get_wp_upgrader(); $destination = 'plugin' === $this->state->get_product_type() @@ -30,15 +35,17 @@ class WC_WCCOM_Site_Installation_Step_Move_Product implements WC_WCCOM_Site_Inst * If install package returns error 'folder_exists' treat as success. */ if ( is_wp_error( $result ) && array_key_exists( 'folder_exists', $result->errors ) ) { - $existing_folder_path = $result->error_data[ 'folder_exists' ]; - $plugin_info = WC_WCCOM_Site_Installer::get_plugin_info( $existing_folder_path ); + $existing_folder_path = $result->error_data['folder_exists']; + $plugin_info = WC_WCCOM_Site_Installer::get_plugin_info( $existing_folder_path ); $this->state->set_installed_path( $existing_folder_path ); $this->state->set_already_installed_plugin_info( $plugin_info ); + return $this->state; } $this->state->set_installed_path( $result['destination'] ); + return $this->state; } -} \ No newline at end of file +} diff --git a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-unpack-product.php b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-unpack-product.php similarity index 54% rename from plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-unpack-product.php rename to plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-unpack-product.php index 773d166cc34..87d6a28bfd4 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-installation-step-unpack-product.php +++ b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/class-wc-wccom-site-installation-step-unpack-product.php @@ -1,4 +1,9 @@ state = $state; } public function run() { - $upgrader = WC_WCCOM_Site_Installer::get_wp_upgrader(); - $unpacked_path = $upgrader->unpack_package ($this->state->get_download_path(), true ); + $upgrader = WC_WCCOM_Site_Installer::get_wp_upgrader(); + $unpacked_path = $upgrader->unpack_package( $this->state->get_download_path(), true ); - if ( empty( $unpacked_path) ) { - return new Installer_Error( Installer_Error_Codes::MISSING_UNPACKED_PATH); + if ( empty( $unpacked_path ) ) { + return new Installer_Error( Installer_Error_Codes::MISSING_UNPACKED_PATH ); } - $this->state->set_unpacked_path($unpacked_path); + $this->state->set_unpacked_path( $unpacked_path ); return $this->state; - } -} \ No newline at end of file +} diff --git a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/interface-installaton-step.php b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/interface-installaton-step.php index b5349b26e13..833a1b2cd66 100644 --- a/plugins/woocommerce/includes/wccom-site/installation/installation-steps/interface-installaton-step.php +++ b/plugins/woocommerce/includes/wccom-site/installation/installation-steps/interface-installaton-step.php @@ -1,9 +1,14 @@ 'Authentication required', - self::NO_ACCESS_TOKEN => 'No access token provided', - self::NO_SIGNATURE => 'No signature provided', - self::SITE_NOT_CONNECTED => 'Site not connected to WooCommerce.com', - self::INVALID_TOKEN => 'Invalid access token provided', - self::REQUEST_VERIFICATION_FAILED => 'Request verification by signature failed', - self::USER_NOT_FOUND => 'Token owning user not found', - self::NO_PERMISSION => 'You do not have permission to install plugin or theme', - self::IDEMPOTENCY_KEY_MISMATCH => 'Idempotency key mismatch', - self::NO_INITIATED_INSTALLATION_FOUND => 'No initiated installation for the product found', - self::ALL_INSTALLATION_STEPS_RUN => 'All installation steps have been run', - self::REQUESTED_STEP_ALREADY_RUN => 'Requested step has already been run', - self::PLUGIN_ALREADY_INSTALLED => 'The plugin has already been installed', + const ERROR_MESSAGES = [ + self::NOT_AUTHENTICATED => 'Authentication required', + self::NO_ACCESS_TOKEN => 'No access token provided', + self::NO_SIGNATURE => 'No signature provided', + self::SITE_NOT_CONNECTED => 'Site not connected to WooCommerce.com', + self::INVALID_TOKEN => 'Invalid access token provided', + self::REQUEST_VERIFICATION_FAILED => 'Request verification by signature failed', + self::USER_NOT_FOUND => 'Token owning user not found', + self::NO_PERMISSION => 'You do not have permission to install plugin or theme', + self::IDEMPOTENCY_KEY_MISMATCH => 'Idempotency key mismatch', + self::NO_INITIATED_INSTALLATION_FOUND => 'No initiated installation for the product found', + self::ALL_INSTALLATION_STEPS_RUN => 'All installation steps have been run', + self::REQUESTED_STEP_ALREADY_RUN => 'Requested step has already been run', + self::PLUGIN_ALREADY_INSTALLED => 'The plugin has already been installed', self::INSTALLATION_ALREADY_RUNNING => 'The installation of the plugin is already running', - self::INSTALLATION_FAILED => 'The installation of the plugin failed', - self::FILESYSTEM_REQUIREMENTS_NOT_MET => 'The filesystem requirements are not met', - self::FAILED_GETTING_PRODUCT_INFO => 'Failed to retrieve product info from woocommerce.com', - self::INVALID_PRODUCT_INFO_RESPONSE => 'Invalid product info response from woocommerce.com', - self::WCCOM_PRODUCT_MISSING_SUBSCRIPTION => 'Product subscription is missing', - self::WCCOM_PRODUCT_MISSING_PACKAGE => 'Could not find product package', - self::MISSING_DOWNLOAD_PATH => 'Download path is missing', - self::MISSING_UNPACKED_PATH => 'Unpacked path is missing', - self::UNKNOWN_FILENAME => 'Unknown product filename', - self::PLUGIN_ACTIVATION_ERROR => 'Plugin activation error', - self::UNEXPECTED_ERROR => 'Unexpected error', - self::FAILED_TO_RESET_INSTALLATION_STATE => 'Failed to reset installation state', - ]; + self::INSTALLATION_FAILED => 'The installation of the plugin failed', + self::FILESYSTEM_REQUIREMENTS_NOT_MET => 'The filesystem requirements are not met', + self::FAILED_GETTING_PRODUCT_INFO => 'Failed to retrieve product info from woocommerce.com', + self::INVALID_PRODUCT_INFO_RESPONSE => 'Invalid product info response from woocommerce.com', + self::WCCOM_PRODUCT_MISSING_SUBSCRIPTION => 'Product subscription is missing', + self::WCCOM_PRODUCT_MISSING_PACKAGE => 'Could not find product package', + self::MISSING_DOWNLOAD_PATH => 'Download path is missing', + self::MISSING_UNPACKED_PATH => 'Unpacked path is missing', + self::UNKNOWN_FILENAME => 'Unknown product filename', + self::PLUGIN_ACTIVATION_ERROR => 'Plugin activation error', + self::UNEXPECTED_ERROR => 'Unexpected error', + self::FAILED_TO_RESET_INSTALLATION_STATE => 'Failed to reset installation state', + ]; - const HTTP_CODES = [ - self::NOT_AUTHENTICATED => 401, - self::NO_ACCESS_TOKEN => 400, - self::NO_SIGNATURE => 400, - self::SITE_NOT_CONNECTED => 401, - self::INVALID_TOKEN => 401, - self::REQUEST_VERIFICATION_FAILED => 400, - self::USER_NOT_FOUND => 401, - self::NO_PERMISSION => 403, - self::IDEMPOTENCY_KEY_MISMATCH => 400, - self::NO_INITIATED_INSTALLATION_FOUND => 400, - self::ALL_INSTALLATION_STEPS_RUN => 400, - self::REQUESTED_STEP_ALREADY_RUN => 400, - self::UNEXPECTED_ERROR => 500, - ]; + const HTTP_CODES = [ + self::NOT_AUTHENTICATED => 401, + self::NO_ACCESS_TOKEN => 400, + self::NO_SIGNATURE => 400, + self::SITE_NOT_CONNECTED => 401, + self::INVALID_TOKEN => 401, + self::REQUEST_VERIFICATION_FAILED => 400, + self::USER_NOT_FOUND => 401, + self::NO_PERMISSION => 403, + self::IDEMPOTENCY_KEY_MISMATCH => 400, + self::NO_INITIATED_INSTALLATION_FOUND => 400, + self::ALL_INSTALLATION_STEPS_RUN => 400, + self::REQUESTED_STEP_ALREADY_RUN => 400, + self::UNEXPECTED_ERROR => 500, + ]; } diff --git a/plugins/woocommerce/includes/wccom-site/rest-api/class-wc-rest-wccom-site-installer-error.php b/plugins/woocommerce/includes/wccom-site/rest-api/class-wc-rest-wccom-site-installer-error.php index 4ccb8ff3b86..c26bc5ba87f 100644 --- a/plugins/woocommerce/includes/wccom-site/rest-api/class-wc-rest-wccom-site-installer-error.php +++ b/plugins/woocommerce/includes/wccom-site/rest-api/class-wc-rest-wccom-site-installer-error.php @@ -19,7 +19,7 @@ class WC_REST_WCCOM_Site_Installer_Error extends Exception { $this->error_message = $error_message ?? WC_REST_WCCOM_Site_Installer_Error_Codes::ERROR_MESSAGES[ $error_code ] ?? ''; $this->http_code = $http_code ?? WC_REST_WCCOM_Site_Installer_Error_Codes::HTTP_CODES[ $error_code ] ?? 400; - parent::__construct( $error_code ); + parent::__construct( $error_code ); } public function get_error_code( ) { diff --git a/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller-v2.php b/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller-v2.php index 745c8b3858d..6db70ebfe53 100644 --- a/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller-v2.php +++ b/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller-v2.php @@ -59,27 +59,27 @@ class WC_REST_WCCOM_Site_Installer_Controller_V2 extends WC_REST_Controller { 'type' => 'string', 'enum' => WC_WCCOM_Site_Installation_Manager::STEPS, ], - 'idempotency-key' => [ - 'required' => true, + 'idempotency-key' => [ + 'required' => true, + 'type' => 'string', + ], + ], + ], + [ + 'methods' => WP_REST_Server::DELETABLE, + 'callback' => [ $this, 'reset_install' ], + 'permission_callback' => [ $this, 'check_permission' ], + 'args' => [ + 'product-id' => [ + 'required' => true, + 'type' => 'integer', + ], + 'idempotency-key' => [ + 'required' => true, 'type' => 'string', ], ], ], - [ - 'methods' => WP_REST_Server::DELETABLE, - 'callback' => [ $this, 'reset_install' ], - 'permission_callback' => [ $this, 'check_permission' ], - 'args' => [ - 'product-id' => [ - 'required' => true, - 'type' => 'integer', - ], - 'idempotency-key' => [ - 'required' => true, - 'type' => 'string', - ], - ], - ], ] ); } @@ -144,29 +144,29 @@ class WC_REST_WCCOM_Site_Installer_Controller_V2 extends WC_REST_Controller { return $response; } - /** - * Reset installation state. - * - * @since 7.7.0 - * @param WP_REST_Request $request Full details about the request. - * @return WP_REST_Response|WP_Error - */ - public function reset_install( $request ) { - try { - $product_id = $request['product-id']; - $idempotency_key = $request['idempotency-key']; + /** + * Reset installation state. + * + * @since 7.7.0 + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error + */ + public function reset_install( $request ) { + try { + $product_id = $request['product-id']; + $idempotency_key = $request['idempotency-key']; - $installation_manager = new WC_WCCOM_Site_Installation_Manager($product_id, $idempotency_key); - $installation_manager->reset_installation(); + $installation_manager = new WC_WCCOM_Site_Installation_Manager($product_id, $idempotency_key); + $installation_manager->reset_installation(); - $response = $this->success_response($product_id); + $response = $this->success_response($product_id); - } catch (Installer_Error $exception) { - $response = $this->failure_response($product_id, $exception); - } + } catch (Installer_Error $exception) { + $response = $this->failure_response($product_id, $exception); + } - return $response; - } + return $response; + } protected function success_response( $product_id ) { $state = WC_WCCOM_Site_Installation_State_Storage::get_state( $product_id ); diff --git a/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php b/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php index f41e179a077..868d62fe03e 100644 --- a/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php +++ b/plugins/woocommerce/includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php @@ -76,32 +76,32 @@ class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller { * @param WP_REST_Request $request Full details about the request. * @return bool|WP_Error */ - public function check_permission( $request ) { - $current_user = wp_get_current_user(); + public function check_permission( $request ) { + $current_user = wp_get_current_user(); - if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) { - $error = apply_filters( - WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME, - new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED ) - ); - return new WP_Error( - $error->get_error_code(), - $error->get_error_message(), - array( 'status' => $error->get_http_code() ) - ); - } + if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) { + $error = apply_filters( + WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME, + new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED ) + ); + return new WP_Error( + $error->get_error_code(), + $error->get_error_message(), + array( 'status' => $error->get_http_code() ) + ); + } - if ( ! user_can( $current_user, 'install_plugins' ) || ! user_can( $current_user, 'install_themes' ) ) { - $error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION ); - return new WP_Error( - $error->get_error_code(), - $error->get_error_message(), - array( 'status' => $error->get_http_code() ) - ); - } + if ( ! user_can( $current_user, 'install_plugins' ) || ! user_can( $current_user, 'install_themes' ) ) { + $error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION ); + return new WP_Error( + $error->get_error_code(), + $error->get_error_message(), + array( 'status' => $error->get_http_code() ) + ); + } - return true; - } + return true; + } /** * Get installation state.