Controller.php
415 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports customers stats controller |
| 4 | * |
| 5 | * Handles requests to the /reports/customers/stats endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\Customers\Query; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\Customers\Controller as CustomersController; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | use Automattic\WooCommerce\Admin\API\Reports\TimeInterval; |
| 16 | |
| 17 | /** |
| 18 | * REST API Reports customers stats controller class. |
| 19 | * |
| 20 | * @internal |
| 21 | * @extends WC_REST_Reports_Controller |
| 22 | */ |
| 23 | class Controller extends \WC_REST_Reports_Controller { |
| 24 | /** |
| 25 | * Endpoint namespace. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $namespace = 'wc-analytics'; |
| 30 | |
| 31 | /** |
| 32 | * Route base. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $rest_base = 'reports/customers/stats'; |
| 37 | |
| 38 | /** |
| 39 | * Maps query arguments from the REST request. |
| 40 | * |
| 41 | * @param array $request Request array. |
| 42 | * @return array |
| 43 | */ |
| 44 | protected function prepare_reports_query( $request ) { |
| 45 | $args = array(); |
| 46 | $args['registered_before'] = $request['registered_before']; |
| 47 | $args['registered_after'] = $request['registered_after']; |
| 48 | $args['match'] = $request['match']; |
| 49 | $args['search'] = $request['search']; |
| 50 | $args['name_includes'] = $request['name_includes']; |
| 51 | $args['name_excludes'] = $request['name_excludes']; |
| 52 | $args['username_includes'] = $request['username_includes']; |
| 53 | $args['username_excludes'] = $request['username_excludes']; |
| 54 | $args['email_includes'] = $request['email_includes']; |
| 55 | $args['email_excludes'] = $request['email_excludes']; |
| 56 | $args['country_includes'] = $request['country_includes']; |
| 57 | $args['country_excludes'] = $request['country_excludes']; |
| 58 | $args['last_active_before'] = $request['last_active_before']; |
| 59 | $args['last_active_after'] = $request['last_active_after']; |
| 60 | $args['orders_count_min'] = $request['orders_count_min']; |
| 61 | $args['orders_count_max'] = $request['orders_count_max']; |
| 62 | $args['total_spend_min'] = $request['total_spend_min']; |
| 63 | $args['total_spend_max'] = $request['total_spend_max']; |
| 64 | $args['avg_order_value_min'] = $request['avg_order_value_min']; |
| 65 | $args['avg_order_value_max'] = $request['avg_order_value_max']; |
| 66 | $args['last_order_before'] = $request['last_order_before']; |
| 67 | $args['last_order_after'] = $request['last_order_after']; |
| 68 | $args['customers'] = $request['customers']; |
| 69 | $args['customers_exclude'] = $request['customers_exclude']; |
| 70 | $args['fields'] = $request['fields']; |
| 71 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 72 | |
| 73 | $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); |
| 74 | $normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false ); |
| 75 | $between_params_date = array( 'last_active', 'registered' ); |
| 76 | $normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true ); |
| 77 | $args = array_merge( $args, $normalized_params_numeric, $normalized_params_date ); |
| 78 | |
| 79 | $args = CustomersController::consolidate_customer_id_filters( $args ); |
| 80 | |
| 81 | return $args; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get all reports. |
| 86 | * |
| 87 | * @param WP_REST_Request $request Request data. |
| 88 | * @return array|WP_Error |
| 89 | */ |
| 90 | public function get_items( $request ) { |
| 91 | $query_args = $this->prepare_reports_query( $request ); |
| 92 | $customers_query = new Query( $query_args, 'customers-stats' ); |
| 93 | $report_data = $customers_query->get_data(); |
| 94 | $out_data = array( |
| 95 | 'totals' => $report_data, |
| 96 | ); |
| 97 | |
| 98 | return rest_ensure_response( $out_data ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Prepare a report data item for serialization. |
| 103 | * |
| 104 | * @param array $report Report data item as returned from Data Store. |
| 105 | * @param \WP_REST_Request $request Request object. |
| 106 | * @return \WP_REST_Response |
| 107 | */ |
| 108 | public function prepare_item_for_response( $report, $request ) { |
| 109 | $data = $report; |
| 110 | |
| 111 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 112 | $data = $this->add_additional_fields_to_object( $data, $request ); |
| 113 | $data = $this->filter_response_by_context( $data, $context ); |
| 114 | |
| 115 | // Wrap the data in a response object. |
| 116 | $response = rest_ensure_response( $data ); |
| 117 | |
| 118 | /** |
| 119 | * Filter a report returned from the API. |
| 120 | * |
| 121 | * Allows modification of the report data right before it is returned. |
| 122 | * |
| 123 | * @param WP_REST_Response $response The response object. |
| 124 | * @param object $report The original report object. |
| 125 | * @param WP_REST_Request $request Request used to generate the response. |
| 126 | */ |
| 127 | return apply_filters( 'woocommerce_rest_prepare_report_customers_stats', $response, $report, $request ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the Report's schema, conforming to JSON Schema. |
| 132 | * |
| 133 | * @return array |
| 134 | */ |
| 135 | public function get_item_schema() { |
| 136 | // @todo Should any of these be 'indicator's? |
| 137 | $totals = array( |
| 138 | 'customers_count' => array( |
| 139 | 'description' => __( 'Number of customers.', 'woocommerce' ), |
| 140 | 'type' => 'integer', |
| 141 | 'context' => array( 'view', 'edit' ), |
| 142 | 'readonly' => true, |
| 143 | ), |
| 144 | 'avg_orders_count' => array( |
| 145 | 'description' => __( 'Average number of orders.', 'woocommerce' ), |
| 146 | 'type' => 'integer', |
| 147 | 'context' => array( 'view', 'edit' ), |
| 148 | 'readonly' => true, |
| 149 | ), |
| 150 | 'avg_total_spend' => array( |
| 151 | 'description' => __( 'Average total spend per customer.', 'woocommerce' ), |
| 152 | 'type' => 'number', |
| 153 | 'context' => array( 'view', 'edit' ), |
| 154 | 'readonly' => true, |
| 155 | 'format' => 'currency', |
| 156 | ), |
| 157 | 'avg_avg_order_value' => array( |
| 158 | 'description' => __( 'Average AOV per customer.', 'woocommerce' ), |
| 159 | 'type' => 'number', |
| 160 | 'context' => array( 'view', 'edit' ), |
| 161 | 'readonly' => true, |
| 162 | 'format' => 'currency', |
| 163 | ), |
| 164 | ); |
| 165 | |
| 166 | $schema = array( |
| 167 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 168 | 'title' => 'report_customers_stats', |
| 169 | 'type' => 'object', |
| 170 | 'properties' => array( |
| 171 | 'totals' => array( |
| 172 | 'description' => __( 'Totals data.', 'woocommerce' ), |
| 173 | 'type' => 'object', |
| 174 | 'context' => array( 'view', 'edit' ), |
| 175 | 'readonly' => true, |
| 176 | 'properties' => $totals, |
| 177 | ), |
| 178 | ), |
| 179 | ); |
| 180 | |
| 181 | return $this->add_additional_fields_schema( $schema ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get the query params for collections. |
| 186 | * |
| 187 | * @return array |
| 188 | */ |
| 189 | public function get_collection_params() { |
| 190 | $params = array(); |
| 191 | $params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); |
| 192 | $params['registered_before'] = array( |
| 193 | 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 194 | 'type' => 'string', |
| 195 | 'format' => 'date-time', |
| 196 | 'validate_callback' => 'rest_validate_request_arg', |
| 197 | ); |
| 198 | $params['registered_after'] = array( |
| 199 | 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 200 | 'type' => 'string', |
| 201 | 'format' => 'date-time', |
| 202 | 'validate_callback' => 'rest_validate_request_arg', |
| 203 | ); |
| 204 | $params['match'] = array( |
| 205 | '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' ), |
| 206 | 'type' => 'string', |
| 207 | 'default' => 'all', |
| 208 | 'enum' => array( |
| 209 | 'all', |
| 210 | 'any', |
| 211 | ), |
| 212 | 'validate_callback' => 'rest_validate_request_arg', |
| 213 | ); |
| 214 | $params['search'] = array( |
| 215 | 'description' => __( 'Limit response to objects with a customer field containing the search term. Searches the field provided by `searchby`.', 'woocommerce' ), |
| 216 | 'type' => 'string', |
| 217 | 'validate_callback' => 'rest_validate_request_arg', |
| 218 | ); |
| 219 | $params['searchby'] = array( |
| 220 | 'description' => 'Limit results with `search` and `searchby` to specific fields containing the search term.', |
| 221 | 'type' => 'string', |
| 222 | 'default' => 'name', |
| 223 | 'enum' => array( |
| 224 | 'name', |
| 225 | 'username', |
| 226 | 'email', |
| 227 | 'all', |
| 228 | ), |
| 229 | ); |
| 230 | $params['name_includes'] = array( |
| 231 | 'description' => __( 'Limit response to objects with specific names.', 'woocommerce' ), |
| 232 | 'type' => 'string', |
| 233 | 'validate_callback' => 'rest_validate_request_arg', |
| 234 | ); |
| 235 | $params['name_excludes'] = array( |
| 236 | 'description' => __( 'Limit response to objects excluding specific names.', 'woocommerce' ), |
| 237 | 'type' => 'string', |
| 238 | 'validate_callback' => 'rest_validate_request_arg', |
| 239 | ); |
| 240 | $params['username_includes'] = array( |
| 241 | 'description' => __( 'Limit response to objects with specific usernames.', 'woocommerce' ), |
| 242 | 'type' => 'string', |
| 243 | 'validate_callback' => 'rest_validate_request_arg', |
| 244 | ); |
| 245 | $params['username_excludes'] = array( |
| 246 | 'description' => __( 'Limit response to objects excluding specific usernames.', 'woocommerce' ), |
| 247 | 'type' => 'string', |
| 248 | 'validate_callback' => 'rest_validate_request_arg', |
| 249 | ); |
| 250 | $params['email_includes'] = array( |
| 251 | 'description' => __( 'Limit response to objects including emails.', 'woocommerce' ), |
| 252 | 'type' => 'string', |
| 253 | 'validate_callback' => 'rest_validate_request_arg', |
| 254 | ); |
| 255 | $params['email_excludes'] = array( |
| 256 | 'description' => __( 'Limit response to objects excluding emails.', 'woocommerce' ), |
| 257 | 'type' => 'string', |
| 258 | 'validate_callback' => 'rest_validate_request_arg', |
| 259 | ); |
| 260 | $params['country_includes'] = array( |
| 261 | 'description' => __( 'Limit response to objects with specific countries.', 'woocommerce' ), |
| 262 | 'type' => 'string', |
| 263 | 'validate_callback' => 'rest_validate_request_arg', |
| 264 | ); |
| 265 | $params['country_excludes'] = array( |
| 266 | 'description' => __( 'Limit response to objects excluding specific countries.', 'woocommerce' ), |
| 267 | 'type' => 'string', |
| 268 | 'validate_callback' => 'rest_validate_request_arg', |
| 269 | ); |
| 270 | $params['last_active_before'] = array( |
| 271 | 'description' => __( 'Limit response to objects last active before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 272 | 'type' => 'string', |
| 273 | 'format' => 'date-time', |
| 274 | 'validate_callback' => 'rest_validate_request_arg', |
| 275 | ); |
| 276 | $params['last_active_after'] = array( |
| 277 | 'description' => __( 'Limit response to objects last active after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 278 | 'type' => 'string', |
| 279 | 'format' => 'date-time', |
| 280 | 'validate_callback' => 'rest_validate_request_arg', |
| 281 | ); |
| 282 | $params['last_active_between'] = array( |
| 283 | 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ), |
| 284 | 'type' => 'array', |
| 285 | 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ), |
| 286 | 'items' => array( |
| 287 | 'type' => 'string', |
| 288 | ), |
| 289 | ); |
| 290 | $params['registered_before'] = array( |
| 291 | 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 292 | 'type' => 'string', |
| 293 | 'format' => 'date-time', |
| 294 | 'validate_callback' => 'rest_validate_request_arg', |
| 295 | ); |
| 296 | $params['registered_after'] = array( |
| 297 | 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 298 | 'type' => 'string', |
| 299 | 'format' => 'date-time', |
| 300 | 'validate_callback' => 'rest_validate_request_arg', |
| 301 | ); |
| 302 | $params['registered_between'] = array( |
| 303 | 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ), |
| 304 | 'type' => 'array', |
| 305 | 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ), |
| 306 | 'items' => array( |
| 307 | 'type' => 'string', |
| 308 | ), |
| 309 | ); |
| 310 | $params['orders_count_min'] = array( |
| 311 | 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce' ), |
| 312 | 'type' => 'integer', |
| 313 | 'sanitize_callback' => 'absint', |
| 314 | 'validate_callback' => 'rest_validate_request_arg', |
| 315 | ); |
| 316 | $params['orders_count_max'] = array( |
| 317 | 'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'woocommerce' ), |
| 318 | 'type' => 'integer', |
| 319 | 'sanitize_callback' => 'absint', |
| 320 | 'validate_callback' => 'rest_validate_request_arg', |
| 321 | ); |
| 322 | $params['orders_count_between'] = array( |
| 323 | 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce' ), |
| 324 | 'type' => 'array', |
| 325 | 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ), |
| 326 | 'items' => array( |
| 327 | 'type' => 'integer', |
| 328 | ), |
| 329 | ); |
| 330 | $params['total_spend_min'] = array( |
| 331 | 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce' ), |
| 332 | 'type' => 'number', |
| 333 | 'validate_callback' => 'rest_validate_request_arg', |
| 334 | ); |
| 335 | $params['total_spend_max'] = array( |
| 336 | 'description' => __( 'Limit response to objects with a total order spend less than or equal to given number.', 'woocommerce' ), |
| 337 | 'type' => 'number', |
| 338 | 'validate_callback' => 'rest_validate_request_arg', |
| 339 | ); |
| 340 | $params['total_spend_between'] = array( |
| 341 | 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce' ), |
| 342 | 'type' => 'array', |
| 343 | 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ), |
| 344 | 'items' => array( |
| 345 | 'type' => 'integer', |
| 346 | ), |
| 347 | ); |
| 348 | $params['avg_order_value_min'] = array( |
| 349 | 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce' ), |
| 350 | 'type' => 'number', |
| 351 | 'validate_callback' => 'rest_validate_request_arg', |
| 352 | ); |
| 353 | $params['avg_order_value_max'] = array( |
| 354 | 'description' => __( 'Limit response to objects with an average order spend less than or equal to given number.', 'woocommerce' ), |
| 355 | 'type' => 'number', |
| 356 | 'validate_callback' => 'rest_validate_request_arg', |
| 357 | ); |
| 358 | $params['avg_order_value_between'] = array( |
| 359 | 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce' ), |
| 360 | 'type' => 'array', |
| 361 | 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ), |
| 362 | 'items' => array( |
| 363 | 'type' => 'integer', |
| 364 | ), |
| 365 | ); |
| 366 | $params['last_order_before'] = array( |
| 367 | 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 368 | 'type' => 'string', |
| 369 | 'format' => 'date-time', |
| 370 | 'validate_callback' => 'rest_validate_request_arg', |
| 371 | ); |
| 372 | $params['last_order_after'] = array( |
| 373 | 'description' => __( 'Limit response to objects with last order after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), |
| 374 | 'type' => 'string', |
| 375 | 'format' => 'date-time', |
| 376 | 'validate_callback' => 'rest_validate_request_arg', |
| 377 | ); |
| 378 | $params['customers'] = array( |
| 379 | 'description' => __( 'Limit result to items with specified customer ids.', 'woocommerce' ), |
| 380 | 'type' => 'array', |
| 381 | 'sanitize_callback' => 'wp_parse_id_list', |
| 382 | 'validate_callback' => 'rest_validate_request_arg', |
| 383 | 'items' => array( |
| 384 | 'type' => 'integer', |
| 385 | ), |
| 386 | ); |
| 387 | $params['customers_exclude'] = array( |
| 388 | 'description' => __( 'Limit result to exclude items with specified customer ids.', 'woocommerce' ), |
| 389 | 'type' => 'array', |
| 390 | 'sanitize_callback' => 'wp_parse_id_list', |
| 391 | 'validate_callback' => 'rest_validate_request_arg', |
| 392 | 'items' => array( |
| 393 | 'type' => 'integer', |
| 394 | ), |
| 395 | ); |
| 396 | $params['fields'] = array( |
| 397 | 'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ), |
| 398 | 'type' => 'array', |
| 399 | 'sanitize_callback' => 'wp_parse_slug_list', |
| 400 | 'validate_callback' => 'rest_validate_request_arg', |
| 401 | 'items' => array( |
| 402 | 'type' => 'string', |
| 403 | ), |
| 404 | ); |
| 405 | $params['force_cache_refresh'] = array( |
| 406 | 'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ), |
| 407 | 'type' => 'boolean', |
| 408 | 'sanitize_callback' => 'wp_validate_boolean', |
| 409 | 'validate_callback' => 'rest_validate_request_arg', |
| 410 | ); |
| 411 | |
| 412 | return $params; |
| 413 | } |
| 414 | } |
| 415 |