diff --git a/includes/api/class-wc-rest-settings-controller.php b/includes/api/class-wc-rest-settings-controller.php index 1a2b9449bd2..91bdec0a962 100644 --- a/includes/api/class-wc-rest-settings-controller.php +++ b/includes/api/class-wc-rest-settings-controller.php @@ -34,7 +34,17 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller { 'schema' => array( $this, 'get_public_item_schema' ), ) ); - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/(?P[\w-]+)', array( + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/batch', array( + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'batch_items' ), + 'permission_callback' => array( $this, 'permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + 'schema' => array( $this, 'get_public_batch_schema' ), + ) ); + + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/(?P[\w-]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), @@ -57,7 +67,7 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller { * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { - $setting = $this->get_setting( $request['group'], $request['setting'] ); + $setting = $this->get_setting( $request['group'], $request['id'] ); if ( is_wp_error( $setting ) ) { return $setting; @@ -157,6 +167,32 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller { return $setting; } + /** + * Bulk create, update and delete items. + * + * @param WP_REST_Request $request Full details about the request. + * @return array Of WP_Error or WP_REST_Response. + */ + public function batch_items( $request ) { + // Get the request params. + $items = array_filter( $request->get_params() ); + + /* + * Since our batch settings update is group-specific and matches based on the route, + * we inject the URL parameters (containing group) into the batch items + */ + if ( ! empty( $items['update'] ) ) { + $to_update = array(); + foreach ( $items['update'] as $item ) { + $to_update[] = array_merge( $request->get_url_params(), $item ); + } + $request = new WP_REST_Request( $request->get_method() ); + $request->set_body_params( array( 'update' => $to_update ) ); + } + + return parent::batch_items( $request ); + } + /** * Update a single setting in a group. * @since 2.7.0 @@ -164,15 +200,16 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller { * @return WP_Error|WP_REST_Response */ public function update_item( $request ) { - $setting = $this->get_setting( $request['group'], $request['setting'] ); + $setting = $this->get_setting( $request['group'], $request['id'] ); if ( is_wp_error( $setting ) ) { return $setting; } - $response = $this->prepare_item_for_response( $setting, $request ); - $value = $this->sanitize_setting_value( $setting, $request['value'] ); - $response['value'] = $value; + $response = $this->prepare_item_for_response( $setting, $request ); + $value = $this->sanitize_setting_value( $setting, $request['value'] ); + + $response->set_data( array_merge( $response->get_data(), compact( 'value' ) ) ); update_option( $setting['id'], $value ); diff --git a/includes/api/class-wc-rest-settings-groups-controller.php b/includes/api/class-wc-rest-settings-groups-controller.php index 9f3f6004adf..310f00b8895 100644 --- a/includes/api/class-wc-rest-settings-groups-controller.php +++ b/includes/api/class-wc-rest-settings-groups-controller.php @@ -33,16 +33,6 @@ class WC_Rest_Settings_Groups_Controller extends WC_REST_Settings_API_Controller ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); - - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'update_item' ), - 'permission_callback' => array( $this, 'permissions_check' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) ); } /** @@ -94,30 +84,6 @@ class WC_Rest_Settings_Groups_Controller extends WC_REST_Settings_API_Controller return rest_ensure_response( $group ); } - /** - * Update a multiple settings at once. - * - * @since 2.7.0 - * @param WP_REST_Request $request - * @return WP_Error|WP_REST_Response - */ - public function update_item( $request ) { - $group = $this->prepare_item_for_response( $request ); - if ( is_wp_error( $group ) ) { - return $group; - } - - foreach ( $group['settings'] as $array_key => $setting ) { - if ( isset( $request['values'][ $setting['id'] ] ) ) { - $value = $this->sanitize_setting_value( $setting, $request['values'][ $setting['id'] ] ); - $group['settings'][ $array_key ]['value'] = $value; - update_option( $setting['id'], $value ); - } - } - - return rest_ensure_response( $group ); - } - /** * Prepare a report sales object for serialization. *