Categories
4 weeks ago
Coupons
4 weeks ago
Customers
2 months ago
Downloads
1 year ago
Export
3 years ago
Import
3 years ago
Orders
4 weeks ago
PerformanceIndicators
9 months ago
Products
4 weeks ago
Revenue
3 months ago
Stock
4 months ago
Taxes
4 weeks ago
Variations
4 weeks ago
Cache.php
4 years ago
Controller.php
11 months ago
DataStore.php
4 weeks ago
DataStoreInterface.php
4 years ago
ExportableInterface.php
4 years ago
ExportableTraits.php
4 years ago
FilteredGetDataTrait.php
1 year ago
GenericController.php
1 year ago
GenericQuery.php
1 year ago
GenericStatsController.php
1 year ago
OrderAwareControllerTrait.php
1 year ago
ParameterException.php
4 years ago
Query.php
1 year ago
Segmenter.php
1 year ago
SqlQuery.php
3 years ago
StatsDataStoreTrait.php
1 year ago
TimeInterval.php
2 years ago
StatsDataStoreTrait.php
121 lines
| 1 | <?php |
| 2 | declare( strict_types = 1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Admin\API\Reports; |
| 5 | |
| 6 | // Exit if accessed directly. |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\SqlQuery; |
| 12 | |
| 13 | /** |
| 14 | * Trait to contain *stats-specific methods for data stores. |
| 15 | * |
| 16 | * It does preliminary intervals & page calculations |
| 17 | * and prepares intervals & totals data structure by implementing the `get_noncached_data()` method. |
| 18 | * So, this time, you'll need to prepare `get_noncached_stats_data()` which will be called only if |
| 19 | * the requested page is within the date range. |
| 20 | * |
| 21 | * The trait also exposes the `initialize_queries()` method to initialize the interval and total queries. |
| 22 | * |
| 23 | * Example: |
| 24 | * <pre><code class="language-php">class MyStatsDataStore extends DataStore implements DataStoreInterface { |
| 25 | * // Use the trait. |
| 26 | * use StatsDataStoreTrait; |
| 27 | * // Provide all the necessary properties and methods for a regular DataStore. |
| 28 | * // ... |
| 29 | * /** |
| 30 | * * Return your results with the help of the interval & total methods and queries. |
| 31 | * * @return stdClass|WP_Error $data filled with your results. |
| 32 | * */ |
| 33 | * public function get_noncached_stats_data( $query_args, $params, &$data, $expected_interval_count ) { |
| 34 | * $this->initialize_queries(); |
| 35 | * // Do your magic ... |
| 36 | * // ... with a help of things like: |
| 37 | * $this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name ); |
| 38 | * $this->total_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) ); |
| 39 | * |
| 40 | * $totals = $wpdb->get_results( |
| 41 | * $this->total_query->get_query_statement(), |
| 42 | * ARRAY_A |
| 43 | * ); |
| 44 | * |
| 45 | * $intervals = $wpdb->get_results( |
| 46 | * $this->interval_query->get_query_statement(), |
| 47 | * ARRAY_A |
| 48 | * ); |
| 49 | * |
| 50 | * $data->totals = (object) $this->cast_numbers( $totals[0] ); |
| 51 | * $data->intervals = $intervals; |
| 52 | * |
| 53 | * if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { |
| 54 | * $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); |
| 55 | * $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); |
| 56 | * $this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); |
| 57 | * } else { |
| 58 | * $this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals ); |
| 59 | * } |
| 60 | * |
| 61 | * return $data; |
| 62 | * } |
| 63 | * } |
| 64 | * </code></pre> |
| 65 | * |
| 66 | * @see DataStore |
| 67 | */ |
| 68 | trait StatsDataStoreTrait { |
| 69 | /** |
| 70 | * Initialize query objects. |
| 71 | */ |
| 72 | protected function initialize_queries() { |
| 73 | $this->clear_all_clauses(); |
| 74 | unset( $this->subquery ); |
| 75 | $table_name = self::get_db_table_name(); |
| 76 | |
| 77 | $this->total_query = new SqlQuery( $this->context . '_total' ); |
| 78 | $this->total_query->add_sql_clause( 'from', $table_name ); |
| 79 | |
| 80 | $this->interval_query = new SqlQuery( $this->context . '_interval' ); |
| 81 | $this->interval_query->add_sql_clause( 'from', $table_name ); |
| 82 | $this->interval_query->add_sql_clause( 'group_by', 'time_interval' ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the stats report data based on normalized parameters. |
| 87 | * Prepares the basic intervals and object structure |
| 88 | * Will be called by `get_data` if there is no data in cache. |
| 89 | * Will call `get_noncached_stats_data` to fetch the actual data. |
| 90 | * |
| 91 | * @see get_data |
| 92 | * @param array $query_args Query parameters. |
| 93 | * @return stdClass|WP_Error Data object, or error. |
| 94 | */ |
| 95 | public function get_noncached_data( $query_args ) { |
| 96 | $params = $this->get_limit_params( $query_args ); |
| 97 | $expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); |
| 98 | $total_pages = (int) ceil( $expected_interval_count / $params['per_page'] ); |
| 99 | |
| 100 | // Default, empty data object. |
| 101 | $data = (object) array( |
| 102 | 'totals' => null, |
| 103 | 'intervals' => array(), |
| 104 | 'total' => $expected_interval_count, |
| 105 | 'pages' => $total_pages, |
| 106 | 'page_no' => (int) $query_args['page'], |
| 107 | ); |
| 108 | // If the requested page is out off range, return the default empty object. |
| 109 | if ( $query_args['page'] >= 1 && $query_args['page'] <= $total_pages ) { |
| 110 | // Fetch the actual data. |
| 111 | $data = $this->get_noncached_stats_data( $query_args, $params, $data, $expected_interval_count ); |
| 112 | |
| 113 | if ( ! is_wp_error( $data ) && is_array( $data->intervals ) ) { |
| 114 | $this->create_interval_subtotals( $data->intervals ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return $data; |
| 119 | } |
| 120 | } |
| 121 |