2019-06-26 03:09:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-07-09 08:28:43 +00:00
|
|
|
* WCCOM Site Installer REST API Controller
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-09 08:28:43 +00:00
|
|
|
* Handles requests to /installer.
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-15 16:13:16 +00:00
|
|
|
* @package WooCommerce\WooCommerce_Site\Rest_Api
|
2019-06-26 03:09:56 +00:00
|
|
|
* @since 3.7.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
2019-07-15 16:19:03 +00:00
|
|
|
* REST API WCCOM Site Installer Controller Class.
|
2019-06-26 03:09:56 +00:00
|
|
|
*
|
2019-07-11 11:19:08 +00:00
|
|
|
* @package WooCommerce/WCCOM_Site/REST_API
|
2019-06-26 03:09:56 +00:00
|
|
|
* @extends WC_REST_Controller
|
|
|
|
*/
|
2019-07-15 16:19:03 +00:00
|
|
|
class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller {
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-07-09 08:28:43 +00:00
|
|
|
protected $namespace = 'wccom-site/v1';
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-07-09 08:28:43 +00:00
|
|
|
protected $rest_base = 'installer';
|
2019-06-26 03:09:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the routes for product reviews.
|
2019-07-15 08:42:36 +00:00
|
|
|
*
|
|
|
|
* @since 3.7.0
|
2019-06-26 03:09:56 +00:00
|
|
|
*/
|
|
|
|
public function register_routes() {
|
|
|
|
register_rest_route(
|
2019-07-11 12:58:16 +00:00
|
|
|
$this->namespace,
|
|
|
|
'/' . $this->rest_base,
|
|
|
|
array(
|
2019-06-26 03:09:56 +00:00
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( $this, 'get_install_state' ),
|
|
|
|
'permission_callback' => array( $this, 'check_permission' ),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
|
|
'callback' => array( $this, 'install' ),
|
|
|
|
'permission_callback' => array( $this, 'check_permission' ),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::DELETABLE,
|
|
|
|
'callback' => array( $this, 'reset_install' ),
|
|
|
|
'permission_callback' => array( $this, 'check_permission' ),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-09 08:55:40 +00:00
|
|
|
/**
|
|
|
|
* Check permissions.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-09 08:55:40 +00:00
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return bool|WP_Error
|
|
|
|
*/
|
2019-06-26 03:09:56 +00:00
|
|
|
public function check_permission( $request ) {
|
2019-11-25 16:32:10 +00:00
|
|
|
$current_user = wp_get_current_user();
|
|
|
|
|
|
|
|
if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) {
|
|
|
|
return apply_filters(
|
|
|
|
WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
|
|
|
|
new WP_Error(
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NOT_AUTHENTICATED_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NOT_AUTHENTICATED_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::NOT_AUTHENTICATED_HTTP_CODE )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! user_can( $current_user, 'install_plugins' ) || ! user_can( $current_user, 'install_themes' ) ) {
|
2019-11-25 09:47:18 +00:00
|
|
|
return new WP_Error(
|
2019-11-25 16:32:10 +00:00
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NO_PERMISSION_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NO_PERMISSION_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::NO_PERMISSION_HTTP_CODE )
|
2019-11-25 09:47:18 +00:00
|
|
|
);
|
2019-07-01 01:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:55:40 +00:00
|
|
|
/**
|
|
|
|
* Get installation state.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-09 08:55:40 +00:00
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return bool|WP_Error
|
|
|
|
*/
|
2019-06-26 03:09:56 +00:00
|
|
|
public function get_install_state( $request ) {
|
2019-10-01 19:45:30 +00:00
|
|
|
$requirements_met = WC_WCCOM_Site_Installer_Requirements_Check::met_requirements();
|
|
|
|
if ( is_wp_error( $requirements_met ) ) {
|
|
|
|
return $requirements_met;
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:47:44 +00:00
|
|
|
return rest_ensure_response( WC_WCCOM_Site_Installer::get_state() );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:55:40 +00:00
|
|
|
/**
|
|
|
|
* Install WooCommerce.com products.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-09 08:55:40 +00:00
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return bool|WP_Error
|
|
|
|
*/
|
2019-06-26 03:09:56 +00:00
|
|
|
public function install( $request ) {
|
2019-09-30 21:20:11 +00:00
|
|
|
$requirements_met = WC_WCCOM_Site_Installer_Requirements_Check::met_requirements();
|
|
|
|
if ( is_wp_error( $requirements_met ) ) {
|
|
|
|
return $requirements_met;
|
|
|
|
}
|
|
|
|
|
2019-07-15 16:30:17 +00:00
|
|
|
if ( empty( $request['products'] ) ) {
|
2019-07-01 09:06:02 +00:00
|
|
|
return new WP_Error( 'missing_products', __( 'Missing products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
|
|
|
|
}
|
|
|
|
|
2019-07-15 16:30:17 +00:00
|
|
|
$validation_result = $this->validate_products( $request['products'] );
|
2019-07-10 18:40:24 +00:00
|
|
|
if ( is_wp_error( $validation_result ) ) {
|
|
|
|
return $validation_result;
|
|
|
|
}
|
|
|
|
|
2019-07-15 16:30:17 +00:00
|
|
|
return rest_ensure_response( WC_WCCOM_Site_Installer::schedule_install( $request['products'] ) );
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:55:40 +00:00
|
|
|
/**
|
|
|
|
* Reset installation state.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-09 08:55:40 +00:00
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return bool|WP_Error
|
|
|
|
*/
|
2019-06-26 03:09:56 +00:00
|
|
|
public function reset_install( $request ) {
|
2019-07-10 15:47:44 +00:00
|
|
|
$resp = rest_ensure_response( WC_WCCOM_Site_Installer::reset_state() );
|
2019-07-01 01:14:00 +00:00
|
|
|
$resp->set_status( 204 );
|
2019-06-26 03:09:56 +00:00
|
|
|
|
2019-07-01 01:14:00 +00:00
|
|
|
return $resp;
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|
2019-07-10 18:40:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate products from request body.
|
|
|
|
*
|
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.
|
|
|
|
* @return bool|WP_Error
|
|
|
|
*/
|
|
|
|
protected function validate_products( $products ) {
|
|
|
|
$err = new WP_Error( 'invalid_products', __( 'Invalid products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
|
|
|
|
|
|
|
|
if ( ! is_array( $products ) ) {
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $products as $product_id => $install_args ) {
|
|
|
|
if ( ! absint( $product_id ) ) {
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $install_args ) || ! is_array( $install_args ) ) {
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-26 03:09:56 +00:00
|
|
|
}
|