WC_Admin_Marketplace_Promotions::rules_are_validprivate staticWC 1.0

Recursively validate a rule (or array of rules) before evaluation.

Validation must cover nested not/or operands: an empty or malformed operand evaluates to false, and not would then flip that to true, showing the promo on a malformed payload. Unknown rule types resolve to a fail processor (which validates but always fails), so they are rejected here too. Anything not well-formed fails closed.

Method of the class: WC_Admin_Marketplace_Promotions{}

No Hooks.

Returns

bool.

Usage

$result = WC_Admin_Marketplace_Promotions::rules_are_valid( $rules ): bool;
$rules(mixed) (required)
A decoded rule object or array of rule objects.

WC_Admin_Marketplace_Promotions::rules_are_valid() code WC 11.0.1

private static function rules_are_valid( $rules ): bool {
	if ( is_object( $rules ) ) {
		$rules = array( $rules );
	}

	if ( ! is_array( $rules ) || 0 === count( $rules ) ) {
		return false;
	}

	foreach ( $rules as $rule ) {
		if ( ! is_object( $rule ) || empty( $rule->type ) ) {
			return false;
		}

		$processor = GetRuleProcessor::get_processor( $rule->type );

		// Unknown types resolve to the fail processor; reject them so `not` cannot flip them to true.
		if ( $processor instanceof FailRuleProcessor
			&& 'fail' !== $rule->type ) {
			return false;
		}

		if ( ! $processor->validate( $rule ) ) {
			return false;
		}

		if ( 'not' === $rule->type && ! self::rules_are_valid( $rule->operand ?? null ) ) {
			return false;
		}

		if ( 'or' === $rule->type ) {
			$operands = $rule->operands ?? null;
			if ( ! is_array( $operands ) || 0 === count( $operands ) ) {
				return false;
			}

			// Each OR operand may itself be a single rule or an AND group (array of rules).
			foreach ( $operands as $operand ) {
				if ( ! self::rules_are_valid( $operand ) ) {
					return false;
				}
			}
		}
	}

	return true;
}