Files
4 years ago
Stats
1 year ago
Controller.php
1 year ago
DataStore.php
1 year ago
Query.php
1 year ago
Controller.php
370 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports downloads controller |
| 4 | * |
| 5 | * Handles requests to the /reports/downloads endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Downloads; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\GenericController; |
| 14 | use Automattic\WooCommerce\Admin\API\Reports\GenericQuery; |
| 15 | use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait; |
| 16 | |
| 17 | /** |
| 18 | * REST API Reports downloads controller class. |
| 19 | * |
| 20 | * @internal |
| 21 | * @extends Automattic\WooCommerce\Admin\API\Reports\GenericController |
| 22 | */ |
| 23 | class Controller extends GenericController implements ExportableInterface { |
| 24 | |
| 25 | use OrderAwareControllerTrait; |
| 26 | |
| 27 | /** |
| 28 | * Route base. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $rest_base = 'reports/downloads'; |
| 33 | |
| 34 | /** |
| 35 | * Get data from `'downloads'` GenericQuery. |
| 36 | * |
| 37 | * @override GenericController::get_datastore_data() |
| 38 | * |
| 39 | * @param array $query_args Query arguments. |
| 40 | * @return mixed Results from the data store. |
| 41 | */ |
| 42 | protected function get_datastore_data( $query_args = array() ) { |
| 43 | $query = new GenericQuery( $query_args, 'downloads' ); |
| 44 | return $query->get_data(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Prepare a report data item for serialization. |
| 49 | * |
| 50 | * @param Array $report Report data item as returned from Data Store. |
| 51 | * @param WP_REST_Request $request Request object. |
| 52 | * @return WP_REST_Response |
| 53 | */ |
| 54 | public function prepare_item_for_response( $report, $request ) { |
| 55 | // Wrap the data in a response object. |
| 56 | $response = parent::prepare_item_for_response( $report, $request ); |
| 57 | $response->add_links( $this->prepare_links( $report ) ); |
| 58 | |
| 59 | $response->data['date'] = get_date_from_gmt( $report['date_gmt'], 'Y-m-d H:i:s' ); |
| 60 | |
| 61 | // Figure out file name. |
| 62 | // Matches https://github.com/woocommerce/woocommerce/blob/4be0018c092e617c5d2b8c46b800eb71ece9ddef/includes/class-wc-download-handler.php#L197. |
| 63 | $product_id = intval( $report['product_id'] ); |
| 64 | $_product = wc_get_product( $product_id ); |
| 65 | |
| 66 | // Make sure the product hasn't been deleted. |
| 67 | if ( $_product ) { |
| 68 | $file_path = $_product->get_file_download_path( $report['download_id'] ); |
| 69 | $filename = basename( $file_path ); |
| 70 | $response->data['file_name'] = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id ); |
| 71 | $response->data['file_path'] = $file_path; |
| 72 | } else { |
| 73 | $response->data['file_name'] = ''; |
| 74 | $response->data['file_path'] = ''; |
| 75 | } |
| 76 | |
| 77 | $customer = new \WC_Customer( $report['user_id'] ); |
| 78 | $response->data['username'] = $customer->get_username(); |
| 79 | $response->data['order_number'] = $this->get_order_number( $report['order_id'] ); |
| 80 | |
| 81 | /** |
| 82 | * Filter a report returned from the API. |
| 83 | * |
| 84 | * Allows modification of the report data right before it is returned. |
| 85 | * |
| 86 | * @param WP_REST_Response $response The response object. |
| 87 | * @param object $report The original report object. |
| 88 | * @param WP_REST_Request $request Request used to generate the response. |
| 89 | */ |
| 90 | return apply_filters( 'woocommerce_rest_prepare_report_downloads', $response, $report, $request ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Prepare links for the request. |
| 95 | * |
| 96 | * @param Array $object Object data. |
| 97 | * @return array Links for the given post. |
| 98 | */ |
| 99 | protected function prepare_links( $object ) { |
| 100 | $links = array( |
| 101 | 'product' => array( |
| 102 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ), |
| 103 | 'embeddable' => true, |
| 104 | ), |
| 105 | ); |
| 106 | |
| 107 | return $links; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Maps query arguments from the REST request. |
| 112 | * |
| 113 | * @param array $request Request array. |
| 114 | * @return array |
| 115 | */ |
| 116 | protected function prepare_reports_query( $request ) { |
| 117 | $args = array(); |
| 118 | $registered = array_keys( $this->get_collection_params() ); |
| 119 | foreach ( $registered as $param_name ) { |
| 120 | if ( isset( $request[ $param_name ] ) ) { |
| 121 | $args[ $param_name ] = $request[ $param_name ]; |
| 122 | } |
| 123 | } |
| 124 | return $args; |
| 125 | } |
| 126 | /** |
| 127 | * Get the Report's schema, conforming to JSON Schema. |
| 128 | * |
| 129 | * @return array |
| 130 | */ |
| 131 | public function get_item_schema() { |
| 132 | $schema = array( |
| 133 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 134 | 'title' => 'report_downloads', |
| 135 | 'type' => 'object', |
| 136 | 'properties' => array( |
| 137 | 'id' => array( |
| 138 | 'type' => 'integer', |
| 139 | 'readonly' => true, |
| 140 | 'context' => array( 'view', 'edit' ), |
| 141 | 'description' => __( 'ID.', 'woocommerce' ), |
| 142 | ), |
| 143 | 'product_id' => array( |
| 144 | 'type' => 'integer', |
| 145 | 'readonly' => true, |
| 146 | 'context' => array( 'view', 'edit' ), |
| 147 | 'description' => __( 'Product ID.', 'woocommerce' ), |
| 148 | ), |
| 149 | 'date' => array( |
| 150 | 'description' => __( "The date of the download, in the site's timezone.", 'woocommerce' ), |
| 151 | 'type' => 'date-time', |
| 152 | 'context' => array( 'view', 'edit' ), |
| 153 | 'readonly' => true, |
| 154 | ), |
| 155 | 'date_gmt' => array( |
| 156 | 'description' => __( 'The date of the download, as GMT.', 'woocommerce' ), |
| 157 | 'type' => 'date-time', |
| 158 | 'context' => array( 'view', 'edit' ), |
| 159 | 'readonly' => true, |
| 160 | ), |
| 161 | 'download_id' => array( |
| 162 | 'type' => 'string', |
| 163 | 'readonly' => true, |
| 164 | 'context' => array( 'view', 'edit' ), |
| 165 | 'description' => __( 'Download ID.', 'woocommerce' ), |
| 166 | ), |
| 167 | 'file_name' => array( |
| 168 | 'type' => 'string', |
| 169 | 'readonly' => true, |
| 170 | 'context' => array( 'view', 'edit' ), |
| 171 | 'description' => __( 'File name.', 'woocommerce' ), |
| 172 | ), |
| 173 | 'file_path' => array( |
| 174 | 'type' => 'string', |
| 175 | 'readonly' => true, |
| 176 | 'context' => array( 'view', 'edit' ), |
| 177 | 'description' => __( 'File URL.', 'woocommerce' ), |
| 178 | ), |
| 179 | 'order_id' => array( |
| 180 | 'type' => 'integer', |
| 181 | 'readonly' => true, |
| 182 | 'context' => array( 'view', 'edit' ), |
| 183 | 'description' => __( 'Order ID.', 'woocommerce' ), |
| 184 | ), |
| 185 | 'order_number' => array( |
| 186 | 'type' => 'string', |
| 187 | 'readonly' => true, |
| 188 | 'context' => array( 'view', 'edit' ), |
| 189 | 'description' => __( 'Order Number.', 'woocommerce' ), |
| 190 | ), |
| 191 | 'user_id' => array( |
| 192 | 'type' => 'integer', |
| 193 | 'readonly' => true, |
| 194 | 'context' => array( 'view', 'edit' ), |
| 195 | 'description' => __( 'User ID for the downloader.', 'woocommerce' ), |
| 196 | ), |
| 197 | 'username' => array( |
| 198 | 'type' => 'string', |
| 199 | 'readonly' => true, |
| 200 | 'context' => array( 'view', 'edit' ), |
| 201 | 'description' => __( 'User name of the downloader.', 'woocommerce' ), |
| 202 | ), |
| 203 | 'ip_address' => array( |
| 204 | 'type' => 'string', |
| 205 | 'readonly' => true, |
| 206 | 'context' => array( 'view', 'edit' ), |
| 207 | 'description' => __( 'IP address for the downloader.', 'woocommerce' ), |
| 208 | ), |
| 209 | ), |
| 210 | ); |
| 211 | |
| 212 | return $this->add_additional_fields_schema( $schema ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Get the query params for collections. |
| 217 | * |
| 218 | * @return array |
| 219 | */ |
| 220 | public function get_collection_params() { |
| 221 | $params = parent::get_collection_params(); |
| 222 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 223 | array( |
| 224 | 'date', |
| 225 | 'product', |
| 226 | ) |
| 227 | ); |
| 228 | $params['match'] = array( |
| 229 | '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: products, orders, username, ip_address.', 'woocommerce' ), |
| 230 | 'type' => 'string', |
| 231 | 'default' => 'all', |
| 232 | 'enum' => array( |
| 233 | 'all', |
| 234 | 'any', |
| 235 | ), |
| 236 | 'validate_callback' => 'rest_validate_request_arg', |
| 237 | ); |
| 238 | $params['product_includes'] = array( |
| 239 | 'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ), |
| 240 | 'type' => 'array', |
| 241 | 'items' => array( |
| 242 | 'type' => 'integer', |
| 243 | ), |
| 244 | 'default' => array(), |
| 245 | 'sanitize_callback' => 'wp_parse_id_list', |
| 246 | 'validate_callback' => 'rest_validate_request_arg', |
| 247 | ); |
| 248 | $params['product_excludes'] = array( |
| 249 | 'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ), |
| 250 | 'type' => 'array', |
| 251 | 'items' => array( |
| 252 | 'type' => 'integer', |
| 253 | ), |
| 254 | 'default' => array(), |
| 255 | 'validate_callback' => 'rest_validate_request_arg', |
| 256 | 'sanitize_callback' => 'wp_parse_id_list', |
| 257 | ); |
| 258 | $params['order_includes'] = array( |
| 259 | 'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ), |
| 260 | 'type' => 'array', |
| 261 | 'sanitize_callback' => 'wp_parse_id_list', |
| 262 | 'validate_callback' => 'rest_validate_request_arg', |
| 263 | 'items' => array( |
| 264 | 'type' => 'integer', |
| 265 | ), |
| 266 | ); |
| 267 | $params['order_excludes'] = array( |
| 268 | 'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ), |
| 269 | 'type' => 'array', |
| 270 | 'sanitize_callback' => 'wp_parse_id_list', |
| 271 | 'validate_callback' => 'rest_validate_request_arg', |
| 272 | 'items' => array( |
| 273 | 'type' => 'integer', |
| 274 | ), |
| 275 | ); |
| 276 | $params['customer_includes'] = array( |
| 277 | 'description' => __( 'Limit response to objects that have the specified user ids.', 'woocommerce' ), |
| 278 | 'type' => 'array', |
| 279 | 'sanitize_callback' => 'wp_parse_id_list', |
| 280 | 'validate_callback' => 'rest_validate_request_arg', |
| 281 | 'items' => array( |
| 282 | 'type' => 'integer', |
| 283 | ), |
| 284 | ); |
| 285 | $params['customer_excludes'] = array( |
| 286 | 'description' => __( 'Limit response to objects that don\'t have the specified user ids.', 'woocommerce' ), |
| 287 | 'type' => 'array', |
| 288 | 'sanitize_callback' => 'wp_parse_id_list', |
| 289 | 'validate_callback' => 'rest_validate_request_arg', |
| 290 | 'items' => array( |
| 291 | 'type' => 'integer', |
| 292 | ), |
| 293 | ); |
| 294 | $params['ip_address_includes'] = array( |
| 295 | 'description' => __( 'Limit response to objects that have a specified ip address.', 'woocommerce' ), |
| 296 | 'type' => 'array', |
| 297 | 'validate_callback' => 'rest_validate_request_arg', |
| 298 | 'items' => array( |
| 299 | 'type' => 'string', |
| 300 | ), |
| 301 | ); |
| 302 | $params['ip_address_excludes'] = array( |
| 303 | 'description' => __( 'Limit response to objects that don\'t have a specified ip address.', 'woocommerce' ), |
| 304 | 'type' => 'array', |
| 305 | 'validate_callback' => 'rest_validate_request_arg', |
| 306 | 'items' => array( |
| 307 | 'type' => 'string', |
| 308 | ), |
| 309 | ); |
| 310 | |
| 311 | return $params; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Get the column names for export. |
| 316 | * |
| 317 | * @return array Key value pair of Column ID => Label. |
| 318 | */ |
| 319 | public function get_export_columns() { |
| 320 | $export_columns = array( |
| 321 | 'date' => __( 'Date', 'woocommerce' ), |
| 322 | 'product' => __( 'Product title', 'woocommerce' ), |
| 323 | 'file_name' => __( 'File name', 'woocommerce' ), |
| 324 | 'order_number' => __( 'Order #', 'woocommerce' ), |
| 325 | 'user_id' => __( 'User Name', 'woocommerce' ), |
| 326 | 'ip_address' => __( 'IP', 'woocommerce' ), |
| 327 | ); |
| 328 | |
| 329 | /** |
| 330 | * Filter to add or remove column names from the downloads report for |
| 331 | * export. |
| 332 | * |
| 333 | * @since 1.6.0 |
| 334 | */ |
| 335 | return apply_filters( |
| 336 | 'woocommerce_filter_downloads_export_columns', |
| 337 | $export_columns |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get the column values for export. |
| 343 | * |
| 344 | * @param array $item Single report item/row. |
| 345 | * @return array Key value pair of Column ID => Row Value. |
| 346 | */ |
| 347 | public function prepare_item_for_export( $item ) { |
| 348 | $export_item = array( |
| 349 | 'date' => $item['date'], |
| 350 | 'product' => $item['_embedded']['product'][0]['name'], |
| 351 | 'file_name' => $item['file_name'], |
| 352 | 'order_number' => $item['order_number'], |
| 353 | 'user_id' => $item['username'], |
| 354 | 'ip_address' => $item['ip_address'], |
| 355 | ); |
| 356 | |
| 357 | /** |
| 358 | * Filter to prepare extra columns in the export item for the downloads |
| 359 | * report. |
| 360 | * |
| 361 | * @since 1.6.0 |
| 362 | */ |
| 363 | return apply_filters( |
| 364 | 'woocommerce_report_downloads_prepare_export_item', |
| 365 | $export_item, |
| 366 | $item |
| 367 | ); |
| 368 | } |
| 369 | } |
| 370 |