plugin updates

This commit is contained in:
Tony Volpe
2024-02-21 16:19:46 +00:00
parent c72f206574
commit 21d4c85c00
1214 changed files with 102269 additions and 179257 deletions

View File

@@ -0,0 +1,78 @@
<?php
/**
* WCCOM Site Base REST API Controller
*
* Handles requests to /ssr.
*
* @package WooCommerce\WCCom\API
* @since 8.6.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;
/**
* REST API WCCOM Site Base REST API Controller Astract Class.
*
* @extends WC_REST_Controller
*/
abstract class WC_REST_WCCOM_Site_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wccom-site/v3';
/**
* Check whether user has permission to access controller's endpoints.
*
* @since 8.6.0
* @param WP_USER $user User object.
* @return bool
*/
abstract protected function user_has_permission( $user ) : bool;
/**
* Check permissions.
*
* Please note that access to this endpoint is also governed by the WC_WCCOM_Site::authenticate_wccom() method.
*
* @since 7.8.0
* @return bool|WP_Error
*/
public function check_permission() {
$current_user = wp_get_current_user();
if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) {
/**
* This filter allows to provide a custom error message when the user is not authenticated.
*
* @since 3.7.0
*/
$error = apply_filters(
WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED )
);
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
if ( ! $this->user_has_permission( $current_user ) ) {
$error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION );
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
return true;
}
}

View File

@@ -1,234 +0,0 @@
<?php
/**
* WCCOM Site Installer REST API Controller Version 2
*
* Handles requests to /installer.
*
* @package WooCommerce\WCCom\API
* @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;
/**
* REST API WCCOM Site Installer Controller Class.
*
* @extends WC_REST_Controller
*/
class WC_REST_WCCOM_Site_Installer_Controller_V2 extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wccom-site/v2';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'installer';
/**
* Register the routes for product reviews.
*
* @since 7.7.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install' ),
'permission_callback' => array( $this, 'check_permission' ),
'args' => array(
'product-id' => array(
'required' => true,
'type' => 'integer',
),
'run-until-step' => array(
'required' => true,
'type' => 'string',
'enum' => WC_WCCOM_Site_Installation_Manager::STEPS,
),
'idempotency-key' => array(
'required' => true,
'type' => 'string',
),
),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'reset_install' ),
'permission_callback' => array( $this, 'check_permission' ),
'args' => array(
'product-id' => array(
'required' => true,
'type' => 'integer',
),
'idempotency-key' => array(
'required' => true,
'type' => 'string',
),
),
),
)
);
}
/**
* Check permissions.
*
* @since 7.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() ) ) {
/**
* This filter allows to provide a custom error message when the user is not authenticated.
*
* @since 3.7.0
*/
$error = apply_filters(
WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED )
);
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
if ( ! user_can( $current_user, 'install_plugins' ) || ! user_can( $current_user, 'install_themes' ) ) {
$error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION );
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
return true;
}
/**
* Install Woo.com products.
*
* @since 7.7.0
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function install( $request ) {
try {
$product_id = $request['product-id'];
$run_until_step = $request['run-until-step'];
$idempotency_key = $request['idempotency-key'];
$installation_manager = new WC_WCCOM_Site_Installation_Manager( $product_id, $idempotency_key );
$installation_manager->run_installation( $run_until_step );
$response = $this->success_response( $product_id );
} catch ( Installer_Error $exception ) {
$response = $this->failure_response( $product_id, $exception );
}
return $response;
}
/**
* Reset installation state.
*
* @since 7.7.0
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function reset_install( $request ) {
try {
$product_id = $request['product-id'];
$idempotency_key = $request['idempotency-key'];
$installation_manager = new WC_WCCOM_Site_Installation_Manager( $product_id, $idempotency_key );
$installation_manager->reset_installation();
$response = $this->success_response( $product_id );
} catch ( Installer_Error $exception ) {
$response = $this->failure_response( $product_id, $exception );
}
return $response;
}
/**
* Generate a standardized response for a successful request.
*
* @param int $product_id Product ID.
* @return WP_REST_Response|WP_Error
*/
protected function success_response( $product_id ) {
$state = WC_WCCOM_Site_Installation_State_Storage::get_state( $product_id );
$response = rest_ensure_response(
array(
'success' => true,
'state' => $state ? $this->map_state_to_response( $state ) : null,
)
);
$response->set_status( 200 );
return $response;
}
/**
* Generate a standardized response for a failed request.
*
* @param int $product_id Product ID.
* @param Installer_Error $exception The exception.
* @return WP_REST_Response|WP_Error
*/
protected function failure_response( $product_id, $exception ) {
$state = WC_WCCOM_Site_Installation_State_Storage::get_state( $product_id );
$response = rest_ensure_response(
array(
'success' => false,
'error_code' => $exception->get_error_code(),
'error_message' => $exception->get_error_message(),
'state' => $state ? $this->map_state_to_response( $state ) : null,
)
);
$response->set_status( $exception->get_http_code() );
return $response;
}
/**
* Map the installation state to a response.
*
* @param WC_WCCOM_Site_Installation_State $state The installation state.
* @return array
*/
protected function map_state_to_response( $state ) {
return array(
'product_id' => $state->get_product_id(),
'idempotency_key' => $state->get_idempotency_key(),
'last_step_name' => $state->get_last_step_name(),
'last_step_status' => $state->get_last_step_status(),
'last_step_error' => $state->get_last_step_error(),
'product_type' => $state->get_product_type(),
'product_name' => $state->get_product_name(),
'already_installed_plugin_info' => $state->get_already_installed_plugin_info(),
'started_seconds_ago' => time() - $state->get_started_date(),
);
}
}

View File

@@ -1,14 +1,13 @@
<?php
/**
* WCCOM Site Installer REST API Controller
* WCCOM Site Installer REST API Controller Version
*
* Handles requests to /installer.
*
* @package WooCommerce\WCCom\API
* @since 3.7.0
* @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;
@@ -16,16 +15,9 @@ defined( 'ABSPATH' ) || exit;
/**
* REST API WCCOM Site Installer Controller Class.
*
* @extends WC_REST_Controller
* @extends WC_REST_WCCOM_Site_Controller
*/
class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wccom-site/v1';
class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_WCCOM_Site_Controller {
/**
* Route base.
@@ -35,159 +27,179 @@ class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller {
protected $rest_base = 'installer';
/**
* Register the routes for WCCCOM Installer Controller.
* Register the routes for plugin auto-installer.
*
* @since 3.7.0
* @since 7.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,
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install' ),
'permission_callback' => array( $this, 'check_permission' ),
'args' => array(
'products' => array(
'product-id' => array(
'required' => true,
'type' => 'object',
'type' => 'integer',
),
'run-until-step' => array(
'required' => true,
'type' => 'string',
'enum' => WC_WCCOM_Site_Installation_Manager::STEPS,
),
'idempotency-key' => array(
'required' => true,
'type' => 'string',
),
),
),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/reset',
array(
array(
'methods' => WP_REST_Server::DELETABLE,
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'reset_install' ),
'permission_callback' => array( $this, 'check_permission' ),
'args' => array(
'product-id' => array(
'required' => true,
'type' => 'integer',
),
'idempotency-key' => array(
'required' => true,
'type' => 'string',
),
),
),
)
);
}
/**
* Check permissions.
* Check whether user has permission to access controller's endpoints.
*
* @since 3.7.0
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
* @since 8.6.0
* @param WP_USER $user User object.
* @return bool
*/
public function check_permission( $request ) {
$current_user = wp_get_current_user();
if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) {
/**
* This filter allows to provide a custom error message when the user is not authenticated.
*
* @since 3.7.0
*/
$error = apply_filters(
WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED )
);
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
if ( ! user_can( $current_user, 'install_plugins' ) || ! user_can( $current_user, 'install_themes' ) ) {
$error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION );
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_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() );
public function user_has_permission( $user ) : bool {
return user_can( $user, 'install_plugins' ) && user_can( $user, 'install_themes' );
}
/**
* Install Woo.com products.
*
* @since 3.7.0
* @since 7.7.0
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
* @return WP_REST_Response|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;
try {
$product_id = $request['product-id'];
$run_until_step = $request['run-until-step'];
$idempotency_key = $request['idempotency-key'];
$installation_manager = new WC_WCCOM_Site_Installation_Manager( $product_id, $idempotency_key );
$installation_manager->run_installation( $run_until_step );
$response = $this->success_response( $product_id );
} catch ( Installer_Error $exception ) {
$response = $this->failure_response( $product_id, $exception );
}
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'] ) );
return $response;
}
/**
* Reset installation state.
*
* @since 3.7.0
* @since 7.7.0
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
* @return WP_REST_Response|WP_Error
*/
public function reset_install( $request ) {
$resp = rest_ensure_response( WC_WCCOM_Site_Installer::reset_state() );
$resp->set_status( 204 );
try {
$product_id = $request['product-id'];
$idempotency_key = $request['idempotency-key'];
return $resp;
$installation_manager = new WC_WCCOM_Site_Installation_Manager( $product_id, $idempotency_key );
$installation_manager->reset_installation();
$response = $this->success_response( $product_id );
} catch ( Installer_Error $exception ) {
$response = $this->failure_response( $product_id, $exception );
}
return $response;
}
/**
* Validate products from request body.
* Generate a standardized response for a successful request.
*
* @since 3.7.0
* @param array $products Array of products where key is product ID and
* element is install args.
* @return bool|WP_Error
* @param int $product_id Product ID.
* @return WP_REST_Response|WP_Error
*/
protected function validate_products( $products ) {
$err = new WP_Error( 'invalid_products', __( 'Invalid products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
protected function success_response( $product_id ) {
$state = WC_WCCOM_Site_Installation_State_Storage::get_state( $product_id );
$response = rest_ensure_response(
array(
'success' => true,
'state' => $state ? $this->map_state_to_response( $state ) : null,
)
);
$response->set_status( 200 );
return $response;
}
if ( ! is_array( $products ) ) {
return $err;
}
/**
* Generate a standardized response for a failed request.
*
* @param int $product_id Product ID.
* @param Installer_Error $exception The exception.
* @return WP_REST_Response|WP_Error
*/
protected function failure_response( $product_id, $exception ) {
$state = WC_WCCOM_Site_Installation_State_Storage::get_state( $product_id );
$response = rest_ensure_response(
array(
'success' => false,
'error_code' => $exception->get_error_code(),
'error_message' => $exception->get_error_message(),
'state' => $state ? $this->map_state_to_response( $state ) : null,
)
);
$response->set_status( $exception->get_http_code() );
return $response;
}
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;
/**
* Map the installation state to a response.
*
* @param WC_WCCOM_Site_Installation_State $state The installation state.
* @return array
*/
protected function map_state_to_response( $state ) {
return array(
'product_id' => $state->get_product_id(),
'idempotency_key' => $state->get_idempotency_key(),
'last_step_name' => $state->get_last_step_name(),
'last_step_status' => $state->get_last_step_status(),
'last_step_error' => $state->get_last_step_error(),
'product_type' => $state->get_product_type(),
'product_name' => $state->get_product_name(),
'already_installed_plugin_info' => $state->get_already_installed_plugin_info(),
'started_seconds_ago' => time() - $state->get_started_date(),
);
}
}

View File

@@ -8,24 +8,14 @@
* @since 7.8.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;
/**
* REST API WCCOM System Status Report Controller Class.
*
* @extends WC_REST_Controller
* @extends WC_REST_WCCOM_Site_Controller
*/
class WC_REST_WCCOM_Site_SSR_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wccom-site/v1';
class WC_REST_WCCOM_Site_SSR_Controller extends WC_REST_WCCOM_Site_Controller {
/**
* Route base.
@@ -54,44 +44,14 @@ class WC_REST_WCCOM_Site_SSR_Controller extends WC_REST_Controller {
}
/**
* Check permissions.
* Check whether user has permission to access controller's endpoints.
*
* Please note that access to this endpoint is also governed by the WC_WCCOM_Site::authenticate_wccom() method.
*
* @since 7.8.0
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
* @since 8.6.0
* @param WP_USER $user User object.
* @return bool
*/
public function check_permission( $request ) {
$current_user = wp_get_current_user();
if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) {
/**
* This filter allows to provide a custom error message when the user is not authenticated.
*
* @since 7.8.0
*/
$error = apply_filters(
WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED )
);
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
if ( ! user_can( $current_user, 'manage_woocommerce' ) ) {
$error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION );
return new WP_Error(
$error->get_error_code(),
$error->get_error_message(),
array( 'status' => $error->get_http_code() )
);
}
return true;
public function user_has_permission( $user ) : bool {
return user_can( $user, 'manage_woocommerce' );
}
/**