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 / SummaryApi.php

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

124 lines 3.5 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 — Summary 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\SummaryController;
15 use WP_REST_Server;
16
17 /**
18 * Class SummaryApi
19 *
20 * Registers and handles the following REST API route:
21 *
22 * - GET /disco/v1/analytics/summary — Top-level KPI summary with growth comparison.
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 SummaryApi extends Base {
32
33 /**
34 * Registers the REST API routes for the summary endpoint.
35 */
36 public function register_routes(): void {
37 // GET /analytics/summary
38 register_rest_route(
39 $this->namespace,
40 '/' . $this->rest_base . '/summary',
41 array(
42 array(
43 'methods' => WP_REST_Server::READABLE,
44 'callback' => array( $this, 'get_summary' ),
45 'permission_callback' => array( $this, 'permissions_check' ),
46 'args' => $this->get_summary_params(),
47 ),
48 'schema' => array( $this, 'get_summary_schema' ),
49 )
50 );
51 }
52
53 // =========================================================================
54 // Callbacks
55 // =========================================================================
56
57 /**
58 * GET /disco/v1/analytics/summary
59 *
60 * Returns top-level KPI summary with growth comparison against the previous period
61 * of the same duration.
62 *
63 * Query params:
64 * date_from (string Y-m-d) Start of the current period. Default: 30 days ago.
65 * date_to (string Y-m-d) End of the current period. Default: today.
66 * compare (string) Only accepted value: previous_period (default).
67 *
68 * @param \WP_REST_Request $request Incoming REST request.
69 * @return \WP_REST_Response|\WP_Error
70 */
71 public function get_summary( $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( SummaryController::get_summary( $args ) );
84 }
85
86 // =========================================================================
87 // Schema
88 // =========================================================================
89
90 /**
91 * Retrieves the summary schema, conforming to JSON Schema.
92 *
93 * @return array
94 */
95 public function get_summary_schema() { //phpcs:ignore
96 return array(
97 '$schema' => 'http://json-schema.org/draft-04/schema#',
98 'title' => 'analytics-summary',
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 'data' => array(
114 'description' => __( 'KPI values with growth comparison.', 'disco' ),
115 'type' => 'object',
116 'context' => array( 'view' ),
117 'readonly' => true,
118 ),
119 ),
120 );
121 }
122
123 }
124