Controller.php
219 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports taxes stats controller |
| 4 | * |
| 5 | * Handles requests to the /reports/taxes/stats endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\GenericQuery; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController; |
| 14 | use WP_REST_Request; |
| 15 | use WP_REST_Response; |
| 16 | |
| 17 | /** |
| 18 | * REST API Reports taxes stats controller class. |
| 19 | * |
| 20 | * @internal |
| 21 | * @extends GenericStatsController |
| 22 | */ |
| 23 | class Controller extends GenericStatsController { |
| 24 | |
| 25 | /** |
| 26 | * Route base. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $rest_base = 'reports/taxes/stats'; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | */ |
| 35 | public function __construct() { |
| 36 | add_filter( 'woocommerce_analytics_taxes_stats_select_query', array( $this, 'set_default_report_data' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Set the default results to 0 if API returns an empty array |
| 41 | * |
| 42 | * @internal |
| 43 | * @param Mixed $results Report data. |
| 44 | * @return object |
| 45 | */ |
| 46 | public function set_default_report_data( $results ) { |
| 47 | if ( empty( $results ) ) { |
| 48 | $results = new \stdClass(); |
| 49 | $results->total = 0; |
| 50 | $results->totals = new \stdClass(); |
| 51 | $results->totals->tax_codes = 0; |
| 52 | $results->totals->total_tax = 0; |
| 53 | $results->totals->order_tax = 0; |
| 54 | $results->totals->shipping_tax = 0; |
| 55 | $results->totals->orders = 0; |
| 56 | $results->intervals = array(); |
| 57 | $results->pages = 1; |
| 58 | $results->page_no = 1; |
| 59 | } |
| 60 | return $results; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Maps query arguments from the REST request. |
| 65 | * |
| 66 | * @param array $request Request array. |
| 67 | * @return array |
| 68 | */ |
| 69 | protected function prepare_reports_query( $request ) { |
| 70 | $args = array(); |
| 71 | $args['before'] = $request['before']; |
| 72 | $args['after'] = $request['after']; |
| 73 | $args['interval'] = $request['interval']; |
| 74 | $args['page'] = $request['page']; |
| 75 | $args['per_page'] = $request['per_page']; |
| 76 | $args['orderby'] = $request['orderby']; |
| 77 | $args['order'] = $request['order']; |
| 78 | $args['taxes'] = (array) $request['taxes']; |
| 79 | $args['segmentby'] = $request['segmentby']; |
| 80 | $args['fields'] = $request['fields']; |
| 81 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 82 | |
| 83 | return $args; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get data from `'taxes-stats'` GenericQuery. |
| 88 | * |
| 89 | * @override GenericController::get_datastore_data() |
| 90 | * |
| 91 | * @param array $query_args Query arguments. |
| 92 | * @return mixed Results from the data store. |
| 93 | */ |
| 94 | protected function get_datastore_data( $query_args = array() ) { |
| 95 | $query = new GenericQuery( $query_args, 'taxes-stats' ); |
| 96 | return $query->get_data(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Prepare a report data item for serialization. |
| 101 | * |
| 102 | * @param mixed $report Report data item as returned from Data Store. |
| 103 | * @param WP_REST_Request $request Request object. |
| 104 | * @return WP_REST_Response |
| 105 | */ |
| 106 | public function prepare_item_for_response( $report, $request ) { |
| 107 | $response = parent::prepare_item_for_response( $report, $request ); |
| 108 | |
| 109 | // Map to `object` for backwards compatibility. |
| 110 | $report = (object) $report; |
| 111 | /** |
| 112 | * Filter a report returned from the API. |
| 113 | * |
| 114 | * Allows modification of the report data right before it is returned. |
| 115 | * |
| 116 | * @param WP_REST_Response $response The response object. |
| 117 | * @param object $report The original report object. |
| 118 | * @param WP_REST_Request $request Request used to generate the response. |
| 119 | */ |
| 120 | return apply_filters( 'woocommerce_rest_prepare_report_taxes_stats', $response, $report, $request ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the Report's item properties schema. |
| 125 | * Will be used by `get_item_schema` as `totals` and `subtotals`. |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | protected function get_item_properties_schema() { |
| 130 | return array( |
| 131 | 'total_tax' => array( |
| 132 | 'description' => __( 'Total tax.', 'woocommerce' ), |
| 133 | 'type' => 'number', |
| 134 | 'context' => array( 'view', 'edit' ), |
| 135 | 'readonly' => true, |
| 136 | 'indicator' => true, |
| 137 | 'format' => 'currency', |
| 138 | ), |
| 139 | 'order_tax' => array( |
| 140 | 'description' => __( 'Order tax.', 'woocommerce' ), |
| 141 | 'type' => 'number', |
| 142 | 'context' => array( 'view', 'edit' ), |
| 143 | 'readonly' => true, |
| 144 | 'indicator' => true, |
| 145 | 'format' => 'currency', |
| 146 | ), |
| 147 | 'shipping_tax' => array( |
| 148 | 'description' => __( 'Shipping tax.', 'woocommerce' ), |
| 149 | 'type' => 'number', |
| 150 | 'context' => array( 'view', 'edit' ), |
| 151 | 'readonly' => true, |
| 152 | 'indicator' => true, |
| 153 | 'format' => 'currency', |
| 154 | ), |
| 155 | 'orders_count' => array( |
| 156 | 'description' => __( 'Number of orders.', 'woocommerce' ), |
| 157 | 'type' => 'integer', |
| 158 | 'context' => array( 'view', 'edit' ), |
| 159 | 'readonly' => true, |
| 160 | ), |
| 161 | 'tax_codes' => array( |
| 162 | 'description' => __( 'Amount of tax codes.', 'woocommerce' ), |
| 163 | 'type' => 'integer', |
| 164 | 'context' => array( 'view', 'edit' ), |
| 165 | 'readonly' => true, |
| 166 | ), |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the Report's schema, conforming to JSON Schema. |
| 172 | * |
| 173 | * @return array |
| 174 | */ |
| 175 | public function get_item_schema() { |
| 176 | $schema = parent::get_item_schema(); |
| 177 | $schema['title'] = 'report_taxes_stats'; |
| 178 | |
| 179 | return $this->add_additional_fields_schema( $schema ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the query params for collections. |
| 184 | * |
| 185 | * @return array |
| 186 | */ |
| 187 | public function get_collection_params() { |
| 188 | $params = parent::get_collection_params(); |
| 189 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 190 | array( |
| 191 | 'date', |
| 192 | 'items_sold', |
| 193 | 'total_sales', |
| 194 | 'orders_count', |
| 195 | 'products_count', |
| 196 | ) |
| 197 | ); |
| 198 | $params['taxes'] = array( |
| 199 | 'description' => __( 'Limit result set to all items that have the specified term assigned in the taxes taxonomy.', 'woocommerce' ), |
| 200 | 'type' => 'array', |
| 201 | 'sanitize_callback' => 'wp_parse_id_list', |
| 202 | 'validate_callback' => 'rest_validate_request_arg', |
| 203 | 'items' => array( |
| 204 | 'type' => 'integer', |
| 205 | ), |
| 206 | ); |
| 207 | $params['segmentby'] = array( |
| 208 | 'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ), |
| 209 | 'type' => 'string', |
| 210 | 'enum' => array( |
| 211 | 'tax_rate_id', |
| 212 | ), |
| 213 | 'validate_callback' => 'rest_validate_request_arg', |
| 214 | ); |
| 215 | |
| 216 | return $params; |
| 217 | } |
| 218 | } |
| 219 |