Controller.php
389 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports orders stats controller |
| 4 | * |
| 5 | * Handles requests to the /reports/orders/stats endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait; |
| 14 | use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Query; |
| 15 | |
| 16 | /** |
| 17 | * REST API Reports orders stats controller class. |
| 18 | * |
| 19 | * @internal |
| 20 | * @extends \Automattic\WooCommerce\Admin\API\Reports\GenericStatsController |
| 21 | */ |
| 22 | class Controller extends GenericStatsController { |
| 23 | |
| 24 | use OrderAwareControllerTrait; |
| 25 | |
| 26 | /** |
| 27 | * Route base. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $rest_base = 'reports/orders/stats'; |
| 32 | |
| 33 | /** |
| 34 | * Get data from Orders\Stats\Query. |
| 35 | * |
| 36 | * @override GenericController::get_datastore_data() |
| 37 | * |
| 38 | * @param array $query_args Query arguments. |
| 39 | * @return mixed Results from the data store. |
| 40 | */ |
| 41 | protected function get_datastore_data( $query_args = array() ) { |
| 42 | $query = new Query( $query_args ); |
| 43 | return $query->get_data(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Maps query arguments from the REST request. |
| 48 | * |
| 49 | * @param array $request Request array. |
| 50 | * @return array |
| 51 | */ |
| 52 | protected function prepare_reports_query( $request ) { |
| 53 | $args = array(); |
| 54 | $args['before'] = $request['before']; |
| 55 | $args['after'] = $request['after']; |
| 56 | $args['interval'] = $request['interval']; |
| 57 | $args['page'] = $request['page']; |
| 58 | $args['per_page'] = $request['per_page']; |
| 59 | $args['orderby'] = $request['orderby']; |
| 60 | $args['order'] = $request['order']; |
| 61 | $args['fields'] = $request['fields']; |
| 62 | $args['match'] = $request['match']; |
| 63 | $args['status_is'] = (array) $request['status_is']; |
| 64 | $args['status_is_not'] = (array) $request['status_is_not']; |
| 65 | $args['product_includes'] = (array) $request['product_includes']; |
| 66 | $args['product_excludes'] = (array) $request['product_excludes']; |
| 67 | $args['variation_includes'] = (array) $request['variation_includes']; |
| 68 | $args['variation_excludes'] = (array) $request['variation_excludes']; |
| 69 | $args['coupon_includes'] = (array) $request['coupon_includes']; |
| 70 | $args['coupon_excludes'] = (array) $request['coupon_excludes']; |
| 71 | $args['tax_rate_includes'] = (array) $request['tax_rate_includes']; |
| 72 | $args['tax_rate_excludes'] = (array) $request['tax_rate_excludes']; |
| 73 | $args['customer_type'] = $request['customer_type']; |
| 74 | $args['refunds'] = $request['refunds']; |
| 75 | $args['attribute_is'] = (array) $request['attribute_is']; |
| 76 | $args['attribute_is_not'] = (array) $request['attribute_is_not']; |
| 77 | $args['category_includes'] = (array) $request['categories']; |
| 78 | $args['segmentby'] = $request['segmentby']; |
| 79 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 80 | |
| 81 | // For backwards compatibility, `customer` is aliased to `customer_type`. |
| 82 | if ( empty( $request['customer_type'] ) && ! empty( $request['customer'] ) ) { |
| 83 | $args['customer_type'] = $request['customer']; |
| 84 | } |
| 85 | |
| 86 | return $args; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Prepare a report data item for serialization. |
| 91 | * |
| 92 | * @param Array $report Report data item as returned from Data Store. |
| 93 | * @param WP_REST_Request $request Request object. |
| 94 | * @return WP_REST_Response |
| 95 | */ |
| 96 | public function prepare_item_for_response( $report, $request ) { |
| 97 | // Wrap the data in a response object. |
| 98 | $response = parent::prepare_item_for_response( $report, $request ); |
| 99 | |
| 100 | /** |
| 101 | * Filter a report returned from the API. |
| 102 | * |
| 103 | * Allows modification of the report data right before it is returned. |
| 104 | * |
| 105 | * @param WP_REST_Response $response The response object. |
| 106 | * @param object $report The original report object. |
| 107 | * @param WP_REST_Request $request Request used to generate the response. |
| 108 | */ |
| 109 | return apply_filters( 'woocommerce_rest_prepare_report_orders_stats', $response, $report, $request ); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Get the Report's item properties schema. |
| 115 | * Will be used by `get_item_schema` as `totals` and `subtotals`. |
| 116 | * |
| 117 | * @return array |
| 118 | */ |
| 119 | protected function get_item_properties_schema() { |
| 120 | return array( |
| 121 | 'net_revenue' => array( |
| 122 | 'description' => __( 'Net sales.', 'woocommerce' ), |
| 123 | 'type' => 'number', |
| 124 | 'context' => array( 'view', 'edit' ), |
| 125 | 'readonly' => true, |
| 126 | 'format' => 'currency', |
| 127 | ), |
| 128 | 'orders_count' => array( |
| 129 | 'title' => __( 'Orders', 'woocommerce' ), |
| 130 | 'description' => __( 'Number of orders', 'woocommerce' ), |
| 131 | 'type' => 'integer', |
| 132 | 'context' => array( 'view', 'edit' ), |
| 133 | 'readonly' => true, |
| 134 | 'indicator' => true, |
| 135 | ), |
| 136 | 'avg_order_value' => array( |
| 137 | 'description' => __( 'Average order value.', 'woocommerce' ), |
| 138 | 'type' => 'number', |
| 139 | 'context' => array( 'view', 'edit' ), |
| 140 | 'readonly' => true, |
| 141 | 'indicator' => true, |
| 142 | 'format' => 'currency', |
| 143 | ), |
| 144 | 'avg_items_per_order' => array( |
| 145 | 'description' => __( 'Average items per order', 'woocommerce' ), |
| 146 | 'type' => 'number', |
| 147 | 'context' => array( 'view', 'edit' ), |
| 148 | 'readonly' => true, |
| 149 | ), |
| 150 | 'num_items_sold' => array( |
| 151 | 'description' => __( 'Number of items sold', 'woocommerce' ), |
| 152 | 'type' => 'integer', |
| 153 | 'context' => array( 'view', 'edit' ), |
| 154 | 'readonly' => true, |
| 155 | ), |
| 156 | 'coupons' => array( |
| 157 | 'description' => __( 'Amount discounted by coupons.', 'woocommerce' ), |
| 158 | 'type' => 'number', |
| 159 | 'context' => array( 'view', 'edit' ), |
| 160 | 'readonly' => true, |
| 161 | ), |
| 162 | 'coupons_count' => array( |
| 163 | 'description' => __( 'Unique coupons count.', 'woocommerce' ), |
| 164 | 'type' => 'number', |
| 165 | 'context' => array( 'view', 'edit' ), |
| 166 | 'readonly' => true, |
| 167 | ), |
| 168 | 'total_customers' => array( |
| 169 | 'description' => __( 'Total distinct customers.', 'woocommerce' ), |
| 170 | 'type' => 'integer', |
| 171 | 'context' => array( 'view', 'edit' ), |
| 172 | 'readonly' => true, |
| 173 | ), |
| 174 | 'products' => array( |
| 175 | 'description' => __( 'Number of distinct products sold.', 'woocommerce' ), |
| 176 | 'type' => 'number', |
| 177 | 'context' => array( 'view', 'edit' ), |
| 178 | 'readonly' => true, |
| 179 | ), |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get the Report's schema, conforming to JSON Schema. |
| 185 | * |
| 186 | * @return array |
| 187 | */ |
| 188 | public function get_item_schema() { |
| 189 | $schema = parent::get_item_schema(); |
| 190 | $schema['title'] = 'report_orders_stats'; |
| 191 | |
| 192 | // Products is not shown in intervals. |
| 193 | unset( $schema['properties']['intervals']['items']['properties']['subtotals']['properties']['products'] ); |
| 194 | |
| 195 | return $this->add_additional_fields_schema( $schema ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get the query params for collections. |
| 200 | * |
| 201 | * @return array |
| 202 | */ |
| 203 | public function get_collection_params() { |
| 204 | $params = parent::get_collection_params(); |
| 205 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 206 | array( |
| 207 | 'date', |
| 208 | 'net_revenue', |
| 209 | 'orders_count', |
| 210 | 'avg_order_value', |
| 211 | ) |
| 212 | ); |
| 213 | $params['match'] = array( |
| 214 | 'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ), |
| 215 | 'type' => 'string', |
| 216 | 'default' => 'all', |
| 217 | 'enum' => array( |
| 218 | 'all', |
| 219 | 'any', |
| 220 | ), |
| 221 | 'validate_callback' => 'rest_validate_request_arg', |
| 222 | ); |
| 223 | $params['status_is'] = array( |
| 224 | 'description' => __( 'Limit result set to items that have the specified order status.', 'woocommerce' ), |
| 225 | 'type' => 'array', |
| 226 | 'sanitize_callback' => 'wp_parse_slug_list', |
| 227 | 'validate_callback' => 'rest_validate_request_arg', |
| 228 | 'default' => null, |
| 229 | 'items' => array( |
| 230 | 'enum' => self::get_order_statuses(), |
| 231 | 'type' => 'string', |
| 232 | ), |
| 233 | ); |
| 234 | $params['status_is_not'] = array( |
| 235 | 'description' => __( 'Limit result set to items that don\'t have the specified order status.', 'woocommerce' ), |
| 236 | 'type' => 'array', |
| 237 | 'sanitize_callback' => 'wp_parse_slug_list', |
| 238 | 'validate_callback' => 'rest_validate_request_arg', |
| 239 | 'items' => array( |
| 240 | 'enum' => self::get_order_statuses(), |
| 241 | 'type' => 'string', |
| 242 | ), |
| 243 | ); |
| 244 | $params['product_includes'] = array( |
| 245 | 'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ), |
| 246 | 'type' => 'array', |
| 247 | 'items' => array( |
| 248 | 'type' => 'integer', |
| 249 | ), |
| 250 | 'default' => array(), |
| 251 | 'sanitize_callback' => 'wp_parse_id_list', |
| 252 | |
| 253 | ); |
| 254 | $params['product_excludes'] = array( |
| 255 | 'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ), |
| 256 | 'type' => 'array', |
| 257 | 'items' => array( |
| 258 | 'type' => 'integer', |
| 259 | ), |
| 260 | 'default' => array(), |
| 261 | 'sanitize_callback' => 'wp_parse_id_list', |
| 262 | ); |
| 263 | // Split assignments for PHPCS complaining on aligned. |
| 264 | $params['variation_includes'] = array( |
| 265 | 'description' => __( 'Limit result set to items that have the specified variation(s) assigned.', 'woocommerce' ), |
| 266 | 'type' => 'array', |
| 267 | 'items' => array( |
| 268 | 'type' => 'integer', |
| 269 | ), |
| 270 | 'default' => array(), |
| 271 | 'sanitize_callback' => 'wp_parse_id_list', |
| 272 | 'validate_callback' => 'rest_validate_request_arg', |
| 273 | ); |
| 274 | $params['variation_excludes'] = array( |
| 275 | 'description' => __( 'Limit result set to items that don\'t have the specified variation(s) assigned.', 'woocommerce' ), |
| 276 | 'type' => 'array', |
| 277 | 'items' => array( |
| 278 | 'type' => 'integer', |
| 279 | ), |
| 280 | 'default' => array(), |
| 281 | 'validate_callback' => 'rest_validate_request_arg', |
| 282 | 'sanitize_callback' => 'wp_parse_id_list', |
| 283 | ); |
| 284 | $params['coupon_includes'] = array( |
| 285 | 'description' => __( 'Limit result set to items that have the specified coupon(s) assigned.', 'woocommerce' ), |
| 286 | 'type' => 'array', |
| 287 | 'items' => array( |
| 288 | 'type' => 'integer', |
| 289 | ), |
| 290 | 'default' => array(), |
| 291 | 'sanitize_callback' => 'wp_parse_id_list', |
| 292 | ); |
| 293 | $params['coupon_excludes'] = array( |
| 294 | 'description' => __( 'Limit result set to items that don\'t have the specified coupon(s) assigned.', 'woocommerce' ), |
| 295 | 'type' => 'array', |
| 296 | 'items' => array( |
| 297 | 'type' => 'integer', |
| 298 | ), |
| 299 | 'default' => array(), |
| 300 | 'sanitize_callback' => 'wp_parse_id_list', |
| 301 | ); |
| 302 | $params['tax_rate_includes'] = array( |
| 303 | 'description' => __( 'Limit result set to items that have the specified tax rate(s) assigned.', 'woocommerce' ), |
| 304 | 'type' => 'array', |
| 305 | 'items' => array( |
| 306 | 'type' => 'integer', |
| 307 | ), |
| 308 | 'default' => array(), |
| 309 | 'sanitize_callback' => 'wp_parse_id_list', |
| 310 | 'validate_callback' => 'rest_validate_request_arg', |
| 311 | ); |
| 312 | $params['tax_rate_excludes'] = array( |
| 313 | 'description' => __( 'Limit result set to items that don\'t have the specified tax rate(s) assigned.', 'woocommerce' ), |
| 314 | 'type' => 'array', |
| 315 | 'items' => array( |
| 316 | 'type' => 'integer', |
| 317 | ), |
| 318 | 'default' => array(), |
| 319 | 'validate_callback' => 'rest_validate_request_arg', |
| 320 | 'sanitize_callback' => 'wp_parse_id_list', |
| 321 | ); |
| 322 | $params['customer'] = array( |
| 323 | 'description' => __( 'Alias for customer_type (deprecated).', 'woocommerce' ), |
| 324 | 'type' => 'string', |
| 325 | 'enum' => array( |
| 326 | 'new', |
| 327 | 'returning', |
| 328 | ), |
| 329 | 'validate_callback' => 'rest_validate_request_arg', |
| 330 | ); |
| 331 | $params['customer_type'] = array( |
| 332 | 'description' => __( 'Limit result set to orders that have the specified customer_type', 'woocommerce' ), |
| 333 | 'type' => 'string', |
| 334 | 'enum' => array( |
| 335 | 'new', |
| 336 | 'returning', |
| 337 | ), |
| 338 | 'validate_callback' => 'rest_validate_request_arg', |
| 339 | ); |
| 340 | $params['refunds'] = array( |
| 341 | 'description' => __( 'Limit result set to specific types of refunds.', 'woocommerce' ), |
| 342 | 'type' => 'string', |
| 343 | 'default' => '', |
| 344 | 'enum' => array( |
| 345 | '', |
| 346 | 'all', |
| 347 | 'partial', |
| 348 | 'full', |
| 349 | 'none', |
| 350 | ), |
| 351 | 'validate_callback' => 'rest_validate_request_arg', |
| 352 | ); |
| 353 | $params['attribute_is'] = array( |
| 354 | 'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce' ), |
| 355 | 'type' => 'array', |
| 356 | 'items' => array( |
| 357 | 'type' => 'array', |
| 358 | ), |
| 359 | 'default' => array(), |
| 360 | 'validate_callback' => 'rest_validate_request_arg', |
| 361 | ); |
| 362 | $params['attribute_is_not'] = array( |
| 363 | 'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce' ), |
| 364 | 'type' => 'array', |
| 365 | 'items' => array( |
| 366 | 'type' => 'array', |
| 367 | ), |
| 368 | 'default' => array(), |
| 369 | 'validate_callback' => 'rest_validate_request_arg', |
| 370 | ); |
| 371 | $params['segmentby'] = array( |
| 372 | 'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ), |
| 373 | 'type' => 'string', |
| 374 | 'enum' => array( |
| 375 | 'product', |
| 376 | 'category', |
| 377 | 'variation', |
| 378 | 'coupon', |
| 379 | 'customer_type', // new vs returning. |
| 380 | ), |
| 381 | 'validate_callback' => 'rest_validate_request_arg', |
| 382 | ); |
| 383 | unset( $params['intervals'] ); |
| 384 | unset( $params['fields'] ); |
| 385 | |
| 386 | return $params; |
| 387 | } |
| 388 | } |
| 389 |