diff --git a/packages/php/remote-specs-validation/bundles/obw-free-extensions.json b/packages/php/remote-specs-validation/bundles/obw-free-extensions.json index a7a93802158e..4f6eab2bf3f3 100644 --- a/packages/php/remote-specs-validation/bundles/obw-free-extensions.json +++ b/packages/php/remote-specs-validation/bundles/obw-free-extensions.json @@ -59,7 +59,8 @@ "contains", "!contains", "in", - "!in" + "!in", + "range" ] }, "rules": { diff --git a/packages/php/remote-specs-validation/bundles/payment-gateway-suggestions.json b/packages/php/remote-specs-validation/bundles/payment-gateway-suggestions.json index d7453fe12dad..78a72622f7ba 100644 --- a/packages/php/remote-specs-validation/bundles/payment-gateway-suggestions.json +++ b/packages/php/remote-specs-validation/bundles/payment-gateway-suggestions.json @@ -728,7 +728,8 @@ "contains", "!contains", "in", - "!in" + "!in", + "range" ] }, "rules": { diff --git a/packages/php/remote-specs-validation/bundles/remote-inbox-notification.json b/packages/php/remote-specs-validation/bundles/remote-inbox-notification.json index 2a1e3052b9d5..c64ac57f605d 100644 --- a/packages/php/remote-specs-validation/bundles/remote-inbox-notification.json +++ b/packages/php/remote-specs-validation/bundles/remote-inbox-notification.json @@ -147,7 +147,8 @@ "contains", "!contains", "in", - "!in" + "!in", + "range" ] }, "rules": { diff --git a/packages/php/remote-specs-validation/bundles/shipping-partner-suggestions.json b/packages/php/remote-specs-validation/bundles/shipping-partner-suggestions.json index ce17d69e25aa..0e886b8aca7b 100644 --- a/packages/php/remote-specs-validation/bundles/shipping-partner-suggestions.json +++ b/packages/php/remote-specs-validation/bundles/shipping-partner-suggestions.json @@ -711,7 +711,8 @@ "contains", "!contains", "in", - "!in" + "!in", + "range" ] }, "rules": { diff --git a/packages/php/remote-specs-validation/bundles/wc-pay-promotions.json b/packages/php/remote-specs-validation/bundles/wc-pay-promotions.json index d616fe68aa69..75f8d2fd7d16 100644 --- a/packages/php/remote-specs-validation/bundles/wc-pay-promotions.json +++ b/packages/php/remote-specs-validation/bundles/wc-pay-promotions.json @@ -708,7 +708,8 @@ "contains", "!contains", "in", - "!in" + "!in", + "range" ] }, "rules": { diff --git a/packages/php/remote-specs-validation/changelog/45201-add-44787-add-range-rule b/packages/php/remote-specs-validation/changelog/45201-add-44787-add-range-rule new file mode 100644 index 000000000000..332381bbbbeb --- /dev/null +++ b/packages/php/remote-specs-validation/changelog/45201-add-44787-add-range-rule @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Support range operator in Remote Inbox Notification \ No newline at end of file diff --git a/packages/php/remote-specs-validation/schemas/shared/operations.json b/packages/php/remote-specs-validation/schemas/shared/operations.json index c7b5fffef05f..10f14903da29 100644 --- a/packages/php/remote-specs-validation/schemas/shared/operations.json +++ b/packages/php/remote-specs-validation/schemas/shared/operations.json @@ -1,4 +1,4 @@ { "type": "string", - "enum": ["=", "<", "<=", ">", ">=", "!=", "contains", "!contains", "in", "!in"] + "enum": ["=", "<", "<=", ">", ">=", "!=", "contains", "!contains", "in", "!in", "range"] } \ No newline at end of file diff --git a/plugins/woocommerce/changelog/45201-add-44787-add-range-rule b/plugins/woocommerce/changelog/45201-add-44787-add-range-rule new file mode 100644 index 000000000000..332381bbbbeb --- /dev/null +++ b/plugins/woocommerce/changelog/45201-add-44787-add-range-rule @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Support range operator in Remote Inbox Notification \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php index 501a32a875ce..57f4a2ba1f4d 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php @@ -15,7 +15,7 @@ class ComparisonOperation { * Compare two operands using the specified operation. * * @param object $left_operand The left hand operand. - * @param object $right_operand The right hand operand. + * @param object $right_operand The right hand operand -- 'value' from the rule definition. * @param string $operation The operation used to compare the operands. */ public static function compare( $left_operand, $right_operand, $operation ) { @@ -64,6 +64,11 @@ public static function compare( $left_operand, $right_operand, $operation ) { return strpos( $left_operand, $right_operand ) === false; } break; + case 'range': + if ( ! is_array( $right_operand ) || count( $right_operand ) !== 2 ) { + return false; + } + return $left_operand >= $right_operand[0] && $left_operand <= $right_operand[1]; } return false; diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/README.md b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/README.md index 8931578aae12..92f7d0a58c5f 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/README.md +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/README.md @@ -143,16 +143,19 @@ Rules in an array are executed as an AND operation. If there are no rules in the Some rule types support an `operation` value, which is used to compare two values. The following operations are implemented: -- `=` -- `<` -- `<=` -- `>` -- `>=` -- `!=` -- `contains` -- `!contains` -- `in` (Added in WooCommerce 8.2.0) -- `!in` (Added in WooCommerce 8.2.0) +- `=` +- `<` +- `<=` +- `>` +- `>=` +- `!=` +- `contains` +- `!contains` +- `in` (Added in WooCommerce 8.2.0) +- `!in` (Added in WooCommerce 8.2.0) +- `range` (Added in WooCommerce 8.8.0) + +#### contains and !contains `contains` and `!contains` allow checking if the provided value is present (or not present) in the haystack value. An example of this is using the @@ -169,6 +172,8 @@ onboarding profile: } ``` +#### in and !in + `in` and `!in` allow checking if a value is found (or not found) in a provided array. For example, using the `in` comparison operator to check if the base country location value is found in a given array, as below. This rule matches if the `base_location_country` is `US`, `NZ`, or `ZA`. **NOTE:** These comparisons were added in **WooCommerce 8.2.0**. If the spec is read by an older version of WooCommerce, the rule will evaluate to `false`. ```json @@ -183,6 +188,23 @@ onboarding profile: } ``` +#### range + +`range` operator performs an inclusive check to determine if a number falls within a certain range. +This means that both the 'from' and 'to' values of the specified range are included in the check. + +The following rule returns true when `woocommerce_remote_variant_assignment` value is between 1 and 10. + +```json +{ + "type": "option", + "value": [ 1, 10 ], + "default": 0, + "operation": "range", + "option_name": "woocommerce_remote_variant_assignment", +} +``` + ### Plugins activated This passes if all of the listed plugins are installed and activated. diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/comparison-operation.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/comparison-operation.php new file mode 100644 index 000000000000..09e5265d79ca --- /dev/null +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/comparison-operation.php @@ -0,0 +1,85 @@ +operation = new ComparisonOperation(); + } + + /** + * Test range + * + * @group fast + * @return void + */ + public function test_range() { + $this->assertTrue( $this->operation->compare( 1, array( 1, 10 ), 'range' ) ); + $this->assertFalse( $this->operation->compare( 11, array( 1, 10 ), 'range' ) ); + $this->assertFalse( $this->operation->compare( 11, array( 1, 10, 2 ), 'range' ) ); + $this->assertFalse( $this->operation->compare( 11, 'string', 'range' ) ); + + } + + /** + * Test contains + * + * @group fast + * @return void + */ + public function test_contains() { + $this->assertTrue( $this->operation->compare( array( 'test', 'test1' ), 'test', 'contains' ) ); + $this->assertFalse( $this->operation->compare( array( 'a', 'b' ), 'test', 'contains' ) ); + } + + /** + * Test !contains + * + * @group fast + * @return void + */ + public function test_not_contains() { + $this->assertTrue( $this->operation->compare( array( 'test1', 'test2' ), 'test', '!contains' ) ); + $this->assertFalse( $this->operation->compare( array( 'test' ), 'test', '!contains' ) ); + } + + /** + * Test in + * + * @group fast + * @return void + */ + public function test_in() { + $this->assertTrue( $this->operation->compare( 'test', array( 'test', 'test2' ), 'in' ) ); + $this->assertFalse( $this->operation->compare( 'test', array( 'test1', 'test2' ), 'in' ) ); + } + + /** + * Test !in + * + * @group fast + * @return void + */ + public function test_not_in() { + $this->assertTrue( $this->operation->compare( 'test', array( 'test1', 'test2' ), '!in' ) ); + $this->assertFalse( $this->operation->compare( 'test', array( 'test', 'test2' ), '!in' ) ); + } +}