* Included PHPCS and git hooks

* Fixed coding standards

* New Products endpoint

* Fixed product controller schema

* Improved products controller schema and response

* New product categories controller

* Updated REST API base to match WooCommerce authentication

* Fixed products/categories endpoint

* Better docblock

* Product attributes controller

* Product attribute terms controller

* Update REST API urls

* Reverted PHPCS changes

* Check permission only once while fetching posts

* Included raw price

* Included sku to products endpoint response

* Fixed endpoints in unit test

* Fixed routes in unit tests

* Check and use subscriber, contributor and author rules in unit tests
This commit is contained in:
Claudio Sanches 2018-11-30 17:12:56 -02:00 committed by Kelly Dwan
parent 7a209367a7
commit 81605bae30
13 changed files with 751 additions and 733 deletions

View File

@ -26,7 +26,7 @@ class ProductCategoryControl extends Component {
componentDidMount() {
apiFetch( {
path: addQueryArgs( '/wc/v3/products/categories', { per_page: -1 } ),
path: addQueryArgs( '/wc-pb/v3/products/categories', { per_page: -1 } ),
} )
.then( ( list ) => {
this.setState( { list } );

View File

@ -453,7 +453,7 @@ class ProductsBlockPreview extends Component {
query_string += key + '=' + query[ key ] + '&';
}
const endpoint = '/wgbp/v3/products' + query_string;
const endpoint = '/wc-pb/v3/products' + query_string;
return endpoint;
}
@ -561,13 +561,13 @@ class ProductsBlockSidebarInfo extends Component {
const ID = getAttributeID( display_setting[ 0 ] );
const terms = display_setting.slice( 1 ).join( ', ' );
endpoints.attribute = '/wc/v2/products/attributes/' + ID;
endpoints.attribute = '/wc-pb/v3/products/attributes/' + ID;
if ( terms.length ) {
endpoints.terms = '/wc/v2/products/attributes/' + ID + '/terms?include=' + terms;
endpoints.terms = '/wc-pb/v3/products/attributes/' + ID + '/terms?include=' + terms;
}
} else if ( 'category' === display && display_setting.length ) {
endpoints.categories = '/wc/v2/products/categories?include=' + display_setting.join( ',' );
endpoints.categories = '/wc-pb/v3/products/categories?include=' + display_setting.join( ',' );
}
return endpoints;

View File

@ -199,7 +199,7 @@ class ProductAttributeList extends Component {
* @return string
*/
getQuery() {
const endpoint = '/wc/v2/products/attributes';
const endpoint = '/wc-pb/v3/products/attributes';
return endpoint;
}
@ -386,7 +386,7 @@ class AttributeTerms extends Component {
* @return string
*/
getQuery() {
const endpoint = '/wc/v2/products/attributes/' + this.props.attribute.id + '/terms';
const endpoint = '/wc-pb/v3/products/attributes/' + this.props.attribute.id + '/terms';
return endpoint;
}

View File

@ -165,7 +165,7 @@ class ProductCategoryList extends Component {
* @return string
*/
getQuery() {
const endpoint = '/wc/v2/products/categories';
const endpoint = '/wc-pb/v3/products/categories';
return endpoint;
}

View File

@ -216,7 +216,7 @@ class ProductSpecificSearchResults extends Component {
return '';
}
return '/wc/v2/products?per_page=10&status=publish&search=' + this.props.searchString;
return '/wc-pb/v3/products?per_page=10&status=publish&search=' + this.props.searchString;
}
/**
@ -412,7 +412,7 @@ class ProductSpecificSelectedProducts extends Component {
}
}
return uncachedProducts.length ? '/wc/v2/products?include=' + uncachedProducts.join( ',' ) : '';
return uncachedProducts.length ? '/wc-pb/v3/products?include=' + uncachedProducts.join( ',' ) : '';
}
/**

View File

@ -69,7 +69,7 @@ export default class ProductByCategoryBlock extends Component {
getProducts() {
this.setState( { products: [], loaded: false } );
apiFetch( {
path: addQueryArgs( '/wgbp/v3/products', getQuery( this.props.attributes ) ),
path: addQueryArgs( '/wc-pb/v3/products', getQuery( this.props.attributes ) ),
} )
.then( ( products ) => {
this.setState( { products, loaded: true } );

View File

@ -0,0 +1,150 @@
<?php
/**
* REST API Product Attribute Terms controller customized for Products Block.
*
* Handles requests to the /products/categories endpoint.
*
* @package WooCommerce\Blocks\Products\Rest\Controller
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Attribute Terms controller class.
*
* @package WooCommerce/API
*/
class WGPB_Product_Attribute_Terms_Controller extends WC_REST_Product_Attribute_Terms_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-pb/v3';
/**
* Register the routes for products.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param(
array(
'default' => 'view',
)
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check permissions.
*
* @param WP_REST_Request $request Full details about the request.
* @param string $context Request context.
* @return bool|WP_Error
*/
protected function check_permissions( $request, $context = 'read' ) {
// Get taxonomy.
$taxonomy = $this->get_taxonomy( $request );
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
// Check permissions for a single term.
$id = intval( $request['id'] );
if ( $id ) {
$term = get_term( $id, $taxonomy );
if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) {
return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
}
return current_user_can( 'edit_posts' );
}
/**
* Prepare a single product category output for response.
*
* @param WP_Term $item Term object.
* @param WP_REST_Request $request Request instance.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
// Get term order.
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order_' . $this->taxonomy );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'count' => (int) $item->count,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
return $response;
}
/**
* Get the Product's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$raw_schema = parent::get_item_schema();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'product_block_category',
'type' => 'object',
'properties' => array(),
);
$schema['properties']['id'] = $raw_schema['properties']['id'];
$schema['properties']['name'] = $raw_schema['properties']['name'];
$schema['properties']['slug'] = $raw_schema['properties']['slug'];
$schema['properties']['count'] = $raw_schema['properties']['count'];
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -0,0 +1,178 @@
<?php
/**
* REST API Product Attributes controller customized for Products Block.
*
* Handles requests to the /products/categories endpoint.
*
* @package WooCommerce\Blocks\Products\Rest\Controller
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Attributes controller class.
*
* @package WooCommerce/API
*/
class WGPB_Product_Attributes_Controller extends WC_REST_Product_Attributes_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-pb/v3';
/**
* Register the routes for products.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param(
array(
'default' => 'view',
)
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check if a given request has access to read the attributes.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to read a attribute.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$taxonomy = $this->get_taxonomy( $request );
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check permissions.
*
* @param WP_REST_Request $request Full details about the request.
* @param string $context Request context.
* @return bool|WP_Error
*/
protected function check_permissions( $request, $context = 'read' ) {
// Get taxonomy.
$taxonomy = $this->get_taxonomy( $request );
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
// Check permissions for a single term.
$id = intval( $request['id'] );
if ( $id ) {
$term = get_term( $id, $taxonomy );
if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) {
return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
}
return current_user_can( 'edit_posts' );
}
/**
* Prepare a single product category output for response.
*
* @param WP_Term $item Term object.
* @param WP_REST_Request $request Request instance.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$data = array(
'id' => (int) $item->attribute_id,
'name' => $item->attribute_label,
'slug' => wc_attribute_taxonomy_name( $item->attribute_name ),
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item ) );
return $response;
}
/**
* Get the Product's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$raw_schema = parent::get_item_schema();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'product_block_attribute',
'type' => 'object',
'properties' => array(),
);
$schema['properties']['id'] = $raw_schema['properties']['id'];
$schema['properties']['name'] = $raw_schema['properties']['name'];
$schema['properties']['slug'] = $raw_schema['properties']['slug'];
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -0,0 +1,149 @@
<?php
/**
* REST API Product Categories controller customized for Products Block.
*
* Handles requests to the /products/categories endpoint.
*
* @package WooCommerce\Blocks\Products\Rest\Controller
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Categories controller class.
*
* @package WooCommerce/API
*/
class WGPB_Product_Categories_Controller extends WC_REST_Product_Categories_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-pb/v3';
/**
* Register the routes for products.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param(
array(
'default' => 'view',
)
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check permissions.
*
* @param WP_REST_Request $request Full details about the request.
* @param string $context Request context.
* @return bool|WP_Error
*/
protected function check_permissions( $request, $context = 'read' ) {
// Get taxonomy.
$taxonomy = $this->get_taxonomy( $request );
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
// Check permissions for a single term.
$id = intval( $request['id'] );
if ( $id ) {
$term = get_term( $id, $taxonomy );
if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) {
return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
}
return current_user_can( 'edit_posts' );
}
/**
* Prepare a single product category output for response.
*
* @param WP_Term $item Term object.
* @param WP_REST_Request $request Request instance.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'parent' => (int) $item->parent,
'count' => (int) $item->count,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
return $response;
}
/**
* Get the Product's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$raw_schema = parent::get_item_schema();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'product_block_category',
'type' => 'object',
'properties' => array(),
);
$schema['properties']['id'] = $raw_schema['properties']['id'];
$schema['properties']['name'] = $raw_schema['properties']['name'];
$schema['properties']['slug'] = $raw_schema['properties']['slug'];
$schema['properties']['parent'] = $raw_schema['properties']['parent'];
$schema['properties']['count'] = $raw_schema['properties']['count'];
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -1,9 +1,10 @@
<?php
/**
* REST API Products controller with extra functionality not yet available in WC core.
* REST API Products controller customized for Products Block.
*
* Handles requests to the /products endpoint.
* @todo Remove this once the orderby features are available in WC core.
*
* @package WooCommerce\Blocks\Products\Rest\Controller
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -22,7 +23,136 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller {
*
* @var string
*/
protected $namespace = 'wgbp/v3';
protected $namespace = 'wc-pb/v3';
/**
* Register the routes for products.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param(
array(
'default' => 'view',
)
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check if a given request has access to read items.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to read an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get a collection of posts.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$query_args = $this->prepare_objects_query( $request );
$query_results = $this->get_objects( $query_args );
$objects = array();
foreach ( $query_results['objects'] as $object ) {
$data = $this->prepare_object_for_response( $object, $request );
$objects[] = $this->prepare_response_for_collection( $data );
}
$page = (int) $query_args['paged'];
$max_pages = $query_results['pages'];
$response = rest_ensure_response( $objects );
$response->header( 'X-WP-Total', $query_results['total'] );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
$base = $this->rest_base;
$attrib_prefix = '(?P<';
if ( strpos( $base, $attrib_prefix ) !== false ) {
$attrib_names = array();
preg_match( '/\(\?P<[^>]+>.*\)/', $base, $attrib_names, PREG_OFFSET_CAPTURE );
foreach ( $attrib_names as $attrib_name_match ) {
$beginning_offset = strlen( $attrib_prefix );
$attrib_name_end = strpos( $attrib_name_match[0], '>', $attrib_name_match[1] );
$attrib_name = substr( $attrib_name_match[0], $beginning_offset, $attrib_name_end - $beginning_offset );
if ( isset( $request[ $attrib_name ] ) ) {
$base = str_replace( "(?P<$attrib_name>[\d]+)", $request[ $attrib_name ], $base );
}
}
}
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}
return $response;
}
/**
* Get the images for a product or product variation.
@ -31,7 +161,7 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller {
* @return array
*/
protected function get_images( $product ) {
$images = array();
$images = array();
$attachment_ids = array();
// Add featured image.
@ -55,20 +185,41 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller {
}
$images[] = array(
'id' => (int) $attachment_id,
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
'src' => current( $attachment ),
'name' => get_the_title( $attachment_id ),
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
'id' => (int) $attachment_id,
'src' => current( $attachment ),
'name' => get_the_title( $attachment_id ),
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
);
}
return $images;
}
/**
* Prepare a single product output for response.
*
* @deprecated 3.0.0
*
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $post, $request ) {
$product = wc_get_product( $post );
$data = $this->get_product_data( $product );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $product, $request ) );
return $response;
}
/**
* Make extra product orderby features supported by WooCommerce available to the WC API.
* This includes 'price', 'popularity', and 'rating'.
@ -80,77 +231,38 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller {
$args = parent::prepare_objects_query( $request );
$orderby = $request->get_param( 'orderby' );
$order = $request->get_param( 'order' );
$order = $request->get_param( 'order' );
$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
$args['orderby'] = $ordering_args['orderby'];
$args['order'] = $ordering_args['order'];
if ( $ordering_args['meta_key'] ) {
$args['meta_key'] = $ordering_args['meta_key'];
$args['meta_key'] = $ordering_args['meta_key']; // WPCS: slow query ok.
}
return $args;
}
/**
* Set product images.
* Get product data.
*
* @throws WC_REST_Exception REST API exceptions.
* @param WC_Product $product Product instance.
* @param array $images Images data.
* @return WC_Product
* @param string $context Request context.
* Options: 'view' and 'edit'.
* @return array
*/
protected function set_product_images( $product, $images ) {
if ( is_array( $images ) ) {
$gallery = array();
protected function get_product_data( $product, $context = 'view' ) {
$raw_data = parent::get_product_data( $product, $context );
$data = array();
foreach ( $images as $index => $image ) {
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
$data['id'] = $raw_data['id'];
$data['name'] = $raw_data['name'];
$data['sku'] = $raw_data['sku'];
$data['price'] = $raw_data['price'];
$data['price_html'] = $raw_data['price_html'];
$data['images'] = $raw_data['images'];
if ( 0 === $attachment_id && isset( $image['src'] ) ) {
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
if ( is_wp_error( $upload ) ) {
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images ) ) {
throw new WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
} else {
continue;
}
}
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $product->get_id() );
}
if ( ! wp_attachment_is_image( $attachment_id ) ) {
throw new WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
}
$featured_image = $product->get_image_id();
if ( 0 === $index ) {
$product->set_image_id( $attachment_id );
} else {
$gallery[] = $attachment_id;
}
// Set the image alt if present.
if ( ! empty( $image['alt'] ) ) {
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
}
// Set the image name if present.
if ( ! empty( $image['name'] ) ) {
wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['name'] ) );
}
}
$product->set_gallery_image_ids( $gallery );
} else {
$product->set_image_id( '' );
$product->set_gallery_image_ids( array() );
}
return $product;
return $data;
}
/**
@ -159,7 +271,7 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller {
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params = parent::get_collection_params();
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'price', 'popularity', 'rating', 'menu_order' ) );
return $params;
}
@ -170,615 +282,36 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller {
* @return array
*/
public function get_item_schema() {
$weight_unit = get_option( 'woocommerce_weight_unit' );
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
$schema = array(
$raw_schema = parent::get_item_schema();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => $this->post_type,
'title' => 'product_block_product',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Product name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'slug' => array(
'description' => __( 'Product slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'permalink' => array(
'description' => __( 'Product URL.', 'woocommerce' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created' => array(
'description' => __( "The date the product was created, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created_gmt' => array(
'description' => __( "The date the product was created, as GMT.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_modified' => array(
'description' => __( "The date the product was last modified, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_modified_gmt' => array(
'description' => __( "The date the product was last modified, as GMT.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'type' => array(
'description' => __( 'Product type.', 'woocommerce' ),
'type' => 'string',
'default' => 'simple',
'enum' => array_keys( wc_get_product_types() ),
'context' => array( 'view', 'edit' ),
),
'status' => array(
'description' => __( 'Product status (post status).', 'woocommerce' ),
'type' => 'string',
'default' => 'publish',
'enum' => array_keys( get_post_statuses() ),
'context' => array( 'view', 'edit' ),
),
'featured' => array(
'description' => __( 'Featured product.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'catalog_visibility' => array(
'description' => __( 'Catalog visibility.', 'woocommerce' ),
'type' => 'string',
'default' => 'visible',
'enum' => array( 'visible', 'catalog', 'search', 'hidden' ),
'context' => array( 'view', 'edit' ),
),
'description' => array(
'description' => __( 'Product description.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'short_description' => array(
'description' => __( 'Product short description.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'sku' => array(
'description' => __( 'Unique identifier.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'price' => array(
'description' => __( 'Current product price.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'regular_price' => array(
'description' => __( 'Product regular price.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'sale_price' => array(
'description' => __( 'Product sale price.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'date_on_sale_from' => array(
'description' => __( "Start date of sale price, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'date_on_sale_from_gmt' => array(
'description' => __( 'Start date of sale price, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'date_on_sale_to' => array(
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'date_on_sale_to_gmt' => array(
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'price_html' => array(
'description' => __( 'Price formatted in HTML.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'on_sale' => array(
'description' => __( 'Shows if the product is on sale.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'purchasable' => array(
'description' => __( 'Shows if the product can be bought.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'total_sales' => array(
'description' => __( 'Amount of sales.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'virtual' => array(
'description' => __( 'If the product is virtual.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'downloadable' => array(
'description' => __( 'If the product is downloadable.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'downloads' => array(
'description' => __( 'List of downloadable files.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'File MD5 hash.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'File name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'file' => array(
'description' => __( 'File URL.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
),
),
),
'download_limit' => array(
'description' => __( 'Number of times downloadable files can be downloaded after purchase.', 'woocommerce' ),
'type' => 'integer',
'default' => -1,
'context' => array( 'view', 'edit' ),
),
'download_expiry' => array(
'description' => __( 'Number of days until access to downloadable files expires.', 'woocommerce' ),
'type' => 'integer',
'default' => -1,
'context' => array( 'view', 'edit' ),
),
'external_url' => array(
'description' => __( 'Product external URL. Only for external products.', 'woocommerce' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'view', 'edit' ),
),
'button_text' => array(
'description' => __( 'Product external button text. Only for external products.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'tax_status' => array(
'description' => __( 'Tax status.', 'woocommerce' ),
'type' => 'string',
'default' => 'taxable',
'enum' => array( 'taxable', 'shipping', 'none' ),
'context' => array( 'view', 'edit' ),
),
'tax_class' => array(
'description' => __( 'Tax class.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'manage_stock' => array(
'description' => __( 'Stock management at product level.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'stock_quantity' => array(
'description' => __( 'Stock quantity.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'in_stock' => array(
'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ),
'type' => 'boolean',
'default' => true,
'context' => array( 'view', 'edit' ),
),
'backorders' => array(
'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ),
'type' => 'string',
'default' => 'no',
'enum' => array( 'no', 'notify', 'yes' ),
'context' => array( 'view', 'edit' ),
),
'backorders_allowed' => array(
'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'backordered' => array(
'description' => __( 'Shows if the product is on backordered.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'sold_individually' => array(
'description' => __( 'Allow one item to be bought in a single order.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'weight' => array(
/* translators: %s: weight unit */
'description' => sprintf( __( 'Product weight (%s).', 'woocommerce' ), $weight_unit ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'dimensions' => array(
'description' => __( 'Product dimensions.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'length' => array(
/* translators: %s: dimension unit */
'description' => sprintf( __( 'Product length (%s).', 'woocommerce' ), $dimension_unit ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'width' => array(
/* translators: %s: dimension unit */
'description' => sprintf( __( 'Product width (%s).', 'woocommerce' ), $dimension_unit ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'height' => array(
/* translators: %s: dimension unit */
'description' => sprintf( __( 'Product height (%s).', 'woocommerce' ), $dimension_unit ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
),
),
'shipping_required' => array(
'description' => __( 'Shows if the product need to be shipped.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'shipping_taxable' => array(
'description' => __( 'Shows whether or not the product shipping is taxable.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'shipping_class' => array(
'description' => __( 'Shipping class slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'shipping_class_id' => array(
'description' => __( 'Shipping class ID.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'reviews_allowed' => array(
'description' => __( 'Allow reviews.', 'woocommerce' ),
'type' => 'boolean',
'default' => true,
'context' => array( 'view', 'edit' ),
),
'average_rating' => array(
'description' => __( 'Reviews average rating.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'rating_count' => array(
'description' => __( 'Amount of reviews that the product have.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'related_ids' => array(
'description' => __( 'List of related products IDs.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'upsell_ids' => array(
'description' => __( 'List of up-sell products IDs.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
'cross_sell_ids' => array(
'description' => __( 'List of cross-sell products IDs.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
),
'parent_id' => array(
'description' => __( 'Product parent ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'purchase_note' => array(
'description' => __( 'Optional note to send the customer after purchase.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'categories' => array(
'description' => __( 'List of categories.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Category ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'name' => array(
'description' => __( 'Category name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'Category slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
),
'tags' => array(
'description' => __( 'List of tags.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Tag ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'name' => array(
'description' => __( 'Tag name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'Tag slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
),
'images' => array(
'description' => __( 'List of images.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Image ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'date_created' => array(
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created_gmt' => array(
'description' => __( "The date the image was created, as GMT.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_modified' => array(
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_modified_gmt' => array(
'description' => __( "The date the image was last modified, as GMT.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'src' => array(
'description' => __( 'Image URL.', 'woocommerce' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'view', 'edit' ),
),
'name' => array(
'description' => __( 'Image name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'alt' => array(
'description' => __( 'Image alternative text.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
),
),
),
'attributes' => array(
'description' => __( 'List of attributes.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Attribute ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'name' => array(
'description' => __( 'Attribute name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'position' => array(
'description' => __( 'Attribute position.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'visible' => array(
'description' => __( "Define if the attribute is visible on the \"Additional information\" tab in the product's page.", 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'variation' => array(
'description' => __( 'Define if the attribute can be used as variation.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'view', 'edit' ),
),
'options' => array(
'description' => __( 'List of available term names of the attribute.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
'context' => array( 'view', 'edit' ),
),
),
),
),
'default_attributes' => array(
'description' => __( 'Defaults variation attributes.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Attribute ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'name' => array(
'description' => __( 'Attribute name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'option' => array(
'description' => __( 'Selected attribute term name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
),
),
),
'variations' => array(
'description' => __( 'List of variations IDs.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'integer',
),
'readonly' => true,
),
'grouped_products' => array(
'description' => __( 'List of grouped products ID.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'menu_order' => array(
'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'meta_data' => array(
'description' => __( 'Meta data.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Meta ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'key' => array(
'description' => __( 'Meta key.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'value' => array(
'description' => __( 'Meta value.', 'woocommerce' ),
'type' => 'mixed',
'context' => array( 'view', 'edit' ),
),
),
),
),
'properties' => array(),
);
$schema['properties']['id'] = $raw_schema['properties']['id'];
$schema['properties']['name'] = $raw_schema['properties']['name'];
$schema['properties']['sku'] = $raw_schema['properties']['sku'];
$schema['properties']['price'] = $raw_schema['properties']['price'];
$schema['properties']['price_html'] = $raw_schema['properties']['price_html'];
$schema['properties']['images'] = array(
'description' => $raw_schema['properties']['images']['description'],
'type' => 'object',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(),
),
);
$images_schema = $raw_schema['properties']['images']['items']['properties'];
$schema['properties']['images']['items']['properties']['id'] = $images_schema['id'];
$schema['properties']['images']['items']['properties']['src'] = $images_schema['src'];
$schema['properties']['images']['items']['properties']['name'] = $images_schema['name'];
$schema['properties']['images']['items']['properties']['alt'] = $images_schema['alt'];
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -6233,8 +6233,7 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"aproba": {
"version": "1.2.0",
@ -6255,14 +6254,12 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -6277,20 +6274,17 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@ -6407,8 +6401,7 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"ini": {
"version": "1.3.5",
@ -6420,7 +6413,6 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -6435,7 +6427,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -6443,14 +6434,12 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"minipass": {
"version": "2.2.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.1",
"yallist": "^3.0.0"
@ -6469,7 +6458,6 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@ -6550,8 +6538,7 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"object-assign": {
"version": "4.1.1",
@ -6563,7 +6550,6 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@ -6649,8 +6635,7 @@
"safe-buffer": {
"version": "5.1.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
@ -6686,7 +6671,6 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@ -6706,7 +6690,6 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@ -6750,14 +6733,12 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"yallist": {
"version": "3.0.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
}
}
},

View File

@ -12,7 +12,7 @@ class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
*
* @var string
*/
protected $endpoint = '/wgbp/v3'; // @todo Fix endpoint typo?
protected $endpoint = '/wc-pb/v3';
/**
* Setup test products data. Called before every test.
@ -24,12 +24,17 @@ class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
'role' => 'author',
)
);
$this->author = $this->factory->user->create(
$this->contributor = $this->factory->user->create(
array(
'role' => 'author',
'role' => 'contributor',
)
);
$this->subscriber = $this->factory->user->create(
array(
'role' => 'subscriber',
)
);
}
@ -42,9 +47,8 @@ class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wgbp/v3/products', $routes );
$this->assertArrayHasKey( '/wgbp/v3/products/(?P<id>[\d]+)', $routes );
$this->assertArrayHasKey( '/wgbp/v3/products/batch', $routes );
$this->assertArrayHasKey( '/wc-pb/v3/products', $routes );
$this->assertArrayHasKey( '/wc-pb/v3/products/(?P<id>[\d]+)', $routes );
}
/**
@ -57,7 +61,7 @@ class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
WC_Helper_Product::create_external_product();
sleep( 1 ); // So both products have different timestamps.
$product = WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wgbp/v3/products' ) );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-pb/v3/products' ) );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
@ -70,14 +74,26 @@ class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
}
/**
* Test getting products as an author.
* Test getting products as an contributor.
*
* @since 1.2.0
*/
public function test_get_products_as_author() {
wp_set_current_user( $this->author );
public function test_get_products_as_contributor() {
wp_set_current_user( $this->contributor );
WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wgbp/v3/products' ) );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-pb/v3/products' ) );
$this->assertEquals( 200, $response->get_status() );
}
/**
* Test getting products as an subscriber.
*
* @since 1.2.0
*/
public function test_get_products_as_subscriber() {
wp_set_current_user( $this->subscriber );
WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-pb/v3/products' ) );
$this->assertEquals( 403, $response->get_status() );
}
@ -98,7 +114,7 @@ class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
) );
$product->save();
$request = new WP_REST_Request( 'GET', '/wgbp/v3/products' );
$request = new WP_REST_Request( 'GET', '/wc-pb/v3/products' );
$request->set_param( 'orderby', 'price' );
$request->set_param( 'order', 'asc' );
$response = $this->server->dispatch( $request );

View File

@ -161,14 +161,25 @@ function wgpb_print_script_settings() {
add_action( 'admin_print_footer_scripts', 'wgpb_print_script_settings', 1 );
/**
* Register extra API routes with functionality not available in WC core yet.
*
* @todo Remove this function when merging into core because it won't be necessary.
* Register extra API routes with functionality specific for product blocks.
*/
function wgpb_register_api_routes() {
include_once( dirname( __FILE__ ) . '/includes/class-wgpb-products-controller.php' );
$controller = new WGPB_Products_Controller();
$controller->register_routes();
include_once dirname( __FILE__ ) . '/includes/class-wgpb-products-controller.php';
include_once dirname( __FILE__ ) . '/includes/class-wgpb-product-categories-controller.php';
include_once dirname( __FILE__ ) . '/includes/class-wgpb-product-attributes-controller.php';
include_once dirname( __FILE__ ) . '/includes/class-wgpb-product-attribute-terms-controller.php';
$products = new WGPB_Products_Controller();
$products->register_routes();
$categories = new WGPB_Product_Categories_Controller();
$categories->register_routes();
$attributes = new WGPB_Product_Attributes_Controller();
$attributes->register_routes();
$attribute_terms = new WGPB_Product_Attribute_Terms_Controller();
$attribute_terms->register_routes();
}
/**