33
44defined ( 'ABSPATH ' ) || exit;
55
6- use Automattic \WooCommerce \Admin \API \Reports \Query ;
6+ use Automattic \WooCommerce \Admin \API \Reports \GenericController as WCGenericController ;
77use stdClass ;
8- use WC_REST_Reports_Controller ;
98use WP_REST_Request ;
109use WP_REST_Response ;
1110
1211/**
1312 * This is a generic class, to cover bits shared by all reports.
1413 * 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 `,
1615 * so the other extensions and WC itself could make use of it, and get DRYier.
1716 * https://github.com/woocommerce/automatewoo/issues/1238
1817 *
19- * @extends WC_REST_Reports_Controller
18+ * @extends WCGenericController
2019 */
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 {
3221
3322 /**
3423 * Forwards a Query constructor,
3524 * to be able to customize Query class for a specific report.
3625 *
3726 * @param array $query_args Set of args to be forwarded to the constructor.
38- * @return Query
27+ * @return Generic_Query
3928 */
4029 protected function construct_query ( $ query_args ) {
41- return new Query ( $ query_args );
30+ return new Generic_Query ( $ query_args, $ this -> rest_base );
4231 }
4332
4433 /**
@@ -51,7 +40,7 @@ protected function construct_query( $query_args ) {
5140 * @param WP_REST_Request $request Full request object.
5241 * @return array Simplified array of params.
5342 */
54- protected function prepare_reports_query ( $ request ) {
43+ public function prepare_reports_query ( $ request ) {
5544 $ args = wp_parse_args (
5645 array_intersect_key (
5746 $ request ->get_query_params (),
@@ -63,76 +52,17 @@ protected function prepare_reports_query( $request ) {
6352 return $ args ;
6453 }
6554
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-
11555 /**
11656 * Prepare a report object for serialization.
11757 *
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- *
12158 * @param stdClass $report Report data.
12259 * @param WP_REST_Request $request Request object.
12360 * @return WP_REST_Response
12461 */
12562 public function prepare_item_for_response ( $ report , $ request ) {
12663 $ data = get_object_vars ( $ report );
12764
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 );
13666 }
13767
13868 /**
@@ -141,9 +71,6 @@ public function prepare_item_for_response( $report, $request ) {
14171 *
14272 * To be extended by specific report properites.
14373 *
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- *
14774 * @return array
14875 */
14976 public function get_item_properties_schema () {
@@ -160,70 +87,4 @@ public function get_item_properties_schema() {
16087 public function get_item_schema () {
16188 return $ this ->add_additional_fields_schema ( array () );
16289 }
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- }
22990}
0 commit comments