From 7a671ab8a5f5fda511b6b4049c44c14415fd3f5c Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 23 Mar 2017 21:41:56 -0300 Subject: [PATCH] [REST API] Fixed shipping zone locations schema This also fix improper sanitization and validation of the items. --- ...est-shipping-zone-locations-controller.php | 25 +++++++++++-------- tests/unit-tests/api/shipping-zones.php | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/includes/api/class-wc-rest-shipping-zone-locations-controller.php b/includes/api/class-wc-rest-shipping-zone-locations-controller.php index 38edc401f47..fbda19d8ca3 100644 --- a/includes/api/class-wc-rest-shipping-zone-locations-controller.php +++ b/includes/api/class-wc-rest-shipping-zone-locations-controller.php @@ -90,10 +90,20 @@ class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_ $locations = array(); foreach ( (array) $raw_locations as $raw_location ) { - if ( empty( $raw_location['code'] ) || empty( $raw_location['type'] ) ) { + if ( empty( $raw_location['code'] ) ) { continue; } - $locations[] = $raw_location; + + $type = ! empty( $raw_location['type'] ) ? sanitize_text_field( $raw_location['type'] ) : 'country'; + + if ( ! in_array( $type, array( 'postcode', 'state', 'country', 'continent' ), true ) ) { + continue; + } + + $locations[] = array( + 'code' => sanitize_text_field( $raw_location['code'] ), + 'type' => sanitize_text_field( $type ), + ); } $zone->set_locations( $locations ); @@ -157,25 +167,18 @@ class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_ 'description' => __( 'Shipping zone location code.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), - 'required' => true, - 'arg_options' => array( - 'sanitize_callback' => 'sanitize_text_field', - ), ), 'type' => array( 'description' => __( 'Shipping zone location type.', 'woocommerce' ), 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'required' => true, - 'arg_options' => array( - 'sanitize_callback' => 'sanitize_text_field', - ), + 'default' => 'country', 'enum' => array( 'postcode', 'state', 'country', 'continent', ), + 'context' => array( 'view', 'edit' ), ), ), ); diff --git a/tests/unit-tests/api/shipping-zones.php b/tests/unit-tests/api/shipping-zones.php index 74d901452d7..14f465ecf13 100644 --- a/tests/unit-tests/api/shipping-zones.php +++ b/tests/unit-tests/api/shipping-zones.php @@ -467,7 +467,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case { $response = $this->server->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( count( $data ), 2 ); + $this->assertEquals( 3, count( $data ) ); $this->assertEquals( array( array( 'code' => 'UK',