| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Actions\Conditions; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Tools; |
| 7 |
use Jet_Form_Builder\Exceptions\Condition_Silence_Exception; |
| 8 |
use Jet_Form_Builder\Presets\Types\Dynamic_Preset; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
class Condition_Instance { |
| 16 |
|
| 17 |
private $type; |
| 18 |
private $events; |
| 19 |
|
| 20 |
private $operator; |
| 21 |
private $must_be_true = true; |
| 22 |
private $field_name; |
| 23 |
private $compare; |
| 24 |
private $compare_value_format; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var Condition_Manager |
| 28 |
*/ |
| 29 |
private $manager; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param $condition |
| 33 |
* |
| 34 |
* @return $this |
| 35 |
*/ |
| 36 |
public function set_condition( $condition ): Condition_Instance { |
| 37 |
$this->set_type( $condition['type'] ?? '' ); |
| 38 |
|
| 39 |
if ( ! $this->is_field_compare() ) { |
| 40 |
$this->set_events( $condition['events'] ?? array() ); |
| 41 |
|
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
$this->set_operator( $condition['operator'] ?? false ); |
| 46 |
$this->set_field( $condition['field'] ?? '' ); |
| 47 |
$this->set_compare_value_format( $condition['compare_value_format'] ?? '' ); |
| 48 |
$this->set_compare( $this->get_parsed_value( $condition['default'] ?? '' ) ); |
| 49 |
$this->set_must_be( $condition['execute'] ?? '' ); |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return bool |
| 56 |
*/ |
| 57 |
public function is_correct(): bool { |
| 58 |
$this->sanitize_operator(); |
| 59 |
|
| 60 |
$result = $this->check(); |
| 61 |
|
| 62 |
return $this->get_must_be() ? $result : ! $result; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @throws Condition_Silence_Exception |
| 67 |
*/ |
| 68 |
public function is_correct_with_throw() { |
| 69 |
$this->end_condition( |
| 70 |
$this->is_correct(), |
| 71 |
'The condition was not met.', |
| 72 |
get_object_vars( $this ) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function get_compare_value_transformer() { |
| 77 |
return $this->compare_value_format; |
| 78 |
} |
| 79 |
|
| 80 |
public function set_compare_value_format( $format ): Condition_Instance { |
| 81 |
$this->compare_value_format = (string) $format; |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
public function set_operator( string $operator ): Condition_Instance { |
| 88 |
$this->operator = $operator; |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
public function sanitize_operator(): Condition_Instance { |
| 94 |
$operators = $this->get_manager()->get_operators_list(); |
| 95 |
|
| 96 |
if ( ! $this->operator || ! in_array( $this->operator, $operators, true ) ) { |
| 97 |
$this->operator = ''; |
| 98 |
} |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
public function get_operator() { |
| 104 |
return $this->operator; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
public function get_events() { |
| 111 |
return $this->events; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param mixed $events |
| 116 |
*/ |
| 117 |
public function set_events( array $events ) { |
| 118 |
$this->events = $events; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param $field_name |
| 123 |
*/ |
| 124 |
public function set_field( $field_name ) { |
| 125 |
$this->field_name = $field_name; |
| 126 |
} |
| 127 |
|
| 128 |
public function get_field_name() { |
| 129 |
return $this->field_name; |
| 130 |
} |
| 131 |
|
| 132 |
public function get_field_value() { |
| 133 |
return jet_fb_context()->get_value( $this->get_field_name() ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @return mixed |
| 138 |
*/ |
| 139 |
public function get_type() { |
| 140 |
return $this->type; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param mixed $type |
| 145 |
*/ |
| 146 |
public function set_type( string $type ) { |
| 147 |
$this->type = empty( $type ) ? 'field' : $type; |
| 148 |
} |
| 149 |
|
| 150 |
public function is_field_compare(): bool { |
| 151 |
return 'field' === $this->get_type(); |
| 152 |
} |
| 153 |
|
| 154 |
public function set_compare( $compare ): Condition_Instance { |
| 155 |
return $this->set_compare_raw( $compare )->transform_compare_value(); |
| 156 |
} |
| 157 |
|
| 158 |
public function set_compare_raw( $raw_compare ): Condition_Instance { |
| 159 |
$this->compare = $raw_compare; |
| 160 |
|
| 161 |
return $this; |
| 162 |
} |
| 163 |
|
| 164 |
public function transform_compare_value(): Condition_Instance { |
| 165 |
$transformer = $this->get_compare_value_transformer(); |
| 166 |
|
| 167 |
if ( ! $transformer ) { |
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
$transformer = $this->get_manager()->get_transformers_find( |
| 172 |
'value', |
| 173 |
$transformer |
| 174 |
); |
| 175 |
|
| 176 |
$callback = $transformer['callback'] ?? false; |
| 177 |
|
| 178 |
if ( ! is_callable( $callback ) ) { |
| 179 |
return $this; |
| 180 |
} |
| 181 |
|
| 182 |
$this->set_compare_value_with_callback( $callback ); |
| 183 |
|
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
public function set_compare_value_with_callback( $transformer_callback ) { |
| 188 |
$operators_need_explode = $this->get_manager()->get_operators_filter( 'need_explode', true ); |
| 189 |
|
| 190 |
if ( ! in_array( $this->get_operator(), $operators_need_explode, true ) ) { |
| 191 |
$this->set_compare_raw( call_user_func( $transformer_callback, $this->get_compare() ) ); |
| 192 |
|
| 193 |
return; |
| 194 |
} |
| 195 |
$this->set_compare_raw( array_map( $transformer_callback, $this->get_compare_as_array() ) ); |
| 196 |
} |
| 197 |
|
| 198 |
public function get_compare() { |
| 199 |
return $this->compare; |
| 200 |
} |
| 201 |
|
| 202 |
public function set_must_be( $must_be ): Condition_Instance { |
| 203 |
$this->must_be_true = (bool) $must_be; |
| 204 |
|
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
public function get_must_be(): bool { |
| 209 |
return $this->must_be_true; |
| 210 |
} |
| 211 |
|
| 212 |
public function get_parsed_value( $maybe_json_string ) { |
| 213 |
// Action conditions are stored in the form's `_jf_actions` meta. |
| 214 |
return jet_fb_parse_dynamic_trusted( (string) $maybe_json_string ); |
| 215 |
} |
| 216 |
|
| 217 |
public function get_compare_as_array(): array { |
| 218 |
$compare = $this->get_compare(); |
| 219 |
|
| 220 |
if ( is_array( $compare ) ) { |
| 221 |
return $compare; |
| 222 |
} |
| 223 |
|
| 224 |
$this->compare = array_map( 'trim', explode( ',', $compare ) ); |
| 225 |
|
| 226 |
return $this->compare; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* @return bool |
| 231 |
*/ |
| 232 |
public function check(): bool { |
| 233 |
switch ( $this->get_operator() ) { |
| 234 |
case 'equal': |
| 235 |
return ( (string) $this->get_field_value() ) === ( (string) $this->get_compare() ); |
| 236 |
case 'greater': |
| 237 |
return $this->get_field_value() > $this->get_compare(); |
| 238 |
case 'less': |
| 239 |
return $this->get_field_value() < $this->get_compare(); |
| 240 |
|
| 241 |
case 'between': |
| 242 |
$field = $this->get_field_value(); |
| 243 |
$compare_values = $this->get_compare_as_array(); |
| 244 |
|
| 245 |
if ( count( $compare_values ) !== 2 ) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
return ( $compare_values[0] < $field && $compare_values[1] > $field ); |
| 250 |
case 'one_of': |
| 251 |
return $this->check_one_of(); |
| 252 |
case 'contain': |
| 253 |
$field = Tools::to_string( $this->get_field_value() ); |
| 254 |
$compare = Tools::to_string( $this->get_compare() ); |
| 255 |
|
| 256 |
return ( strpos( $field, $compare ) !== false ); |
| 257 |
default: |
| 258 |
return apply_filters( 'jet-form-builder/actions/process-condition', false, $this ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
protected function check_one_of(): bool { |
| 263 |
$field = $this->get_field_value(); |
| 264 |
$compare_values = $this->get_compare_as_array(); |
| 265 |
|
| 266 |
if ( ! is_array( $compare_values ) ) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
// then value is from checkbox field |
| 271 |
if ( is_array( $field ) ) { |
| 272 |
return $this->check_one_of_list(); |
| 273 |
} |
| 274 |
|
| 275 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 276 |
return in_array( $field, $compare_values ); |
| 277 |
} |
| 278 |
|
| 279 |
public function check_one_of_list(): bool { |
| 280 |
$field = $this->get_field_value(); |
| 281 |
$compare_values = $this->get_compare_as_array(); |
| 282 |
|
| 283 |
foreach ( $field as $current ) { |
| 284 |
if ( in_array( $current, $compare_values, true ) ) { |
| 285 |
return true; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* @param $success |
| 294 |
* @param mixed ...$additional |
| 295 |
* |
| 296 |
* @throws Condition_Silence_Exception |
| 297 |
*/ |
| 298 |
public function end_condition( $success, ...$additional ) { |
| 299 |
$this->get_manager()->throw_by_method( $success, ...$additional ); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* @param mixed ...$additional |
| 304 |
* |
| 305 |
* @throws Condition_Silence_Exception |
| 306 |
*/ |
| 307 |
public function error( ...$additional ) { |
| 308 |
$this->end_condition( false, ...$additional ); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* @param Condition_Manager $manager |
| 313 |
*/ |
| 314 |
public function set_manager( Condition_Manager $manager ): void { |
| 315 |
$this->manager = $manager; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* @return Condition_Manager |
| 320 |
*/ |
| 321 |
public function get_manager(): Condition_Manager { |
| 322 |
return $this->manager; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
} |
| 327 |
|