PSR4-ify the API init class and Notes controllers.

This commit is contained in:
Jeff Stieler 2019-08-02 12:05:18 -06:00
parent f871c96070
commit 9d2757f1b2
4 changed files with 26 additions and 19 deletions

View File

@ -7,9 +7,11 @@
* @package WooCommerce Admin/API
*/
namespace Automattic\WooCommerce\Admin\API\Notes\Actions;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
/**
* REST API Admin Note Action controller class.
@ -17,7 +19,7 @@ use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
* @package WooCommerce/API
* @extends WC_REST_CRUD_Controller
*/
class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Notes_Controller {
class Controller extends \Automattic\WooCommerce\Admin\API\Notes\Controller {
/**
* Register the routes for admin notes.
@ -38,7 +40,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not
),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'trigger_note_action' ),
// @todo - double check these permissions for taking note actions.
'permission_callback' => array( $this, 'get_item_permissions_check' ),
@ -58,7 +60,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not
$note = WC_Admin_Notes::get_note( $request->get_param( 'note_id' ) );
if ( ! $note ) {
return new WP_Error(
return new \WP_Error(
'woocommerce_admin_notes_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 )
@ -77,7 +79,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not
}
if ( ! $triggered_action ) {
return new WP_Error(
return new \WP_Error(
'woocommerce_admin_note_action_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 )

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API
*/
namespace Automattic\WooCommerce\Admin\API\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note;
@ -18,7 +20,7 @@ use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
* @package WooCommerce/API
* @extends WC_REST_CRUD_Controller
*/
class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
class Controller extends \WC_REST_CRUD_Controller {
/**
* Endpoint namespace.
@ -43,7 +45,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
@ -63,12 +65,12 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
),
),
array(
'methods' => WP_REST_Server::READABLE,
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
),
@ -87,7 +89,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
$note = WC_Admin_Notes::get_note( $request->get_param( 'id' ) );
if ( ! $note ) {
return new WP_Error(
return new \WP_Error(
'woocommerce_admin_notes_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 )
@ -170,7 +172,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
*/
public function get_item_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
@ -184,7 +186,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
@ -200,7 +202,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
$note = WC_Admin_Notes::get_note( $request->get_param( 'id' ) );
if ( ! $note ) {
return new WP_Error(
return new \WP_Error(
'woocommerce_admin_notes_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 )
@ -233,7 +235,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
*/
public function update_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}

View File

@ -5,6 +5,8 @@
* @package WooCommerce Admin/Classes
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
@ -17,10 +19,10 @@ class WC_Admin_Api_Init {
*/
public function __construct() {
// Hook in data stores.
add_filter( 'woocommerce_data_stores', array( 'WC_Admin_Api_Init', 'add_data_stores' ) );
add_filter( 'woocommerce_data_stores', array( __CLASS__, 'add_data_stores' ) );
// REST API extensions init.
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_filter( 'rest_endpoints', array( 'WC_Admin_Api_Init', 'filter_rest_endpoints' ), 10, 1 );
add_filter( 'rest_endpoints', array( __CLASS__, 'filter_rest_endpoints' ), 10, 1 );
// Add currency symbol to orders endpoint response.
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) );
@ -31,8 +33,8 @@ class WC_Admin_Api_Init {
*/
public function rest_api_init() {
$controllers = array(
'WC_Admin_REST_Admin_Notes_Controller',
'WC_Admin_REST_Admin_Note_Action_Controller',
'Automattic\WooCommerce\Admin\API\Notes\Controller',
'Automattic\WooCommerce\Admin\API\Notes\Actions\Controller',
'WC_Admin_REST_Coupons_Controller',
'WC_Admin_REST_Customers_Controller',
'WC_Admin_REST_Data_Controller',
@ -70,7 +72,7 @@ class WC_Admin_Api_Init {
'WC_Admin_REST_Themes_Controller',
);
if ( WC_Admin_Loader::is_feature_enabled( 'onboarding' ) ) {
if ( \WC_Admin_Loader::is_feature_enabled( 'onboarding' ) ) {
$controllers = array_merge(
$controllers,
array(

View File

@ -26,6 +26,7 @@ use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Welcome_Message;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Woo_Subscriptions_Notes;
use Automattic\WooCommerce\Admin\WC_Admin_Report_Exporter;
use Automattic\WooCommerce\Admin\WC_Admin_Reports_Sync;
use Automattic\WooCommerce\Admin\API\WC_Admin_Api_Init;
/**
* Autoload packages.