Added endpoint to get coupons
This commit is contained in:
parent
77c968e590
commit
0d88e3c380
|
@ -36,7 +36,7 @@ abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
|
|||
protected $public = false;
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a item.
|
||||
* 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
|
||||
|
@ -45,13 +45,25 @@ abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
|
|||
$post = get_post( (int) $request['id'] );
|
||||
|
||||
if ( $post ) {
|
||||
$post_type = get_post_type_object( $this->post_type );
|
||||
return 'revision' !== $post->post_type && current_user_can( $post_type->cap->read_private_posts, $post->ID );
|
||||
return $this->check_read_permission( $post );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we can read an item.
|
||||
*
|
||||
* Correctly handles posts with the inherit status.
|
||||
*
|
||||
* @param object $post Post object.
|
||||
* @return boolean Can we read it?
|
||||
*/
|
||||
public function check_read_permission( $post ) {
|
||||
$post_type = get_post_type_object( $this->post_type );
|
||||
return 'revision' !== $post->post_type && current_user_can( $post_type->cap->read_private_posts, $post->ID );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read items.
|
||||
*
|
||||
|
@ -75,7 +87,7 @@ abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
|
|||
$post = get_post( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
|
||||
return new WP_Error( sprintf( 'woocommerce_rest_api_invalid_%s_id', $this->post_type ), __( 'Invalid id.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'Invalid id.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$data = $this->prepare_item_for_response( $post, $request );
|
||||
|
@ -88,6 +100,304 @@ abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
|
|||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) {
|
||||
$args = array();
|
||||
$args['offset'] = $request['offset'];
|
||||
$args['order'] = $request['order'];
|
||||
$args['orderby'] = $request['orderby'];
|
||||
$args['paged'] = $request['page'];
|
||||
$args['post__in'] = $request['include'];
|
||||
$args['post__not_in'] = $request['exclude'];
|
||||
$args['posts_per_page'] = $request['per_page'];
|
||||
$args['name'] = $request['slug'];
|
||||
$args['post_parent__in'] = $request['parent'];
|
||||
$args['post_parent__not_in'] = $request['parent_exclude'];
|
||||
$args['s'] = $request['search'];
|
||||
|
||||
$args['date_query'] = array();
|
||||
// Set before into date query. Date query must be specified as an array of an array.
|
||||
if ( isset( $request['before'] ) ) {
|
||||
$args['date_query'][0]['before'] = $request['before'];
|
||||
}
|
||||
|
||||
// Set after into date query. Date query must be specified as an array of an array.
|
||||
if ( isset( $request['after'] ) ) {
|
||||
$args['date_query'][0]['after'] = $request['after'];
|
||||
}
|
||||
|
||||
if ( is_array( $request['filter'] ) ) {
|
||||
$args = array_merge( $args, $request['filter'] );
|
||||
unset( $args['filter'] );
|
||||
}
|
||||
|
||||
// Force the post_type argument, since it's not a user input variable.
|
||||
$args['post_type'] = $this->post_type;
|
||||
|
||||
/**
|
||||
* Filter the query arguments for a request.
|
||||
*
|
||||
* Enables adding extra arguments or setting defaults for a post
|
||||
* collection request.
|
||||
*
|
||||
* @param array $args Key value array of query var to query value.
|
||||
* @param WP_REST_Request $request The request used.
|
||||
*/
|
||||
$args = apply_filters( "woocommerce_rest_{$this->post_type}_query", $args, $request );
|
||||
$query_args = $this->prepare_items_query( $args, $request );
|
||||
|
||||
$posts_query = new WP_Query();
|
||||
$query_result = $posts_query->query( $query_args );
|
||||
|
||||
$posts = array();
|
||||
foreach ( $query_result as $post ) {
|
||||
if ( ! $this->check_read_permission( $post ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = $this->prepare_item_for_response( $post, $request );
|
||||
$posts[] = $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
$page = (int) $query_args['paged'];
|
||||
$total_posts = $posts_query->found_posts;
|
||||
|
||||
if ( $total_posts < 1 ) {
|
||||
// Out-of-bounds, run the query again without LIMIT for total count
|
||||
unset( $query_args['paged'] );
|
||||
$count_query = new WP_Query();
|
||||
$count_query->query( $query_args );
|
||||
$total_posts = $count_query->found_posts;
|
||||
}
|
||||
|
||||
$max_pages = ceil( $total_posts / (int) $query_args['posts_per_page'] );
|
||||
|
||||
$response = rest_ensure_response( $posts );
|
||||
$response->header( 'X-WP-Total', (int) $total_posts );
|
||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||
|
||||
$request_params = $request->get_query_params();
|
||||
if ( ! empty( $request_params['filter'] ) ) {
|
||||
// Normalize the pagination params.
|
||||
unset( $request_params['filter']['posts_per_page'] );
|
||||
unset( $request_params['filter']['paged'] );
|
||||
}
|
||||
$base = add_query_arg( $request_params, rest_url( sprintf( '/%s/%s', WC_API::REST_API_NAMESPACE, $this->rest_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the allowed query_vars for a get_items() response and
|
||||
* prepare for WP_Query.
|
||||
*
|
||||
* @param array $prepared_args
|
||||
* @param WP_REST_Request $request
|
||||
* @return array $query_args
|
||||
*/
|
||||
protected function prepare_items_query( $prepared_args = array(), $request = null ) {
|
||||
|
||||
$valid_vars = array_flip( $this->get_allowed_query_vars() );
|
||||
$query_args = array();
|
||||
foreach ( $valid_vars as $var => $index ) {
|
||||
if ( isset( $prepared_args[ $var ] ) ) {
|
||||
/**
|
||||
* Filter the query_vars used in `get_items` for the constructed query.
|
||||
*
|
||||
* The dynamic portion of the hook name, $var, refers to the query_var key.
|
||||
*
|
||||
* @param mixed $prepared_args[ $var ] The query_var value.
|
||||
*
|
||||
*/
|
||||
$query_args[ $var ] = apply_filters( "woocommerce_rest_query_var-{$var}", $prepared_args[ $var ] );
|
||||
}
|
||||
}
|
||||
|
||||
$query_args['ignore_sticky_posts'] = true;
|
||||
|
||||
if ( 'include' === $query_args['orderby'] ) {
|
||||
$query_args['orderby'] = 'post__in';
|
||||
}
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the WP Query vars that are allowed for the API request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_query_vars() {
|
||||
global $wp;
|
||||
|
||||
/**
|
||||
* Filter the publicly allowed query vars.
|
||||
*
|
||||
* Allows adjusting of the default query vars that are made public.
|
||||
*
|
||||
* @param array Array of allowed WP_Query query vars.
|
||||
*/
|
||||
$valid_vars = apply_filters( 'query_vars', $wp->public_query_vars );
|
||||
|
||||
$post_type_obj = get_post_type_object( $this->post_type );
|
||||
if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
|
||||
/**
|
||||
* Filter the allowed 'private' query vars for authorized users.
|
||||
*
|
||||
* If the user has the `edit_posts` capability, we also allow use of
|
||||
* private query parameters, which are only undesirable on the
|
||||
* frontend, but are safe for use in query strings.
|
||||
*
|
||||
* To disable anyway, use
|
||||
* `add_filter( 'woocommerce_rest_private_query_vars', '__return_empty_array' );`
|
||||
*
|
||||
* @param array $private_query_vars Array of allowed query vars for authorized users.
|
||||
* }
|
||||
*/
|
||||
$private = apply_filters( 'woocommerce_rest_private_query_vars', $wp->private_query_vars );
|
||||
$valid_vars = array_merge( $valid_vars, $private );
|
||||
}
|
||||
// Define our own in addition to WP's normal vars.
|
||||
$rest_valid = array(
|
||||
'date_query',
|
||||
'ignore_sticky_posts',
|
||||
'offset',
|
||||
'post__in',
|
||||
'post__not_in',
|
||||
'post_parent',
|
||||
'post_parent__in',
|
||||
'post_parent__not_in',
|
||||
'posts_per_page',
|
||||
);
|
||||
$valid_vars = array_merge( $valid_vars, $rest_valid );
|
||||
|
||||
/**
|
||||
* Filter allowed query vars for the REST API.
|
||||
*
|
||||
* This filter allows you to add or remove query vars from the final allowed
|
||||
* list for all requests, including unauthenticated ones. To alter the
|
||||
* vars for editors only.
|
||||
*
|
||||
* @param array {
|
||||
* Array of allowed WP_Query query vars.
|
||||
*
|
||||
* @param string $allowed_query_var The query var to allow.
|
||||
* }
|
||||
*/
|
||||
$valid_vars = apply_filters( 'woocommerce_rest_query_vars', $valid_vars );
|
||||
|
||||
return $valid_vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections of attachments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['context']['default'] = 'view';
|
||||
|
||||
$params['after'] = array(
|
||||
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'date-time',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['before'] = array(
|
||||
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'date-time',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['exclude'] = array(
|
||||
'description' => __( 'Ensure result set excludes specific ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['include'] = array(
|
||||
'description' => __( 'Limit result set to specific ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['offset'] = array(
|
||||
'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['order'] = array(
|
||||
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'desc',
|
||||
'enum' => array( 'asc', 'desc' ),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['orderby'] = array(
|
||||
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'date',
|
||||
'enum' => array(
|
||||
'date',
|
||||
'id',
|
||||
'include',
|
||||
'title',
|
||||
'slug',
|
||||
),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
$post_type_obj = get_post_type_object( $this->post_type );
|
||||
if ( $post_type_obj->hierarchical ) {
|
||||
$params['parent'] = array(
|
||||
'description' => _( 'Limit result set to those of particular parent ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
'default' => array(),
|
||||
);
|
||||
$params['parent_exclude'] = array(
|
||||
'description' => _( 'Limit result set to all items except those of a particular parent id.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
'default' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
$params['slug'] = array(
|
||||
'description' => __( 'Limit result set to posts with a specific slug.', 'woocommerce', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
$params['filter'] = array(
|
||||
'description' => __( 'Use WP Query arguments to modify the response; private query vars require appropriate authorization.', 'woocommerce' ),
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the post_date_gmt or modified_gmt and prepare any post or
|
||||
* modified date for single post output.
|
||||
|
|
|
@ -40,6 +40,15 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
* Register the routes for coupons.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( WC_API::REST_API_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(),
|
||||
),
|
||||
) );
|
||||
|
||||
register_rest_route( WC_API::REST_API_NAMESPACE, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
|
@ -109,6 +118,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
* @param WP_Post $post Post object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_api_prepare_' . $this->post_type, $response, $post, $request );
|
||||
return apply_filters( 'woocommerce_rest_prepare_' . $this->post_type, $response, $post, $request );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue