Controller.php
291 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports downloads stats controller |
| 4 | * |
| 5 | * Handles requests to the /reports/downloads/stats endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\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 downloads 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/downloads/stats'; |
| 31 | |
| 32 | /** |
| 33 | * Maps query arguments from the REST request. |
| 34 | * |
| 35 | * @param array $request Request array. |
| 36 | * @return array |
| 37 | */ |
| 38 | protected function prepare_reports_query( $request ) { |
| 39 | $args = array(); |
| 40 | $args['before'] = $request['before']; |
| 41 | $args['after'] = $request['after']; |
| 42 | $args['interval'] = $request['interval']; |
| 43 | $args['page'] = $request['page']; |
| 44 | $args['per_page'] = $request['per_page']; |
| 45 | $args['orderby'] = $request['orderby']; |
| 46 | $args['order'] = $request['order']; |
| 47 | $args['match'] = $request['match']; |
| 48 | $args['product_includes'] = (array) $request['product_includes']; |
| 49 | $args['product_excludes'] = (array) $request['product_excludes']; |
| 50 | $args['customer_includes'] = (array) $request['customer_includes']; |
| 51 | $args['customer_excludes'] = (array) $request['customer_excludes']; |
| 52 | $args['order_includes'] = (array) $request['order_includes']; |
| 53 | $args['order_excludes'] = (array) $request['order_excludes']; |
| 54 | $args['ip_address_includes'] = (array) $request['ip_address_includes']; |
| 55 | $args['ip_address_excludes'] = (array) $request['ip_address_excludes']; |
| 56 | $args['fields'] = $request['fields']; |
| 57 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 58 | |
| 59 | return $args; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get data from `'downloads-stats'` GenericQuery. |
| 64 | * |
| 65 | * @override GenericController::get_datastore_data() |
| 66 | * |
| 67 | * @param array $query_args Query arguments. |
| 68 | * @return mixed Results from the data store. |
| 69 | */ |
| 70 | protected function get_datastore_data( $query_args = array() ) { |
| 71 | $query = new GenericQuery( $query_args, 'downloads-stats' ); |
| 72 | return $query->get_data(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Prepare a report data item for serialization. |
| 77 | * |
| 78 | * @param array $report Report data item as returned from Data Store. |
| 79 | * @param WP_REST_Request $request Request object. |
| 80 | * @return WP_REST_Response |
| 81 | */ |
| 82 | public function prepare_item_for_response( $report, $request ) { |
| 83 | $response = parent::prepare_item_for_response( $report, $request ); |
| 84 | |
| 85 | /** |
| 86 | * Filter a report returned from the API. |
| 87 | * |
| 88 | * Allows modification of the report data right before it is returned. |
| 89 | * |
| 90 | * @param WP_REST_Response $response The response object. |
| 91 | * @param object $report The original report object. |
| 92 | * @param WP_REST_Request $request Request used to generate the response. |
| 93 | */ |
| 94 | return apply_filters( 'woocommerce_rest_prepare_report_downloads_stats', $response, $report, $request ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the Report's item properties schema. |
| 99 | * Will be used by `get_item_schema` as `totals` and `subtotals`. |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | protected function get_item_properties_schema() { |
| 104 | return array( |
| 105 | 'download_count' => array( |
| 106 | 'title' => __( 'Downloads', 'woocommerce' ), |
| 107 | 'description' => __( 'Number of downloads.', 'woocommerce' ), |
| 108 | 'type' => 'number', |
| 109 | 'context' => array( 'view', 'edit' ), |
| 110 | 'readonly' => true, |
| 111 | 'indicator' => true, |
| 112 | ), |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get the Report's schema, conforming to JSON Schema. |
| 118 | * It does not have the segments as in GenericStatsController. |
| 119 | * |
| 120 | * @return array |
| 121 | */ |
| 122 | public function get_item_schema() { |
| 123 | $totals = $this->get_item_properties_schema(); |
| 124 | |
| 125 | $schema = array( |
| 126 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 127 | 'title' => 'report_orders_stats', |
| 128 | 'type' => 'object', |
| 129 | 'properties' => array( |
| 130 | 'totals' => array( |
| 131 | 'description' => __( 'Totals data.', 'woocommerce' ), |
| 132 | 'type' => 'object', |
| 133 | 'context' => array( 'view', 'edit' ), |
| 134 | 'readonly' => true, |
| 135 | 'properties' => $totals, |
| 136 | ), |
| 137 | 'intervals' => array( |
| 138 | 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), |
| 139 | 'type' => 'array', |
| 140 | 'context' => array( 'view', 'edit' ), |
| 141 | 'readonly' => true, |
| 142 | 'items' => array( |
| 143 | 'type' => 'object', |
| 144 | 'properties' => array( |
| 145 | 'interval' => array( |
| 146 | 'description' => __( 'Type of interval.', 'woocommerce' ), |
| 147 | 'type' => 'string', |
| 148 | 'context' => array( 'view', 'edit' ), |
| 149 | 'readonly' => true, |
| 150 | 'enum' => array( 'day', 'week', 'month', 'year' ), |
| 151 | ), |
| 152 | 'date_start' => array( |
| 153 | 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), |
| 154 | 'type' => 'date-time', |
| 155 | 'context' => array( 'view', 'edit' ), |
| 156 | 'readonly' => true, |
| 157 | ), |
| 158 | 'date_start_gmt' => array( |
| 159 | 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), |
| 160 | 'type' => 'date-time', |
| 161 | 'context' => array( 'view', 'edit' ), |
| 162 | 'readonly' => true, |
| 163 | ), |
| 164 | 'date_end' => array( |
| 165 | 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), |
| 166 | 'type' => 'date-time', |
| 167 | 'context' => array( 'view', 'edit' ), |
| 168 | 'readonly' => true, |
| 169 | ), |
| 170 | 'date_end_gmt' => array( |
| 171 | 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), |
| 172 | 'type' => 'date-time', |
| 173 | 'context' => array( 'view', 'edit' ), |
| 174 | 'readonly' => true, |
| 175 | ), |
| 176 | 'subtotals' => array( |
| 177 | 'description' => __( 'Interval subtotals.', 'woocommerce' ), |
| 178 | 'type' => 'object', |
| 179 | 'context' => array( 'view', 'edit' ), |
| 180 | 'readonly' => true, |
| 181 | 'properties' => $totals, |
| 182 | ), |
| 183 | ), |
| 184 | ), |
| 185 | ), |
| 186 | ), |
| 187 | ); |
| 188 | |
| 189 | return $this->add_additional_fields_schema( $schema ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the query params for collections. |
| 194 | * |
| 195 | * @return array |
| 196 | */ |
| 197 | public function get_collection_params() { |
| 198 | $params = parent::get_collection_params(); |
| 199 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 200 | array( |
| 201 | 'date', |
| 202 | 'download_count', |
| 203 | ) |
| 204 | ); |
| 205 | $params['match'] = array( |
| 206 | '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' ), |
| 207 | 'type' => 'string', |
| 208 | 'default' => 'all', |
| 209 | 'enum' => array( |
| 210 | 'all', |
| 211 | 'any', |
| 212 | ), |
| 213 | 'validate_callback' => 'rest_validate_request_arg', |
| 214 | ); |
| 215 | $params['product_includes'] = array( |
| 216 | 'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ), |
| 217 | 'type' => 'array', |
| 218 | 'items' => array( |
| 219 | 'type' => 'integer', |
| 220 | ), |
| 221 | 'default' => array(), |
| 222 | 'sanitize_callback' => 'wp_parse_id_list', |
| 223 | |
| 224 | ); |
| 225 | $params['product_excludes'] = array( |
| 226 | 'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ), |
| 227 | 'type' => 'array', |
| 228 | 'items' => array( |
| 229 | 'type' => 'integer', |
| 230 | ), |
| 231 | 'default' => array(), |
| 232 | 'sanitize_callback' => 'wp_parse_id_list', |
| 233 | ); |
| 234 | $params['order_includes'] = array( |
| 235 | 'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ), |
| 236 | 'type' => 'array', |
| 237 | 'sanitize_callback' => 'wp_parse_id_list', |
| 238 | 'validate_callback' => 'rest_validate_request_arg', |
| 239 | 'items' => array( |
| 240 | 'type' => 'integer', |
| 241 | ), |
| 242 | ); |
| 243 | $params['order_excludes'] = array( |
| 244 | 'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ), |
| 245 | 'type' => 'array', |
| 246 | 'sanitize_callback' => 'wp_parse_id_list', |
| 247 | 'validate_callback' => 'rest_validate_request_arg', |
| 248 | 'items' => array( |
| 249 | 'type' => 'integer', |
| 250 | ), |
| 251 | ); |
| 252 | $params['customer_includes'] = array( |
| 253 | 'description' => __( 'Limit response to objects that have the specified customer ids.', 'woocommerce' ), |
| 254 | 'type' => 'array', |
| 255 | 'sanitize_callback' => 'wp_parse_id_list', |
| 256 | 'validate_callback' => 'rest_validate_request_arg', |
| 257 | 'items' => array( |
| 258 | 'type' => 'integer', |
| 259 | ), |
| 260 | ); |
| 261 | $params['customer_excludes'] = array( |
| 262 | 'description' => __( 'Limit response to objects that don\'t have the specified customer ids.', 'woocommerce' ), |
| 263 | 'type' => 'array', |
| 264 | 'sanitize_callback' => 'wp_parse_id_list', |
| 265 | 'validate_callback' => 'rest_validate_request_arg', |
| 266 | 'items' => array( |
| 267 | 'type' => 'integer', |
| 268 | ), |
| 269 | ); |
| 270 | $params['ip_address_includes'] = array( |
| 271 | 'description' => __( 'Limit response to objects that have a specified ip address.', 'woocommerce' ), |
| 272 | 'type' => 'array', |
| 273 | 'validate_callback' => 'rest_validate_request_arg', |
| 274 | 'items' => array( |
| 275 | 'type' => 'string', |
| 276 | ), |
| 277 | ); |
| 278 | |
| 279 | $params['ip_address_excludes'] = array( |
| 280 | 'description' => __( 'Limit response to objects that don\'t have a specified ip address.', 'woocommerce' ), |
| 281 | 'type' => 'array', |
| 282 | 'validate_callback' => 'rest_validate_request_arg', |
| 283 | 'items' => array( |
| 284 | 'type' => 'string', |
| 285 | ), |
| 286 | ); |
| 287 | |
| 288 | return $params; |
| 289 | } |
| 290 | } |
| 291 |