$data Stored payload. */ public static function from_array( array $data ): Condition { /* * An operator this build does not know is kept, not replaced. * * Rewriting it to `is` — which is what used to happen — turned a rule left * behind by a deactivated add-on into an equality test against the value * its author wrote, so a condition that should have stopped matching * started matching instead, and could reveal a field somebody had hidden. * It also made `Operators::evaluate()`'s custom branch unreachable for any * stored rule, so `formpilot_condition_matches` never fired at all despite being * documented as the way to add an operator. * * Kept as it is, an unrecognised operator reaches that filter, whose * default answer is false: the rule stops matching, and an add-on that * defines the operator can still answer for it. */ $operator = sanitize_key( (string) ( $data['operator'] ?? Operators::IS ) ); return new self( sanitize_key( (string) ( $data['field'] ?? '' ) ), '' === $operator ? Operators::IS : $operator, (string) ( $data['value'] ?? '' ) ); } /** * Returns the key of the field being examined. */ public function field(): string { return $this->field; } /** * Determines whether the condition holds. * * A condition pointing at a field that does not exist never holds. Treating * a missing field as a match would show every dependent field the moment an * author deleted the one they were keyed to. * * @param array $values Submitted values keyed by field key. * * @return bool True when the comparison holds. */ public function evaluate( array $values ): bool { if ( '' === $this->field || ! array_key_exists( $this->field, $values ) ) { return false; } return Operators::evaluate( $this->operator, $values[ $this->field ], $this->value ); } /** * Returns the condition as storable data. * * @return array The payload. */ public function to_array(): array { return array( 'field' => $this->field, 'operator' => $this->operator, 'value' => $this->value, ); } }