3
3
4
4
defined ( 'ABSPATH ' ) || exit;
5
5
6
- use Automattic \WooCommerce \Admin \API \Reports \Query ;
6
+ use Automattic \WooCommerce \Admin \API \Reports \GenericController as WCGenericController ;
7
7
use stdClass ;
8
- use WC_REST_Reports_Controller ;
9
8
use WP_REST_Request ;
10
9
use WP_REST_Response ;
11
10
12
11
/**
13
12
* This is a generic class, to cover bits shared by all reports.
14
13
* Discovered in https://github.com/woocommerce/automatewoo/pull/1226#pullrequestreview-1210449142
15
- * We may consider moving it eventually to `WC_REST_Reports_Controller `,
14
+ * We may consider moving it eventually to `Automattic\WooCommerce\Admin\API\Reports\GenericController `,
16
15
* so the other extensions and WC itself could make use of it, and get DRYier.
17
16
* https://github.com/woocommerce/automatewoo/issues/1238
18
17
*
19
- * @extends WC_REST_Reports_Controller
18
+ * @extends WCGenericController
20
19
*/
21
- class Generic_Controller extends WC_REST_Reports_Controller {
22
-
23
- /**
24
- * Endpoint namespace.
25
- *
26
- * Compatibility-code "WC <= 7.8"
27
- * Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
28
- *
29
- * @var string
30
- */
31
- protected $ namespace = 'wc-analytics ' ;
20
+ class Generic_Controller extends WCGenericController {
32
21
33
22
/**
34
23
* Forwards a Query constructor,
35
24
* to be able to customize Query class for a specific report.
36
25
*
37
26
* @param array $query_args Set of args to be forwarded to the constructor.
38
- * @return Query
27
+ * @return Generic_Query
39
28
*/
40
29
protected function construct_query ( $ query_args ) {
41
- return new Query ( $ query_args );
30
+ return new Generic_Query ( $ query_args, $ this -> rest_base );
42
31
}
43
32
44
33
/**
@@ -51,7 +40,7 @@ protected function construct_query( $query_args ) {
51
40
* @param WP_REST_Request $request Full request object.
52
41
* @return array Simplified array of params.
53
42
*/
54
- protected function prepare_reports_query ( $ request ) {
43
+ public function prepare_reports_query ( $ request ) {
55
44
$ args = wp_parse_args (
56
45
array_intersect_key (
57
46
$ request ->get_query_params (),
@@ -63,76 +52,17 @@ protected function prepare_reports_query( $request ) {
63
52
return $ args ;
64
53
}
65
54
66
- /**
67
- * Add pagination headers and links.
68
- *
69
- * Compatibility-code "WC <= 7.8"
70
- * Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
71
- *
72
- * @param WP_REST_Request $request Request data.
73
- * @param WP_REST_Response|array $response Response data.
74
- * @param int $total Total results.
75
- * @param int $page Current page.
76
- * @param int $max_pages Total amount of pages.
77
- * @return WP_REST_Response
78
- */
79
- public function add_pagination_headers ( $ request , $ response , int $ total , int $ page , int $ max_pages ) {
80
- $ response = rest_ensure_response ( $ response );
81
- $ response ->header ( 'X-WP-Total ' , $ total );
82
- $ response ->header ( 'X-WP-TotalPages ' , $ max_pages );
83
-
84
- // SEMGREP WARNING EXPLANATION
85
- // URL is escaped. However, Semgrep only considers esc_url as valid.
86
- $ base = esc_url_raw (
87
- add_query_arg (
88
- $ request ->get_query_params (),
89
- rest_url ( sprintf ( '/%s/%s ' , $ this ->namespace , $ this ->rest_base ) )
90
- )
91
- );
92
-
93
- if ( $ page > 1 ) {
94
- $ prev_page = $ page - 1 ;
95
- if ( $ prev_page > $ max_pages ) {
96
- $ prev_page = $ max_pages ;
97
- }
98
- // SEMGREP WARNING EXPLANATION
99
- // URL is escaped. However, Semgrep only considers esc_url as valid.
100
- $ prev_link = esc_url_raw ( add_query_arg ( 'page ' , $ prev_page , $ base ) );
101
- $ response ->link_header ( 'prev ' , $ prev_link );
102
- }
103
-
104
- if ( $ max_pages > $ page ) {
105
- $ next_page = $ page + 1 ;
106
- // SEMGREP WARNING EXPLANATION
107
- // URL is escaped. However, Semgrep only considers esc_url as valid.
108
- $ next_link = esc_url_raw ( add_query_arg ( 'page ' , $ next_page , $ base ) );
109
- $ response ->link_header ( 'next ' , $ next_link );
110
- }
111
-
112
- return $ response ;
113
- }
114
-
115
55
/**
116
56
* Prepare a report object for serialization.
117
57
*
118
- * Compatibility-code "WC <= 7.8"
119
- * Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
120
- *
121
58
* @param stdClass $report Report data.
122
59
* @param WP_REST_Request $request Request object.
123
60
* @return WP_REST_Response
124
61
*/
125
62
public function prepare_item_for_response ( $ report , $ request ) {
126
63
$ data = get_object_vars ( $ report );
127
64
128
- $ context = ! empty ( $ request ['context ' ] ) ? $ request ['context ' ] : 'view ' ;
129
- $ data = $ this ->add_additional_fields_to_object ( $ data , $ request );
130
- $ data = $ this ->filter_response_by_context ( $ data , $ context );
131
-
132
- // Wrap the data in a response object.
133
- $ response = rest_ensure_response ( $ data );
134
-
135
- return $ response ;
65
+ return parent ::prepare_item_for_response ( $ data , $ request );
136
66
}
137
67
138
68
/**
@@ -141,9 +71,6 @@ public function prepare_item_for_response( $report, $request ) {
141
71
*
142
72
* To be extended by specific report properites.
143
73
*
144
- * Compatibility-code "WC <= 7.8"
145
- * Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
146
- *
147
74
* @return array
148
75
*/
149
76
public function get_item_properties_schema () {
@@ -160,70 +87,4 @@ public function get_item_properties_schema() {
160
87
public function get_item_schema () {
161
88
return $ this ->add_additional_fields_schema ( array () );
162
89
}
163
-
164
- /**
165
- * Get the query params for collections.
166
- *
167
- * Compatibility-code "WC <= 7.8"
168
- * Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
169
- *
170
- * @return array
171
- */
172
- public function get_collection_params () {
173
- $ params = array ();
174
- $ params ['context ' ] = $ this ->get_context_param ( array ( 'default ' => 'view ' ) );
175
- $ params ['page ' ] = array (
176
- 'description ' => __ ( 'Current page of the collection. ' , 'automatewoo ' ),
177
- 'type ' => 'integer ' ,
178
- 'default ' => 1 ,
179
- 'sanitize_callback ' => 'absint ' ,
180
- 'validate_callback ' => 'rest_validate_request_arg ' ,
181
- 'minimum ' => 1 ,
182
- );
183
- $ params ['per_page ' ] = array (
184
- 'description ' => __ ( 'Maximum number of items to be returned in result set. ' , 'automatewoo ' ),
185
- 'type ' => 'integer ' ,
186
- 'default ' => 10 ,
187
- 'minimum ' => 1 ,
188
- 'maximum ' => 100 ,
189
- 'sanitize_callback ' => 'absint ' ,
190
- 'validate_callback ' => 'rest_validate_request_arg ' ,
191
- );
192
- $ params ['after ' ] = array (
193
- 'description ' => __ ( 'Limit response to resources published after a given ISO8601 compliant date. ' , 'automatewoo ' ),
194
- 'type ' => 'string ' ,
195
- 'format ' => 'date-time ' ,
196
- 'validate_callback ' => 'rest_validate_request_arg ' ,
197
- );
198
- $ params ['before ' ] = array (
199
- 'description ' => __ ( 'Limit response to resources published before a given ISO8601 compliant date. ' , 'automatewoo ' ),
200
- 'type ' => 'string ' ,
201
- 'format ' => 'date-time ' ,
202
- 'validate_callback ' => 'rest_validate_request_arg ' ,
203
- );
204
- $ params ['order ' ] = array (
205
- 'description ' => __ ( 'Order sort attribute ascending or descending. ' , 'automatewoo ' ),
206
- 'type ' => 'string ' ,
207
- 'default ' => 'desc ' ,
208
- 'enum ' => array ( 'asc ' , 'desc ' ),
209
- 'validate_callback ' => 'rest_validate_request_arg ' ,
210
- );
211
- $ params ['orderby ' ] = array (
212
- 'description ' => __ ( 'Sort collection by object attribute. ' , 'automatewoo ' ),
213
- 'type ' => 'string ' ,
214
- 'default ' => 'date ' ,
215
- 'enum ' => array (
216
- 'date ' ,
217
- ),
218
- 'validate_callback ' => 'rest_validate_request_arg ' ,
219
- );
220
- $ params ['force_cache_refresh ' ] = array (
221
- 'description ' => __ ( 'Force retrieval of fresh data instead of from the cache. ' , 'automatewoo ' ),
222
- 'type ' => 'boolean ' ,
223
- 'sanitize_callback ' => 'wp_validate_boolean ' ,
224
- 'validate_callback ' => 'rest_validate_request_arg ' ,
225
- );
226
-
227
- return $ params ;
228
- }
229
90
}
0 commit comments