Skip to content

Conditions class does not match its JS counterpart #9043

@danyj

Description

@danyj

JS Conditions function has contains

case 'contains':
return -1 !== leftValue.indexOf( rightValue );
case '!contains':
return -1 === leftValue.indexOf( rightValue );

which works right in the editor to check if the string is in the control that has array as value

				'conditions' => array(
					'terms' => array(
						array(
							'name' => 'meta_elements',
							'operator' => 'contains',
							'value' => 'author',
						),
					)
				)

this option does not exists in the php version of the Conditions class

public static function compare( $left_value, $right_value, $operator ) {
switch ( $operator ) {
case '==':
return $left_value == $right_value;
case '!=':
return $left_value != $right_value;
case '!==':
return $left_value !== $right_value;
case 'in':
return false !== array_search( $left_value, $right_value );
case '!in':
return false === array_search( $left_value, $right_value );
case '<':
return $left_value < $right_value;
case '<=':
return $left_value <= $right_value;
case '>':
return $left_value > $right_value;
case '>=':
return $left_value >= $right_value;
default:
return $left_value === $right_value;
}
}

Although this works in the editor to show/hide conditional controls, the value is always null in the frontend because the missing cases.

A simple in_array would match both switches

			case 'contains':
				return false !== in_array($right_value,$left_value);
			case '!contains':
				return false === in_array( $right_value, $left_value );

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/mergedIndicates when a Pull Request has been merged to a Release.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions