Merge pull request #17376 from woocommerce/fix/products-api-schema

[REST API] Fixed date_on_sale_to_gmt description on products endpoint
This commit is contained in:
Mike Jolley 2017-10-25 17:33:20 +01:00 committed by GitHub
commit 206ffcdcb3
2 changed files with 47 additions and 22 deletions

View File

@ -89,7 +89,9 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'callback' => array( $this, 'get_item' ), 'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array( 'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ), 'context' => $this->get_context_param( array(
'default' => 'view',
) ),
), ),
), ),
array( array(
@ -231,7 +233,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
} }
if ( ! empty( $tax_query ) ) { if ( ! empty( $tax_query ) ) {
$args['tax_query'] = $tax_query; $args['tax_query'] = $tax_query; // WPCS: slow query ok.
} }
// Filter featured. // Filter featured.
@ -251,7 +253,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
$skus[] = $request['sku']; $skus[] = $request['sku'];
} }
$args['meta_query'] = $this->add_meta_query( $args, array( $args['meta_query'] = $this->add_meta_query( $args, array( // WPCS: slow query ok.
'key' => '_sku', 'key' => '_sku',
'value' => $skus, 'value' => $skus,
'compare' => 'IN', 'compare' => 'IN',
@ -260,7 +262,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
// Filter by tax class. // Filter by tax class.
if ( ! empty( $request['tax_class'] ) ) { if ( ! empty( $request['tax_class'] ) ) {
$args['meta_query'] = $this->add_meta_query( $args, array( $args['meta_query'] = $this->add_meta_query( $args, array( // WPCS: slow query ok.
'key' => '_tax_class', 'key' => '_tax_class',
'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '', 'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
) ); ) );
@ -268,12 +270,12 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
// Price filter. // Price filter.
if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) { if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) {
$args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) ); $args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) ); // WPCS: slow query ok.
} }
// Filter product in stock or out of stock. // Filter product in stock or out of stock.
if ( is_bool( $request['in_stock'] ) ) { if ( is_bool( $request['in_stock'] ) ) {
$args['meta_query'] = $this->add_meta_query( $args, array( $args['meta_query'] = $this->add_meta_query( $args, array( // WPCS: slow query ok.
'key' => '_stock_status', 'key' => '_stock_status',
'value' => true === $request['in_stock'] ? 'instock' : 'outofstock', 'value' => true === $request['in_stock'] ? 'instock' : 'outofstock',
) ); ) );
@ -485,7 +487,9 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
*/ */
protected function get_attribute_options( $product_id, $attribute ) { protected function get_attribute_options( $product_id, $attribute ) {
if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) { if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) {
return wc_get_product_terms( $product_id, $attribute['name'], array( 'fields' => 'names' ) ); return wc_get_product_terms( $product_id, $attribute['name'], array(
'fields' => 'names',
) );
} elseif ( isset( $attribute['value'] ) ) { } elseif ( isset( $attribute['value'] ) ) {
return array_map( 'trim', explode( '|', $attribute['value'] ) ); return array_map( 'trim', explode( '|', $attribute['value'] ) );
} }
@ -547,7 +551,8 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
* Get product data. * Get product data.
* *
* @param WC_Product $product Product instance. * @param WC_Product $product Product instance.
* @param string $context 'view' or 'edit' * @param string $context Request context.
* Options: 'view' and 'edit'.
* @return array * @return array
*/ */
protected function get_product_data( $product, $context = 'view' ) { protected function get_product_data( $product, $context = 'view' ) {
@ -678,7 +683,9 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
} }
if ( 'variation' === $product->get_type() ) { if ( 'variation' === $product->get_type() ) {
return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array( 'status' => 404 ) ); return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array(
'status' => 404,
) );
} }
// Post title. // Post title.
@ -1060,6 +1067,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
} }
if ( ! wp_attachment_is_image( $attachment_id ) ) { if ( ! wp_attachment_is_image( $attachment_id ) ) {
/* translators: %s: attachment id */
throw new WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 ); throw new WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
} }
@ -1076,7 +1084,10 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
// Set the image name if present. // Set the image name if present.
if ( ! empty( $image['name'] ) ) { if ( ! empty( $image['name'] ) ) {
wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['name'] ) ); wp_update_post( array(
'ID' => $attachment_id,
'post_title' => $image['name'],
) );
} }
// Set the image source if present, for future reference. // Set the image source if present, for future reference.
@ -1273,11 +1284,15 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
$result = false; $result = false;
if ( ! $object || 0 === $object->get_id() ) { if ( ! $object || 0 === $object->get_id() ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array(
'status' => 404,
) );
} }
if ( 'variation' === $object->get_type() ) { if ( 'variation' === $object->get_type() ) {
return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array( 'status' => 404 ) ); return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array(
'status' => 404,
) );
} }
$supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) ); $supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) );
@ -1294,7 +1309,9 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) { if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) {
/* translators: %s: post type */ /* translators: %s: post type */
return new WP_Error( "woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf( __( 'Sorry, you are not allowed to delete %s.', 'woocommerce' ), $this->post_type ), array( 'status' => rest_authorization_required_code() ) ); return new WP_Error( "woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf( __( 'Sorry, you are not allowed to delete %s.', 'woocommerce' ), $this->post_type ), array(
'status' => rest_authorization_required_code(),
) );
} }
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
@ -1321,14 +1338,18 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
// If we don't support trashing for this type, error out. // If we don't support trashing for this type, error out.
if ( ! $supports_trash ) { if ( ! $supports_trash ) {
/* translators: %s: post type */ /* translators: %s: post type */
return new WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( 'status' => 501 ) ); return new WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array(
'status' => 501,
) );
} }
// Otherwise, only trash if we haven't already. // Otherwise, only trash if we haven't already.
if ( is_callable( array( $object, 'get_status' ) ) ) { if ( is_callable( array( $object, 'get_status' ) ) ) {
if ( 'trash' === $object->get_status() ) { if ( 'trash' === $object->get_status() ) {
/* translators: %s: post type */ /* translators: %s: post type */
return new WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) ); return new WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array(
'status' => 410,
) );
} }
$object->delete(); $object->delete();
@ -1338,7 +1359,9 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
if ( ! $result ) { if ( ! $result ) {
/* translators: %s: post type */ /* translators: %s: post type */
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) ); return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array(
'status' => 500,
) );
} }
// Delete parent product transients. // Delete parent product transients.
@ -1401,7 +1424,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'readonly' => true, 'readonly' => true,
), ),
'date_created_gmt' => array( 'date_created_gmt' => array(
'description' => __( "The date the product was created, as GMT.", 'woocommerce' ), 'description' => __( 'The date the product was created, as GMT.', 'woocommerce' ),
'type' => 'date-time', 'type' => 'date-time',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,
@ -1413,7 +1436,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'readonly' => true, 'readonly' => true,
), ),
'date_modified_gmt' => array( 'date_modified_gmt' => array(
'description' => __( "The date the product was last modified, as GMT.", 'woocommerce' ), 'description' => __( 'The date the product was last modified, as GMT.', 'woocommerce' ),
'type' => 'date-time', 'type' => 'date-time',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,
@ -1492,7 +1515,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
), ),
'date_on_sale_to_gmt' => array( 'date_on_sale_to_gmt' => array(
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ), 'description' => __( 'End date of sale price, as GMT.', 'woocommerce' ),
'type' => 'date-time', 'type' => 'date-time',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
), ),
@ -1815,7 +1838,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'readonly' => true, 'readonly' => true,
), ),
'date_created_gmt' => array( 'date_created_gmt' => array(
'description' => __( "The date the image was created, as GMT.", 'woocommerce' ), 'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ),
'type' => 'date-time', 'type' => 'date-time',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,
@ -1827,7 +1850,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'readonly' => true, 'readonly' => true,
), ),
'date_modified_gmt' => array( 'date_modified_gmt' => array(
'description' => __( "The date the image was last modified, as GMT.", 'woocommerce' ), 'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ),
'type' => 'date-time', 'type' => 'date-time',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,

View File

@ -5,7 +5,9 @@
<description>WooCommerce dev PHP_CodeSniffer ruleset.</description> <description>WooCommerce dev PHP_CodeSniffer ruleset.</description>
<rule ref="WordPress"></rule> <rule ref="WordPress">
<exclude name="WordPress.VIP.RestrictedFunctions.get_term_by_get_term_by" />
</rule>
<rule ref="WordPress.VIP.ValidatedSanitizedInput"> <rule ref="WordPress.VIP.ValidatedSanitizedInput">
<properties> <properties>