Remove all blocks APIs

This commit is contained in:
Mike Jolley 2019-06-11 16:15:02 +01:00
parent 411054fb5f
commit 013934d8b7
9 changed files with 0 additions and 1609 deletions

View File

@ -1,27 +0,0 @@
<?php
/**
* Returns controllers in this REST API namespace.
*
* @package WooCommerce/RestApi
*/
defined( 'ABSPATH' ) || exit;
/**
* Controllers class.
*/
class WC_REST_Blocks_Controllers {
/**
* Return a list of controller classes for this REST API namespace.
*
* @return array
*/
public static function get_controllers() {
return [
'product-attributes' => 'WC_REST_Blocks_Product_Attributes_Controller',
'product-attribute-terms' => 'WC_REST_Blocks_Product_Attribute_Terms_Controller',
'product-categories' => 'WC_REST_Blocks_Product_Categories_Controller',
'products' => 'WC_REST_Blocks_Products_Controller',
];
}
}

View File

@ -1,185 +0,0 @@
<?php
/**
* REST API Product Attribute Terms controller customized for Products Block.
*
* Handles requests to the /products/attributes/<attribute_id/terms endpoint.
*
* @internal This API is used internally by the block post editor--it is still in flux. It should not be used outside of wc-blocks.
* @package WooCommerce/RestApi
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Attribute Terms controller class.
*
* @package WooCommerce/RestApi
*/
class WC_REST_Blocks_Product_Attribute_Terms_Controller extends WC_REST_Product_Attribute_Terms_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-blocks/v1';
/**
* 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.', 'woocommerce' ),
'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.', 'woocommerce' ), 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.', 'woocommerce' ), 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 the attribute slug.
$attribute_id = absint( $request->get_param( 'attribute_id' ) );
$attribute = wc_get_attribute( $attribute_id );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'count' => (int) $item->count,
'attribute' => array(
'id' => $attribute->id,
'name' => $attribute->name,
'slug' => $attribute->slug,
),
);
$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->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
$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_attribute_term',
'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'];
$schema['properties']['attribute'] = array(
'description' => __( 'Attribute.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => array(
'id' => array(
'description' => __( 'Attribute ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Attribute name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'Attribute slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -1,188 +0,0 @@
<?php
/**
* REST API Product Attributes controller customized for Products Block.
*
* Handles requests to the /products/attributes endpoint.
*
* @internal This API is used internally by the block post editor--it is still in flux. It should not be used outside of wc-blocks.
* @package WooCommerce/RestApi
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Attributes controller class.
*
* @package WooCommerce/RestApi
*/
class WC_REST_Blocks_Product_Attributes_Controller extends WC_REST_Product_Attributes_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-blocks/v1';
/**
* 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.', 'woocommerce' ),
'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.', 'woocommerce' ), 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.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), 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.', 'woocommerce' ), 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.', 'woocommerce' ), 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 ) {
$taxonomy = wc_attribute_taxonomy_name( $item->attribute_name );
$data = array(
'id' => (int) $item->attribute_id,
'name' => $item->attribute_label,
'slug' => $taxonomy,
'count' => wp_count_terms( $taxonomy ),
);
$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->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
$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'];
$schema['properties']['count'] = array(
'description' => __( 'Number of terms in the attribute taxonomy.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
);
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -1,151 +0,0 @@
<?php
/**
* REST API Product Categories controller customized for Products Block.
*
* Handles requests to the /products/categories endpoint.
*
* @internal This API is used internally by the block post editor--it is still in flux. It should not be used outside of wc-blocks.
* @package WooCommerce/RestApi
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Categories controller class.
*
* @package WooCommerce/RestApi
*/
class WC_REST_Blocks_Product_Categories_Controller extends WC_REST_Product_Categories_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-blocks/v1';
/**
* 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.', 'woocommerce' ),
'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.', 'woocommerce' ), 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.', 'woocommerce' ), 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->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
$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,390 +0,0 @@
<?php
/**
* REST API Products controller customized for Products Block.
*
* Handles requests to the /products endpoint.
*
* @internal This API is used internally by the block post editor--it is still in flux. It should not be used outside of wc-blocks.
* @package WooCommerce/RestApi
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Products controller class.
*
* @package WooCommerce/RestApi
*/
class WC_REST_Blocks_Products_Controller extends WC_REST_Products_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-blocks/v1';
/**
* 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.', 'woocommerce' ),
'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.', 'woocommerce' ), 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.', 'woocommerce' ), 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 );
$response->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
$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.
*
* @param WC_Product|WC_Product_Variation $product Product instance.
* @return array
*/
protected function get_images( $product ) {
$images = array();
$attachment_ids = array();
// Add featured image.
if ( has_post_thumbnail( $product->get_id() ) ) {
$attachment_ids[] = $product->get_image_id();
}
// Add gallery images.
$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );
// Build image data.
foreach ( $attachment_ids as $attachment_id ) {
$attachment_post = get_post( $attachment_id );
if ( is_null( $attachment_post ) ) {
continue;
}
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
if ( ! is_array( $attachment ) ) {
continue;
}
$images[] = array(
'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->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
$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'.
*
* @param WP_REST_Request $request Request data.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );
$operator_mapping = array(
'in' => 'IN',
'not_in' => 'NOT IN',
'and' => 'AND',
);
$orderby = $request->get_param( 'orderby' );
$order = $request->get_param( 'order' );
$category_operator = $operator_mapping[ $request->get_param( 'category_operator' ) ];
$attribute_operator = $operator_mapping[ $request->get_param( 'attribute_operator' ) ];
$catalog_visibility = $request->get_param( 'catalog_visibility' );
$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']; // WPCS: slow query ok.
}
if ( $category_operator && isset( $args['tax_query'] ) ) {
foreach ( $args['tax_query'] as $i => $tax_query ) {
if ( 'product_cat' === $tax_query['taxonomy'] ) {
$args['tax_query'][ $i ]['operator'] = $category_operator;
$args['tax_query'][ $i ]['include_children'] = 'AND' === $category_operator ? false : true;
}
}
}
if ( $attribute_operator && isset( $args['tax_query'] ) ) {
foreach ( $args['tax_query'] as $i => $tax_query ) {
if ( in_array( $tax_query['taxonomy'], wc_get_attribute_taxonomy_names(), true ) ) {
$args['tax_query'][ $i ]['operator'] = $attribute_operator;
}
}
}
if ( in_array( $catalog_visibility, array_keys( wc_get_product_visibility_options() ), true ) ) {
$exclude_from_catalog = 'search' === $catalog_visibility ? '' : 'exclude-from-catalog';
$exclude_from_search = 'catalog' === $catalog_visibility ? '' : 'exclude-from-search';
$args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array( $exclude_from_catalog, $exclude_from_search ),
'operator' => 'hidden' === $catalog_visibility ? 'AND' : 'NOT IN',
);
}
return $args;
}
/**
* Get product data.
*
* @param WC_Product $product Product instance.
* @param string $context Request context.
* Options: 'view' and 'edit'.
* @return array
*/
protected function get_product_data( $product, $context = 'view' ) {
$raw_data = parent::get_product_data( $product, $context );
$data = array();
$data['id'] = $raw_data['id'];
$data['name'] = $raw_data['name'];
$data['permalink'] = $raw_data['permalink'];
$data['sku'] = $raw_data['sku'];
$data['description'] = $raw_data['description'];
$data['short_description'] = $raw_data['short_description'];
$data['price'] = $raw_data['price'];
$data['price_html'] = $raw_data['price_html'];
$data['images'] = $raw_data['images'];
$data['average_rating'] = $raw_data['average_rating'];
return $data;
}
/**
* Update the collection params.
*
* Adds new options for 'orderby', and new parameters 'category_operator', 'attribute_operator'.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'price', 'popularity', 'rating', 'menu_order' ) );
$params['category_operator'] = array(
'description' => __( 'Operator to compare product category terms.', 'woocommerce' ),
'type' => 'string',
'enum' => array( 'in', 'not_in', 'and' ),
'default' => 'in',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_operator'] = array(
'description' => __( 'Operator to compare product attribute terms.', 'woocommerce' ),
'type' => 'string',
'enum' => array( 'in', 'not_in', 'and' ),
'default' => 'in',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
);
$params['catalog_visibility'] = array(
'description' => __( 'Determines if hidden or visible catalog products are shown.', 'woocommerce' ),
'type' => 'string',
'enum' => array( 'visible', 'catalog', 'search', 'hidden' ),
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* 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_product',
'type' => 'object',
'properties' => array(),
);
$schema['properties']['id'] = $raw_schema['properties']['id'];
$schema['properties']['name'] = $raw_schema['properties']['name'];
$schema['properties']['permalink'] = $raw_schema['properties']['permalink'];
$schema['properties']['sku'] = $raw_schema['properties']['sku'];
$schema['properties']['description'] = $raw_schema['properties']['description'];
$schema['properties']['short_description'] = $raw_schema['properties']['short_description'];
$schema['properties']['price'] = $raw_schema['properties']['price'];
$schema['properties']['price_html'] = $raw_schema['properties']['price_html'];
$schema['properties']['average_rating'] = $raw_schema['properties']['average_rating'];
$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

@ -1,113 +0,0 @@
<?php
/**
* @package WooCommerce\Tests\API
*/
namespace WooCommerce\RestApi\UnitTests\Tests\Blocks;
defined( 'ABSPATH' ) || exit;
use \WP_REST_Request;
use \WC_REST_Unit_Test_Case;
use \WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
/**
* Product Controller "products attributes terms" REST API Test
*
* @since 3.6.0
*/
class ProductAttributeTerms extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-blocks/v1';
/**
* Setup test products data. Called before every test.
*
* @since 3.6.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
$this->contributor = $this->factory->user->create(
array(
'role' => 'contributor',
)
);
// Create 2 product attributes with terms.
$this->attr_color = ProductHelper::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) );
$this->attr_size = ProductHelper::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) );
}
/**
* Test getting attribute terms.
*
* @since 3.6.0
*/
public function test_get_terms() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_color['attribute_id'] . '/terms' );
$response = $this->server->dispatch( $request );
$response_terms = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 3, count( $response_terms ) );
$term = $response_terms[0];
$this->assertArrayHasKey( 'attribute', $term );
$attribute = $term['attribute'];
$this->assertArrayHasKey( 'id', $attribute );
$this->assertArrayHasKey( 'name', $attribute );
$this->assertArrayHasKey( 'slug', $attribute );
}
/**
* Test getting invalid attribute terms.
*
* @since 3.6.0
*/
public function test_get_invalid_attribute_terms() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/99999/terms' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test un-authorized getting attribute terms.
*
* @since 3.6.0
*/
public function test_get_unauthed_attribute_terms() {
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test getting attribute terms as contributor.
*
* @since 3.6.0
*/
public function test_get_attribute_terms_contributor() {
wp_set_current_user( $this->contributor );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' );
$response = $this->server->dispatch( $request );
$response_terms = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 4, count( $response_terms ) );
}
}

View File

@ -1,113 +0,0 @@
<?php
/**
* @package WooCommerce\Tests\API
*/
namespace WooCommerce\RestApi\UnitTests\Tests\Blocks;
defined( 'ABSPATH' ) || exit;
use \WP_REST_Request;
use \WC_REST_Unit_Test_Case;
use \WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
/**
* Product Controller "products attributes" REST API Test
*
* @since 3.6.0
*/
class ProductAttributes extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-blocks/v1';
/**
* Setup test products data. Called before every test.
*
* @since 3.6.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
$this->contributor = $this->factory->user->create(
array(
'role' => 'contributor',
)
);
// Create 2 product attributes with terms.
$this->attr_color = ProductHelper::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) );
$this->attr_size = ProductHelper::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) );
}
/**
* Test getting attributes.
*
* @since 3.6.0
*/
public function test_get_attributes() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes' );
$response = $this->server->dispatch( $request );
$response_attributes = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $response_attributes ) );
$attribute = $response_attributes[0];
$this->assertArrayHasKey( 'id', $attribute );
$this->assertArrayHasKey( 'name', $attribute );
$this->assertArrayHasKey( 'slug', $attribute );
$this->assertArrayHasKey( 'count', $attribute );
}
/**
* Test getting invalid attribute.
*
* @since 3.6.0
*/
public function test_get_invalid_attribute() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/11111' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test un-authorized getting attribute.
*
* @since 3.6.0
*/
public function test_get_unauthed_attribute() {
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test getting attribute as contributor.
*
* @since 3.6.0
*/
public function test_get_attribute_contributor() {
wp_set_current_user( $this->contributor );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] );
$response = $this->server->dispatch( $request );
$attribute = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $this->attr_size['attribute_id'], $attribute['id'] );
$this->assertEquals( $this->attr_size['attribute_name'], $attribute['name'] );
}
}

View File

@ -1,129 +0,0 @@
<?php
/**
* @package WooCommerce\Tests\API
*/
namespace WooCommerce\RestApi\UnitTests\Tests\Blocks;
defined( 'ABSPATH' ) || exit;
use \WP_REST_Request;
use \WC_REST_Unit_Test_Case;
use \WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
/**
* Product Categories Controller REST API Test
*
* @since 3.6.0
*/
class ProductCategories extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-blocks/v1';
/**
* Setup test products data. Called before every test.
*
* @since 3.6.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
$this->contributor = $this->factory->user->create(
array(
'role' => 'contributor',
)
);
// Create 3 product categories.
$parent = wp_insert_term( 'Parent Category', 'product_cat' );
$child = wp_insert_term(
'Child Category',
'product_cat',
array( 'parent' => $parent['term_id'] )
);
$single = wp_insert_term( 'Standalone Category', 'product_cat' );
$this->categories = array(
'parent' => $parent,
'child' => $child,
'single' => $single,
);
// Create two products for the parent category.
$this->products = array();
$this->products[0] = ProductHelper::create_simple_product( false );
$this->products[0]->set_category_ids( array( $parent['term_id'] ) );
$this->products[0]->save();
$this->products[3] = ProductHelper::create_simple_product( false );
$this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) );
$this->products[3]->save();
}
/**
* Test getting product categories.
*
* @since 3.6.0
*/
public function test_get_product_categories() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories' );
$response = $this->server->dispatch( $request );
$categories = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 4, count( $categories ) ); // Three created and `uncategorized`.
}
/**
* Test getting invalid product category.
*
* @since 3.6.0
*/
public function test_get_invalid_product_category() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/007' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test un-authorized getting product category.
*
* @since 3.6.0
*/
public function test_get_unauthed_product_category() {
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/' . $this->categories['parent']['term_id'] );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test getting category as contributor.
*
* @since 3.6.0
*/
public function test_get_attribute_terms_contributor() {
wp_set_current_user( $this->contributor );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/' . $this->categories['parent']['term_id'] );
$response = $this->server->dispatch( $request );
$category = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $category['name'], 'Parent Category' );
$this->assertEquals( $category['parent'], 0 );
$this->assertEquals( $category['count'], 2 );
}
}

View File

@ -1,313 +0,0 @@
<?php
/**
* @package WooCommerce\Tests\API
*/
namespace WooCommerce\RestApi\UnitTests\Tests\Blocks;
defined( 'ABSPATH' ) || exit;
use \WP_REST_Request;
use \WC_REST_Unit_Test_Case;
use \WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
/**
* Blocks Product Controller REST API Test
*
* @since 3.6.0
*/
class Products extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-blocks/v1';
/**
* Setup test products data. Called before every test.
*
* @since 1.2.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'author',
)
);
$this->contributor = $this->factory->user->create(
array(
'role' => 'contributor',
)
);
$this->subscriber = $this->factory->user->create(
array(
'role' => 'subscriber',
)
);
// Create 3 product categories.
$parent = wp_insert_term( 'Parent Category', 'product_cat' );
$child = wp_insert_term(
'Child Category',
'product_cat',
array( 'parent' => $parent['term_id'] )
);
$single = wp_insert_term( 'Standalone Category', 'product_cat' );
$this->categories = array(
'parent' => $parent,
'child' => $child,
'single' => $single,
);
// Create two products, one with 'parent', and one with 'single'.
$this->products = array();
$this->products[0] = ProductHelper::create_simple_product( false );
$this->products[0]->set_category_ids( array( $parent['term_id'] ) );
$this->products[0]->save();
$this->products[1] = ProductHelper::create_simple_product( false );
$this->products[1]->set_category_ids( array( $single['term_id'] ) );
$this->products[1]->save();
$this->products[2] = ProductHelper::create_simple_product( false );
$this->products[2]->set_category_ids( array( $child['term_id'], $single['term_id'] ) );
$this->products[2]->save();
$this->products[3] = ProductHelper::create_simple_product( false );
$this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) );
$this->products[3]->save();
}
/**
* Test route registration.
*
* @since 3.6.0
*/
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wc-blocks/v1/products', $routes );
$this->assertArrayHasKey( '/wc-blocks/v1/products/(?P<id>[\d]+)', $routes );
}
/**
* Test getting products.
*
* @since 3.6.0
*/
public function test_get_products() {
wp_set_current_user( $this->user );
ProductHelper::create_external_product();
sleep( 1 ); // So both products have different timestamps.
$product = ProductHelper::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 6, count( $products ) );
}
/**
* Test getting products as an contributor.
*
* @since 3.6.0
*/
public function test_get_products_as_contributor() {
wp_set_current_user( $this->contributor );
ProductHelper::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) );
$this->assertEquals( 200, $response->get_status() );
}
/**
* Test getting products as an subscriber.
*
* @since 3.6.0
*/
public function test_get_products_as_subscriber() {
wp_set_current_user( $this->subscriber );
ProductHelper::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) );
$this->assertEquals( 403, $response->get_status() );
}
/**
* Test getting products with custom ordering.
*
* @since 3.6.0
*/
public function test_get_products_order_by_price() {
wp_set_current_user( $this->user );
ProductHelper::create_external_product();
sleep( 1 ); // So both products have different timestamps.
$product = ProductHelper::create_simple_product( false ); // Prevent saving, since we save here.
// Customize the price, otherwise both are 10.
$product->set_props(
array(
'regular_price' => 15,
'price' => 15,
)
);
$product->save();
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
$request->set_param( 'orderby', 'price' );
$request->set_param( 'order', 'asc' );
$response = $this->server->dispatch( $request );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 6, count( $products ) );
$this->assertEquals( 'Dummy Product', $products[1]['name'] );
$this->assertEquals( '10', $products[1]['price'] );
}
/**
* Test product_visibility queries.
*
* @since 3.6.0
*/
public function test_product_visibility() {
wp_set_current_user( $this->user );
$visible_product = ProductHelper::create_simple_product();
$visible_product->set_name( 'Visible Product' );
$visible_product->set_catalog_visibility( 'visible' );
$visible_product->save();
$catalog_product = ProductHelper::create_simple_product();
$catalog_product->set_name( 'Catalog Product' );
$catalog_product->set_catalog_visibility( 'catalog' );
$catalog_product->save();
$search_product = ProductHelper::create_simple_product();
$search_product->set_name( 'Search Product' );
$search_product->set_catalog_visibility( 'search' );
$search_product->save();
$hidden_product = ProductHelper::create_simple_product();
$hidden_product->set_name( 'Hidden Product' );
$hidden_product->set_catalog_visibility( 'hidden' );
$hidden_product->save();
$query_params = array(
'catalog_visibility' => 'visible',
);
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
$request->set_query_params( $query_params );
$response = $this->server->dispatch( $request );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 5, count( $products ) );
$this->assertEquals( 'Visible Product', $products[0]['name'] );
$query_params = array(
'catalog_visibility' => 'catalog',
'orderby' => 'id',
'order' => 'asc',
);
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
$request->set_query_params( $query_params );
$response = $this->server->dispatch( $request );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 6, count( $products ) );
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
$query_params = array(
'catalog_visibility' => 'search',
'orderby' => 'id',
'order' => 'asc',
);
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
$request->set_query_params( $query_params );
$response = $this->server->dispatch( $request );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 6, count( $products ) );
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
$query_params = array(
'catalog_visibility' => 'hidden',
);
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
$request->set_query_params( $query_params );
$response = $this->server->dispatch( $request );
$products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, count( $products ) );
$this->assertEquals( 'Hidden Product', $products[0]['name'] );
}
/**
* Test product category intersection: Any product in either Single or Child (3).
*
* @since 3.6.0
*/
public function test_get_products_in_any_categories_child() {
wp_set_current_user( $this->user );
$cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id'];
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
$request->set_param( 'category', $cats );
$request->set_param( 'category_operator', 'in' );
$response = $this->server->dispatch( $request );
$response_products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 3, count( $response_products ) );
}
/**
* Test product category intersection: Any product in both Single and Child (1).
*
* @since 3.6.0
*/
public function test_get_products_in_all_categories_child() {
wp_set_current_user( $this->user );
$cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id'];
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
$request->set_param( 'category', $cats );
$request->set_param( 'category_operator', 'and' );
$response = $this->server->dispatch( $request );
$response_products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, count( $response_products ) );
}
/**
* Test product category intersection: Any product in both Single and Parent (1).
*
* @since 3.6.0
*/
public function test_get_products_in_all_categories_parent() {
wp_set_current_user( $this->user );
$cats = $this->categories['parent']['term_id'] . ',' . $this->categories['single']['term_id'];
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
$request->set_param( 'category', $cats );
$request->set_param( 'category_operator', 'and' );
$response = $this->server->dispatch( $request );
$response_products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, count( $response_products ) );
}
}