rebase from live enviornment
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
/**
|
||||
* Activate product step.
|
||||
*
|
||||
* @package WooCommerce\WCCom
|
||||
* @since 7.7.0
|
||||
*/
|
||||
|
||||
use WC_REST_WCCOM_Site_Installer_Error_Codes as Installer_Error_Codes;
|
||||
use WC_REST_WCCOM_Site_Installer_Error as Installer_Error;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_WCCOM_Site_Installation_Step_Activate_Product Class
|
||||
*/
|
||||
class WC_WCCOM_Site_Installation_Step_Activate_Product implements WC_WCCOM_Site_Installation_Step {
|
||||
/**
|
||||
* The current installation state.
|
||||
*
|
||||
* @var WC_WCCOM_Site_Installation_State
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $state The current installation state.
|
||||
*/
|
||||
public function __construct( $state ) {
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the step installation process.
|
||||
*/
|
||||
public function run() {
|
||||
$product_id = $this->state->get_product_id();
|
||||
|
||||
if ( 'plugin' === $this->state->get_product_type() ) {
|
||||
$this->activate_plugin( $product_id );
|
||||
} else {
|
||||
$this->activate_theme( $product_id );
|
||||
}
|
||||
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate plugin.
|
||||
*
|
||||
* @param int $product_id Product ID.
|
||||
*/
|
||||
private function activate_plugin( $product_id ) {
|
||||
// Clear plugins cache used in `WC_Helper::get_local_woo_plugins`.
|
||||
wp_clean_plugins_cache();
|
||||
$filename = false;
|
||||
|
||||
// If product is WP.org one, find out its filename.
|
||||
$dir_name = $this->get_wporg_product_dir_name();
|
||||
if ( false !== $dir_name ) {
|
||||
$filename = \WC_WCCOM_Site_Installer::get_wporg_plugin_main_file( $dir_name );
|
||||
}
|
||||
|
||||
if ( false === $filename ) {
|
||||
$plugins = wp_list_filter(
|
||||
WC_Helper::get_local_woo_plugins(),
|
||||
array(
|
||||
'_product_id' => $product_id,
|
||||
)
|
||||
);
|
||||
|
||||
$filename = is_array( $plugins ) && ! empty( $plugins ) ? key( $plugins ) : '';
|
||||
}
|
||||
|
||||
if ( empty( $filename ) ) {
|
||||
return new Installer_Error( Installer_Error_Codes::UNKNOWN_FILENAME );
|
||||
}
|
||||
|
||||
$result = activate_plugin( $filename );
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return new Installer_Error( Installer_Error_Codes::PLUGIN_ACTIVATION_ERROR, $result->get_error_message() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate theme.
|
||||
*
|
||||
* @param int $product_id Product ID.
|
||||
*/
|
||||
private function activate_theme( $product_id ) {
|
||||
// Clear plugins cache used in `WC_Helper::get_local_woo_themes`.
|
||||
wp_clean_themes_cache();
|
||||
$theme_slug = false;
|
||||
|
||||
// If product is WP.org theme, find out its slug.
|
||||
$dir_name = $this->get_wporg_product_dir_name();
|
||||
if ( false !== $dir_name ) {
|
||||
$theme_slug = basename( $dir_name );
|
||||
}
|
||||
|
||||
if ( false === $theme_slug ) {
|
||||
$themes = wp_list_filter(
|
||||
WC_Helper::get_local_woo_themes(),
|
||||
array(
|
||||
'_product_id' => $product_id,
|
||||
)
|
||||
);
|
||||
|
||||
$theme_slug = is_array( $themes ) && ! empty( $themes ) ? dirname( key( $themes ) ) : '';
|
||||
}
|
||||
|
||||
if ( empty( $theme_slug ) ) {
|
||||
return new Installer_Error( Installer_Error_Codes::UNKNOWN_FILENAME );
|
||||
}
|
||||
|
||||
switch_theme( $theme_slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get WP.org product directory name.
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
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 );
|
||||
if ( ! empty( $parsed_url['host'] ) && 'downloads.wordpress.org' !== $parsed_url['host'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return basename( $this->state->get_installed_path() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Download product step.
|
||||
*
|
||||
* @package WooCommerce\WCCom
|
||||
* @since 7.7.0
|
||||
*/
|
||||
|
||||
use WC_REST_WCCOM_Site_Installer_Error_Codes as Installer_Error_Codes;
|
||||
use WC_REST_WCCOM_Site_Installer_Error as Installer_Error;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_WCCOM_Site_Installation_Step_Download_Product class
|
||||
*/
|
||||
class WC_WCCOM_Site_Installation_Step_Download_Product implements WC_WCCOM_Site_Installation_Step {
|
||||
/**
|
||||
* The current installation state.
|
||||
*
|
||||
* @var WC_WCCOM_Site_Installation_State
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $state The current installation state.
|
||||
*/
|
||||
public function __construct( $state ) {
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the step installation process.
|
||||
*
|
||||
* @throws Installer_Error Installer Error.
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
|
||||
$this->state->set_download_path( $download_path );
|
||||
|
||||
return $this->state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Get product info step.
|
||||
*
|
||||
* @package WooCommerce\WCCom
|
||||
* @since 7.7.0
|
||||
*/
|
||||
|
||||
use WC_REST_WCCOM_Site_Installer_Error_Codes as Installer_Error_Codes;
|
||||
use WC_REST_WCCOM_Site_Installer_Error as Installer_Error;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_WCCOM_Site_Installation_Step_Get_Product_Info class
|
||||
*/
|
||||
class WC_WCCOM_Site_Installation_Step_Get_Product_Info implements WC_WCCOM_Site_Installation_Step {
|
||||
/**
|
||||
* The current installation state.
|
||||
*
|
||||
* @var WC_WCCOM_Site_Installation_State
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $state The current installation state.
|
||||
*/
|
||||
public function __construct( $state ) {
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the step installation process.
|
||||
*
|
||||
* @throws Installer_Error Installer Error.
|
||||
* @return array
|
||||
*/
|
||||
public function run() {
|
||||
$product_id = $this->state->get_product_id();
|
||||
|
||||
// Get product info from Woo.com.
|
||||
$request = WC_Helper_API::get(
|
||||
add_query_arg(
|
||||
array( 'product_id' => $product_id ),
|
||||
'info'
|
||||
),
|
||||
array(
|
||||
'authenticated' => true,
|
||||
)
|
||||
);
|
||||
|
||||
if ( 200 !== wp_remote_retrieve_response_code( $request ) ) {
|
||||
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'] ) ) {
|
||||
throw new Installer_Error( Installer_Error_Codes::INVALID_PRODUCT_INFO_RESPONSE );
|
||||
}
|
||||
|
||||
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 );
|
||||
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get download URL for wporg product.
|
||||
*
|
||||
* @param array $data Product data.
|
||||
*
|
||||
* @return string|null
|
||||
* @throws Installer_Error Installer Error.
|
||||
*/
|
||||
protected function get_wporg_download_url( $data ) {
|
||||
if ( empty( $data['_wporg_product'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( empty( $data['download_link'] ) ) {
|
||||
throw new Installer_Error( Installer_Error_Codes::WPORG_PRODUCT_MISSING_DOWNLOAD_LINK );
|
||||
}
|
||||
|
||||
return $data['download_link'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get download URL for wccom product.
|
||||
*
|
||||
* @param int $product_id Product ID.
|
||||
*
|
||||
* @return string
|
||||
* @throws Installer_Error Installer Error.
|
||||
*/
|
||||
protected function get_wccom_download_url( $product_id ) {
|
||||
WC_Helper::_flush_subscriptions_cache();
|
||||
|
||||
if ( ! WC_Helper::has_product_subscription( $product_id ) ) {
|
||||
throw new Installer_Error( Installer_Error_Codes::WCCOM_PRODUCT_MISSING_SUBSCRIPTION );
|
||||
}
|
||||
|
||||
// Retrieve download URL for non-wporg product.
|
||||
WC_Helper_Updater::flush_updates_cache();
|
||||
$updates = WC_Helper_Updater::get_update_data();
|
||||
|
||||
if ( empty( $updates[ $product_id ]['package'] ) ) {
|
||||
return new Installer_Error( Installer_Error_Codes::WCCOM_PRODUCT_MISSING_PACKAGE );
|
||||
}
|
||||
|
||||
return $updates[ $product_id ]['package'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Move product to the correct location.
|
||||
*
|
||||
* @package WooCommerce\WCCom
|
||||
* @since 7.7.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_WCCOM_Site_Installation_Step_Move_Product class
|
||||
*/
|
||||
class WC_WCCOM_Site_Installation_Step_Move_Product implements WC_WCCOM_Site_Installation_Step {
|
||||
/**
|
||||
* The current installation state.
|
||||
*
|
||||
* @var WC_WCCOM_Site_Installation_State
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $state The current installation state.
|
||||
*/
|
||||
public function __construct( $state ) {
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the step installation process.
|
||||
*/
|
||||
public function run() {
|
||||
$upgrader = WC_WCCOM_Site_Installer::get_wp_upgrader();
|
||||
|
||||
$destination = 'plugin' === $this->state->get_product_type()
|
||||
? WP_PLUGIN_DIR
|
||||
: get_theme_root();
|
||||
|
||||
$package = array(
|
||||
'source' => $this->state->get_unpacked_path(),
|
||||
'destination' => $destination,
|
||||
'clear_working' => true,
|
||||
'hook_extra' => array(
|
||||
'type' => $this->state->get_product_type(),
|
||||
'action' => 'install',
|
||||
),
|
||||
);
|
||||
|
||||
$result = $upgrader->install_package( $package );
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Get product info step.
|
||||
*
|
||||
* @package WooCommerce\WCCom
|
||||
* @since 7.7.0
|
||||
*/
|
||||
|
||||
use WC_REST_WCCOM_Site_Installer_Error_Codes as Installer_Error_Codes;
|
||||
use WC_REST_WCCOM_Site_Installer_Error as Installer_Error;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_WCCOM_Site_Installation_Step_Unpack_Product class
|
||||
*/
|
||||
class WC_WCCOM_Site_Installation_Step_Unpack_Product implements WC_WCCOM_Site_Installation_Step {
|
||||
/**
|
||||
* The current installation state.
|
||||
*
|
||||
* @var WC_WCCOM_Site_Installation_State
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $state The current installation state.
|
||||
*/
|
||||
public function __construct( $state ) {
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the step installation process.
|
||||
*/
|
||||
public function run() {
|
||||
$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 );
|
||||
}
|
||||
|
||||
$this->state->set_unpacked_path( $unpacked_path );
|
||||
|
||||
return $this->state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* Interface for installation steps.
|
||||
*
|
||||
* @package WooCommerce\WCCom
|
||||
* @since 7.7.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
interface WC_WCCOM_Site_Installation_Step {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $state The current installation state.
|
||||
*/
|
||||
public function __construct( $state );
|
||||
|
||||
/**
|
||||
* Run the step installation process.
|
||||
*/
|
||||
public function run();
|
||||
}
|
||||
Reference in New Issue
Block a user