2018-12-05 12:16:46 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WC_Admin_Reports_Coupons_Stats_Data_Store class file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/Classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Reports_Coupons_Stats_Data_Store.
|
|
|
|
*/
|
|
|
|
class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mapping columns to data type to return correct response types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $column_types = array(
|
|
|
|
'date_start' => 'strval',
|
|
|
|
'date_end' => 'strval',
|
|
|
|
'date_start_gmt' => 'strval',
|
|
|
|
'date_end_gmt' => 'strval',
|
2018-12-14 11:40:49 +00:00
|
|
|
'amount' => 'floatval',
|
2018-12-05 12:16:46 +00:00
|
|
|
'coupons_count' => 'intval',
|
|
|
|
'orders_count' => 'intval',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL columns to select in the db query.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $report_columns = array(
|
2018-12-14 11:40:49 +00:00
|
|
|
'amount' => 'SUM(discount_amount) as amount',
|
|
|
|
'coupons_count' => 'COUNT(DISTINCT coupon_id) as coupons_count',
|
|
|
|
'orders_count' => 'COUNT(DISTINCT order_id) as orders_count',
|
2018-12-05 12:16:46 +00:00
|
|
|
);
|
|
|
|
|
2019-01-16 02:23:00 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
global $wpdb;
|
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
// Avoid ambigious column order_id in SQL query.
|
|
|
|
$this->report_columns['orders_count'] = str_replace( 'order_id', $table_name . '.order_id', $this->report_columns['orders_count'] );
|
|
|
|
}
|
|
|
|
|
2018-12-05 12:16:46 +00:00
|
|
|
/**
|
|
|
|
* Updates the database query with parameters used for Products Stats report: categories and order status.
|
|
|
|
*
|
|
|
|
* @param array $query_args Query arguments supplied by the user.
|
|
|
|
* @param array $totals_params SQL parameters for the totals query.
|
|
|
|
* @param array $intervals_params SQL parameters for the intervals query.
|
|
|
|
*/
|
|
|
|
protected function update_sql_query_params( $query_args, &$totals_params, &$intervals_params ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$coupons_where_clause = '';
|
|
|
|
$coupons_from_clause = '';
|
|
|
|
|
|
|
|
$order_coupon_lookup_table = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
|
|
|
|
$included_coupons = $this->get_included_coupons( $query_args );
|
|
|
|
if ( $included_coupons ) {
|
|
|
|
$coupons_where_clause .= " AND {$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})";
|
|
|
|
}
|
|
|
|
|
|
|
|
$order_status_filter = $this->get_status_subquery( $query_args );
|
|
|
|
if ( $order_status_filter ) {
|
2019-01-16 02:23:00 +00:00
|
|
|
$coupons_from_clause .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_coupon_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
|
2018-12-05 12:16:46 +00:00
|
|
|
$coupons_where_clause .= " AND ( {$order_status_filter} )";
|
|
|
|
}
|
|
|
|
|
|
|
|
$totals_params = array_merge( $totals_params, $this->get_time_period_sql_params( $query_args, $order_coupon_lookup_table ) );
|
|
|
|
$totals_params['where_clause'] .= $coupons_where_clause;
|
|
|
|
$totals_params['from_clause'] .= $coupons_from_clause;
|
|
|
|
|
|
|
|
$intervals_params = array_merge( $intervals_params, $this->get_intervals_sql_params( $query_args, $order_coupon_lookup_table ) );
|
|
|
|
$intervals_params['where_clause'] .= $coupons_where_clause;
|
|
|
|
$intervals_params['from_clause'] .= $coupons_from_clause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the report data based on parameters supplied by the user.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @param array $query_args Query parameters.
|
|
|
|
* @return stdClass|WP_Error Data.
|
|
|
|
*/
|
|
|
|
public function get_data( $query_args ) {
|
|
|
|
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',
|
2018-12-19 12:37:54 +00:00
|
|
|
'coupons' => array(),
|
2018-12-05 12:16:46 +00:00
|
|
|
);
|
|
|
|
$query_args = wp_parse_args( $query_args, $defaults );
|
2019-02-08 17:11:04 +00:00
|
|
|
$this->normalize_timezones( $query_args );
|
2018-12-05 12:16:46 +00:00
|
|
|
|
|
|
|
$cache_key = $this->get_cache_key( $query_args );
|
|
|
|
$data = wp_cache_get( $cache_key, $this->cache_group );
|
|
|
|
|
|
|
|
if ( false === $data ) {
|
|
|
|
$data = (object) array(
|
|
|
|
'data' => array(),
|
|
|
|
'total' => 0,
|
|
|
|
'pages' => 0,
|
|
|
|
'page_no' => 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
$selections = $this->selected_columns( $query_args );
|
|
|
|
$totals_query = array();
|
|
|
|
$intervals_query = array();
|
|
|
|
$this->update_sql_query_params( $query_args, $totals_query, $intervals_query );
|
|
|
|
|
|
|
|
$db_intervals = $wpdb->get_col(
|
|
|
|
"SELECT
|
|
|
|
{$intervals_query['select_clause']} AS time_interval
|
|
|
|
FROM
|
|
|
|
{$table_name}
|
|
|
|
{$intervals_query['from_clause']}
|
|
|
|
WHERE
|
|
|
|
1=1
|
|
|
|
{$intervals_query['where_time_clause']}
|
|
|
|
{$intervals_query['where_clause']}
|
|
|
|
GROUP BY
|
|
|
|
time_interval"
|
|
|
|
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
|
|
|
|
|
|
|
$db_interval_count = count( $db_intervals );
|
2018-12-06 09:40:59 +00:00
|
|
|
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
|
|
|
|
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
|
2018-12-05 12:16:46 +00:00
|
|
|
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$totals = $wpdb->get_results(
|
|
|
|
"SELECT
|
|
|
|
{$selections}
|
|
|
|
FROM
|
|
|
|
{$table_name}
|
|
|
|
{$totals_query['from_clause']}
|
|
|
|
WHERE
|
|
|
|
1=1
|
|
|
|
{$totals_query['where_time_clause']}
|
|
|
|
{$totals_query['where_clause']}",
|
|
|
|
ARRAY_A
|
|
|
|
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
|
|
|
|
|
|
|
if ( null === $totals ) {
|
|
|
|
return $data;
|
|
|
|
}
|
2019-01-18 16:11:46 +00:00
|
|
|
$segmenter = new WC_Admin_Reports_Coupons_Stats_Segmenting( $query_args, $this->report_columns );
|
|
|
|
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
|
|
|
|
$totals = (object) $this->cast_numbers( $totals[0] );
|
2018-12-05 12:16:46 +00:00
|
|
|
|
2018-12-06 09:40:59 +00:00
|
|
|
// Intervals.
|
2019-01-16 02:23:00 +00:00
|
|
|
$this->update_intervals_sql_params( $intervals_query, $query_args, $db_interval_count, $expected_interval_count, $table_name );
|
2018-12-05 12:16:46 +00:00
|
|
|
|
|
|
|
if ( '' !== $selections ) {
|
|
|
|
$selections = ', ' . $selections;
|
|
|
|
}
|
|
|
|
|
|
|
|
$intervals = $wpdb->get_results(
|
|
|
|
"SELECT
|
2019-01-16 02:23:00 +00:00
|
|
|
MAX({$table_name}.date_created) AS datetime_anchor,
|
2018-12-05 12:16:46 +00:00
|
|
|
{$intervals_query['select_clause']} AS time_interval
|
|
|
|
{$selections}
|
|
|
|
FROM
|
|
|
|
{$table_name}
|
|
|
|
{$intervals_query['from_clause']}
|
|
|
|
WHERE
|
|
|
|
1=1
|
|
|
|
{$intervals_query['where_time_clause']}
|
|
|
|
{$intervals_query['where_clause']}
|
|
|
|
GROUP BY
|
|
|
|
time_interval
|
|
|
|
ORDER BY
|
|
|
|
{$intervals_query['order_by_clause']}
|
|
|
|
{$intervals_query['limit']}",
|
|
|
|
ARRAY_A
|
|
|
|
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
|
|
|
|
|
|
|
if ( null === $intervals ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:40:59 +00:00
|
|
|
$data = (object) array(
|
|
|
|
'totals' => $totals,
|
|
|
|
'intervals' => $intervals,
|
|
|
|
'total' => $expected_interval_count,
|
|
|
|
'pages' => $total_pages,
|
|
|
|
'page_no' => (int) $query_args['page'],
|
|
|
|
);
|
2018-12-05 12:16:46 +00:00
|
|
|
|
|
|
|
if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
|
|
|
|
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
|
|
|
|
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
|
2019-01-23 16:18:39 +00:00
|
|
|
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
|
2018-12-05 12:16:46 +00:00
|
|
|
} else {
|
|
|
|
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
|
|
|
|
}
|
2019-01-18 16:11:46 +00:00
|
|
|
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
|
2018-12-05 12:16:46 +00:00
|
|
|
$this->create_interval_subtotals( $data->intervals );
|
|
|
|
|
|
|
|
wp_cache_set( $cache_key, $data, $this->cache_group );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-12-14 13:13:57 +00:00
|
|
|
/**
|
|
|
|
* Returns string to be used as cache key for the data.
|
|
|
|
*
|
|
|
|
* @param array $params Query parameters.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_cache_key( $params ) {
|
|
|
|
return 'woocommerce_' . self::TABLE_NAME . '_stats_' . md5( wp_json_encode( $params ) );
|
|
|
|
}
|
2018-12-05 12:16:46 +00:00
|
|
|
}
|