PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / rest / Analytics / RevenueChartApi.php

RevenueChartApi.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at rest/Analytics/RevenueChartApi.php

155 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore
2
3 /**
4 * Analytics REST API — Revenue Chart Endpoint
5 *
6 * @package Disco
7 * @subpackage \Rest\Analytics
8 * @since 1.1.13
9 * @category Rest
10 */
11
12 namespace Disco\Rest\Analytics;
13
14 use Disco\App\Analytics\Controllers\RevenueController;
15 use WP_REST_Server;
16
17 /**
18 * Class RevenueChartApi
19 *
20 * Registers and handles the following REST API route:
21 *
22 * - GET /disco/v1/analytics/revenue-chart — Time-bucketed net_sales and discount_sales.
23 *
24 * @package Disco
25 * @subpackage \Rest\Analytics
26 * @author Ohidul Islam <wahid0003@gmail.com>
27 * @link https://webappick.com
28 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
29 * @category Rest
30 */
31 class RevenueChartApi extends Base {
32
33 /**
34 * Registers the REST API routes for the revenue chart endpoint.
35 */
36 public function register_routes(): void {
37 // GET /analytics/revenue-chart
38 register_rest_route(
39 $this->namespace,
40 '/' . $this->rest_base . '/revenue-chart',
41 array(
42 array(
43 'methods' => WP_REST_Server::READABLE,
44 'callback' => array( $this, 'get_revenue' ),
45 'permission_callback' => array( $this, 'permissions_check' ),
46 'args' => $this->get_date_params(),
47 ),
48 'schema' => array( $this, 'get_revenue_schema' ),
49 )
50 );
51 }
52
53 // =========================================================================
54 // Callbacks
55 // =========================================================================
56
57 /**
58 * GET /disco/v1/analytics/revenue-chart
59 *
60 * Returns time-bucketed net_sales and discount_sales for completed orders.
61 * The grouping interval is auto-selected from the date range:
62 * ≤ 30 days → day | 31–180 days → week | > 180 days → month
63 *
64 * Query params:
65 * date_from (string Y-m-d) Start of range. Default: 30 days ago.
66 * date_to (string Y-m-d) End of range. Default: today.
67 *
68 * @param \WP_REST_Request $request Incoming REST request.
69 * @return \WP_REST_Response|\WP_Error
70 */
71 public function get_revenue( $request ) {
72 $error = $this->validate_date_params( $request );
73
74 if ( is_wp_error( $error ) ) {
75 return $error;
76 }
77
78 $args = array(
79 'date_from' => is_string( $request['date_from'] ) ? sanitize_text_field( $request['date_from'] ) : '',
80 'date_to' => is_string( $request['date_to'] ) ? sanitize_text_field( $request['date_to'] ) : '',
81 );
82
83 return rest_ensure_response( RevenueController::get_revenue( $args ) );
84 }
85
86 // =========================================================================
87 // Schema
88 // =========================================================================
89
90 /**
91 * Retrieves the revenue time-series schema, conforming to JSON Schema.
92 *
93 * @return array
94 */
95 public function get_revenue_schema() { //phpcs:ignore
96 return array(
97 '$schema' => 'http://json-schema.org/draft-04/schema#',
98 'title' => 'analytics-revenue-chart',
99 'type' => 'object',
100 'properties' => array(
101 'current_period' => array(
102 'description' => __( 'Current period date range.', 'disco' ),
103 'type' => 'object',
104 'context' => array( 'view' ),
105 'readonly' => true,
106 ),
107 'compare_period' => array(
108 'description' => __( 'Comparison period date range.', 'disco' ),
109 'type' => 'object',
110 'context' => array( 'view' ),
111 'readonly' => true,
112 ),
113 'interval' => array(
114 'description' => __( 'Grouping interval: day, week, or month.', 'disco' ),
115 'type' => 'string',
116 'context' => array( 'view' ),
117 'readonly' => true,
118 ),
119 'data' => array(
120 'description' => __( 'Time-series revenue rows.', 'disco' ),
121 'type' => 'array',
122 'context' => array( 'view' ),
123 'readonly' => true,
124 'items' => array(
125 'type' => 'object',
126 'properties' => array(
127 'date' => array(
128 'description' => __( 'Period start date (Y-m-d).', 'disco' ),
129 'type' => 'string',
130 ),
131 'net_sales' => array(
132 'description' => __( 'Total revenue from all completed orders.', 'disco' ),
133 'type' => 'number',
134 ),
135 'discount_sales' => array(
136 'description' => __( 'Revenue from completed disco campaign orders.', 'disco' ),
137 'type' => 'number',
138 ),
139 'total_orders' => array(
140 'description' => __( 'Count of all completed orders.', 'disco' ),
141 'type' => 'integer',
142 ),
143 'discount_orders' => array(
144 'description' => __( 'Count of completed disco campaign orders.', 'disco' ),
145 'type' => 'integer',
146 ),
147 ),
148 ),
149 ),
150 ),
151 );
152 }
153
154 }
155