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
GenericQuery.php
92 lines
| 1 | <?php |
| 2 | declare( strict_types = 1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Admin\API\Reports; |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | use WC_Data_Store; |
| 9 | |
| 10 | /** |
| 11 | * A generic class for a report-specific query to be used in Analytics. |
| 12 | * |
| 13 | * Example usage: |
| 14 | * <pre><code class="language-php">$args = array( |
| 15 | * 'before' => '2018-07-19 00:00:00', |
| 16 | * 'after' => '2018-07-05 00:00:00', |
| 17 | * 'page' => 2, |
| 18 | * ); |
| 19 | * $report = new GenericQuery( $args, 'coupons' ); |
| 20 | * $mydata = $report->get_data(); |
| 21 | * </code></pre> |
| 22 | * |
| 23 | * It uses the name provided in the class property or in the constructor call to load the `report-{name}` data store. |
| 24 | * |
| 25 | * It's used by the {@see GenericController GenericController}. |
| 26 | * |
| 27 | * @since 9.3.0 |
| 28 | */ |
| 29 | class GenericQuery extends \WC_Object_Query { |
| 30 | |
| 31 | /** |
| 32 | * Specific query name. |
| 33 | * Will be used to load the `report-{name}` data store, |
| 34 | * and to call `woocommerce_analytics_{snake_case(name)}_*` filters. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | protected $name; |
| 39 | |
| 40 | /** |
| 41 | * Create a new query. |
| 42 | * |
| 43 | * @param array $args Criteria to query on in a format similar to WP_Query. |
| 44 | * @param string $name Query name. |
| 45 | * @extends WC_Object_Query::_construct |
| 46 | */ |
| 47 | public function __construct( $args, $name = null ) { |
| 48 | $this->name = $name ?? $this->name; |
| 49 | |
| 50 | return parent::__construct( $args ); // phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound |
| 51 | } |
| 52 | /** |
| 53 | * Valid fields for Products report. |
| 54 | * |
| 55 | * @return array |
| 56 | */ |
| 57 | protected function get_default_query_vars() { |
| 58 | return array(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get data from `report-{$name}` store, based on the current query vars. |
| 63 | * Filters query vars through `woocommerce_analytics_{snake_case(name)}_query_args` filter. |
| 64 | * Filters results through `woocommerce_analytics_{snake_case(name)}_select_query` filter. |
| 65 | * |
| 66 | * @return mixed filtered results from the data store. |
| 67 | */ |
| 68 | public function get_data() { |
| 69 | $snake_name = str_replace( '-', '_', $this->name ); |
| 70 | /** |
| 71 | * Filter query args given for the report. |
| 72 | * |
| 73 | * @since 9.3.0 |
| 74 | * |
| 75 | * @param array $query_args Query args. |
| 76 | */ |
| 77 | $args = apply_filters( "woocommerce_analytics_{$snake_name}_query_args", $this->get_query_vars() ); |
| 78 | |
| 79 | $data_store = \WC_Data_Store::load( "report-{$this->name}" ); |
| 80 | $results = $data_store->get_data( $args ); |
| 81 | /** |
| 82 | * Filter report query results. |
| 83 | * |
| 84 | * @since 9.3.0 |
| 85 | * |
| 86 | * @param stdClass|WP_Error $results Results from the data store. |
| 87 | * @param array $args Query args used to get the data (potentially filtered). |
| 88 | */ |
| 89 | return apply_filters( "woocommerce_analytics_{$snake_name}_select_query", $results, $args ); |
| 90 | } |
| 91 | } |
| 92 |