Merge pull request #13755 from woocommerce/fix/shipping-zone-locations-schema

[REST API] Fixed shipping zone locations schema
This commit is contained in:
Mike Jolley 2017-03-24 10:27:52 +00:00 committed by GitHub
commit 803786ceeb
2 changed files with 15 additions and 12 deletions

View File

@ -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' ),
),
),
);

View File

@ -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',