initial commit
This commit is contained in:
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* WCCOM Site Installer Errors Class
|
||||
*
|
||||
* @package WooCommerce\WCCom\API
|
||||
* @since 3.9.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WCCOM Site Installer Errors Class
|
||||
*
|
||||
* Stores data for errors, returned by installer API.
|
||||
*/
|
||||
class WC_REST_WCCOM_Site_Installer_Errors {
|
||||
|
||||
/**
|
||||
* Not unauthenticated generic error
|
||||
*/
|
||||
const NOT_AUTHENTICATED_CODE = 'not_authenticated';
|
||||
const NOT_AUTHENTICATED_MESSAGE = 'Authentication required';
|
||||
const NOT_AUTHENTICATED_HTTP_CODE = 401;
|
||||
|
||||
/**
|
||||
* No access token provided
|
||||
*/
|
||||
const NO_ACCESS_TOKEN_CODE = 'no_access_token';
|
||||
const NO_ACCESS_TOKEN_MESSAGE = 'No access token provided';
|
||||
const NO_ACCESS_TOKEN_HTTP_CODE = 400;
|
||||
|
||||
/**
|
||||
* No signature provided
|
||||
*/
|
||||
const NO_SIGNATURE_CODE = 'no_signature';
|
||||
const NO_SIGNATURE_MESSAGE = 'No signature provided';
|
||||
const NO_SIGNATURE_HTTP_CODE = 400;
|
||||
|
||||
/**
|
||||
* Site not connected to WooCommerce.com
|
||||
*/
|
||||
const SITE_NOT_CONNECTED_CODE = 'site_not_connnected';
|
||||
const SITE_NOT_CONNECTED_MESSAGE = 'Site not connected to WooCommerce.com';
|
||||
const SITE_NOT_CONNECTED_HTTP_CODE = 401;
|
||||
|
||||
/**
|
||||
* Provided access token is not valid
|
||||
*/
|
||||
const INVALID_TOKEN_CODE = 'invalid_token';
|
||||
const INVALID_TOKEN_MESSAGE = 'Invalid access token provided';
|
||||
const INVALID_TOKEN_HTTP_CODE = 401;
|
||||
|
||||
/**
|
||||
* Request verification by provided signature failed
|
||||
*/
|
||||
const REQUEST_VERIFICATION_FAILED_CODE = 'request_verification_failed';
|
||||
const REQUEST_VERIFICATION_FAILED_MESSAGE = 'Request verification by signature failed';
|
||||
const REQUEST_VERIFICATION_FAILED_HTTP_CODE = 400;
|
||||
|
||||
/**
|
||||
* User doesn't exist
|
||||
*/
|
||||
const USER_NOT_FOUND_CODE = 'user_not_found';
|
||||
const USER_NOT_FOUND_MESSAGE = 'Token owning user not found';
|
||||
const USER_NOT_FOUND_HTTP_CODE = 401;
|
||||
|
||||
/**
|
||||
* No permissions error
|
||||
*/
|
||||
const NO_PERMISSION_CODE = 'forbidden';
|
||||
const NO_PERMISSION_MESSAGE = 'You do not have permission to install plugin or theme';
|
||||
const NO_PERMISSION_HTTP_CODE = 403;
|
||||
}
|
@ -0,0 +1,183 @@
|
||||
<?php
|
||||
/**
|
||||
* WCCOM Site Installer REST API Controller
|
||||
*
|
||||
* Handles requests to /installer.
|
||||
*
|
||||
* @package WooCommerce\WCCom\API
|
||||
* @since 3.7.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* REST API WCCOM Site Installer Controller Class.
|
||||
*
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wccom-site/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'installer';
|
||||
|
||||
/**
|
||||
* Register the routes for product reviews.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$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' ),
|
||||
'args' => array(
|
||||
'products' => array(
|
||||
'required' => true,
|
||||
'type' => 'object',
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'reset_install' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check permissions.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @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();
|
||||
|
||||
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' ) ) {
|
||||
return new WP_Error(
|
||||
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 )
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get installation state.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_install_state( $request ) {
|
||||
$requirements_met = WC_WCCOM_Site_Installer_Requirements_Check::met_requirements();
|
||||
if ( is_wp_error( $requirements_met ) ) {
|
||||
return $requirements_met;
|
||||
}
|
||||
|
||||
return rest_ensure_response( WC_WCCOM_Site_Installer::get_state() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Install WooCommerce.com products.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function install( $request ) {
|
||||
$requirements_met = WC_WCCOM_Site_Installer_Requirements_Check::met_requirements();
|
||||
if ( is_wp_error( $requirements_met ) ) {
|
||||
return $requirements_met;
|
||||
}
|
||||
|
||||
if ( empty( $request['products'] ) ) {
|
||||
return new WP_Error( 'missing_products', __( 'Missing products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$validation_result = $this->validate_products( $request['products'] );
|
||||
if ( is_wp_error( $validation_result ) ) {
|
||||
return $validation_result;
|
||||
}
|
||||
|
||||
return rest_ensure_response( WC_WCCOM_Site_Installer::schedule_install( $request['products'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset installation state.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @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.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user