1, * 'match' => 'all' | 'any', * 'rules' => array( array( 'field' => 'other_name', 'operator' => '==', 'value' => 'x' ), ... ), * ) * * The browser evaluates it live (admin/js/meta-box.js). A hidden field is * only hidden, not disabled: whatever is in it is still saved. * * @package Fieldloom */ if ( ! defined( 'ABSPATH' ) ) { exit; } class CFStudio_Conditional { /** operator => label. */ public static function operators() { return array( '==' => __( 'is equal to', 'fieldloom' ), '!=' => __( 'is not equal to', 'fieldloom' ), 'empty' => __( 'is empty', 'fieldloom' ), '!empty' => __( 'is not empty', 'fieldloom' ), 'contains' => __( 'contains', 'fieldloom' ), '>' => __( 'is greater than', 'fieldloom' ), '<' => __( 'is less than', 'fieldloom' ), ); } /** Clean the posted conditional_logic of one field. Empty array = none. */ public static function sanitize( $raw ) { $raw = is_array( $raw ) ? $raw : array(); if ( empty( $raw['enabled'] ) || empty( $raw['rules'] ) || ! is_array( $raw['rules'] ) ) { return array(); } $rules = array(); foreach ( $raw['rules'] as $rule ) { if ( ! is_array( $rule ) ) { continue; } $field = isset( $rule['field'] ) && is_scalar( $rule['field'] ) ? sanitize_key( $rule['field'] ) : ''; $op = isset( $rule['operator'] ) && is_scalar( $rule['operator'] ) ? (string) $rule['operator'] : '=='; if ( '' === $field ) { continue; } $rules[] = array( 'field' => $field, 'operator' => array_key_exists( $op, self::operators() ) ? $op : '==', 'value' => isset( $rule['value'] ) && is_scalar( $rule['value'] ) ? sanitize_text_field( $rule['value'] ) : '', ); } if ( ! $rules ) { return array(); } return array( 'enabled' => 1, 'match' => ( isset( $raw['match'] ) && 'any' === $raw['match'] ) ? 'any' : 'all', 'rules' => $rules, ); } /** JSON for the data attribute on a field wrapper, or '' when none. */ public static function data_attr( $field ) { $logic = $field['conditional_logic'] ?? array(); if ( empty( $logic['enabled'] ) || empty( $logic['rules'] ) ) { return ''; } return wp_json_encode( array( 'match' => $logic['match'] ?? 'all', 'rules' => $logic['rules'], ) ); } /** Builder UI for one field row (inside its generic settings). */ public static function render_builder( $field, $prefix ) { $logic = $field['conditional_logic'] ?? array(); $rules = isset( $logic['rules'] ) && is_array( $logic['rules'] ) ? array_values( $logic['rules'] ) : array(); $name = $prefix . '[conditional_logic]'; ?>