condition-instance.php
326 lines
| 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 | return ( new Dynamic_Preset() )->parse_json( $maybe_json_string ); |
| 214 | } |
| 215 | |
| 216 | public function get_compare_as_array(): array { |
| 217 | $compare = $this->get_compare(); |
| 218 | |
| 219 | if ( is_array( $compare ) ) { |
| 220 | return $compare; |
| 221 | } |
| 222 | |
| 223 | $this->compare = array_map( 'trim', explode( ',', $compare ) ); |
| 224 | |
| 225 | return $this->compare; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @return bool |
| 230 | */ |
| 231 | public function check(): bool { |
| 232 | switch ( $this->get_operator() ) { |
| 233 | case 'equal': |
| 234 | return ( (string) $this->get_field_value() ) === ( (string) $this->get_compare() ); |
| 235 | case 'greater': |
| 236 | return $this->get_field_value() > $this->get_compare(); |
| 237 | case 'less': |
| 238 | return $this->get_field_value() < $this->get_compare(); |
| 239 | |
| 240 | case 'between': |
| 241 | $field = $this->get_field_value(); |
| 242 | $compare_values = $this->get_compare_as_array(); |
| 243 | |
| 244 | if ( count( $compare_values ) !== 2 ) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return ( $compare_values[0] < $field && $compare_values[1] > $field ); |
| 249 | case 'one_of': |
| 250 | return $this->check_one_of(); |
| 251 | case 'contain': |
| 252 | $field = Tools::to_string( $this->get_field_value() ); |
| 253 | $compare = Tools::to_string( $this->get_compare() ); |
| 254 | |
| 255 | return ( strpos( $field, $compare ) !== false ); |
| 256 | default: |
| 257 | return apply_filters( 'jet-form-builder/actions/process-condition', false, $this ); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | protected function check_one_of(): bool { |
| 262 | $field = $this->get_field_value(); |
| 263 | $compare_values = $this->get_compare_as_array(); |
| 264 | |
| 265 | if ( ! is_array( $compare_values ) ) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // then value is from checkbox field |
| 270 | if ( is_array( $field ) ) { |
| 271 | return $this->check_one_of_list(); |
| 272 | } |
| 273 | |
| 274 | // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 275 | return in_array( $field, $compare_values ); |
| 276 | } |
| 277 | |
| 278 | public function check_one_of_list(): bool { |
| 279 | $field = $this->get_field_value(); |
| 280 | $compare_values = $this->get_compare_as_array(); |
| 281 | |
| 282 | foreach ( $field as $current ) { |
| 283 | if ( in_array( $current, $compare_values, true ) ) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * @param $success |
| 293 | * @param mixed ...$additional |
| 294 | * |
| 295 | * @throws Condition_Silence_Exception |
| 296 | */ |
| 297 | public function end_condition( $success, ...$additional ) { |
| 298 | $this->get_manager()->throw_by_method( $success, ...$additional ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @param mixed ...$additional |
| 303 | * |
| 304 | * @throws Condition_Silence_Exception |
| 305 | */ |
| 306 | public function error( ...$additional ) { |
| 307 | $this->end_condition( false, ...$additional ); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @param Condition_Manager $manager |
| 312 | */ |
| 313 | public function set_manager( Condition_Manager $manager ): void { |
| 314 | $this->manager = $manager; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @return Condition_Manager |
| 319 | */ |
| 320 | public function get_manager(): Condition_Manager { |
| 321 | return $this->manager; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | } |
| 326 |