Move "no permissions" error data (error code, message, http code) to

a constant within a dedicated error messages class.
This commit is contained in:
Denis Dvali 2019-11-25 12:47:18 +03:00
parent ac2bb33ba0
commit 234bb9139f
3 changed files with 30 additions and 1 deletions

View File

@ -166,6 +166,7 @@ class WC_WCCOM_Site {
* @return array Registered namespaces.
*/
public static function register_rest_namespace( $namespaces ) {
require_once WC_ABSPATH . 'includes/wccom-site/rest-api/class-wc-rest-wccom-site-installer-errors.php';
require_once WC_ABSPATH . 'includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php';
$namespaces['wccom-site/v1'] = array(

View File

@ -0,0 +1,24 @@
<?php
/**
* WCCOM Site Installer Errors Class
*
* @package WooCommerce\WooCommerce_Site\Rest_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 {
/**
* No permissions error
*/
const NO_PERMISSION_CODE = 'woocommerce_rest_cannot_install_product';
const NO_PERMISSION_MESSAGE = 'You do not have permission to install plugin or theme';
const NO_PERMISSION_HTTP_CODE = 401;
}

View File

@ -70,7 +70,11 @@ class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller {
*/
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 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;