Skip to content

Commit 128d38e

Browse files
author
Woo
committed
Updates to 6.0.33
1 parent 8be4be1 commit 128d38e

20 files changed

+130
-410
lines changed

admin/Analytics/Rest_API/Conversions/Stats/Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ public function get_item_schema() {
7171
$schema = parent::get_item_schema();
7272
$schema['title'] = 'report_conversions_stats';
7373

74-
return $schema;
74+
return $this->add_additional_fields_schema( $schema );
7575
}
7676
}

admin/Analytics/Rest_API/Conversions/Stats/Store.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function update_sql_query_params( $query_args ) {
125125
* @param int $expected_interval_count Number of expected intervals.
126126
* @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
127127
*/
128-
public function get_noncached_data( $query_args, $params, &$data, $expected_interval_count ) {
128+
public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) {
129129
global $wpdb;
130130
$table_name = self::get_db_table_name();
131131

admin/Analytics/Rest_API/Email_Tracking/Stats_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public function get_item_schema() {
7373
$schema = parent::get_item_schema();
7474
$schema['title'] = 'report_email_tracking_stats';
7575

76-
return $schema;
76+
return $this->add_additional_fields_schema( $schema );
7777
}
7878
}

admin/Analytics/Rest_API/Email_Tracking/Stats_Store.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ protected function selected_totals( $query_args, $totals ) {
9797
* Returns the report data based on normalized parameters.
9898
* Will be called by `get_data` if there is no data in cache.
9999
*
100-
* @overwrites Log_Stats_Store::get_noncached_data
100+
* @overwrites Log_Stats_Store::get_noncached_stats_data
101101
* @see get_data
102102
* @param array $query_args Query parameters.
103103
* @param array $params Query limit parameters.
104104
* @param stdClass $data Reference to the data object to fill.
105105
* @param int $expected_interval_count Number of expected intervals.
106106
* @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
107107
*/
108-
public function get_noncached_data( $query_args, $params, &$data, $expected_interval_count ) {
108+
public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) {
109109
global $wpdb;
110110

111111
$table_name = self::get_db_table_name();

admin/Analytics/Rest_API/Log_Stats_Controller.php

+10
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public function get_collection_params() {
4444

4545
return $params;
4646
}
47+
48+
/**
49+
* Get the report's item properties schema.
50+
* Will be used by `get_item_schema` as `totals` and `subtotals`.
51+
*
52+
* @return array
53+
*/
54+
public function get_item_properties_schema() {
55+
return array();
56+
}
4757
}

admin/Analytics/Rest_API/Unsubscribers/Stats_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public function get_item_schema() {
5555
$schema = parent::get_item_schema();
5656
$schema['title'] = 'report_unsubscribers_stats';
5757

58-
return $schema;
58+
return $this->add_additional_fields_schema( $schema );
5959
}
6060
}

admin/Analytics/Rest_API/Unsubscribers/Stats_Store.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function assign_report_columns() {
7979
* @param int $expected_interval_count Number of expected intervals.
8080
* @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
8181
*/
82-
public function get_noncached_data( $query_args, $params, &$data, $expected_interval_count ) {
82+
public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) {
8383
global $wpdb;
8484
$table_name = self::get_db_table_name();
8585

admin/Analytics/Rest_API/Upstream/Generic_Controller.php

+8-147
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,31 @@
33

44
defined( 'ABSPATH' ) || exit;
55

6-
use Automattic\WooCommerce\Admin\API\Reports\Query;
6+
use Automattic\WooCommerce\Admin\API\Reports\GenericController as WCGenericController;
77
use stdClass;
8-
use WC_REST_Reports_Controller;
98
use WP_REST_Request;
109
use 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
}

admin/Analytics/Rest_API/Upstream/Generic_Query.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @see Automattic\WooCommerce\Admin\API\Reports\Query
2727
* @since 5.6.9
2828
*/
29-
class Generic_Query extends WooReportsQuery {
29+
class Generic_Query extends \WC_Object_Query {
3030

3131
/**
3232
* Store name
@@ -45,7 +45,7 @@ class Generic_Query extends WooReportsQuery {
4545
public function __construct( $args, $store_name ) {
4646
$this->store_name = $store_name;
4747

48-
return parent::__construct( $args ); // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
48+
parent::__construct( $args );
4949
}
5050
/**
5151
* Valid fields for Products report.

0 commit comments

Comments
 (0)