Controller.php
141 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports stock stats controller |
| 4 | * |
| 5 | * Handles requests to the /reports/stock/stats endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * REST API Reports stock stats controller class. |
| 14 | * |
| 15 | * @internal |
| 16 | * @extends WC_REST_Reports_Controller |
| 17 | */ |
| 18 | class Controller extends \WC_REST_Reports_Controller { |
| 19 | |
| 20 | /** |
| 21 | * Endpoint namespace. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $namespace = 'wc-analytics'; |
| 26 | |
| 27 | /** |
| 28 | * Route base. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $rest_base = 'reports/stock/stats'; |
| 33 | |
| 34 | /** |
| 35 | * Get Stock Status Totals. |
| 36 | * |
| 37 | * @param WP_REST_Request $request Request data. |
| 38 | * @return array|WP_Error |
| 39 | */ |
| 40 | public function get_items( $request ) { |
| 41 | $stock_query = new Query(); |
| 42 | $report_data = $stock_query->get_data(); |
| 43 | $out_data = array( |
| 44 | 'totals' => $report_data, |
| 45 | ); |
| 46 | return rest_ensure_response( $out_data ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Prepare a report data item for serialization. |
| 51 | * |
| 52 | * @param WC_Product $report Report data item as returned from Data Store. |
| 53 | * @param WP_REST_Request $request Request object. |
| 54 | * @return WP_REST_Response |
| 55 | */ |
| 56 | public function prepare_item_for_response( $report, $request ) { |
| 57 | $data = $report; |
| 58 | |
| 59 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 60 | $data = $this->add_additional_fields_to_object( $data, $request ); |
| 61 | $data = $this->filter_response_by_context( $data, $context ); |
| 62 | |
| 63 | // Wrap the data in a response object. |
| 64 | $response = rest_ensure_response( $data ); |
| 65 | |
| 66 | /** |
| 67 | * Filter a report returned from the API. |
| 68 | * |
| 69 | * Allows modification of the report data right before it is returned. |
| 70 | * |
| 71 | * @since 6.5.0 |
| 72 | * |
| 73 | * @param WP_REST_Response $response The response object. |
| 74 | * @param WC_Product $report The original object. |
| 75 | * @param WP_REST_Request $request Request used to generate the response. |
| 76 | */ |
| 77 | return apply_filters( 'woocommerce_rest_prepare_report_stock_stats', $response, $report, $request ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the Report's schema, conforming to JSON Schema. |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public function get_item_schema() { |
| 86 | $totals = array( |
| 87 | 'products' => array( |
| 88 | 'description' => __( 'Number of products.', 'woocommerce' ), |
| 89 | 'type' => 'integer', |
| 90 | 'context' => array( 'view', 'edit' ), |
| 91 | 'readonly' => true, |
| 92 | ), |
| 93 | 'lowstock' => array( |
| 94 | 'description' => __( 'Number of low stock products.', 'woocommerce' ), |
| 95 | 'type' => 'integer', |
| 96 | 'context' => array( 'view', 'edit' ), |
| 97 | 'readonly' => true, |
| 98 | ), |
| 99 | ); |
| 100 | |
| 101 | $status_options = wc_get_product_stock_status_options(); |
| 102 | foreach ( $status_options as $status => $label ) { |
| 103 | $totals[ $status ] = array( |
| 104 | /* translators: Stock status. Example: "Number of low stock products */ |
| 105 | 'description' => sprintf( __( 'Number of %s products.', 'woocommerce' ), $label ), |
| 106 | 'type' => 'integer', |
| 107 | 'context' => array( 'view', 'edit' ), |
| 108 | 'readonly' => true, |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $schema = array( |
| 113 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 114 | 'title' => 'report_customers_stats', |
| 115 | 'type' => 'object', |
| 116 | 'properties' => array( |
| 117 | 'totals' => array( |
| 118 | 'description' => __( 'Totals data.', 'woocommerce' ), |
| 119 | 'type' => 'object', |
| 120 | 'context' => array( 'view', 'edit' ), |
| 121 | 'readonly' => true, |
| 122 | 'properties' => $totals, |
| 123 | ), |
| 124 | ), |
| 125 | ); |
| 126 | |
| 127 | return $this->add_additional_fields_schema( $schema ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the query params for collections. |
| 132 | * |
| 133 | * @return array |
| 134 | */ |
| 135 | public function get_collection_params() { |
| 136 | $params = array(); |
| 137 | $params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); |
| 138 | return $params; |
| 139 | } |
| 140 | } |
| 141 |