Transformers
1 year ago
BaseLocationCountryRuleProcessor.php
2 years ago
BaseLocationStateRuleProcessor.php
2 years ago
ComparisonOperation.php
2 years ago
ContextPluginsRuleProcessor.php
1 year ago
EvaluateAndGetStatus.php
2 years ago
EvaluateOverrides.php
1 year ago
EvaluationLogger.php
1 year ago
FailRuleProcessor.php
2 years ago
GetRuleProcessor.php
2 years ago
GetRuleProcessorForContext.php
1 year ago
IsEcommerceRuleProcessor.php
2 years ago
IsWooExpressRuleProcessor.php
2 years ago
NotRuleProcessor.php
2 years ago
NoteStatusRuleProcessor.php
2 years ago
OnboardingProfileRuleProcessor.php
2 years ago
OptionRuleProcessor.php
2 years ago
OrRuleProcessor.php
2 years ago
OrderCountRuleProcessor.php
2 years ago
OrdersProvider.php
2 years ago
PassRuleProcessor.php
2 years ago
PluginVersionRuleProcessor.php
2 years ago
PluginsActivatedRuleProcessor.php
2 years ago
ProductCountRuleProcessor.php
1 year ago
PublishAfterTimeRuleProcessor.php
2 years ago
PublishBeforeTimeRuleProcessor.php
2 years ago
RuleEvaluator.php
1 year ago
RuleProcessorInterface.php
2 years ago
StoredStateRuleProcessor.php
2 years ago
StoredStateSetupForProducts.php
2 years ago
TotalPaymentsVolumeProcessor.php
1 year ago
WCAdminActiveForProvider.php
2 years ago
WCAdminActiveForRuleProcessor.php
2 years ago
WooCommerceAdminUpdatedRuleProcessor.php
2 years ago
TotalPaymentsVolumeProcessor.php
109 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rule processor that passes when a store's payments volume exceeds a provided amount. |
| 4 | */ |
| 5 | |
| 6 | declare(strict_types=1); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\TimeInterval; |
| 14 | |
| 15 | /** |
| 16 | * Rule processor that passes when a store's payments volume exceeds a provided amount. |
| 17 | */ |
| 18 | class TotalPaymentsVolumeProcessor implements RuleProcessorInterface { |
| 19 | /** |
| 20 | * Compare against the store's total payments volume. |
| 21 | * |
| 22 | * @param object $rule The rule being processed by this rule processor. |
| 23 | * @param object $stored_state Stored state. |
| 24 | * |
| 25 | * @return bool The result of the operation. |
| 26 | */ |
| 27 | public function process( $rule, $stored_state ) { |
| 28 | $dates = TimeInterval::get_timeframe_dates( $rule->timeframe ); |
| 29 | $reports_revenue = $this->get_reports_query( |
| 30 | array( |
| 31 | 'before' => $dates['end'], |
| 32 | 'after' => $dates['start'], |
| 33 | 'interval' => 'year', |
| 34 | 'fields' => array( 'total_sales' ), |
| 35 | ) |
| 36 | ); |
| 37 | $report_data = $reports_revenue->get_data(); |
| 38 | |
| 39 | if ( ! $report_data || ! isset( $report_data->totals->total_sales ) ) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | $value = $report_data->totals->total_sales; |
| 44 | |
| 45 | return ComparisonOperation::compare( |
| 46 | $value, |
| 47 | $rule->value, |
| 48 | $rule->operation |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Validates the rule. |
| 54 | * |
| 55 | * @param object $rule The rule to validate. |
| 56 | * |
| 57 | * @return bool Pass/fail. |
| 58 | */ |
| 59 | public function validate( $rule ) { |
| 60 | $allowed_timeframes = array( |
| 61 | 'last_week', |
| 62 | 'last_month', |
| 63 | 'last_quarter', |
| 64 | 'last_6_months', |
| 65 | 'last_year', |
| 66 | ); |
| 67 | |
| 68 | if ( ! isset( $rule->timeframe ) || ! in_array( $rule->timeframe, $allowed_timeframes, true ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | if ( ! isset( $rule->value ) ) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if ( ! isset( $rule->operation ) ) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // If the operation is range, the value must be an array of two numbers. |
| 81 | if ( 'range' === $rule->operation ) { |
| 82 | if ( ! is_array( $rule->value ) || count( $rule->value ) !== 2 ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if ( ! is_numeric( $rule->value[0] ) || ! is_numeric( $rule->value[1] ) ) { |
| 87 | return false; |
| 88 | } |
| 89 | } elseif ( ! is_numeric( $rule->value ) ) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the report query. |
| 98 | * |
| 99 | * @param array $args The query args. |
| 100 | * |
| 101 | * @return RevenueQuery The report query. |
| 102 | */ |
| 103 | protected function get_reports_query( $args ) { |
| 104 | return new RevenueQuery( |
| 105 | $args |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 |