| @@ 106-138 (lines=33) @@ | ||
| 103 | * @param WP_REST_Request $request Request object. |
|
| 104 | * @return WP_REST_Response $response Response data. |
|
| 105 | */ |
|
| 106 | public function prepare_item_for_response( $top_seller, $request ) { |
|
| 107 | $data = array( |
|
| 108 | 'name' => $top_seller->name, |
|
| 109 | 'product_id' => $top_seller->product_id, |
|
| 110 | 'quantity' => $top_seller->quantity, |
|
| 111 | ); |
|
| 112 | ||
| 113 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 114 | $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 115 | $data = $this->filter_response_by_context( $data, $context ); |
|
| 116 | ||
| 117 | // Wrap the data in a response object. |
|
| 118 | $response = rest_ensure_response( $data ); |
|
| 119 | $response->add_links( array( |
|
| 120 | 'about' => array( |
|
| 121 | 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 122 | ), |
|
| 123 | 'product' => array( |
|
| 124 | 'href' => rest_url( sprintf( '/%s/products/%s', $this->namespace, $top_seller->product_id ) ), |
|
| 125 | ), |
|
| 126 | ) ); |
|
| 127 | ||
| 128 | /** |
|
| 129 | * Filter a report top sellers returned from the API. |
|
| 130 | * |
|
| 131 | * Allows modification of the report top sellers data right before it is returned. |
|
| 132 | * |
|
| 133 | * @param WP_REST_Response $response The response object. |
|
| 134 | * @param stdClass $top_seller The original report object. |
|
| 135 | * @param WP_REST_Request $request Request used to generate the response. |
|
| 136 | */ |
|
| 137 | return apply_filters( 'woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request ); |
|
| 138 | } |
|
| 139 | ||
| 140 | /** |
|
| 141 | * Get the Report's schema, conforming to JSON Schema. |
|
| @@ 102-133 (lines=32) @@ | ||
| 99 | * @param WP_REST_Request $request Request object. |
|
| 100 | * @return WP_REST_Response $response Response data. |
|
| 101 | */ |
|
| 102 | public function prepare_item_for_response( $report, $request ) { |
|
| 103 | $data = array( |
|
| 104 | 'slug' => $report->slug, |
|
| 105 | 'description' => $report->description, |
|
| 106 | ); |
|
| 107 | ||
| 108 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 109 | $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 110 | $data = $this->filter_response_by_context( $data, $context ); |
|
| 111 | ||
| 112 | // Wrap the data in a response object. |
|
| 113 | $response = rest_ensure_response( $data ); |
|
| 114 | $response->add_links( array( |
|
| 115 | 'self' => array( |
|
| 116 | 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ), |
|
| 117 | ), |
|
| 118 | 'collection' => array( |
|
| 119 | 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 120 | ), |
|
| 121 | ) ); |
|
| 122 | ||
| 123 | /** |
|
| 124 | * Filter a report returned from the API. |
|
| 125 | * |
|
| 126 | * Allows modification of the report data right before it is returned. |
|
| 127 | * |
|
| 128 | * @param WP_REST_Response $response The response object. |
|
| 129 | * @param object $report The original report object. |
|
| 130 | * @param WP_REST_Request $request Request used to generate the response. |
|
| 131 | */ |
|
| 132 | return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request ); |
|
| 133 | } |
|
| 134 | ||
| 135 | /** |
|
| 136 | * Get the Report's schema, conforming to JSON Schema. |
|