registry = $registry; $this->entity = $entity; $this->target_context = $target_context; } /** * Gets the reason why a condition failed. * * @return string */ public function get_reason(): string { return $this->reason; } /** * Evaluates the entire condition tree based on the new two-level structure. * Conditions of another context (e.g. frontend JS) count as unknown: they never fail the tree, * but they don't make an OR true either - the browser decides those (see resolve_tree()). * * @param string|array|null $conditions_tree JSON String or already decoded array. * * @return bool False only if the tree is certainly not met. */ public function evaluate_tree( string|array|null $conditions_tree, $placement_id = 0 ): bool { $tree = $this->decode_tree( $conditions_tree ); if ( null === $tree ) { return true; } [ $outer_connector, $inner_connector ] = $this->connectors( $tree ); $results = []; foreach ( $tree['groups'] as $group_node ) { $group_result = is_array( $group_node ) ? $this->evaluate_group( $group_node, $inner_connector ) : true; // Short-circuit on a decided result. if ( ( $outer_connector === 'AND' && false === $group_result ) || ( $outer_connector === 'OR' && true === $group_result ) ) { return (bool) $group_result; } $results[] = $group_result; } return false !== self::combine( $results, $outer_connector ); } /** * The tree for the browser: every condition of this evaluator's context is replaced by its result, * so the browser evaluates e.g. "false OR device=mobile" instead of treating the backend part as true. * Depends on the page only (no visitor data), so it is safe in full-page-cached HTML. * * @param string|null $conditions_tree * * @return string|null The resolved JSON, or the input if it can't be parsed. */ public function resolve_tree( ?string $conditions_tree ): ?string { $tree = $this->decode_tree( $conditions_tree ); if ( null === $tree ) { return $conditions_tree; } foreach ( $tree['groups'] as $group_index => $group_node ) { if ( ! is_array( $group_node ) || ! isset( $group_node['conditions'] ) || ! is_array( $group_node['conditions'] ) ) { continue; } foreach ( $group_node['conditions'] as $condition_index => $condition_node ) { if ( ! is_array( $condition_node ) || ! $this->is_own_context( $condition_node ) ) { continue; } $tree['groups'][ $group_index ]['conditions'][ $condition_index ] = [ 'type' => self::RESOLVED_TYPE, 'value' => (bool) $this->evaluate_condition( $condition_node ), ]; } } return (string) wp_json_encode( $tree ); } /** * @param string|array|null $conditions_tree * * @return array|null The tree, or null if there is nothing (valid) to evaluate. */ private function decode_tree( string|array|null $conditions_tree ): ?array { if ( empty( $conditions_tree ) ) { return null; // No conditions means the check passes. } $tree = is_string( $conditions_tree ) ? json_decode( $conditions_tree, true ) : $conditions_tree; // Validate the structure. if ( ! is_array( $tree ) || ! isset( $tree['outer_connector'] ) || ! isset( $tree['groups'] ) ) { RuntimeDebugger::add_doing_it_wrong_entry( esc_html( 'AdPresso Evaluator: Invalid JSON or main condition tree structure. Expected "outer_connector" and "groups".' ) ); return null; // Failsafe: if structure is invalid, don't block the ad. } if ( empty( $tree['groups'] ) || ! is_array( $tree['groups'] ) ) { return null; // No groups of conditions means the check passes. } return $tree; } /** * The inner connector is always the opposite of the outer one - the condition builder only ever exposes the * outer toggle. Any per-group 'inner_connector' in stored data is ignored. * * @param array $tree * * @return array{0: string, 1: string} Outer and inner connector. */ private function connectors( array $tree ): array { $outer_connector = strtoupper( (string) $tree['outer_connector'] ); return [ $outer_connector, 'OR' === $outer_connector ? 'AND' : 'OR' ]; } /** * Three-valued AND/OR: null (unknown) never decides on its own. * * @param array $results * @param string $connector * * @return bool|null */ private static function combine( array $results, string $connector ): ?bool { if ( 'AND' === $connector ) { if ( in_array( false, $results, true ) ) { return false; } return in_array( null, $results, true ) ? null : true; } if ( in_array( true, $results, true ) ) { return true; } return in_array( null, $results, true ) ? null : false; } /** * Evaluates a single group of conditions. * * @param array $group_node A single group object from the "groups" array. * @param string $inner_connector 'AND' or 'OR', derived from the outer connector. * * @return bool|null The result for this group, null if it depends on another context. */ private function evaluate_group( array $group_node, string $inner_connector ): ?bool { if ( ! isset( $group_node['conditions'] ) || ! is_array( $group_node['conditions'] ) || empty( $group_node['conditions'] ) ) { return true; // Invalid or empty group, failsafe. } $results = []; foreach ( $group_node['conditions'] as $condition_node ) { $condition_result = is_array( $condition_node ) ? $this->evaluate_condition( $condition_node ) : true; if ( ( $inner_connector === 'AND' && false === $condition_result ) || ( $inner_connector === 'OR' && true === $condition_result ) ) { return $condition_result; } $results[] = $condition_result; } return self::combine( $results, $inner_connector ); } /** * Whether a condition node belongs to this evaluator's context. * * @param array $condition_node * * @return bool */ private function is_own_context( array $condition_node ): bool { $condition_object = isset( $condition_node['type'] ) ? $this->registry->get_condition( $condition_node['type'] ) : null; return $condition_object && $condition_object->get_execution_context() === $this->target_context; } /** * Evaluates a single condition leaf node. * * @param array $condition_node The condition data object. * * @return bool|null The result, or null for a condition of another context. */ private function evaluate_condition( array $condition_node ): ?bool { if ( ! isset( $condition_node['type'] ) ) { return true; // Invalid node. } $condition_object = $this->registry->get_condition( $condition_node['type'] ); if ( ! $condition_object ) { // Unknown condition type, don't block the ad. return true; } if ( $condition_object->get_execution_context() !== $this->target_context ) { // This condition is for a different context (e.g., frontend JS): unknown here. return null; } $operator = $condition_node['operator'] ?? 'is'; $value = $condition_node['value'] ?? null; $result = (bool) $condition_object->evaluate( $operator, $value, $this->entity ); if ( ! $result ) { $this->reason = $condition_object->get_label(); } return $result; } }