| Server IP : 151.101.66.159 / Your IP : 216.73.216.66 Web Server : Flywheel/5.1.0 System : Linux wordpress 6.12.113+ #1 SMP Thu Oct 22 02:38:20 UTC 2026 x86_64 User : root ( ) PHP Version : 7.4.33.18 Disable Function : disk_free_space,diskfreespace,dl,exec,fpassthru,getmypid,getmyuid,highlight_file,link,opcache_compile_file,opcache_get_configuration,opcache_is_script_cached,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifcontinued,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,phpinfo,popen,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,show_source,socket_listen,source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wp-content/plugins/pods/src/Pods/Data/ |
Upload File : |
<?php
namespace Pods\Data;
// Don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
use Pods\Whatsit;
/**
* Conditional logic class.
*
* @since 3.0
*/
class Conditional_Logic {
/**
* The action to take (show/hide).
*
* @var string
*/
protected $action = 'show';
/**
* The logic to use (any/all).
*
* @var string
*/
protected $logic = 'any';
/**
* The conditional rules.
*
* @var array
*/
protected $rules = [];
/**
* The full list of conditional logic sets.
*
* @var array
*/
protected $logic_sets = [];
/**
* Set up the object.
*
* @since 3.0
*
* @param string $action The action to take (show/hide).
* @param string $logic The logic to use (any/all).
* @param array $rules The conditional rules.
* @param array $logic_sets The full list of conditional logic sets.
*/
public function __construct( string $action = 'show', string $logic = 'any', array $rules = [], array $logic_sets = [] ) {
if ( $action ) {
$this->set_action( $action );
}
if ( $logic ) {
$this->set_logic( $logic );
}
if ( $rules ) {
$this->set_rules( $rules );
}
if ( $logic_sets ) {
$this->set_logic_sets( $logic_sets );
}
}
/**
* Set up the conditional logic object from a object.
*
* @since 3.0
*
* @param Whatsit|array $object The object data.
*
* @return Conditional_Logic|null The conditional logic object or null if rules not set.
*/
public static function maybe_setup_from_object( $object ): ?Conditional_Logic {
if ( $object instanceof Whatsit && $object->is_conditional_logic_enabled() ) {
$object_logic = $object->get_conditional_logic_config();
if ( empty( $object_logic ) ) {
return null;
}
return self::setup_from_object(
$object,
$object_logic['action'],
$object_logic['logic'],
$object_logic['rules']
);
}
return self::maybe_setup_from_old_syntax( $object );
}
/**
* Maybe migrate the field name from old prefix naming convention.
*
* @since 3.0
*
* @param string|int $field_name The field name.
*
* @return string The migrated field name.
*/
public static function maybe_migrate_field_name( $field_name ): string {
if ( ! is_string( $field_name ) ) {
return (string) $field_name;
}
if ( 0 === strpos( $field_name, 'pods_field_' ) ) {
$field_name = substr( $field_name, strlen( 'pods_field_' ) );
} elseif ( 0 === strpos( $field_name, 'pods_meta_' ) ) {
$field_name = substr( $field_name, strlen( 'pods_meta_' ) );
}
return $field_name;
}
/**
* Maybe set up the conditional logic object from old syntax.
*
* @since 3.0
*
* @param Whatsit|array $object The object data.
*
* @return Conditional_Logic|null The conditional logic object or null if rules not set.
*/
public static function maybe_setup_from_old_syntax( $object ): ?Conditional_Logic {
$old_syntax = [
'depends-on' => pods_v( 'depends-on', $object, [], true ),
'depends-on-any' => pods_v( 'depends-on-any', $object, [], true ),
'depends-on-multi' => pods_v( 'depends-on-multi', $object, [], true ),
'excludes-on' => pods_v( 'excludes-on', $object, [], true ),
'wildcard-on' => pods_v( 'wildcard-on', $object, [], true ),
];
foreach ( $old_syntax as $old_syntax_key => $old_syntax_value ) {
if ( empty( $old_syntax_value ) ) {
$old_syntax_value = [];
} elseif ( ! is_array( $old_syntax_value ) ) {
$old_syntax_value = pods_maybe_safely_unserialize( $old_syntax_value );
if ( ! is_array( $old_syntax_value ) ) {
$old_syntax_value = [];
}
}
$old_syntax[ $old_syntax_key ] = $old_syntax_value;
}
$action = 'show';
$rules = [];
$logic_sets = [];
if ( $old_syntax['depends-on'] ) {
$logic = 'all';
$rules = [];
foreach ( $old_syntax['depends-on'] as $field_name => $value ) {
$field_name = self::maybe_migrate_field_name( $field_name );
$rules[] = [
'field' => $field_name,
'compare' => ( is_array( $value ) ? 'in' : '=' ),
'value' => $value,
];
}
$logic_sets[] = [
'action' => $action,
'logic' => $logic,
'rules' => $rules,
];
}
if ( $old_syntax['depends-on-any'] ) {
$logic = 'any';
$rules = [];
foreach ( $old_syntax['depends-on-any'] as $field_name => $value ) {
$field_name = self::maybe_migrate_field_name( $field_name );
$rules[] = [
'field' => $field_name,
'compare' => ( is_array( $value ) ? 'in' : '=' ),
'value' => $value,
];
}
if ( ! empty( $rules ) ) {
$logic_sets[] = [
'action' => $action,
'logic' => $logic,
'rules' => $rules,
];
}
}
if ( $old_syntax['depends-on-multi'] ) {
$logic = 'all';
$rules = [];
foreach ( $old_syntax['depends-on-multi'] as $field_name => $value ) {
$field_name = self::maybe_migrate_field_name( $field_name );
$rules[] = [
'field' => $field_name,
'compare' => 'in-values',
'value' => $value,
];
}
if ( ! empty( $rules ) ) {
$logic_sets[] = [
'action' => $action,
'logic' => $logic,
'rules' => $rules,
];
}
}
if ( $old_syntax['excludes-on'] ) {
$logic = 'any';
$rules = [];
foreach ( $old_syntax['excludes-on'] as $field_name => $value ) {
$field_name = self::maybe_migrate_field_name( $field_name );
$rules[] = [
'field' => $field_name,
'compare' => ( is_array( $value ) ? 'not-in' : '!=' ),
'value' => $value,
];
}
if ( ! empty( $rules ) ) {
$logic_sets[] = [
'action' => $action,
'logic' => $logic,
'rules' => $rules,
];
}
}
if ( $old_syntax['wildcard-on'] ) {
$logic = 'any';
$rules = [];
foreach ( $old_syntax['wildcard-on'] as $field_name => $value ) {
$field_name = self::maybe_migrate_field_name( $field_name );
$value = (array) $value;
foreach ( $value as $wildcard_value ) {
$rules[] = [
'field' => $field_name,
'compare' => 'matches',
'value' => $wildcard_value,
];
}
}
if ( ! empty( $rules ) ) {
$logic_sets[] = [
'action' => $action,
'logic' => $logic,
'rules' => $rules,
];
}
}
if ( empty( $logic_sets ) ) {
return null;
}
return self::setup_from_object(
$object,
$action,
$logic,
$rules,
$logic_sets
);
}
/**
* Set up the Conditional Logic instance from an object.
*
* @since 3.0
*
* @param Whatsit|array $object The object data.
* @param string $action The action to take (show/hide).
* @param string $logic The logic to use (any/all).
* @param array $rules The conditional rules.
* @param array $logic_sets The full list of conditional logic sets.
*/
private static function setup_from_object( $object, $action, $logic, array $rules, array $logic_sets = [] ): Conditional_Logic {
foreach ( $logic_sets as $key => $logic_set ) {
$logic_sets[ $key ] = new self(
pods_v( 'action', $logic_set, $action, true ),
pods_v( 'logic', $logic_set, $logic, true ),
pods_v( 'rules', $logic_set, [] )
);
}
$conditional_logic = new self( $action, $logic, $rules, $logic_sets );
if ( $object instanceof Whatsit ) {
/**
* Allow filtering the conditional logic object used for a Whatsit object.
*
* @since 3.0
*
* @param Conditional_Logic $conditional_logic The conditional logic object.
* @param Whatsit $object The object data.
* @param string $action The action to take (show/hide).
* @param string $logic The logic to use (any/all).
* @param array $rules The conditional rules.
* @param array $logic_sets The full list of conditional logic sets.
*/
$conditional_logic = apply_filters( 'pods_data_conditional_logic_for_object', $conditional_logic, $object, $action, $logic, $rules, $logic_sets );
}
return $conditional_logic;
}
/**
* Get the action.
*
* @since 3.0
*
* @return string The action.
*/
public function get_action(): string {
return $this->action;
}
/**
* Set the action.
*
* @since 3.0
*
* @param string $action The action.
*/
public function set_action( string $action ): void {
if ( '' !== $action ) {
$this->action = $action;
}
}
/**
* Get the logic.
*
* @since 3.0
*
* @return string The logic.
*/
public function get_logic(): string {
return $this->logic;
}
/**
* Set the logic.
*
* @since 3.0
*
* @param string $logic The logic.
*/
public function set_logic( string $logic ): void {
if ( '' !== $logic ) {
$this->logic = $logic;
}
}
/**
* Get the conditional rules.
*
* @since 3.0
*
* @return array The conditional rules.
*/
public function get_rules(): array {
return $this->rules;
}
/**
* Set the conditional rules.
*
* @since 3.0
*
* @param array $rules The conditional rules.
*/
public function set_rules( array $rules ): void {
$this->rules = $rules;
}
/**
* Set the conditional logic sets.
*
* @since 3.3.5
*
* @param array $logic_sets The conditional logic sets.
*/
public function set_logic_sets( array $logic_sets ): void {
$this->logic_sets = $logic_sets;
}
/**
* Get the conditional logic data as an array.
*
* @since 3.0
*
* @return array The conditional logic data as an array.
*/
public function to_array(): array {
$conditional_logic = [
'action' => $this->action ?: 'show',
'logic' => $this->logic ?: 'any',
'rules' => $this->rules,
];
if ( $this->logic_sets ) {
$conditional_logic['logic_sets'] = array_map(
function ( Conditional_Logic $logic ) {
return $logic->to_array();
},
$this->logic_sets
);
}
return $conditional_logic;
}
/**
* Determine whether the field is visible.
*
* @since 3.0
*
* @param array $values The field values.
*
* @return bool Whether the field is visible.
*/
public function is_visible( array $values ): bool {
$rules_passed = $this->validate_rules( $values );
if ( 'show' === $this->action ) {
// Determine whether rules passed and this field should be shown.
return $rules_passed;
}
// Determine whether rules passed and this field should NOT be shown.
return ! $rules_passed;
}
/**
* Determine whether the rules validate for the field values provided.
*
* @since 3.0
*
* @param array $values The field values.
*
* @return bool Whether the rules validate for the field values provided.
*/
public function validate_rules( array $values ): bool {
if ( $this->logic_sets ) {
// Validate rules across logic sets.
return !! array_filter(
array_map(
function ( Conditional_Logic $logic ) use ( $values ) {
return $logic->validate_rules( $values );
},
$this->logic_sets
)
);
}
if ( empty( $this->rules ) ) {
return true;
}
$logic = strtoupper( $this->logic );
$rules_passed = 0;
$rules_not_passed = 0;
foreach ( $this->rules as $rule ) {
$passed = $this->validate_rule( $rule, $values );
if ( $passed ) {
$rules_passed ++;
} else {
$rules_not_passed ++;
}
}
if ( 'ANY' === $logic ) {
return 0 < $rules_passed;
}
if ( 'ALL' === $logic ) {
return 0 === $rules_not_passed;
}
return true;
}
/**
* Determine whether the rule validates for the field values provided.
*
* @since 3.0
*
* @param array $rule The conditional rule.
* @param array $values The field values.
*
* @return bool Whether the rule validates for the field values provided.
*/
public function validate_rule( array $rule, array $values ): bool {
$field = $rule['field'];
$compare = ! empty( $rule['compare'] ) ? $rule['compare'] : '=';
$value = $rule['value'];
if ( empty( $field ) || empty( $compare ) ) {
return true;
}
// Format for easier readability.
$compare = strtoupper( str_replace( '-', ' ', $compare ) );
$check_value = pods_v( $field, $values );
if ( ! in_array( $compare, [
'EMPTY',
'NOT EMPTY',
], true ) ) {
if ( null === $value ) {
$value = '';
} elseif ( is_bool( $value ) ) {
$value = (int) $value;
}
if ( null === $check_value ) {
$check_value = '';
} elseif ( is_bool( $check_value ) ) {
$check_value = (int) $check_value;
}
}
if ( 'LIKE' === $compare ) {
if ( '' === $value ) {
return true;
}
if ( null !== $check_value && ! is_scalar( $check_value ) ) {
return false;
}
if ( null !== $value && ! is_scalar( $value ) ) {
return false;
}
if ( function_exists( 'str_contains' ) ) {
return str_contains( strtolower( (string) $check_value ), strtolower( (string) $value ) );
}
return false !== stripos( (string) $check_value, (string) $value );
}
if ( 'NOT LIKE' === $compare ) {
if ( '' === $value ) {
return false;
}
if ( null !== $check_value && ! is_scalar( $check_value ) ) {
return true;
}
if ( null !== $value && ! is_scalar( $value ) ) {
return true;
}
if ( function_exists( 'str_contains' ) ) {
return ! str_contains( strtolower( (string) $check_value ), strtolower( (string) $value ) );
}
return false === stripos( (string) $check_value, (string) $value );
}
if ( 'BEGINS' === $compare ) {
if ( '' === $value ) {
return true;
}
if ( null !== $check_value && ! is_scalar( $check_value ) ) {
return false;
}
if ( null !== $value && ! is_scalar( $value ) ) {
return false;
}
if ( function_exists( 'str_starts_with' ) ) {
return str_starts_with( strtolower( (string) $check_value ), strtolower( (string) $value ) );
}
return 0 === stripos( (string) $check_value, (string) $value );
}
if ( 'NOT BEGINS' === $compare ) {
if ( '' === $value ) {
return false;
}
if ( null !== $check_value && ! is_scalar( $check_value ) ) {
return true;
}
if ( null !== $value && ! is_scalar( $value ) ) {
return true;
}
if ( function_exists( 'str_starts_with' ) ) {
return ! str_starts_with( strtolower( (string) $check_value ), strtolower( (string) $value ) );
}
return 0 !== stripos( (string) $check_value, (string) $value );
}
if ( 'ENDS' === $compare ) {
if ( '' === $value ) {
return true;
}
if ( null !== $check_value && ! is_scalar( $check_value ) ) {
return false;
}
if ( null !== $value && ! is_scalar( $value ) ) {
return false;
}
if ( function_exists( 'str_ends_with' ) ) {
return str_ends_with( strtolower( (string) $check_value ), strtolower( (string) $value ) );
}
return 0 === substr_compare( (string) $check_value, (string) $value, - strlen( (string) $value ) );
}
if ( 'NOT ENDS' === $compare ) {
if ( '' === $value ) {
return false;
}
if ( null !== $check_value && ! is_scalar( $check_value ) ) {
return true;
}
if ( null !== $value && ! is_scalar( $value ) ) {
return true;
}
if ( function_exists( 'str_ends_with' ) ) {
return ! str_ends_with( strtolower( (string) $check_value ), strtolower( (string) $value ) );
}
return 0 !== substr_compare( (string) $check_value, (string) $value, - strlen( (string) $value ) );
}
if ( 'MATCHES' === $compare ) {
if ( is_array( $check_value ) ) {
foreach ( $check_value as $check_value_item ) {
if ( 1 === preg_match( '/' . str_replace( '/', '\/', (string) $value ) . '/', (string) $check_value_item ) ) {
return true;
}
}
return false;
}
if ( ! is_scalar( $check_value ) ) {
return false;
}
return 1 === preg_match( '/' . str_replace( '/', '\/', (string) $value ) . '/', (string) $check_value );
}
if ( 'NOT MATCHES' === $compare ) {
if ( is_array( $check_value ) ) {
foreach ( $check_value as $check_value_item ) {
if ( 0 === preg_match( '/' . str_replace( '/', '\/', (string) $value ) . '/', (string) $check_value_item ) ) {
return true;
}
}
return false;
}
if ( ! is_scalar( $check_value ) ) {
return true;
}
return 0 === preg_match( '/' . str_replace( '/', '\/', (string) $value ) . '/', (string) $check_value );
}
if ( 'IN' === $compare ) {
if ( ! is_scalar( $check_value ) ) {
return false;
}
return in_array( $check_value, (array) $value, false );
}
if ( 'NOT IN' === $compare ) {
if ( ! is_scalar( $check_value ) ) {
return true;
}
return ! in_array( $check_value, (array) $value, false );
}
if ( 'IN VALUES' === $compare ) {
if ( ! is_scalar( $value ) ) {
return false;
}
return in_array( $value, (array) $check_value, false );
}
if ( 'NOT IN VALUES' === $compare ) {
if ( ! is_scalar( $value ) ) {
return true;
}
return ! in_array( $value, (array) $check_value, false );
}
if ( 'EMPTY' === $compare ) {
return in_array( $check_value, [ '', null, [], false ], true );
}
if ( 'NOT EMPTY' === $compare ) {
return ! in_array( $check_value, [ '', null, [], false ], true );
}
// Numeric comparisons enforce floats on numeric values for strict checks.
if ( is_numeric( $value ) ) {
$value = (float) $value;
}
if ( is_numeric( $check_value ) ) {
$check_value = (float) $check_value;
}
if ( '=' === $compare ) {
if ( ! is_scalar( $check_value ) ) {
return false;
}
return $check_value === $value;
}
if ( '!=' === $compare ) {
if ( ! is_scalar( $check_value ) ) {
return true;
}
return $check_value !== $value;
}
if ( ! is_scalar( $check_value ) ) {
return false;
}
if ( '<' === $compare ) {
return (float) $value < (float) $check_value;
}
if ( '<=' === $compare ) {
return (float) $value <= (float) $check_value;
}
if ( '>' === $compare ) {
return (float) $value > (float) $check_value;
}
if ( '>=' === $compare ) {
return (float) $value >= (float) $check_value;
}
return false;
}
}