DataStore.php
231 lines
| 1 | <?php |
| 2 | /** |
| 3 | * API\Reports\Coupons\Stats\DataStore class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | use Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore; |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\TimeInterval; |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\StatsDataStoreTrait; |
| 13 | |
| 14 | /** |
| 15 | * API\Reports\Coupons\Stats\DataStore. |
| 16 | */ |
| 17 | class DataStore extends CouponsDataStore implements DataStoreInterface { |
| 18 | use StatsDataStoreTrait; |
| 19 | |
| 20 | /** |
| 21 | * Mapping columns to data type to return correct response types. |
| 22 | * |
| 23 | * @override CouponsDataStore::$column_types |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | protected $column_types = array( |
| 28 | 'date_start' => 'strval', |
| 29 | 'date_end' => 'strval', |
| 30 | 'date_start_gmt' => 'strval', |
| 31 | 'date_end_gmt' => 'strval', |
| 32 | 'amount' => 'floatval', |
| 33 | 'coupons_count' => 'intval', |
| 34 | 'orders_count' => 'intval', |
| 35 | ); |
| 36 | |
| 37 | /** |
| 38 | * SQL columns to select in the db query. |
| 39 | * |
| 40 | * @override CouponsDataStore::$report_columns |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $report_columns; |
| 45 | |
| 46 | /** |
| 47 | * Data store context used to pass to filters. |
| 48 | * |
| 49 | * @override CouponsDataStore::$context |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $context = 'coupons_stats'; |
| 54 | |
| 55 | /** |
| 56 | * Cache identifier. |
| 57 | * |
| 58 | * @override CouponsDataStore::get_default_query_vars() |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | protected $cache_key = 'coupons_stats'; |
| 63 | |
| 64 | /** |
| 65 | * Assign report columns once full table name has been assigned. |
| 66 | * |
| 67 | * @override CouponsDataStore::assign_report_columns() |
| 68 | */ |
| 69 | protected function assign_report_columns() { |
| 70 | $table_name = self::get_db_table_name(); |
| 71 | $this->report_columns = array( |
| 72 | 'amount' => 'SUM(discount_amount) as amount', |
| 73 | 'coupons_count' => 'COUNT(DISTINCT coupon_id) as coupons_count', |
| 74 | 'orders_count' => "COUNT(DISTINCT {$table_name}.order_id) as orders_count", |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Updates the database query with parameters used for Products Stats report: categories and order status. |
| 80 | * |
| 81 | * @param array $query_args Query arguments supplied by the user. |
| 82 | */ |
| 83 | protected function update_sql_query_params( $query_args ) { |
| 84 | global $wpdb; |
| 85 | |
| 86 | $clauses = array( |
| 87 | 'where' => '', |
| 88 | 'join' => '', |
| 89 | ); |
| 90 | |
| 91 | $order_coupon_lookup_table = self::get_db_table_name(); |
| 92 | |
| 93 | $included_coupons = $this->get_included_coupons( $query_args, 'coupons' ); |
| 94 | if ( $included_coupons ) { |
| 95 | $clauses['where'] .= " AND {$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})"; |
| 96 | } |
| 97 | |
| 98 | $order_status_filter = $this->get_status_subquery( $query_args ); |
| 99 | if ( $order_status_filter ) { |
| 100 | $clauses['join'] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_coupon_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id"; |
| 101 | $clauses['where'] .= " AND ( {$order_status_filter} )"; |
| 102 | } |
| 103 | |
| 104 | $this->add_time_period_sql_params( $query_args, $order_coupon_lookup_table ); |
| 105 | $this->add_intervals_sql_params( $query_args, $order_coupon_lookup_table ); |
| 106 | $clauses['where_time'] = $this->get_sql_clause( 'where_time' ); |
| 107 | |
| 108 | $this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) ); |
| 109 | $this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) ); |
| 110 | $this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) ); |
| 111 | $this->interval_query->add_sql_clause( 'select', 'AS time_interval' ); |
| 112 | |
| 113 | foreach ( array( 'join', 'where_time', 'where' ) as $clause ) { |
| 114 | $this->interval_query->add_sql_clause( $clause, $clauses[ $clause ] ); |
| 115 | $this->total_query->add_sql_clause( $clause, $clauses[ $clause ] ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the default query arguments to be used by get_data(). |
| 121 | * These defaults are only partially applied when used via REST API, as that has its own defaults. |
| 122 | * |
| 123 | * @override CouponsDataStore::get_default_query_vars() |
| 124 | * |
| 125 | * @return array Query parameters. |
| 126 | */ |
| 127 | public function get_default_query_vars() { |
| 128 | $defaults = parent::get_default_query_vars(); |
| 129 | $defaults['coupons'] = array(); |
| 130 | $defaults['interval'] = 'week'; |
| 131 | |
| 132 | return $defaults; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the report data based on normalized parameters. |
| 137 | * Will be called by `get_data` if there is no data in cache. |
| 138 | * |
| 139 | * @override CouponsDataStore::get_noncached_stats_data() |
| 140 | * |
| 141 | * @see get_data |
| 142 | * @see get_noncached_stats_data |
| 143 | * @param array $query_args Query parameters. |
| 144 | * @param array $params Query limit parameters. |
| 145 | * @param stdClass $data Reference to the data object to fill. |
| 146 | * @param int $expected_interval_count Number of expected intervals. |
| 147 | * @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error. |
| 148 | */ |
| 149 | public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) { |
| 150 | global $wpdb; |
| 151 | |
| 152 | $table_name = self::get_db_table_name(); |
| 153 | |
| 154 | $this->initialize_queries(); |
| 155 | |
| 156 | $selections = $this->selected_columns( $query_args ); |
| 157 | $totals_query = array(); |
| 158 | $intervals_query = array(); |
| 159 | $limit_params = $this->get_limit_sql_params( $query_args ); |
| 160 | $this->update_sql_query_params( $query_args, $totals_query, $intervals_query ); |
| 161 | |
| 162 | $db_intervals = $wpdb->get_col( |
| 163 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- cache ok, DB call ok, unprepared SQL ok. |
| 164 | $this->interval_query->get_query_statement() |
| 165 | ); |
| 166 | |
| 167 | $db_interval_count = count( $db_intervals ); |
| 168 | |
| 169 | $this->total_query->add_sql_clause( 'select', $selections ); |
| 170 | $totals = $wpdb->get_results( |
| 171 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- cache ok, DB call ok, unprepared SQL ok. |
| 172 | $this->total_query->get_query_statement(), |
| 173 | ARRAY_A |
| 174 | ); |
| 175 | |
| 176 | if ( null === $totals ) { |
| 177 | return $data; |
| 178 | } |
| 179 | |
| 180 | // phpcs:ignore Generic.Commenting.Todo.TaskFound |
| 181 | // @todo remove these assignments when refactoring segmenter classes to use query objects. |
| 182 | $totals_query = array( |
| 183 | 'from_clause' => $this->total_query->get_sql_clause( 'join' ), |
| 184 | 'where_time_clause' => $this->total_query->get_sql_clause( 'where_time' ), |
| 185 | 'where_clause' => $this->total_query->get_sql_clause( 'where' ), |
| 186 | ); |
| 187 | $intervals_query = array( |
| 188 | 'select_clause' => $this->get_sql_clause( 'select' ), |
| 189 | 'from_clause' => $this->interval_query->get_sql_clause( 'join' ), |
| 190 | 'where_time_clause' => $this->interval_query->get_sql_clause( 'where_time' ), |
| 191 | 'where_clause' => $this->interval_query->get_sql_clause( 'where' ), |
| 192 | 'limit' => $this->get_sql_clause( 'limit' ), |
| 193 | ); |
| 194 | $segmenter = new Segmenter( $query_args, $this->report_columns ); |
| 195 | $totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name ); |
| 196 | $totals = (object) $this->cast_numbers( $totals[0] ); |
| 197 | |
| 198 | // Intervals. |
| 199 | $this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name ); |
| 200 | $this->interval_query->add_sql_clause( 'select', ", MAX({$table_name}.date_created) AS datetime_anchor" ); |
| 201 | |
| 202 | if ( '' !== $selections ) { |
| 203 | $this->interval_query->add_sql_clause( 'select', ', ' . $selections ); |
| 204 | } |
| 205 | |
| 206 | $intervals = $wpdb->get_results( |
| 207 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- cache ok, DB call ok, unprepared SQL ok. |
| 208 | $this->interval_query->get_query_statement(), |
| 209 | ARRAY_A |
| 210 | ); |
| 211 | |
| 212 | if ( null === $intervals ) { |
| 213 | return $data; |
| 214 | } |
| 215 | |
| 216 | $data->totals = $totals; |
| 217 | $data->intervals = $intervals; |
| 218 | |
| 219 | if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $limit_params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { |
| 220 | $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); |
| 221 | $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); |
| 222 | $this->remove_extra_records( $data, $query_args['page'], $limit_params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); |
| 223 | } else { |
| 224 | $this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals ); |
| 225 | } |
| 226 | $segmenter->add_intervals_segments( $data, $intervals_query, $table_name ); |
| 227 | |
| 228 | return $data; |
| 229 | } |
| 230 | } |
| 231 |