2013-11-04 06:36:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce API Products Class
|
|
|
|
*
|
|
|
|
* Handles requests to the /products endpoint
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2013-11-09 21:20:23 +00:00
|
|
|
class WC_API_Products extends WC_API_Resource {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
/** @var string $base the route base */
|
|
|
|
protected $base = '/products';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the routes for this class
|
|
|
|
*
|
2013-11-11 00:29:36 +00:00
|
|
|
* GET /products
|
2013-11-04 06:36:31 +00:00
|
|
|
* GET /products/count
|
|
|
|
* GET|PUT|DELETE /products/<id>
|
|
|
|
* GET /products/<id>/reviews
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param array $routes
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function register_routes( $routes ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
# GET /products
|
2013-11-04 06:36:31 +00:00
|
|
|
$routes[ $this->base ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_products' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET /products/count
|
|
|
|
$routes[ $this->base . '/count'] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_products_count' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET|PUT|DELETE /products/<id>
|
|
|
|
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_product' ), WC_API_Server::READABLE ),
|
|
|
|
array( array( $this, 'edit_product' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
|
|
|
|
array( array( $this, 'delete_product' ), WC_API_Server::DELETABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET /products/<id>/reviews
|
|
|
|
$routes[ $this->base . '/(?P<id>\d+)/reviews' ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_product_reviews' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $routes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all products
|
|
|
|
*
|
|
|
|
* @since 2.1
|
2013-11-11 00:29:36 +00:00
|
|
|
* @param string $fields
|
2013-11-04 06:36:31 +00:00
|
|
|
* @param string $type
|
2013-11-11 00:29:36 +00:00
|
|
|
* @param array $filter
|
2013-11-04 06:36:31 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_products( $fields = null, $type = null, $filter = array() ) {
|
|
|
|
|
|
|
|
if ( ! empty( $type ) )
|
|
|
|
$filter['type'] = $type;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$query = $this->query_products( $filter );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
$products = array();
|
|
|
|
|
|
|
|
foreach( $query->posts as $product_id ) {
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( ! $this->is_readable( $product_id ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$products[] = $this->get_product( $product_id, $fields );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$this->server->query_navigation_headers( $query );
|
|
|
|
|
2013-11-04 06:36:31 +00:00
|
|
|
return array( 'products' => $products );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the product for the given ID
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param int $id the product ID
|
|
|
|
* @param string $fields
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_product( $id, $fields = null ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$id = $this->validate_request( $id, 'product', 'read' );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
$product = get_product( $id );
|
|
|
|
|
|
|
|
$product_data = array(
|
|
|
|
'id' => $product->id
|
|
|
|
);
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total number of orders
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param string $type
|
2013-11-11 00:29:36 +00:00
|
|
|
* @param array $filter
|
2013-11-04 06:36:31 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_products_count( $type = null, $filter = array() ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( ! empty( $type ) )
|
|
|
|
$filter['type'] = $type;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
// TODO: permissions?
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$query = $this->query_products( $filter );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
return array( 'count' => $query->found_posts );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit a product
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param int $id the product ID
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function edit_product( $id, $data ) {
|
|
|
|
|
|
|
|
$id = $this->validate_request( $id, 'product', 'edit' );
|
|
|
|
|
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
// TODO: implement
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
return $this->get_product( $id );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a product
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param int $id the product ID
|
|
|
|
* @param bool $force true to permanently delete order, false to move to trash
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function delete_product( $id, $force = false ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$id = $this->validate_request( $id, 'product', 'delete' );
|
|
|
|
|
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
|
|
|
|
|
|
|
return $this->delete( $id, 'product', ( 'true' === $force ) );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the reviews for a product
|
|
|
|
* @param $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_product_reviews( $id ) {
|
|
|
|
|
|
|
|
$id = $this->validate_request( $id, 'product', 'read' );
|
|
|
|
|
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
|
|
|
|
|
|
|
// TODO: implement
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to get product post objects
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param array $args request arguments for filtering query
|
2013-11-11 00:29:36 +00:00
|
|
|
* @return WP_Query
|
2013-11-04 06:36:31 +00:00
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
private function query_products( $args ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
// set base query arguments
|
|
|
|
$query_args = array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'post_type' => 'product',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'post_parent' => 0,
|
|
|
|
'meta_query' => array(),
|
|
|
|
);
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( ! empty( $args['type'] ) ) {
|
|
|
|
|
|
|
|
$query_args['tax_query'] = array(
|
|
|
|
array(
|
|
|
|
'taxonomy' => 'product_type',
|
|
|
|
'field' => 'slug',
|
|
|
|
'terms' => $args['type'],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
unset( $args['type'] );
|
|
|
|
}
|
|
|
|
|
2013-11-04 06:36:31 +00:00
|
|
|
// TODO: some param to show hidden products, but hide by default
|
|
|
|
$query_args['meta_query'][] = WC()->query->visibility_meta_query();
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$query_args = $this->merge_query_args( $query_args, $args );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
return new WP_Query( $query_args );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|