Categories
4 weeks ago
Coupons
4 weeks ago
Customers
2 months ago
Downloads
1 year ago
Export
3 years ago
Import
3 years ago
Orders
4 weeks ago
PerformanceIndicators
9 months ago
Products
4 weeks ago
Revenue
3 months ago
Stock
4 months ago
Taxes
4 weeks ago
Variations
4 weeks ago
Cache.php
4 years ago
Controller.php
11 months ago
DataStore.php
4 weeks ago
DataStoreInterface.php
4 years ago
ExportableInterface.php
4 years ago
ExportableTraits.php
4 years ago
FilteredGetDataTrait.php
1 year ago
GenericController.php
1 year ago
GenericQuery.php
1 year ago
GenericStatsController.php
1 year ago
OrderAwareControllerTrait.php
1 year ago
ParameterException.php
4 years ago
Query.php
1 year ago
Segmenter.php
1 year ago
SqlQuery.php
3 years ago
StatsDataStoreTrait.php
1 year ago
TimeInterval.php
2 years ago
Controller.php
236 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports controller extended to handle requests to the reports endpoint. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\GenericController; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait; |
| 12 | |
| 13 | /** |
| 14 | * Reports controller class. |
| 15 | * |
| 16 | * Controller that handles the endpoint that returns all available analytics endpoints. |
| 17 | * |
| 18 | * @internal |
| 19 | * @extends GenericController |
| 20 | */ |
| 21 | class Controller extends GenericController { |
| 22 | |
| 23 | use OrderAwareControllerTrait; |
| 24 | |
| 25 | /** |
| 26 | * Get all reports. |
| 27 | * |
| 28 | * @param WP_REST_Request $request Request data. |
| 29 | * @return array|WP_Error |
| 30 | */ |
| 31 | public function get_items( $request ) { |
| 32 | $data = array(); |
| 33 | $reports = array( |
| 34 | array( |
| 35 | 'slug' => 'performance-indicators', |
| 36 | 'description' => __( 'Batch endpoint for getting specific performance indicators from `stats` endpoints.', 'woocommerce' ), |
| 37 | ), |
| 38 | array( |
| 39 | 'slug' => 'revenue/stats', |
| 40 | 'description' => __( 'Stats about revenue.', 'woocommerce' ), |
| 41 | ), |
| 42 | array( |
| 43 | 'slug' => 'orders/stats', |
| 44 | 'description' => __( 'Stats about orders.', 'woocommerce' ), |
| 45 | ), |
| 46 | array( |
| 47 | 'slug' => 'products', |
| 48 | 'description' => __( 'Products detailed reports.', 'woocommerce' ), |
| 49 | ), |
| 50 | array( |
| 51 | 'slug' => 'products/stats', |
| 52 | 'description' => __( 'Stats about products.', 'woocommerce' ), |
| 53 | ), |
| 54 | array( |
| 55 | 'slug' => 'variations', |
| 56 | 'description' => __( 'Variations detailed reports.', 'woocommerce' ), |
| 57 | ), |
| 58 | array( |
| 59 | 'slug' => 'variations/stats', |
| 60 | 'description' => __( 'Stats about variations.', 'woocommerce' ), |
| 61 | ), |
| 62 | array( |
| 63 | 'slug' => 'categories', |
| 64 | 'description' => __( 'Product categories detailed reports.', 'woocommerce' ), |
| 65 | ), |
| 66 | array( |
| 67 | 'slug' => 'categories/stats', |
| 68 | 'description' => __( 'Stats about product categories.', 'woocommerce' ), |
| 69 | ), |
| 70 | array( |
| 71 | 'slug' => 'coupons', |
| 72 | 'description' => __( 'Coupons detailed reports.', 'woocommerce' ), |
| 73 | ), |
| 74 | array( |
| 75 | 'slug' => 'coupons/stats', |
| 76 | 'description' => __( 'Stats about coupons.', 'woocommerce' ), |
| 77 | ), |
| 78 | array( |
| 79 | 'slug' => 'taxes', |
| 80 | 'description' => __( 'Taxes detailed reports.', 'woocommerce' ), |
| 81 | ), |
| 82 | array( |
| 83 | 'slug' => 'taxes/stats', |
| 84 | 'description' => __( 'Stats about taxes.', 'woocommerce' ), |
| 85 | ), |
| 86 | array( |
| 87 | 'slug' => 'downloads', |
| 88 | 'description' => __( 'Product downloads detailed reports.', 'woocommerce' ), |
| 89 | ), |
| 90 | array( |
| 91 | 'slug' => 'downloads/files', |
| 92 | 'description' => __( 'Product download files detailed reports.', 'woocommerce' ), |
| 93 | ), |
| 94 | array( |
| 95 | 'slug' => 'downloads/stats', |
| 96 | 'description' => __( 'Stats about product downloads.', 'woocommerce' ), |
| 97 | ), |
| 98 | array( |
| 99 | 'slug' => 'customers', |
| 100 | 'description' => __( 'Customers detailed reports.', 'woocommerce' ), |
| 101 | ), |
| 102 | array( |
| 103 | 'slug' => 'customers/stats', |
| 104 | 'description' => __( 'Stats about groups of customers.', 'woocommerce' ), |
| 105 | ), |
| 106 | ); |
| 107 | |
| 108 | /** |
| 109 | * Filter the list of allowed reports, so that data can be loaded from third party extensions in addition to WooCommerce core. |
| 110 | * Array items should be in format of array( 'slug' => 'downloads/stats', 'description' => '', |
| 111 | * 'url' => '', and 'path' => '/wc-ext/v1/...'. |
| 112 | * |
| 113 | * @param array $endpoints The list of allowed reports.. |
| 114 | */ |
| 115 | $reports = apply_filters( 'woocommerce_admin_reports', $reports ); |
| 116 | |
| 117 | foreach ( $reports as $report ) { |
| 118 | // Silently skip non-compliant reports. Like the ones for WC_Admin_Reports::get_reports(). |
| 119 | if ( empty( $report['slug'] ) ) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | if ( empty( $report['path'] ) ) { |
| 124 | $report['path'] = '/' . $this->namespace . '/reports/' . $report['slug']; |
| 125 | } |
| 126 | |
| 127 | // Allows a different admin page to be loaded here, |
| 128 | // or allows an empty url if no report exists for a set of performance indicators. |
| 129 | if ( ! isset( $report['url'] ) ) { |
| 130 | if ( '/stats' === substr( $report['slug'], -6 ) ) { |
| 131 | $url_slug = substr( $report['slug'], 0, -6 ); |
| 132 | } else { |
| 133 | $url_slug = $report['slug']; |
| 134 | } |
| 135 | |
| 136 | $report['url'] = '/analytics/' . $url_slug; |
| 137 | } |
| 138 | |
| 139 | $item = $this->prepare_item_for_response( (object) $report, $request ); |
| 140 | $data[] = $this->prepare_response_for_collection( $item ); |
| 141 | } |
| 142 | |
| 143 | return rest_ensure_response( $data ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Prepare a report object for serialization. |
| 148 | * |
| 149 | * @param stdClass $report Report data. |
| 150 | * @param WP_REST_Request $request Request object. |
| 151 | * @return WP_REST_Response |
| 152 | */ |
| 153 | public function prepare_item_for_response( $report, $request ) { |
| 154 | $data = array( |
| 155 | 'slug' => $report->slug, |
| 156 | 'description' => $report->description, |
| 157 | 'path' => $report->path, |
| 158 | ); |
| 159 | |
| 160 | // Wrap the data in a response object. |
| 161 | $response = parent::prepare_item_for_response( $data, $request ); |
| 162 | $response->add_links( |
| 163 | array( |
| 164 | 'self' => array( |
| 165 | 'href' => rest_url( $report->path ), |
| 166 | ), |
| 167 | 'report' => array( |
| 168 | 'href' => $report->url, |
| 169 | ), |
| 170 | 'collection' => array( |
| 171 | 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
| 172 | ), |
| 173 | ) |
| 174 | ); |
| 175 | |
| 176 | /** |
| 177 | * Filter a report returned from the API. |
| 178 | * |
| 179 | * Allows modification of the report data right before it is returned. |
| 180 | * |
| 181 | * @param WP_REST_Response $response The response object. |
| 182 | * @param object $report The original report object. |
| 183 | * @param WP_REST_Request $request Request used to generate the response. |
| 184 | */ |
| 185 | return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get the Report's schema, conforming to JSON Schema. |
| 190 | * |
| 191 | * @override WP_REST_Controller::get_item_schema() |
| 192 | * |
| 193 | * @return array |
| 194 | */ |
| 195 | public function get_item_schema() { |
| 196 | $schema = array( |
| 197 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 198 | 'title' => 'report', |
| 199 | 'type' => 'object', |
| 200 | 'properties' => array( |
| 201 | 'slug' => array( |
| 202 | 'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ), |
| 203 | 'type' => 'string', |
| 204 | 'context' => array( 'view' ), |
| 205 | 'readonly' => true, |
| 206 | ), |
| 207 | 'description' => array( |
| 208 | 'description' => __( 'A human-readable description of the resource.', 'woocommerce' ), |
| 209 | 'type' => 'string', |
| 210 | 'context' => array( 'view' ), |
| 211 | 'readonly' => true, |
| 212 | ), |
| 213 | 'path' => array( |
| 214 | 'description' => __( 'API path.', 'woocommerce' ), |
| 215 | 'type' => 'string', |
| 216 | 'context' => array( 'view' ), |
| 217 | 'readonly' => true, |
| 218 | ), |
| 219 | ), |
| 220 | ); |
| 221 | |
| 222 | return $this->add_additional_fields_schema( $schema ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get the query params for collections. |
| 227 | * |
| 228 | * @return array |
| 229 | */ |
| 230 | public function get_collection_params() { |
| 231 | return array( |
| 232 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 233 | ); |
| 234 | } |
| 235 | } |
| 236 |