add REST routes for customers, handler for single customer (https://github.com/woocommerce/woocommerce-admin/pull/3174)
* add REST routes for customers, handler for single customer * use customer id vs user_id, add collection link
This commit is contained in:
parent
319cc65d22
commit
4987b7a1a7
|
@ -26,6 +26,45 @@ class Customers extends \Automattic\WooCommerce\Admin\API\Reports\Customers\Cont
|
||||||
*/
|
*/
|
||||||
protected $rest_base = 'customers';
|
protected $rest_base = 'customers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the routes for customers.
|
||||||
|
*/
|
||||||
|
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 ID for the resource.', 'woocommerce-admin' ),
|
||||||
|
'type' => 'integer',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'methods' => \WP_REST_Server::READABLE,
|
||||||
|
'callback' => array( $this, 'get_item' ),
|
||||||
|
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||||
|
'args' => $this->get_collection_params(),
|
||||||
|
),
|
||||||
|
'schema' => array( $this, 'get_public_item_schema' ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps query arguments from the REST request.
|
* Maps query arguments from the REST request.
|
||||||
*
|
*
|
||||||
|
|
|
@ -131,6 +131,33 @@ class Controller extends \WC_REST_Reports_Controller implements ExportableInterf
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get one report.
|
||||||
|
*
|
||||||
|
* @param WP_REST_Request $request Request data.
|
||||||
|
* @return array|WP_Error
|
||||||
|
*/
|
||||||
|
public function get_item( $request ) {
|
||||||
|
$query_args = $this->prepare_reports_query( $request );
|
||||||
|
$query_args['customers'] = array( $request->get_param( 'id' ) );
|
||||||
|
$customers_query = new Query( $query_args );
|
||||||
|
$report_data = $customers_query->get_data();
|
||||||
|
|
||||||
|
$data = array();
|
||||||
|
|
||||||
|
foreach ( $report_data->data as $customer_data ) {
|
||||||
|
$item = $this->prepare_item_for_response( $customer_data, $request );
|
||||||
|
$data[] = $this->prepare_response_for_collection( $item );
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = rest_ensure_response( $data );
|
||||||
|
$response->header( 'X-WP-Total', (int) $report_data->total );
|
||||||
|
$response->header( 'X-WP-TotalPages', (int) $report_data->pages );
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* Prepare a report object for serialization.
|
||||||
*
|
*
|
||||||
|
@ -174,8 +201,11 @@ class Controller extends \WC_REST_Reports_Controller implements ExportableInterf
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'customer' => array(
|
'customer' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object['user_id'] ) ),
|
'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object['id'] ) ),
|
||||||
|
),
|
||||||
|
'collection' => array(
|
||||||
|
'href' => rest_url( sprintf( '/%s/customers', $this->namespace ) ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue