From 2b343e8a6712397eca1a959547f235b6847d926f Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 30 May 2019 15:54:11 +0100 Subject: [PATCH] Settings --- .../Version4/Controllers/PaymentGateways.php | 8 +- .../Settings.php} | 73 ++++++++---- .../class-wc-rest-settings-controller.php | 112 ------------------ 3 files changed, 56 insertions(+), 137 deletions(-) rename src/RestApi/Version4/{class-wc-rest-settings-v2-controller.php => Controllers/Settings.php} (78%) delete mode 100644 src/RestApi/Version4/class-wc-rest-settings-controller.php diff --git a/src/RestApi/Version4/Controllers/PaymentGateways.php b/src/RestApi/Version4/Controllers/PaymentGateways.php index eae55e59cbb..e5c511de844 100644 --- a/src/RestApi/Version4/Controllers/PaymentGateways.php +++ b/src/RestApi/Version4/Controllers/PaymentGateways.php @@ -28,7 +28,9 @@ class PaymentGateways extends AbstractController { */ public function register_routes() { register_rest_route( - $this->namespace, '/' . $this->rest_base, array( + $this->namespace, + '/' . $this->rest_base, + array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), @@ -39,7 +41,9 @@ class PaymentGateways extends AbstractController { ) ); register_rest_route( - $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( + $this->namespace, + '/' . $this->rest_base . '/(?P[\w-]+)', + array( 'args' => array( 'id' => array( 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), diff --git a/src/RestApi/Version4/class-wc-rest-settings-v2-controller.php b/src/RestApi/Version4/Controllers/Settings.php similarity index 78% rename from src/RestApi/Version4/class-wc-rest-settings-v2-controller.php rename to src/RestApi/Version4/Controllers/Settings.php index 3b1dd0be705..38e59c3fe79 100644 --- a/src/RestApi/Version4/class-wc-rest-settings-v2-controller.php +++ b/src/RestApi/Version4/Controllers/Settings.php @@ -5,25 +5,16 @@ * Handles requests to the /settings endpoints. * * @package WooCommerce/RestApi - * @since 3.0.0 */ +namespace WooCommerce\RestApi\Version4\Controllers; + defined( 'ABSPATH' ) || exit; /** * REST API Settings controller class. - * - * @package WooCommerce/RestApi - * @extends WC_REST_Controller */ -class WC_REST_Settings_V2_Controller extends WC_REST_Controller { - - /** - * WP REST API namespace/version. - * - * @var string - */ - protected $namespace = 'wc/v2'; +class Settings extends AbstractController { /** * Route base. @@ -39,7 +30,9 @@ class WC_REST_Settings_V2_Controller extends WC_REST_Controller { */ public function register_routes() { register_rest_route( - $this->namespace, '/' . $this->rest_base, array( + $this->namespace, + '/' . $this->rest_base, + array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), @@ -48,6 +41,45 @@ class WC_REST_Settings_V2_Controller extends WC_REST_Controller { 'schema' => array( $this, 'get_public_item_schema' ), ) ); + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/batch', + array( + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'batch_items' ), + 'permission_callback' => array( $this, 'update_items_permissions_check' ), + ), + 'schema' => array( $this, 'get_public_batch_schema' ), + ) + ); + } + + /** + * Makes sure the current user has access to WRITE the settings APIs. + * + * @param WP_REST_Request $request Full data about the request. + * @return WP_Error|bool + */ + 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' ), array( 'status' => rest_authorization_required_code() ) ); + } + + return true; + } + + /** + * Update a setting. + * + * @param WP_REST_Request $request Request data. + * @return WP_Error|WP_REST_Response + */ + public function update_item( $request ) { + $options_controller = new WC_REST_Setting_Options_Controller(); + $response = $options_controller->update_item( $request ); + + return $response; } /** @@ -197,32 +229,27 @@ class WC_REST_Settings_V2_Controller extends WC_REST_Controller { 'id' => array( 'description' => __( 'A unique identifier that can be used to link settings together.', 'woocommerce' ), 'type' => 'string', - 'context' => array( 'view' ), - 'readonly' => true, + 'context' => array( 'view', 'edit' ), ), 'label' => array( 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ), 'type' => 'string', - 'context' => array( 'view' ), - 'readonly' => true, + 'context' => array( 'view', 'edit' ), ), 'description' => array( 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ), 'type' => 'string', - 'context' => array( 'view' ), - 'readonly' => true, + 'context' => array( 'view', 'edit' ), ), 'parent_id' => array( 'description' => __( 'ID of parent grouping.', 'woocommerce' ), 'type' => 'string', - 'context' => array( 'view' ), - 'readonly' => true, + 'context' => array( 'view', 'edit' ), ), 'sub_groups' => array( 'description' => __( 'IDs for settings sub groups.', 'woocommerce' ), 'type' => 'string', - 'context' => array( 'view' ), - 'readonly' => true, + 'context' => array( 'view', 'edit' ), ), ), ); diff --git a/src/RestApi/Version4/class-wc-rest-settings-controller.php b/src/RestApi/Version4/class-wc-rest-settings-controller.php deleted file mode 100644 index 869f725144f..00000000000 --- a/src/RestApi/Version4/class-wc-rest-settings-controller.php +++ /dev/null @@ -1,112 +0,0 @@ -namespace, '/' . $this->rest_base . '/batch', array( - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'batch_items' ), - 'permission_callback' => array( $this, 'update_items_permissions_check' ), - ), - 'schema' => array( $this, 'get_public_batch_schema' ), - ) ); - } - - /** - * Makes sure the current user has access to WRITE the settings APIs. - * - * @param WP_REST_Request $request Full data about the request. - * @return WP_Error|bool - */ - 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' ), array( 'status' => rest_authorization_required_code() ) ); - } - - return true; - } - - /** - * Update a setting. - * - * @param WP_REST_Request $request Request data. - * @return WP_Error|WP_REST_Response - */ - public function update_item( $request ) { - $options_controller = new WC_REST_Setting_Options_Controller(); - $response = $options_controller->update_item( $request ); - - return $response; - } - - /** - * Get the groups schema, conforming to JSON Schema. - * - * @since 3.0.0 - * @return array - */ - public function get_item_schema() { - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'setting_group', - 'type' => 'object', - 'properties' => array( - 'id' => array( - 'description' => __( 'A unique identifier that can be used to link settings together.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - ), - 'label' => array( - 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - ), - 'description' => array( - 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - ), - 'parent_id' => array( - 'description' => __( 'ID of parent grouping.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - ), - 'sub_groups' => array( - 'description' => __( 'IDs for settings sub groups.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - ), - ), - ); - - return $this->add_additional_fields_schema( $schema ); - } -}