woocommerce/includes/wccom-site/rest-api/v1/class-wc-rest-wccom-site-in...

158 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
2019-07-09 08:28:43 +00:00
* WCCOM Site Installer REST API Controller
*
2019-07-09 08:28:43 +00:00
* Handles requests to /installer.
*
2019-07-15 16:13:16 +00:00
* @package WooCommerce\WooCommerce_Site\Rest_Api
* @since 3.7.0
*/
defined( 'ABSPATH' ) || exit;
/**
* REST API WCCOM Site Installer v1 Controller Class.
*
* @package WooCommerce/WCCOM_Site/REST_API
* @extends WC_REST_Controller
*/
class WC_REST_WCCOM_Site_Installer_V1_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
2019-07-09 08:28:43 +00:00
protected $namespace = 'wccom-site/v1';
/**
* Route base.
*
* @var string
*/
2019-07-09 08:28:43 +00:00
protected $rest_base = 'installer';
/**
* Register the routes for product reviews.
2019-07-15 08:42:36 +00:00
*
* @since 3.7.0
*/
public function register_routes() {
register_rest_route(
2019-07-11 12:58:16 +00:00
$this->namespace,
'/' . $this->rest_base,
array(
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
*/
public function check_permission( $request ) {
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'install_themes' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_install_product', __( 'You do not have permission to install plugin or theme', 'woocommerce' ), array( 'status' => 401 ) );
}
return true;
}
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
*/
public function get_install_state( $request ) {
return rest_ensure_response( WC_WCCOM_Site_Installer::get_state() );
}
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
*/
public function install( $request ) {
$body = WP_REST_Server::get_raw_data();
if ( empty( $body ) ) {
return new WP_Error( 'empty_body', __( 'Request body is empty.', 'woocommerce' ), array( 'status' => 400 ) );
}
$data = json_decode( $body, true );
if ( empty( $data['products'] ) ) {
return new WP_Error( 'missing_products', __( 'Missing products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
}
$validation_result = $this->validate_products( $data['products'] );
if ( is_wp_error( $validation_result ) ) {
return $validation_result;
}
return rest_ensure_response( WC_WCCOM_Site_Installer::schedule_install( $data['products'] ) );
}
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
*/
public function reset_install( $request ) {
$resp = rest_ensure_response( WC_WCCOM_Site_Installer::reset_state() );
$resp->set_status( 204 );
return $resp;
}
/**
* Validate products from request body.
*
2019-07-15 08:42:36 +00:00
* @since 3.7.0
* @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;
}
}