DataStore.php
177 lines
| 1 | <?php |
| 2 | /** |
| 3 | * API\Reports\Downloads\Stats\DataStore class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore as DownloadsDataStore; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface; |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\TimeInterval; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\StatsDataStoreTrait; |
| 14 | |
| 15 | /** |
| 16 | * API\Reports\Downloads\Stats\DataStore. |
| 17 | */ |
| 18 | class DataStore extends DownloadsDataStore implements DataStoreInterface { |
| 19 | use StatsDataStoreTrait; |
| 20 | |
| 21 | /** |
| 22 | * Mapping columns to data type to return correct response types. |
| 23 | * |
| 24 | * @override DownloadsDataStore::$column_types |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $column_types = array( |
| 29 | 'download_count' => 'intval', |
| 30 | ); |
| 31 | |
| 32 | /** |
| 33 | * Cache identifier. |
| 34 | * |
| 35 | * @override DownloadsDataStore::$cache_key |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | protected $cache_key = 'downloads_stats'; |
| 40 | |
| 41 | /** |
| 42 | * Data store context used to pass to filters. |
| 43 | * |
| 44 | * @override DownloadsDataStore::$context |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $context = 'downloads_stats'; |
| 49 | |
| 50 | /** |
| 51 | * Assign report columns once full table name has been assigned. |
| 52 | * |
| 53 | * @override DownloadsDataStore::assign_report_columns() |
| 54 | */ |
| 55 | protected function assign_report_columns() { |
| 56 | $this->report_columns = array( |
| 57 | 'download_count' => 'COUNT(DISTINCT download_log_id) as download_count', |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the default query arguments to be used by get_data(). |
| 63 | * These defaults are only partially applied when used via REST API, as that has its own defaults. |
| 64 | * |
| 65 | * @override DownloadsDataStore::default_query_args() |
| 66 | * |
| 67 | * @return array Query parameters. |
| 68 | */ |
| 69 | public function get_default_query_vars() { |
| 70 | $defaults = parent::get_default_query_vars(); |
| 71 | $defaults['interval'] = 'week'; |
| 72 | |
| 73 | return $defaults; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the report data based on normalized parameters. |
| 78 | * Will be called by `get_data` if there is no data in cache. |
| 79 | * |
| 80 | * @override DownloadsDataStore::get_noncached_data() |
| 81 | * |
| 82 | * @see get_data |
| 83 | * @see get_noncached_stats_data |
| 84 | * @param array $query_args Query parameters. |
| 85 | * @param array $params Query limit parameters. |
| 86 | * @param stdClass $data Reference to the data object to fill. |
| 87 | * @param int $expected_interval_count Number of expected intervals. |
| 88 | * @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error. |
| 89 | */ |
| 90 | public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) { |
| 91 | global $wpdb; |
| 92 | |
| 93 | $table_name = self::get_db_table_name(); |
| 94 | |
| 95 | $this->initialize_queries(); |
| 96 | $selections = $this->selected_columns( $query_args ); |
| 97 | $this->add_sql_query_params( $query_args ); |
| 98 | $where_time = $this->add_time_period_sql_params( $query_args, $table_name ); |
| 99 | $this->add_intervals_sql_params( $query_args, $table_name ); |
| 100 | |
| 101 | $this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' ); |
| 102 | $this->interval_query->str_replace_clause( 'select', 'date_created', 'timestamp' ); |
| 103 | $this->interval_query->str_replace_clause( 'where_time', 'date_created', 'timestamp' ); |
| 104 | |
| 105 | $db_intervals = $wpdb->get_col( |
| 106 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- cache ok, DB call ok, unprepared SQL ok. |
| 107 | $this->interval_query->get_query_statement() |
| 108 | ); |
| 109 | |
| 110 | $db_records_count = count( $db_intervals ); |
| 111 | |
| 112 | $this->update_intervals_sql_params( $query_args, $db_records_count, $expected_interval_count, $table_name ); |
| 113 | $this->interval_query->str_replace_clause( 'where_time', 'date_created', 'timestamp' ); |
| 114 | $this->total_query->add_sql_clause( 'select', $selections ); |
| 115 | $this->total_query->add_sql_clause( 'where', $this->interval_query->get_sql_clause( 'where' ) ); |
| 116 | if ( $where_time ) { |
| 117 | $this->total_query->add_sql_clause( 'where_time', $where_time ); |
| 118 | } |
| 119 | $totals = $wpdb->get_results( |
| 120 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- cache ok, DB call ok, unprepared SQL ok. |
| 121 | $this->total_query->get_query_statement(), |
| 122 | ARRAY_A |
| 123 | ); |
| 124 | if ( null === $totals ) { |
| 125 | return new \WP_Error( 'woocommerce_analytics_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce' ) ); |
| 126 | } |
| 127 | |
| 128 | $this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) ); |
| 129 | $this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) ); |
| 130 | $this->interval_query->add_sql_clause( 'select', ', MAX(timestamp) AS datetime_anchor' ); |
| 131 | if ( '' !== $selections ) { |
| 132 | $this->interval_query->add_sql_clause( 'select', ', ' . $selections ); |
| 133 | } |
| 134 | |
| 135 | $intervals = $wpdb->get_results( |
| 136 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- cache ok, DB call ok, unprepared SQL ok. |
| 137 | $this->interval_query->get_query_statement(), |
| 138 | ARRAY_A |
| 139 | ); |
| 140 | |
| 141 | if ( null === $intervals ) { |
| 142 | return new \WP_Error( 'woocommerce_analytics_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce' ) ); |
| 143 | } |
| 144 | |
| 145 | $totals = (object) $this->cast_numbers( $totals[0] ); |
| 146 | |
| 147 | $data->totals = $totals; |
| 148 | $data->intervals = $intervals; |
| 149 | |
| 150 | if ( $this->intervals_missing( $expected_interval_count, $db_records_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { |
| 151 | $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); |
| 152 | $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); |
| 153 | $this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_records_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); |
| 154 | } else { |
| 155 | $this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals ); |
| 156 | } |
| 157 | |
| 158 | return $data; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Normalizes order_by clause to match to SQL query. |
| 163 | * |
| 164 | * @override DownloadsDataStore::normalize_order_by() |
| 165 | * |
| 166 | * @param string $order_by Order by option requeste by user. |
| 167 | * @return string |
| 168 | */ |
| 169 | protected function normalize_order_by( $order_by ) { |
| 170 | if ( 'date' === $order_by ) { |
| 171 | return 'time_interval'; |
| 172 | } |
| 173 | |
| 174 | return $order_by; |
| 175 | } |
| 176 | } |
| 177 |