prepare_reports_query( $request ); $coupons_query = new WC_Admin_Reports_Coupons_Query( $query_args ); $report_data = $coupons_query->get_data(); $data = array(); foreach ( $report_data->data as $coupons_data ) { $item = $this->prepare_item_for_response( $coupons_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 ); $page = $report_data->page_no; $max_pages = $report_data->pages; $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->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; } /** * Prepare a report object for serialization. * * @param stdClass $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { $data = $report; $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( $report ) ); /** * Filter a report returned from the API. * * Allows modification of the report data right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $report The original report object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters( 'woocommerce_rest_prepare_report_coupons', $response, $report, $request ); } /** * Prepare links for the request. * * @param WC_Reports_Query $object Object data. * @return array */ protected function prepare_links( $object ) { $links = array( 'coupon' => array( 'href' => rest_url( sprintf( '/%s/coupons/%d', $this->namespace, $object['coupon_id'] ) ), ), ); return $links; } /** * Get the Report's schema, conforming to JSON Schema. * * @return array */ public function get_item_schema() { $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'report_coupons', 'type' => 'object', 'properties' => array( 'coupon_id' => array( 'description' => __( 'Coupon ID.', 'wc-admin' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'gross_discount' => array( 'description' => __( 'Gross discount.', 'wc-admin' ), 'type' => 'number', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'orders_count' => array( 'description' => __( 'Amount of orders.', 'wc-admin' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'extended_info' => array( 'code' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Coupon code.', 'wc-admin' ), ), 'date_created' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Coupon creation date.', 'wc-admin' ), ), 'date_created_gmt' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Coupon creation date in GMT.', 'wc-admin' ), ), 'date_expires' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Coupon expiration date.', 'wc-admin' ), ), 'date_expires_gmt' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Coupon expiration date in GMT.', 'wc-admin' ), ), 'discount_type' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'enum' => array_keys( wc_get_coupon_types() ), 'description' => __( 'Coupon discount type.', 'wc-admin' ), ), ), ), ); return $this->add_additional_fields_schema( $schema ); } /** * Get the query params for collections. * * @return array */ public function get_collection_params() { $params = array(); $params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); $params['page'] = array( 'description' => __( 'Current page of the collection.', 'wc-admin' ), 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', 'minimum' => 1, ); $params['per_page'] = array( 'description' => __( 'Maximum number of items to be returned in result set.', 'wc-admin' ), 'type' => 'integer', 'default' => 10, 'minimum' => 1, 'maximum' => 100, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ); $params['after'] = array( 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'wc-admin' ), '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.', 'wc-admin' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['order'] = array( 'description' => __( 'Order sort attribute ascending or descending.', 'wc-admin' ), 'type' => 'string', 'default' => 'desc', 'enum' => array( 'asc', 'desc' ), 'validate_callback' => 'rest_validate_request_arg', ); $params['orderby'] = array( 'description' => __( 'Sort collection by object attribute.', 'wc-admin' ), 'type' => 'string', 'default' => 'coupon_id', 'enum' => array( 'coupon_id', 'gross_discount', 'orders_count', ), 'validate_callback' => 'rest_validate_request_arg', ); $params['code'] = array( 'description' => __( 'Limit result set to items assigned one or more code.', 'wc-admin' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_slug_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'string', ), ); $params['extended_info'] = array( 'description' => __( 'Add additional piece of info about each coupon to the report.', 'wc-admin' ), 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'wc_string_to_bool', 'validate_callback' => 'rest_validate_request_arg', ); return $params; } }