class-wc-rest-report-coupons-totals-controller.php
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* REST API Reports Coupons Totals controller
*
* Handles requests to the /reports/coupons/count endpoint.
*
* @package WooCommerce\RestApi
* @since 3.5.0
*/
defined( 'ABSPATH' ) || exit;
/**
* REST API Reports Coupons Totals controller class.
*
* @package WooCommerce\RestApi
* @extends WC_REST_Reports_Controller
*/
class WC_REST_Report_Coupons_Totals_Controller extends WC_REST_Reports_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v3';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/coupons/totals';
/**
* Get reports list.
*
* @since 3.5.0
* @return array
*/
protected function get_reports() {
global $wpdb;
$data = get_transient( 'rest_api_coupons_type_count' );
if ( false !== $data ) {
return $data;
}
$types = wc_get_coupon_types();
$data = array();
foreach ( $types as $slug => $name ) {
$results = $wpdb->get_results(
$wpdb->prepare( "
SELECT count(meta_id) AS total
FROM $wpdb->postmeta
WHERE meta_key = 'discount_type'
AND meta_value = %s
", $slug )
);
$total = isset( $results[0] ) ? (int) $results[0]->total : 0;
$data[] = array(
'slug' => $slug,
'name' => $name,
'total' => $total,
);
}
set_transient( 'rest_api_coupons_type_count', $data, YEAR_IN_SECONDS );
return $data;
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $report, $request ) {
$data = array(
'slug' => $report->slug,
'name' => $report->name,
'total' => $report->total,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_coupons_count', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_coupon_total',
'type' => 'object',
'properties' => array(
'slug' => array(
'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Coupon type name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'total' => array(
'description' => __( 'Amount of coupons.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}