2018-06-01 17:22:58 +00:00
< ? php
/**
2018-11-30 19:12:56 +00:00
* REST API Products controller customized for Products Block .
2018-06-01 17:22:58 +00:00
*
* Handles requests to the / products endpoint .
2018-11-30 19:12:56 +00:00
*
* @ package WooCommerce\Blocks\Products\Rest\Controller
2018-06-01 17:22:58 +00:00
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Products controller class .
*
* @ package WooCommerce / API
*/
class WGPB_Products_Controller extends WC_REST_Products_Controller {
/**
* Endpoint namespace .
*
* @ var string
*/
2018-11-30 19:12:56 +00:00
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 ;
}
2018-06-01 17:22:58 +00:00
/**
* 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 ) {
2018-11-30 19:12:56 +00:00
$images = array ();
2018-06-01 17:22:58 +00:00
$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 (
2018-11-30 19:12:56 +00:00
'id' => ( int ) $attachment_id ,
'src' => current ( $attachment ),
'name' => get_the_title ( $attachment_id ),
'alt' => get_post_meta ( $attachment_id , '_wp_attachment_image_alt' , true ),
2018-06-01 17:22:58 +00:00
);
}
return $images ;
}
2018-11-30 19:12:56 +00:00
/**
* 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 ;
}
2018-06-01 17:22:58 +00:00
/**
2018-11-19 16:33:17 +00:00
* Make extra product orderby features supported by WooCommerce available to the WC API .
2018-06-01 17:22:58 +00:00
* 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 );
2019-02-19 16:09:16 +00:00
$orderby = $request -> get_param ( 'orderby' );
$order = $request -> get_param ( 'order' );
$cat_operator = $request -> get_param ( 'cat_operator' );
$attr_operator = $request -> get_param ( 'attr_operator' );
$catalog_visibility = $request -> get_param ( 'catalog_visibility' );
2018-06-01 17:22:58 +00:00
$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' ] ) {
2018-11-30 19:12:56 +00:00
$args [ 'meta_key' ] = $ordering_args [ 'meta_key' ]; // WPCS: slow query ok.
2018-06-01 17:22:58 +00:00
}
2018-12-17 20:16:01 +00:00
if ( $cat_operator && isset ( $args [ 'tax_query' ] ) ) {
foreach ( $args [ 'tax_query' ] as $i => $tax_query ) {
if ( 'product_cat' === $tax_query [ 'taxonomy' ] ) {
2018-12-20 15:24:11 +00:00
$args [ 'tax_query' ][ $i ][ 'operator' ] = $cat_operator ;
$args [ 'tax_query' ][ $i ][ 'include_children' ] = 'AND' === $cat_operator ? false : true ;
2018-12-17 20:16:01 +00:00
}
}
}
2019-02-06 22:59:34 +00:00
if ( $attr_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' ] = $attr_operator ;
2019-01-09 15:21:23 +00:00
}
}
}
2019-02-19 16:09:16 +00:00
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' ,
);
}
2018-06-01 17:22:58 +00:00
return $args ;
}
/**
2018-11-30 19:12:56 +00:00
* Get product data .
2018-06-01 17:22:58 +00:00
*
* @ param WC_Product $product Product instance .
2018-11-30 19:12:56 +00:00
* @ param string $context Request context .
* Options : 'view' and 'edit' .
* @ return array
2018-06-01 17:22:58 +00:00
*/
2018-11-30 19:12:56 +00:00
protected function get_product_data ( $product , $context = 'view' ) {
$raw_data = parent :: get_product_data ( $product , $context );
$data = array ();
2018-06-01 17:22:58 +00:00
2019-01-11 23:58:58 +00:00
$data [ 'id' ] = $raw_data [ 'id' ];
$data [ 'name' ] = $raw_data [ 'name' ];
2019-02-08 23:57:58 +00:00
$data [ 'permalink' ] = $raw_data [ 'permalink' ];
2019-01-11 23:58:58 +00:00
$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' ];
2018-06-01 17:22:58 +00:00
2018-11-30 19:12:56 +00:00
return $data ;
2018-06-01 17:22:58 +00:00
}
/**
2018-12-17 20:16:01 +00:00
* Update the collection params .
*
2019-02-06 22:59:34 +00:00
* Adds new options for 'orderby' , and new parameters 'cat_operator' , 'attr_operator' .
2018-06-01 17:22:58 +00:00
*
* @ return array
*/
public function get_collection_params () {
2019-02-19 16:09:16 +00:00
$params = parent :: get_collection_params ();
$params [ 'orderby' ][ 'enum' ] = array_merge ( $params [ 'orderby' ][ 'enum' ], array ( 'price' , 'popularity' , 'rating' , 'menu_order' ) );
$params [ 'cat_operator' ] = array (
2018-12-17 20:16:01 +00:00
'description' => __ ( 'Operator to compare product category terms.' , 'woo-gutenberg-products-block' ),
'type' => 'string' ,
'enum' => array ( 'IN' , 'NOT IN' , 'AND' ),
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-02-19 16:09:16 +00:00
$params [ 'attr_operator' ] = array (
2019-01-09 15:21:23 +00:00
'description' => __ ( 'Operator to compare product attribute terms.' , 'woo-gutenberg-products-block' ),
'type' => 'string' ,
2019-02-06 22:59:34 +00:00
'enum' => array ( 'IN' , 'NOT IN' , 'AND' ),
2019-01-30 21:08:21 +00:00
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-02-19 16:09:16 +00:00
$params [ 'catalog_visibility' ] = array (
'description' => __ ( 'Determines if hidden or visible catalog products are shown.' , 'woo-gutenberg-products-block' ),
'type' => 'string' ,
'enum' => array ( 'visible' , 'catalog' , 'search' , 'hidden' ),
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-01-09 15:21:23 +00:00
2018-06-01 17:22:58 +00:00
return $params ;
}
/**
* Get the Product ' s schema , conforming to JSON Schema .
*
* @ return array
*/
public function get_item_schema () {
2018-11-30 19:12:56 +00:00
$raw_schema = parent :: get_item_schema ();
$schema = array (
2018-06-01 17:22:58 +00:00
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
2018-11-30 19:12:56 +00:00
'title' => 'product_block_product' ,
2018-06-01 17:22:58 +00:00
'type' => 'object' ,
2018-11-30 19:12:56 +00:00
'properties' => array (),
);
2019-01-11 23:58:58 +00:00
$schema [ 'properties' ][ 'id' ] = $raw_schema [ 'properties' ][ 'id' ];
$schema [ 'properties' ][ 'name' ] = $raw_schema [ 'properties' ][ 'name' ];
2019-02-08 23:57:58 +00:00
$schema [ 'properties' ][ 'permalink' ] = $raw_schema [ 'properties' ][ 'permalink' ];
2019-01-11 23:58:58 +00:00
$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' ][ 'images' ] = array (
2018-11-30 19:12:56 +00:00
'description' => $raw_schema [ 'properties' ][ 'images' ][ 'description' ],
'type' => 'object' ,
'context' => array ( 'view' , 'edit' ),
'items' => array (
'type' => 'object' ,
'properties' => array (),
2018-06-01 17:22:58 +00:00
),
);
2018-11-30 19:12:56 +00:00
$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' ];
2018-06-01 17:22:58 +00:00
return $this -> add_additional_fields_schema ( $schema );
}
2018-11-19 16:33:17 +00:00
}