Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update : support filter & pagination in product REST API v2 #2496

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions includes/REST/ProductControllerV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function register_routes() {
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'args' => $this->get_product_collection_params(),
'permission_callback' => [ $this, 'get_product_permissions_check' ],
],
'schema' => [ $this, 'get_item_schema' ],
Expand Down Expand Up @@ -119,7 +118,7 @@ private function set_chosen_categories( $response ) {
*
* @return array Query parameters.
*/
public function get_product_collection_params() {
public function get_collection_params() {
$params = parent::get_collection_params();

$params['author'] = array(
Expand Down Expand Up @@ -177,6 +176,24 @@ public function get_product_collection_params() {
'validate_callback' => 'rest_validate_request_arg',
'required' => false,
);
$params['include'] = array(
'description' => __( 'Limit result set to specific ids.', 'dokan-lite' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['exclude'] = array(
'description' => __( 'Ensure result set excludes specific IDs.', 'dokan-lite' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);

return $params;
}
Expand Down Expand Up @@ -217,10 +234,10 @@ protected function prepare_objects_query( $request ) {
$args = array_merge(
$args,
array(
'posts_per_page' => 10,
'paged' => 1,
'posts_per_page' => isset( $request['per_page'] ) ? $request['per_page'] : 10,
'paged' => isset( $request['page'] ) ? $request['page'] : 1,
'author' => dokan_get_current_user_id(),
'orderby' => 'post_date',
'orderby' => isset( $request['orderby'] ) ? $request['orderby'] : 'date',
'post_type' => 'product',
'date_query' => [],
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
Expand Down
Loading