| 1 |
<?php |
| 2 |
/** |
| 3 |
* Conditional logic for Form Builder fields. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Repeater; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Shows or hides a field depending on what was entered in another one. |
| 19 |
*/ |
| 20 |
class Form_Conditional_Logic |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Comparisons a rule can use. |
| 24 |
* |
| 25 |
* @return array<string,string> |
| 26 |
*/ |
| 27 |
public static function operators(): array |
| 28 |
{ |
| 29 |
return [ |
| 30 |
'is' => esc_html__('is', 'king-addons'), |
| 31 |
'is_not' => esc_html__('is not', 'king-addons'), |
| 32 |
'contains' => esc_html__('contains', 'king-addons'), |
| 33 |
'not_contains' => esc_html__('does not contain', 'king-addons'), |
| 34 |
'is_empty' => esc_html__('is empty', 'king-addons'), |
| 35 |
'is_not_empty' => esc_html__('is not empty', 'king-addons'), |
| 36 |
'gt' => esc_html__('is greater than', 'king-addons'), |
| 37 |
'lt' => esc_html__('is less than', 'king-addons'), |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Add the per-field controls to the fields repeater. |
| 43 |
* |
| 44 |
* @param Repeater $repeater Fields repeater. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public static function register_controls(Repeater $repeater): void |
| 49 |
{ |
| 50 |
$repeater->add_control( |
| 51 |
'conditional_enable', |
| 52 |
[ |
| 53 |
'label' => esc_html__('Conditional logic', 'king-addons'), |
| 54 |
'type' => Controls_Manager::SWITCHER, |
| 55 |
'return_value' => 'yes', |
| 56 |
'separator' => 'before', |
| 57 |
] |
| 58 |
); |
| 59 |
|
| 60 |
$repeater->add_control( |
| 61 |
'conditional_action', |
| 62 |
[ |
| 63 |
'label' => esc_html__('Then', 'king-addons'), |
| 64 |
'type' => Controls_Manager::SELECT, |
| 65 |
'options' => [ |
| 66 |
'show' => esc_html__('Show this field', 'king-addons'), |
| 67 |
'hide' => esc_html__('Hide this field', 'king-addons'), |
| 68 |
], |
| 69 |
'default' => 'show', |
| 70 |
'condition' => ['conditional_enable' => 'yes'], |
| 71 |
] |
| 72 |
); |
| 73 |
|
| 74 |
$repeater->add_control( |
| 75 |
'conditional_relation', |
| 76 |
[ |
| 77 |
'label' => esc_html__('When', 'king-addons'), |
| 78 |
'type' => Controls_Manager::SELECT, |
| 79 |
'options' => [ |
| 80 |
'all' => esc_html__('All rules match', 'king-addons'), |
| 81 |
'any' => esc_html__('Any rule matches', 'king-addons'), |
| 82 |
], |
| 83 |
'default' => 'all', |
| 84 |
'condition' => ['conditional_enable' => 'yes'], |
| 85 |
] |
| 86 |
); |
| 87 |
|
| 88 |
foreach ([1, 2] as $index) { |
| 89 |
$suffix = 1 === $index ? '' : '_2'; |
| 90 |
|
| 91 |
$repeater->add_control( |
| 92 |
'conditional_field' . $suffix, |
| 93 |
[ |
| 94 |
'label' => 1 === $index |
| 95 |
? esc_html__('Field ID', 'king-addons') |
| 96 |
: esc_html__('Second field ID', 'king-addons'), |
| 97 |
'type' => Controls_Manager::TEXT, |
| 98 |
'description' => 1 === $index |
| 99 |
? esc_html__('The ID of the field to watch, as set in its own "Field ID" box.', 'king-addons') |
| 100 |
: esc_html__('Leave empty to use one rule only.', 'king-addons'), |
| 101 |
'condition' => ['conditional_enable' => 'yes'], |
| 102 |
] |
| 103 |
); |
| 104 |
|
| 105 |
$repeater->add_control( |
| 106 |
'conditional_operator' . $suffix, |
| 107 |
[ |
| 108 |
'label' => esc_html__('Comparison', 'king-addons'), |
| 109 |
'type' => Controls_Manager::SELECT, |
| 110 |
'options' => self::operators(), |
| 111 |
'default' => 'is', |
| 112 |
'condition' => ['conditional_enable' => 'yes'], |
| 113 |
] |
| 114 |
); |
| 115 |
|
| 116 |
$repeater->add_control( |
| 117 |
'conditional_value' . $suffix, |
| 118 |
[ |
| 119 |
'label' => esc_html__('Value', 'king-addons'), |
| 120 |
'type' => Controls_Manager::TEXT, |
| 121 |
'dynamic' => ['active' => true], |
| 122 |
'condition' => [ |
| 123 |
'conditional_enable' => 'yes', |
| 124 |
'conditional_operator' . $suffix . '!' => ['is_empty', 'is_not_empty'], |
| 125 |
], |
| 126 |
] |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* The rule set for one field, ready to be printed as a data attribute. |
| 133 |
* |
| 134 |
* @param array<string,mixed> $item Repeater item. |
| 135 |
* |
| 136 |
* @return array<string,mixed>|null |
| 137 |
*/ |
| 138 |
public static function build($item): ?array |
| 139 |
{ |
| 140 |
if (!is_array($item) || 'yes' !== ($item['conditional_enable'] ?? '')) { |
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
$operators = array_keys(self::operators()); |
| 145 |
$rules = []; |
| 146 |
|
| 147 |
foreach (['', '_2'] as $suffix) { |
| 148 |
$field = isset($item['conditional_field' . $suffix]) |
| 149 |
? trim((string) $item['conditional_field' . $suffix]) |
| 150 |
: ''; |
| 151 |
|
| 152 |
if ('' === $field) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$operator = (string) ($item['conditional_operator' . $suffix] ?? 'is'); |
| 157 |
if (!in_array($operator, $operators, true)) { |
| 158 |
$operator = 'is'; |
| 159 |
} |
| 160 |
|
| 161 |
$rules[] = [ |
| 162 |
'field' => $field, |
| 163 |
'op' => $operator, |
| 164 |
'value' => (string) ($item['conditional_value' . $suffix] ?? ''), |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
if (empty($rules)) { |
| 169 |
return null; |
| 170 |
} |
| 171 |
|
| 172 |
return [ |
| 173 |
'action' => 'hide' === ($item['conditional_action'] ?? 'show') ? 'hide' : 'show', |
| 174 |
'relation' => 'any' === ($item['conditional_relation'] ?? 'all') ? 'any' : 'all', |
| 175 |
'rules' => $rules, |
| 176 |
]; |
| 177 |
} |
| 178 |
} |
| 179 |
|