2019-06-26 03:09:56 +00:00
|
|
|
<?php
|
2019-07-04 11:01:33 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce.com Product Installation.
|
|
|
|
*
|
2020-08-05 20:49:10 +00:00
|
|
|
* @package WooCommerce\WCCom
|
2019-07-15 16:13:16 +00:00
|
|
|
* @since 3.7.0
|
2019-07-04 11:01:33 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-15 15:58:41 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
/**
|
2019-07-10 15:47:44 +00:00
|
|
|
* WC_WCCOM_Site_Installer Class
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-10 15:47:44 +00:00
|
|
|
* Contains functionalities to install products via WooCommerce.com helper connection.
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
2019-07-10 15:47:44 +00:00
|
|
|
class WC_WCCOM_Site_Installer {
|
2019-07-15 16:13:16 +00:00
|
|
|
|
2019-11-29 14:40:53 +00:00
|
|
|
/**
|
|
|
|
* Error message returned install_package if the folder already exists.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $folder_exists = 'folder_exists';
|
|
|
|
|
2019-06-26 03:09:56 +00:00
|
|
|
/**
|
|
|
|
* Default state.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $default_state = array(
|
|
|
|
'status' => 'idle',
|
|
|
|
'steps' => array(),
|
2019-07-10 18:44:31 +00:00
|
|
|
'current_step' => null,
|
2019-06-26 03:09:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents product step state.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $default_step_state = array(
|
2019-07-15 14:41:53 +00:00
|
|
|
'download_url' => '',
|
2019-07-08 03:31:01 +00:00
|
|
|
'product_type' => '',
|
|
|
|
'last_step' => '',
|
|
|
|
'last_error' => '',
|
|
|
|
'download_path' => '',
|
|
|
|
'unpacked_path' => '',
|
|
|
|
'installed_path' => '',
|
2019-07-10 18:40:24 +00:00
|
|
|
'activate' => false,
|
2019-06-26 03:09:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Product install steps. Each step is a method name in this class that
|
|
|
|
* will be passed with product ID arg \WP_Upgrader instance.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $install_steps = array(
|
2019-07-04 08:53:00 +00:00
|
|
|
'get_product_info',
|
2019-06-26 03:09:56 +00:00
|
|
|
'download_product',
|
|
|
|
'unpack_product',
|
|
|
|
'move_product',
|
|
|
|
'activate_product',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the product install state.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
* @param string $key Key in state data. If empty key is passed array of
|
|
|
|
* state will be returned.
|
|
|
|
* @return array Product install state.
|
|
|
|
*/
|
|
|
|
public static function get_state( $key = '' ) {
|
|
|
|
$state = WC_Helper_Options::get( 'product_install', self::$default_state );
|
|
|
|
if ( ! empty( $key ) ) {
|
|
|
|
return isset( $state[ $key ] ) ? $state[ $key ] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the product install state.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
* @param string $key Key in state data.
|
|
|
|
* @param mixed $value Value.
|
|
|
|
*/
|
|
|
|
public static function update_state( $key, $value ) {
|
|
|
|
$state = WC_Helper_Options::get( 'product_install', self::$default_state );
|
|
|
|
|
|
|
|
$state[ $key ] = $value;
|
|
|
|
WC_Helper_Options::update( 'product_install', $state );
|
|
|
|
}
|
|
|
|
|
2019-07-01 01:12:32 +00:00
|
|
|
/**
|
2019-07-08 02:40:28 +00:00
|
|
|
* Reset product install state.
|
2019-07-10 15:47:44 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-10 15:47:44 +00:00
|
|
|
* @param array $products List of product IDs.
|
2019-07-01 01:12:32 +00:00
|
|
|
*/
|
2019-07-10 15:47:44 +00:00
|
|
|
public static function reset_state( $products = array() ) {
|
|
|
|
WC()->queue()->cancel_all( 'woocommerce_wccom_install_products' );
|
2019-07-01 01:12:32 +00:00
|
|
|
WC_Helper_Options::update( 'product_install', self::$default_state );
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:09:56 +00:00
|
|
|
/**
|
2019-07-10 15:47:44 +00:00
|
|
|
* Schedule installing given list of products.
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-10 18:40:24 +00:00
|
|
|
* @param array $products Array of products where key is product ID and
|
|
|
|
* element is install args.
|
2019-07-01 09:06:02 +00:00
|
|
|
* @return array State.
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
2019-07-10 15:47:44 +00:00
|
|
|
public static function schedule_install( $products ) {
|
2019-06-26 03:09:56 +00:00
|
|
|
$state = self::get_state();
|
|
|
|
$status = ! empty( $state['status'] ) ? $state['status'] : '';
|
|
|
|
if ( 'in-progress' === $status ) {
|
|
|
|
return $state;
|
|
|
|
}
|
|
|
|
self::update_state( 'status', 'in-progress' );
|
|
|
|
|
2019-07-10 18:40:24 +00:00
|
|
|
$steps = array_fill_keys( array_keys( $products ), self::$default_step_state );
|
2019-06-26 03:09:56 +00:00
|
|
|
self::update_state( 'steps', $steps );
|
|
|
|
|
2019-07-10 18:44:31 +00:00
|
|
|
self::update_state( 'current_step', null );
|
|
|
|
|
2019-07-10 15:47:44 +00:00
|
|
|
$args = array(
|
|
|
|
'products' => $products,
|
|
|
|
);
|
|
|
|
|
2019-10-18 05:36:30 +00:00
|
|
|
// Clear the cache of customer's subscription before asking for them.
|
|
|
|
// Thus, they will be re-fetched from WooCommerce.com after a purchase.
|
|
|
|
WC_Helper::_flush_subscriptions_cache();
|
|
|
|
|
2019-07-10 15:47:44 +00:00
|
|
|
WC()->queue()->cancel_all( 'woocommerce_wccom_install_products', $args );
|
|
|
|
WC()->queue()->add( 'woocommerce_wccom_install_products', $args );
|
|
|
|
|
|
|
|
return self::get_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install a given product IDs.
|
|
|
|
*
|
|
|
|
* Run via `woocommerce_wccom_install_products` hook.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-10 18:40:24 +00:00
|
|
|
* @param array $products Array of products where key is product ID and
|
|
|
|
* element is install args.
|
2019-07-10 15:47:44 +00:00
|
|
|
*/
|
|
|
|
public static function install( $products ) {
|
2019-07-11 12:58:16 +00:00
|
|
|
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';
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
WP_Filesystem();
|
|
|
|
$upgrader = new WP_Upgrader( new Automatic_Upgrader_Skin() );
|
2019-07-01 09:26:51 +00:00
|
|
|
$upgrader->init();
|
2019-06-26 03:09:56 +00:00
|
|
|
wp_clean_plugins_cache();
|
|
|
|
|
2019-07-10 18:40:24 +00:00
|
|
|
foreach ( $products as $product_id => $install_args ) {
|
|
|
|
self::install_product( $product_id, $install_args, $upgrader );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
2019-07-01 09:06:02 +00:00
|
|
|
|
2019-07-10 15:47:44 +00:00
|
|
|
self::finish_installation();
|
2019-07-01 10:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish installation by updating the state.
|
2019-07-15 08:42:36 +00:00
|
|
|
*
|
|
|
|
* @since 3.7.0
|
2019-07-01 10:04:26 +00:00
|
|
|
*/
|
|
|
|
private static function finish_installation() {
|
|
|
|
$state = self::get_state();
|
|
|
|
if ( empty( $state['steps'] ) ) {
|
2019-07-10 15:47:44 +00:00
|
|
|
return;
|
2019-07-01 10:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $state['steps'] as $step ) {
|
|
|
|
if ( ! empty( $step['last_error'] ) ) {
|
|
|
|
$state['status'] = 'has_error';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'has_error' !== $state['status'] ) {
|
|
|
|
$state['status'] = 'finished';
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Helper_Options::update( 'product_install', $state );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install a single product given its ID.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-10 18:40:24 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @param array $install_args Install args.
|
|
|
|
* @param \WP_Upgrader $upgrader Core class to handle installation.
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
2019-07-10 18:40:24 +00:00
|
|
|
private static function install_product( $product_id, $install_args, $upgrader ) {
|
2019-07-01 09:06:02 +00:00
|
|
|
foreach ( self::$install_steps as $step ) {
|
2019-07-10 18:40:24 +00:00
|
|
|
self::do_install_step( $product_id, $install_args, $step, $upgrader );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform product installation step.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-10 18:40:24 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @param array $install_args Install args.
|
|
|
|
* @param string $step Installation step.
|
|
|
|
* @param \WP_Upgrader $upgrader Core class to handle installation.
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
2019-07-10 18:40:24 +00:00
|
|
|
private static function do_install_step( $product_id, $install_args, $step, $upgrader ) {
|
2019-06-26 03:09:56 +00:00
|
|
|
$state_steps = self::get_state( 'steps' );
|
|
|
|
if ( empty( $state_steps[ $product_id ] ) ) {
|
|
|
|
$state_steps[ $product_id ] = self::$default_step_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $state_steps[ $product_id ]['last_error'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$state_steps[ $product_id ]['last_step'] = $step;
|
|
|
|
|
2019-07-10 18:40:24 +00:00
|
|
|
if ( ! empty( $install_args['activate'] ) ) {
|
|
|
|
$state_steps[ $product_id ]['activate'] = true;
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:01:33 +00:00
|
|
|
self::update_state(
|
|
|
|
'current_step',
|
|
|
|
array(
|
|
|
|
'product_id' => $product_id,
|
|
|
|
'step' => $step,
|
|
|
|
)
|
|
|
|
);
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
$result = call_user_func( array( __CLASS__, $step ), $product_id, $upgrader );
|
|
|
|
if ( is_wp_error( $result ) ) {
|
|
|
|
$state_steps[ $product_id ]['last_error'] = $result->get_error_message();
|
2019-07-04 08:53:00 +00:00
|
|
|
} else {
|
|
|
|
switch ( $step ) {
|
|
|
|
case 'get_product_info':
|
|
|
|
$state_steps[ $product_id ]['download_url'] = $result['download_url'];
|
|
|
|
$state_steps[ $product_id ]['product_type'] = $result['product_type'];
|
2019-12-02 15:11:44 +00:00
|
|
|
$state_steps[ $product_id ]['product_name'] = $result['product_name'];
|
2019-07-04 08:53:00 +00:00
|
|
|
break;
|
|
|
|
case 'download_product':
|
|
|
|
$state_steps[ $product_id ]['download_path'] = $result;
|
|
|
|
break;
|
|
|
|
case 'unpack_product':
|
|
|
|
$state_steps[ $product_id ]['unpacked_path'] = $result;
|
|
|
|
break;
|
2019-07-08 03:31:01 +00:00
|
|
|
case 'move_product':
|
|
|
|
$state_steps[ $product_id ]['installed_path'] = $result['destination'];
|
2019-12-03 19:03:17 +00:00
|
|
|
if ( isset( $result[ self::$folder_exists ] ) ) {
|
2019-12-02 15:11:44 +00:00
|
|
|
$state_steps[ $product_id ]['warning'] = array(
|
|
|
|
'message' => self::$folder_exists,
|
|
|
|
'plugin_info' => self::get_plugin_info( $state_steps[ $product_id ]['installed_path'] ),
|
|
|
|
);
|
2019-11-29 14:40:53 +00:00
|
|
|
}
|
2019-07-08 03:31:01 +00:00
|
|
|
break;
|
2019-07-04 08:53:00 +00:00
|
|
|
}
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 09:06:02 +00:00
|
|
|
self::update_state( 'steps', $state_steps );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-04 08:53:00 +00:00
|
|
|
* Get product info from its ID.
|
2019-07-01 09:06:02 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-01 09:06:02 +00:00
|
|
|
* @param int $product_id Product ID.
|
2019-10-17 04:52:00 +00:00
|
|
|
* @return array|\WP_Error
|
2019-07-01 09:06:02 +00:00
|
|
|
*/
|
2019-07-04 08:53:00 +00:00
|
|
|
private static function get_product_info( $product_id ) {
|
|
|
|
$product_info = array(
|
|
|
|
'download_url' => '',
|
|
|
|
'product_type' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get product info from woocommerce.com.
|
|
|
|
$request = WC_Helper_API::get(
|
|
|
|
add_query_arg(
|
|
|
|
array( 'product_id' => absint( $product_id ) ),
|
|
|
|
'info'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'authenticated' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( 200 !== wp_remote_retrieve_response_code( $request ) ) {
|
|
|
|
return new WP_Error( 'product_info_failed', __( 'Failed to retrieve product info from woocommerce.com', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = json_decode( wp_remote_retrieve_body( $request ), true );
|
|
|
|
|
|
|
|
$product_info['product_type'] = $result['_product_type'];
|
2019-12-02 15:11:44 +00:00
|
|
|
$product_info['product_name'] = $result['name'];
|
2019-07-04 08:53:00 +00:00
|
|
|
|
|
|
|
if ( ! empty( $result['_wporg_product'] ) && ! empty( $result['download_link'] ) ) {
|
|
|
|
// For wporg product, download is set already from info response.
|
|
|
|
$product_info['download_url'] = $result['download_link'];
|
2019-10-18 05:36:30 +00:00
|
|
|
} elseif ( ! WC_Helper::has_product_subscription( $product_id ) ) {
|
2019-07-04 08:53:00 +00:00
|
|
|
// Non-wporg product needs subscription.
|
2019-07-01 09:06:02 +00:00
|
|
|
return new WP_Error( 'missing_subscription', __( 'Missing product subscription', 'woocommerce' ) );
|
2019-10-18 05:36:30 +00:00
|
|
|
} else {
|
|
|
|
// 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 WP_Error( 'missing_product_package', __( 'Could not find product package.', 'woocommerce' ) );
|
|
|
|
}
|
2019-07-04 08:53:00 +00:00
|
|
|
|
2019-10-18 05:36:30 +00:00
|
|
|
$product_info['download_url'] = $updates[ $product_id ]['package'];
|
2019-07-01 09:06:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 08:53:00 +00:00
|
|
|
return $product_info;
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download product by its ID and returns the path of the zip package.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @param \WP_Upgrader $upgrader Core class to handle installation.
|
|
|
|
* @return \WP_Error|string
|
|
|
|
*/
|
|
|
|
private static function download_product( $product_id, $upgrader ) {
|
2019-07-04 08:53:00 +00:00
|
|
|
$steps = self::get_state( 'steps' );
|
|
|
|
if ( empty( $steps[ $product_id ]['download_url'] ) ) {
|
2019-07-09 06:35:51 +00:00
|
|
|
return new WP_Error( 'missing_download_url', __( 'Could not find download url for the product.', 'woocommerce' ) );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
2019-07-04 08:53:00 +00:00
|
|
|
return $upgrader->download_package( $steps[ $product_id ]['download_url'] );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-01 09:08:45 +00:00
|
|
|
* Unpack downloaded product.
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @param \WP_Upgrader $upgrader Core class to handle installation.
|
2019-07-08 03:25:25 +00:00
|
|
|
* @return \WP_Error|string
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
|
|
|
private static function unpack_product( $product_id, $upgrader ) {
|
|
|
|
$steps = self::get_state( 'steps' );
|
|
|
|
if ( empty( $steps[ $product_id ]['download_path'] ) ) {
|
2019-07-09 06:35:51 +00:00
|
|
|
return new WP_Error( 'missing_download_path', __( 'Could not find download path.', 'woocommerce' ) );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $upgrader->unpack_package( $steps[ $product_id ]['download_path'], true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-01 09:08:45 +00:00
|
|
|
* Move product to plugins directory.
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @param \WP_Upgrader $upgrader Core class to handle installation.
|
2019-07-08 03:25:25 +00:00
|
|
|
* @return array|\WP_Error
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
|
|
|
private static function move_product( $product_id, $upgrader ) {
|
|
|
|
$steps = self::get_state( 'steps' );
|
|
|
|
if ( empty( $steps[ $product_id ]['unpacked_path'] ) ) {
|
2019-07-09 06:35:51 +00:00
|
|
|
return new WP_Error( 'missing_unpacked_path', __( 'Could not find unpacked path.', 'woocommerce' ) );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 14:19:42 +00:00
|
|
|
$destination = 'plugin' === $steps[ $product_id ]['product_type']
|
|
|
|
? WP_PLUGIN_DIR
|
|
|
|
: get_theme_root();
|
|
|
|
|
|
|
|
$package = array(
|
|
|
|
'source' => $steps[ $product_id ]['unpacked_path'],
|
|
|
|
'destination' => $destination,
|
|
|
|
'clear_working' => true,
|
|
|
|
'hook_extra' => array(
|
|
|
|
'type' => $steps[ $product_id ]['product_type'],
|
|
|
|
'action' => 'install',
|
|
|
|
),
|
2019-07-04 11:01:33 +00:00
|
|
|
);
|
2019-07-08 14:19:42 +00:00
|
|
|
|
2019-11-29 14:40:53 +00:00
|
|
|
$result = $upgrader->install_package( $package );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If install package returns error 'folder_exists' threat as success.
|
|
|
|
*/
|
|
|
|
if ( is_wp_error( $result ) && array_key_exists( self::$folder_exists, $result->errors ) ) {
|
|
|
|
return array(
|
|
|
|
self::$folder_exists => true,
|
|
|
|
'destination' => $result->error_data[ self::$folder_exists ],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $result;
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate product given its product ID.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
* @param int $product_id Product ID.
|
2019-07-08 03:25:25 +00:00
|
|
|
* @return \WP_Error|null
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
|
|
|
private static function activate_product( $product_id ) {
|
2019-07-08 14:19:42 +00:00
|
|
|
$steps = self::get_state( 'steps' );
|
2019-07-10 18:40:24 +00:00
|
|
|
if ( ! $steps[ $product_id ]['activate'] ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-08 14:19:42 +00:00
|
|
|
if ( 'plugin' === $steps[ $product_id ]['product_type'] ) {
|
|
|
|
return self::activate_plugin( $product_id );
|
|
|
|
}
|
|
|
|
return self::activate_theme( $product_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate plugin given its product ID.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-08 14:19:42 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @return \WP_Error|null
|
|
|
|
*/
|
|
|
|
private static function activate_plugin( $product_id ) {
|
2019-07-01 09:26:51 +00:00
|
|
|
// Clear plugins cache used in `WC_Helper::get_local_woo_plugins`.
|
|
|
|
wp_clean_plugins_cache();
|
2019-07-08 08:12:58 +00:00
|
|
|
$filename = false;
|
2019-07-01 09:26:51 +00:00
|
|
|
|
2019-07-08 08:12:58 +00:00
|
|
|
// If product is WP.org one, find out its filename.
|
2019-07-08 09:44:26 +00:00
|
|
|
$dir_name = self::get_wporg_product_dir_name( $product_id );
|
|
|
|
if ( false !== $dir_name ) {
|
|
|
|
$filename = self::get_wporg_plugin_main_file( $dir_name );
|
2019-07-08 08:12:58 +00:00
|
|
|
}
|
2019-06-26 03:09:56 +00:00
|
|
|
|
2019-07-08 08:12:58 +00:00
|
|
|
if ( false === $filename ) {
|
|
|
|
$plugins = wp_list_filter(
|
|
|
|
WC_Helper::get_local_woo_plugins(),
|
|
|
|
array(
|
|
|
|
'_product_id' => $product_id,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-07-15 16:13:16 +00:00
|
|
|
$filename = is_array( $plugins ) && ! empty( $plugins ) ? key( $plugins ) : '';
|
2019-07-08 08:12:58 +00:00
|
|
|
}
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
if ( empty( $filename ) ) {
|
|
|
|
return new WP_Error( 'unknown_filename', __( 'Unknown product filename.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return activate_plugin( $filename );
|
|
|
|
}
|
2019-07-08 08:12:58 +00:00
|
|
|
|
|
|
|
/**
|
2019-07-08 14:19:42 +00:00
|
|
|
* Activate theme given its product ID.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-08 14:19:42 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @return \WP_Error|null
|
|
|
|
*/
|
|
|
|
private static 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 = self::get_wporg_product_dir_name( $product_id );
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-07-15 16:13:16 +00:00
|
|
|
$theme_slug = is_array( $themes ) && ! empty( $themes ) ? dirname( key( $themes ) ) : '';
|
2019-07-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $theme_slug ) ) {
|
|
|
|
return new WP_Error( 'unknown_filename', __( 'Unknown product filename.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return switch_theme( $theme_slug );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get installed directory of WP.org product.
|
2019-07-08 08:12:58 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-08 08:12:58 +00:00
|
|
|
* @param int $product_id Product ID.
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
2019-07-08 09:44:26 +00:00
|
|
|
private static function get_wporg_product_dir_name( $product_id ) {
|
2019-07-11 12:58:16 +00:00
|
|
|
$steps = self::get_state( 'steps' );
|
2019-07-08 08:12:58 +00:00
|
|
|
$product = $steps[ $product_id ];
|
|
|
|
|
|
|
|
if ( empty( $product['download_url'] ) || empty( $product['installed_path'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether product was downloaded from WordPress.org.
|
2019-07-11 12:58:16 +00:00
|
|
|
$parsed_url = wp_parse_url( $product['download_url'] );
|
|
|
|
if ( ! empty( $parsed_url['host'] ) && 'downloads.wordpress.org' !== $parsed_url['host'] ) {
|
2019-07-08 08:12:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return basename( $product['installed_path'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-08 14:19:42 +00:00
|
|
|
* Get WP.org plugin's main file.
|
2019-07-08 08:12:58 +00:00
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-08 14:19:42 +00:00
|
|
|
* @param string $dir Directory name of the plugin.
|
2019-07-08 08:12:58 +00:00
|
|
|
* @return bool|string
|
|
|
|
*/
|
2019-07-08 14:19:42 +00:00
|
|
|
private static function get_wporg_plugin_main_file( $dir ) {
|
|
|
|
// Ensure that exact dir name is used.
|
2019-07-15 15:57:16 +00:00
|
|
|
$dir = trailingslashit( $dir );
|
2019-07-08 08:12:58 +00:00
|
|
|
|
|
|
|
if ( ! function_exists( 'get_plugins' ) ) {
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
$plugins = get_plugins();
|
|
|
|
foreach ( $plugins as $path => $plugin ) {
|
2019-07-08 14:19:42 +00:00
|
|
|
if ( 0 === strpos( $path, $dir ) ) {
|
2019-07-08 08:12:58 +00:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-02 15:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get plugin info
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @param string $dir Directory name of the plugin.
|
|
|
|
* @return bool|array
|
|
|
|
*/
|
|
|
|
private static function get_plugin_info( $dir ) {
|
|
|
|
$plugin_folder = basename( $dir );
|
|
|
|
|
|
|
|
if ( ! function_exists( 'get_plugins' ) ) {
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
$plugins = get_plugins();
|
|
|
|
|
|
|
|
$related_plugins = array_filter(
|
|
|
|
$plugins,
|
|
|
|
function( $key ) use ( $plugin_folder ) {
|
|
|
|
return strpos( $key, $plugin_folder . '/' ) === 0;
|
|
|
|
},
|
|
|
|
ARRAY_FILTER_USE_KEY
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( 1 === count( $related_plugins ) ) {
|
2019-12-20 17:21:08 +00:00
|
|
|
$plugin_key = array_keys( $related_plugins )[0];
|
2019-12-02 15:11:44 +00:00
|
|
|
$plugin_data = $plugins[ $plugin_key ];
|
|
|
|
return array(
|
|
|
|
'name' => $plugin_data['Name'],
|
|
|
|
'version' => $plugin_data['Version'],
|
|
|
|
'active' => is_plugin_active( $plugin_key ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|