diff --git a/includes/api/class-wc-rest-setting-options-controller.php b/includes/api/class-wc-rest-setting-options-controller.php index 96fd48b7bbe..8b26ff5d2b8 100644 --- a/includes/api/class-wc-rest-setting-options-controller.php +++ b/includes/api/class-wc-rest-setting-options-controller.php @@ -1,6 +1,6 @@ [\w-]+)'; /** * Register routes. @@ -40,7 +40,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @since 3.0.0 */ public function register_routes() { - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( + register_rest_route( $this->namespace, '/' . $this->rest_base, array( 'args' => array( 'group' => array( 'description' => __( 'Settings group ID.', 'woocommerce' ), @@ -55,7 +55,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { 'schema' => array( $this, 'get_public_item_schema' ), ) ); - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/batch', array( + register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( 'args' => array( 'group' => array( 'description' => __( 'Settings group ID.', 'woocommerce' ), @@ -71,7 +71,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { 'schema' => array( $this, 'get_public_batch_schema' ), ) ); - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/(?P[\w-]+)', array( + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( 'args' => array( 'group' => array( 'description' => __( 'Settings group ID.', 'woocommerce' ), @@ -105,7 +105,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { - $setting = $this->get_setting( $request['group'], $request['id'] ); + $setting = $this->get_setting( $request['group_id'], $request['id'] ); if ( is_wp_error( $setting ) ) { return $setting; @@ -124,7 +124,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { - $settings = $this->get_group_settings( $request['group'] ); + $settings = $this->get_group_settings( $request['group_id'] ); if ( is_wp_error( $settings ) ) { return $settings; @@ -255,7 +255,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return WP_Error|WP_REST_Response */ public function update_item( $request ) { - $setting = $this->get_setting( $request['group'], $request['id'] ); + $setting = $this->get_setting( $request['group_id'], $request['id'] ); if ( is_wp_error( $setting ) ) { return $setting; @@ -303,7 +303,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, empty( $request['context'] ) ? 'view' : $request['context'] ); $response = rest_ensure_response( $data ); - $response->add_links( $this->prepare_links( $data['id'], $request['group'] ) ); + $response->add_links( $this->prepare_links( $data['id'], $request['group_id'] ) ); return $response; } @@ -316,13 +316,13 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return array Links for the given setting. */ protected function prepare_links( $setting_id, $group_id ) { - $base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $group_id; + $base = str_replace( '(?P[\w-]+)', $group_id, $this->rest_base ); $links = array( 'self' => array( - 'href' => rest_url( trailingslashit( $base ) . $setting_id ), + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $base, $setting_id ) ), ), 'collection' => array( - 'href' => rest_url( $base ), + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ), ), ); diff --git a/includes/cli/class-wc-cli-runner.php b/includes/cli/class-wc-cli-runner.php index 33fb7e062ab..e9c93a24179 100644 --- a/includes/cli/class-wc-cli-runner.php +++ b/includes/cli/class-wc-cli-runner.php @@ -19,9 +19,9 @@ class WC_CLI_Runner { */ private static $disabled_endpoints = array( 'settings', - 'settings/(?P[\w-]+)', - 'settings/(?P[\w-]+)/batch', - 'settings/(?P[\w-]+)/(?P[\w-]+)', + 'settings/(?P[\w-]+)', + 'settings/(?P[\w-]+)/batch', + 'settings/(?P[\w-]+)/(?P[\w-]+)', 'system_status', 'system_status/tools', 'system_status/tools/(?P[\w-]+)', diff --git a/tests/unit-tests/api/settings.php b/tests/unit-tests/api/settings.php index cb1192ab4ed..635fdce9ce0 100644 --- a/tests/unit-tests/api/settings.php +++ b/tests/unit-tests/api/settings.php @@ -28,8 +28,8 @@ class Settings extends WC_REST_Unit_Test_Case { public function test_register_routes() { $routes = $this->server->get_routes(); $this->assertArrayHasKey( '/wc/v2/settings', $routes ); - $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)/(?P[\w-]+)', $routes ); + $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)', $routes ); + $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)/(?P[\w-]+)', $routes ); } /** @@ -276,13 +276,14 @@ class Settings extends WC_REST_Unit_Test_Case { $request->set_body_params( array( 'update' => array( array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'both', + 'id' => 'woocommerce_shop_page_display', + 'value' => 'both', ), ), ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); + $this->assertEquals( 'both', $data['update'][0]['value'] ); $this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); @@ -373,8 +374,8 @@ class Settings extends WC_REST_Unit_Test_Case { * @since 3.0.0 */ public function test_get_setting_invalid_setting_type() { - // $controller = $this->getMock( 'WC_Rest_Settings_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); - $controller = $this->getMockBuilder( 'WC_Rest_Settings_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); + // $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); + $controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); $controller ->expects( $this->any() ) @@ -433,7 +434,7 @@ class Settings extends WC_REST_Unit_Test_Case { * Test updating a bad setting ID. * * @since 3.0.0 - * @covers WC_Rest_Settings_Options_Controller::update_item + * @covers WC_Rest_Setting_Options_Controller::update_item */ public function test_update_setting_bad_setting_id() { wp_set_current_user( $this->user );