Skip to content

Commit

Permalink
Updates to 6.0.33
Browse files Browse the repository at this point in the history
  • Loading branch information
Woo committed Aug 28, 2024
1 parent 8be4be1 commit 128d38e
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 410 deletions.
2 changes: 1 addition & 1 deletion admin/Analytics/Rest_API/Conversions/Stats/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_conversions_stats';

return $schema;
return $this->add_additional_fields_schema( $schema );
}
}
2 changes: 1 addition & 1 deletion admin/Analytics/Rest_API/Conversions/Stats/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function update_sql_query_params( $query_args ) {
* @param int $expected_interval_count Number of expected intervals.
* @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
*/
public function get_noncached_data( $query_args, $params, &$data, $expected_interval_count ) {
public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) {
global $wpdb;
$table_name = self::get_db_table_name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_email_tracking_stats';

return $schema;
return $this->add_additional_fields_schema( $schema );
}
}
4 changes: 2 additions & 2 deletions admin/Analytics/Rest_API/Email_Tracking/Stats_Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ protected function selected_totals( $query_args, $totals ) {
* Returns the report data based on normalized parameters.
* Will be called by `get_data` if there is no data in cache.
*
* @overwrites Log_Stats_Store::get_noncached_data
* @overwrites Log_Stats_Store::get_noncached_stats_data
* @see get_data
* @param array $query_args Query parameters.
* @param array $params Query limit parameters.
* @param stdClass $data Reference to the data object to fill.
* @param int $expected_interval_count Number of expected intervals.
* @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
*/
public function get_noncached_data( $query_args, $params, &$data, $expected_interval_count ) {
public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) {
global $wpdb;

$table_name = self::get_db_table_name();
Expand Down
10 changes: 10 additions & 0 deletions admin/Analytics/Rest_API/Log_Stats_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public function get_collection_params() {

return $params;
}

/**
* Get the report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
public function get_item_properties_schema() {
return array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_unsubscribers_stats';

return $schema;
return $this->add_additional_fields_schema( $schema );
}
}
2 changes: 1 addition & 1 deletion admin/Analytics/Rest_API/Unsubscribers/Stats_Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function assign_report_columns() {
* @param int $expected_interval_count Number of expected intervals.
* @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
*/
public function get_noncached_data( $query_args, $params, &$data, $expected_interval_count ) {
public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) {
global $wpdb;
$table_name = self::get_db_table_name();

Expand Down
155 changes: 8 additions & 147 deletions admin/Analytics/Rest_API/Upstream/Generic_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,31 @@

defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Admin\API\Reports\Query;
use Automattic\WooCommerce\Admin\API\Reports\GenericController as WCGenericController;
use stdClass;
use WC_REST_Reports_Controller;
use WP_REST_Request;
use WP_REST_Response;

/**
* This is a generic class, to cover bits shared by all reports.
* Discovered in https://github.com/woocommerce/automatewoo/pull/1226#pullrequestreview-1210449142
* We may consider moving it eventually to `WC_REST_Reports_Controller`,
* We may consider moving it eventually to `Automattic\WooCommerce\Admin\API\Reports\GenericController`,
* so the other extensions and WC itself could make use of it, and get DRYier.
* https://github.com/woocommerce/automatewoo/issues/1238
*
* @extends WC_REST_Reports_Controller
* @extends WCGenericController
*/
class Generic_Controller extends WC_REST_Reports_Controller {

/**
* Endpoint namespace.
*
* Compatibility-code "WC <= 7.8"
* Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
*
* @var string
*/
protected $namespace = 'wc-analytics';
class Generic_Controller extends WCGenericController {

/**
* Forwards a Query constructor,
* to be able to customize Query class for a specific report.
*
* @param array $query_args Set of args to be forwarded to the constructor.
* @return Query
* @return Generic_Query
*/
protected function construct_query( $query_args ) {
return new Query( $query_args );
return new Generic_Query( $query_args, $this->rest_base );
}

/**
Expand All @@ -51,7 +40,7 @@ protected function construct_query( $query_args ) {
* @param WP_REST_Request $request Full request object.
* @return array Simplified array of params.
*/
protected function prepare_reports_query( $request ) {
public function prepare_reports_query( $request ) {
$args = wp_parse_args(
array_intersect_key(
$request->get_query_params(),
Expand All @@ -63,76 +52,17 @@ protected function prepare_reports_query( $request ) {
return $args;
}

/**
* Add pagination headers and links.
*
* Compatibility-code "WC <= 7.8"
* Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
*
* @param WP_REST_Request $request Request data.
* @param WP_REST_Response|array $response Response data.
* @param int $total Total results.
* @param int $page Current page.
* @param int $max_pages Total amount of pages.
* @return WP_REST_Response
*/
public function add_pagination_headers( $request, $response, int $total, int $page, int $max_pages ) {
$response = rest_ensure_response( $response );
$response->header( 'X-WP-Total', $total );
$response->header( 'X-WP-TotalPages', $max_pages );

// SEMGREP WARNING EXPLANATION
// URL is escaped. However, Semgrep only considers esc_url as valid.
$base = esc_url_raw(
add_query_arg(
$request->get_query_params(),
rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) )
)
);

if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
// SEMGREP WARNING EXPLANATION
// URL is escaped. However, Semgrep only considers esc_url as valid.
$prev_link = esc_url_raw( add_query_arg( 'page', $prev_page, $base ) );
$response->link_header( 'prev', $prev_link );
}

if ( $max_pages > $page ) {
$next_page = $page + 1;
// SEMGREP WARNING EXPLANATION
// URL is escaped. However, Semgrep only considers esc_url as valid.
$next_link = esc_url_raw( add_query_arg( 'page', $next_page, $base ) );
$response->link_header( 'next', $next_link );
}

return $response;
}

/**
* Prepare a report object for serialization.
*
* Compatibility-code "WC <= 7.8"
* Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = get_object_vars( $report );

$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 );

return $response;
return parent::prepare_item_for_response( $data, $request );
}

/**
Expand All @@ -141,9 +71,6 @@ public function prepare_item_for_response( $report, $request ) {
*
* To be extended by specific report properites.
*
* Compatibility-code "WC <= 7.8"
* Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
*
* @return array
*/
public function get_item_properties_schema() {
Expand All @@ -160,70 +87,4 @@ public function get_item_properties_schema() {
public function get_item_schema() {
return $this->add_additional_fields_schema( array() );
}

/**
* Get the query params for collections.
*
* Compatibility-code "WC <= 7.8"
* Once WC > 7.8 is out and covers our L-2, we can inherit this from `GenericController`.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'automatewoo' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'automatewoo' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'automatewoo' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'automatewoo' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'automatewoo' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'automatewoo' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'automatewoo' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);

return $params;
}
}
4 changes: 2 additions & 2 deletions admin/Analytics/Rest_API/Upstream/Generic_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @see Automattic\WooCommerce\Admin\API\Reports\Query
* @since 5.6.9
*/
class Generic_Query extends WooReportsQuery {
class Generic_Query extends \WC_Object_Query {

/**
* Store name
Expand All @@ -45,7 +45,7 @@ class Generic_Query extends WooReportsQuery {
public function __construct( $args, $store_name ) {
$this->store_name = $store_name;

return parent::__construct( $args ); // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
parent::__construct( $args );
}
/**
* Valid fields for Products report.
Expand Down
Loading

0 comments on commit 128d38e

Please sign in to comment.