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
OptionRuleProcessor.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rule processor that performs a comparison operation against an option value. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors; |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers\TransformerService; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Rule processor that performs a comparison operation against an option value. |
| 14 | */ |
| 15 | class OptionRuleProcessor implements RuleProcessorInterface { |
| 16 | /** |
| 17 | * Performs a comparison operation against the option value. |
| 18 | * |
| 19 | * @param object $rule The specific rule being processed by this rule processor. |
| 20 | * @param object $stored_state Stored state. |
| 21 | * |
| 22 | * @return bool The result of the operation. |
| 23 | */ |
| 24 | public function process( $rule, $stored_state ) { |
| 25 | $is_contains = $rule->operation && strpos( $rule->operation, 'contains' ) !== false; |
| 26 | $value_when_default_not_provided = $is_contains ? array() : false; |
| 27 | $is_default_set = property_exists( $rule, 'default' ); |
| 28 | $default_value = $is_default_set ? $rule->default : $value_when_default_not_provided; |
| 29 | $option_value = $this->get_option_value( $rule, $default_value, $is_contains ); |
| 30 | |
| 31 | if ( isset( $rule->transformers ) && is_array( $rule->transformers ) ) { |
| 32 | $option_value = TransformerService::apply( $option_value, $rule->transformers, $is_default_set, $default_value ); |
| 33 | } |
| 34 | |
| 35 | return ComparisonOperation::compare( |
| 36 | $option_value, |
| 37 | $rule->value, |
| 38 | $rule->operation |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Retrieves the option value and handles logging if necessary. |
| 44 | * |
| 45 | * @param object $rule The specific rule being processed. |
| 46 | * @param mixed $default_value The default value. |
| 47 | * @param bool $is_contains Indicates whether the operation is "contains". |
| 48 | * |
| 49 | * @return mixed The option value. |
| 50 | */ |
| 51 | private function get_option_value( $rule, $default_value, $is_contains ) { |
| 52 | $option_value = get_option( $rule->option_name, $default_value ); |
| 53 | $is_contains_valid = $is_contains && ( is_array( $option_value ) || ( is_string( $option_value ) && is_string( $rule->value ) ) ); |
| 54 | |
| 55 | if ( $is_contains && ! $is_contains_valid ) { |
| 56 | $logger = wc_get_logger(); |
| 57 | $logger->warning( |
| 58 | sprintf( |
| 59 | 'ComparisonOperation "%s" option value "%s" is not an array, defaulting to empty array.', |
| 60 | $rule->operation, |
| 61 | $rule->option_name |
| 62 | ), |
| 63 | array( |
| 64 | 'option_value' => $option_value, |
| 65 | 'rule' => $rule, |
| 66 | ) |
| 67 | ); |
| 68 | $option_value = array(); |
| 69 | } |
| 70 | |
| 71 | return $option_value; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Validates the rule. |
| 76 | * |
| 77 | * @param object $rule The rule to validate. |
| 78 | * |
| 79 | * @return bool Pass/fail. |
| 80 | */ |
| 81 | public function validate( $rule ) { |
| 82 | if ( ! isset( $rule->option_name ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if ( ! isset( $rule->value ) ) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if ( ! isset( $rule->operation ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if ( isset( $rule->transformers ) && is_array( $rule->transformers ) ) { |
| 95 | foreach ( $rule->transformers as $transform_args ) { |
| 96 | $transformer = TransformerService::create_transformer( $transform_args->use ); |
| 97 | if ( ! $transformer->validate( $transform_args->arguments ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 |