Fixed coding standards

This commit is contained in:
Claudio Sanches 2018-01-30 15:05:25 -02:00
parent 05a80e2d4f
commit e6597cd351
1 changed files with 243 additions and 211 deletions

View File

@ -59,7 +59,8 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
* Register the routes for products.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
register_rest_route(
$this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
@ -73,9 +74,11 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
)
);
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
@ -87,9 +90,11 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array(
'context' => $this->get_context_param(
array(
'default' => 'view',
) ),
)
),
),
),
array(
@ -111,9 +116,11 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
)
);
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
@ -121,7 +128,8 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
)
);
}
/**
@ -251,19 +259,23 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
$skus[] = $request['sku'];
}
$args['meta_query'] = $this->add_meta_query( $args, array( // WPCS: slow query ok.
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args, array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN',
) );
)
);
}
// Filter by tax class.
if ( ! empty( $request['tax_class'] ) ) {
$args['meta_query'] = $this->add_meta_query( $args, array( // WPCS: slow query ok.
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args, array(
'key' => '_tax_class',
'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
) );
)
);
}
// Price filter.
@ -273,10 +285,12 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
// Filter product in stock or out of stock.
if ( is_bool( $request['in_stock'] ) ) {
$args['meta_query'] = $this->add_meta_query( $args, array( // WPCS: slow query ok.
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args, array(
'key' => '_stock_status',
'value' => true === $request['in_stock'] ? 'instock' : 'outofstock',
) );
)
);
}
// Filter by on sale products.
@ -485,9 +499,11 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
*/
protected function get_attribute_options( $product_id, $attribute ) {
if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) {
return wc_get_product_terms( $product_id, $attribute['name'], array(
return wc_get_product_terms(
$product_id, $attribute['name'], array(
'fields' => 'names',
) );
)
);
} elseif ( isset( $attribute['value'] ) ) {
return array_map( 'trim', explode( '|', $attribute['value'] ) );
}
@ -681,9 +697,11 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
}
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/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array(
return new WP_Error(
"woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array(
'status' => 404,
) );
)
);
}
// Post title.
@ -1084,10 +1102,12 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
// Set the image name if present.
if ( ! empty( $image['name'] ) ) {
wp_update_post( array(
wp_update_post(
array(
'ID' => $attachment_id,
'post_title' => $image['name'],
) );
)
);
}
// Set the image source if present, for future reference.
@ -1284,15 +1304,19 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
$result = false;
if ( ! $object || 0 === $object->get_id() ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array(
return new WP_Error(
"woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array(
'status' => 404,
) );
)
);
}
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/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array(
return new WP_Error(
"woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array(
'status' => 404,
) );
)
);
}
$supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) );
@ -1308,10 +1332,12 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
$supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object );
if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) {
return new WP_Error(
/* 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(
"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' );
@ -1337,19 +1363,23 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
} else {
// If we don't support trashing for this type, error out.
if ( ! $supports_trash ) {
return new WP_Error(
/* 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(
'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.
if ( is_callable( array( $object, 'get_status' ) ) ) {
if ( 'trash' === $object->get_status() ) {
return new WP_Error(
/* translators: %s: post type */
return new WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array(
'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array(
'status' => 410,
) );
)
);
}
$object->delete();
@ -1358,10 +1388,12 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
}
if ( ! $result ) {
return new WP_Error(
/* translators: %s: post type */
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array(
'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array(
'status' => 500,
) );
)
);
}
// Delete parent product transients.