Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit 8b38c9b

Browse files
author
Justin Shreve
committed
Add a settings bulk update for updating settings in multiple groups.
1 parent 6b1ccce commit 8b38c9b

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

api/class-wc-rest-dev-settings-controller.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
*
55
* Handles requests to the /settings endpoints.
66
*
7-
* @author WooThemes
7+
* @author Automattic
88
* @category API
99
* @package WooCommerce/API
10-
* @since 3.0.0
1110
*/
1211

1312
if ( ! defined( 'ABSPATH' ) ) {
@@ -26,4 +25,45 @@ class WC_REST_Dev_Settings_Controller extends WC_REST_Settings_Controller {
2625
*/
2726
protected $namespace = 'wc/v3';
2827

28+
/**
29+
* Register routes.
30+
*
31+
*/
32+
public function register_routes() {
33+
parent::register_routes();
34+
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
35+
array(
36+
'methods' => WP_REST_Server::EDITABLE,
37+
'callback' => array( $this, 'batch_items' ),
38+
'permission_callback' => array( $this, 'update_items_permissions_check' ),
39+
),
40+
'schema' => array( $this, 'get_public_batch_schema' ),
41+
) );
42+
}
43+
44+
/**
45+
* Makes sure the current user has access to WRITE the settings APIs.
46+
*
47+
* @param WP_REST_Request $request Full data about the request.
48+
* @return WP_Error|boolean
49+
*/
50+
public function update_items_permissions_check( $request ) {
51+
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
52+
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
53+
}
54+
55+
return true;
56+
}
57+
58+
/**
59+
* Update a setting.
60+
*
61+
* @param WP_REST_Request $request
62+
* @return WP_Error|WP_REST_Response
63+
*/
64+
public function update_item( $request ) {
65+
$options_controller = new WC_REST_Setting_Options_Controller;
66+
return $options_controller->update_item( $request );
67+
}
68+
2969
}

tests/unit-tests/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function test_wc_rest_validate_reports_request_arg() {
6464
* @since 2.6.0
6565
*/
6666
public function test_wc_rest_urlencode_rfc3986() {
67-
$this->assertEquals( 'https%253A%252F%252Fwoocommerce.com%252F', wc_rest_urlencode_rfc3986( 'https://woocommerce.com/' ) );
67+
$this->assertEquals( 'https%3A%2F%2Fwoocommerce.com%2F', wc_rest_urlencode_rfc3986( 'https://woocommerce.com/' ) );
6868
}
6969

7070
/**

tests/unit-tests/settings.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function setUp() {
2828
public function test_register_routes() {
2929
$routes = $this->server->get_routes();
3030
$this->assertArrayHasKey( '/wc/v3/settings', $routes );
31+
$this->assertArrayHasKey( '/wc/v3/settings/batch', $routes );
3132
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)', $routes );
33+
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)/batch', $routes );
3234
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes );
3335
}
3436

@@ -285,6 +287,28 @@ public function test_update_settings() {
285287
$this->assertEquals( 'both', $data['update'][0]['value'] );
286288
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
287289

290+
// test bulk settings batch endpoint
291+
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/batch' );
292+
$request->set_body_params( array(
293+
'update' => array(
294+
array(
295+
'group_id' => 'test',
296+
'id' => 'woocommerce_shop_page_display',
297+
'value' => 'subcategories',
298+
),
299+
array(
300+
'group_id' => 'products',
301+
'id' => 'woocommerce_dimension_unit',
302+
'value' => 'yd',
303+
),
304+
),
305+
) );
306+
$response = $this->server->dispatch( $request );
307+
$data = $response->get_data();
308+
$this->assertEquals( 'subcategories', $data['update'][0]['value'] );
309+
$this->assertEquals( 'yd', $data['update'][1]['value'] );
310+
$this->assertEquals( 'yd', get_option( 'woocommerce_dimension_unit' ) );
311+
288312
// test updating one, but making sure the other value stays the same
289313
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
290314
$request->set_body_params( array(
@@ -539,10 +563,10 @@ public function test_email_settings() {
539563
$this->assertEquals( array(
540564
'id' => 'subject',
541565
'label' => 'Subject',
542-
'description' => 'This controls the email subject line. Leave blank to use the default subject: <code>[{site_title}] New customer order ({order_number}) - {order_date}</code>.',
566+
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
543567
'type' => 'text',
544568
'default' => '',
545-
'tip' => 'This controls the email subject line. Leave blank to use the default subject: <code>[{site_title}] New customer order ({order_number}) - {order_date}</code>.',
569+
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
546570
'value' => '',
547571
), $setting );
548572

@@ -557,10 +581,10 @@ public function test_email_settings() {
557581
$this->assertEquals( array(
558582
'id' => 'subject',
559583
'label' => 'Subject',
560-
'description' => 'This controls the email subject line. Leave blank to use the default subject: <code>[{site_title}] New customer order ({order_number}) - {order_date}</code>.',
584+
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
561585
'type' => 'text',
562586
'default' => '',
563-
'tip' => 'This controls the email subject line. Leave blank to use the default subject: <code>[{site_title}] New customer order ({order_number}) - {order_date}</code>.',
587+
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
564588
'value' => 'This is my subject',
565589
), $setting );
566590

0 commit comments

Comments
 (0)