Centralized default before and after parameters.

This commit is contained in:
Peter Fabian 2019-02-10 17:07:45 +01:00
parent 68c65cfde6
commit b3c3aca973
13 changed files with 60 additions and 56 deletions

View File

@ -38,6 +38,32 @@ class WC_Admin_Reports_Interval {
return $datetime;
}
/**
* Returns default 'before' parameter for the reports.
*
* @return DateTime
*/
public static function default_before() {
$datetime = new DateTime();
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
return $datetime;
}
/**
* Returns default 'after' parameter for the reports.
*
* @return DateTime
*/
public static function default_after() {
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
$datetime = new DateTime();
$datetime->setTimestamp( $week_back );
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
return $datetime;
}
/**
* Returns date format to be used as grouping clause in SQL.
*

View File

@ -212,17 +212,15 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'categories' => array(),
'extended_info' => false,

View File

@ -207,8 +207,6 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -216,8 +214,8 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
'page' => 1,
'order' => 'DESC',
'orderby' => 'coupon_id',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'coupons' => array(),
'extended_info' => false,

View File

@ -95,20 +95,18 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'fields' => '*',
'interval' => 'week',
'coupons' => array(),
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'interval' => 'week',
'coupons' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args );

View File

@ -271,8 +271,6 @@ class WC_Admin_Reports_Downloads_Data_Store extends WC_Admin_Reports_Data_Store
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -280,8 +278,8 @@ class WC_Admin_Reports_Downloads_Data_Store extends WC_Admin_Reports_Data_Store
'page' => 1,
'order' => 'DESC',
'orderby' => 'timestamp',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
);
$query_args = wp_parse_args( $query_args, $defaults );

View File

@ -48,8 +48,6 @@ class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downl
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -59,6 +57,8 @@ class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downl
'orderby' => 'date',
'fields' => '*',
'interval' => 'week',
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args );

View File

@ -124,8 +124,6 @@ class WC_Admin_Reports_Orders_Data_Store extends WC_Admin_Reports_Data_Store imp
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -133,8 +131,8 @@ class WC_Admin_Reports_Orders_Data_Store extends WC_Admin_Reports_Data_Store imp
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_created',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'product_includes' => array(),
'product_excludes' => array(),

View File

@ -180,8 +180,6 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only applied when not using REST API, as the API has its own defaults that overwrite these for most values (except before, after, etc).
$defaults = array(
@ -189,8 +187,8 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'interval' => 'week',
'fields' => '*',
'segmentby' => '',

View File

@ -215,8 +215,6 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -224,8 +222,8 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'categories' => array(),
'product_includes' => array(),

View File

@ -103,8 +103,6 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -112,8 +110,8 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'categories' => array(),
'interval' => 'week',

View File

@ -135,8 +135,6 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -144,8 +142,8 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
'page' => 1,
'order' => 'DESC',
'orderby' => 'tax_rate_id',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'taxes' => array(),
);

View File

@ -112,8 +112,6 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
@ -121,8 +119,8 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
'page' => 1,
'order' => 'DESC',
'orderby' => 'tax_rate_id',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'taxes' => array(),
);

View File

@ -188,17 +188,15 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'products' => array(),
'variations' => array(),