PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.6
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.6
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmFormAction.php +219 -325 6.306.6 View file →
@@ -4,71 +4,19 @@
4 4 }
5 5
6 6 class FrmFormAction {
7 7
8 - /**
9 - * Root id for all actions of this type.
10 - *
11 - * @var string
12 - */
13 - public $id_base;
14 -
15 - /**
16 - * Name for this action type.
17 - *
18 - * @var string
19 - */
20 - public $name;
21 -
22 - /**
23 - * Name of the option used to store the settings for this action type.
24 - *
25 - * @var string
26 - */
8 + public $id_base; // Root id for all actions of this type.
9 + public $name; // Name for this action type.
27 10 public $option_name;
11 + public $action_options; // Option array passed to wp_register_sidebar_widget()
12 + public $control_options; // Option array passed to wp_register_widget_control()
28 13
29 - /**
30 - * Option array passed to wp_register_sidebar_widget().
31 - *
32 - * @var array
33 - */
34 - public $action_options;
14 + public $form_id; // The ID of the form to evaluate
15 + public $number = false; // Unique ID number of the current instance.
16 + public $id = ''; // Unique ID string of the current instance (id_base-number)
17 + public $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice.
35 18
36 - /**
37 - * Option array passed to wp_register_widget_control().
38 - *
39 - * @var array
40 - */
41 - public $control_options;
42 -
43 - /**
44 - * The ID of the form to evaluate.
45 - *
46 - * @var int
47 - */
48 - public $form_id;
49 -
50 - /**
51 - * Unique ID number of the current instance.
52 - *
53 - * @var int
54 - */
55 - public $number = false;
56 -
57 - /**
58 - * Unique ID string of the current instance (id_base-number).
59 - *
60 - * @var string
61 - */
62 - public $id = '';
63 -
64 - /**
65 - * Set true when we update the data after a POST submit - makes sure we don't do it twice.
66 - *
67 - * @var bool
68 - */
69 - public $updated = false;
70 -
71 19 // Member functions that you must over-ride.
72 20
73 21 /**
74 22 * This function should check that $new_instance is set correctly.
@@ -74,10 +22,10 @@
74 22 * This function should check that $new_instance is set correctly.
75 23 * The newly calculated value of $instance should be returned.
76 24 * If "false" is returned, the instance won't be saved/updated.
77 25 *
78 - * @param array $new_instance New settings for this instance as input by the user via form().
79 - * @param array $old_instance Old settings for this instance.
26 + * @param array $new_instance New settings for this instance as input by the user via form()
27 + * @param array $old_instance Old settings for this instance
80 28 *
81 29 * @return array Settings to save or bool false to cancel saving
82 30 */
83 31 public function update( $new_instance, $old_instance ) {
@@ -86,15 +34,14 @@
86 34
87 35 /**
88 36 * Echo the settings update form
89 37 *
90 - * @param WP_Post $instance Current settings.
38 + * @param WP_Post $instance Current settings
91 39 * @param array $args
92 - *
93 - * @return string
94 40 */
95 41 public function form( $instance, $args = array() ) {
96 42 echo '<p class="no-options-widget">' . esc_html__( 'There are no options for this action.', 'formidable' ) . '</p>';
43 +
97 44 return 'noform';
98 45 }
99 46
100 47 /**
@@ -110,14 +57,8 @@
110 57 public function get_switch_fields() {
111 58 return array();
112 59 }
113 60
114 - /**
115 - * @param object $action
116 - * @param object $form
117 - *
118 - * @return object
119 - */
120 61 public function migrate_values( $action, $form ) {
121 62 return $action;
122 63 }
123 64
@@ -128,14 +69,14 @@
128 69 *
129 70 * @param string $id_base Optional Base ID for the widget, lower case,
130 71 * if left empty a portion of the widget's class name will be used. Has to be unique.
131 72 * @param string $name Name for the widget displayed on the configuration page.
132 - * @param array $action_options Optional Passed to wp_register_sidebar_widget().
133 - * - description: shown on the configuration page.
134 - * - classname.
135 - * @param array $control_options Optional Passed to wp_register_widget_control().
136 - * - width: required if more than 250px.
137 - * - height: currently not used but may be needed in the future.
73 + * @param array $action_options Optional Passed to wp_register_sidebar_widget()
74 + * - description: shown on the configuration page
75 + * - classname
76 + * @param array $control_options Optional Passed to wp_register_widget_control()
77 + * - width: required if more than 250px
78 + * - height: currently not used but may be needed in the future
138 79 */
139 80 public function __construct( $id_base, $name, $action_options = array(), $control_options = array() ) {
140 81 if ( ! defined( 'ABSPATH' ) ) {
141 82 die( 'You are not allowed to call this page directly.' );
@@ -170,13 +111,12 @@
170 111 $action_options['color'] = 'var(--' . reset( $colors ) . ')';
171 112 }
172 113
173 114 $upgrade_class = isset( $action_options['classes'] ) && $action_options['classes'] === 'frm_show_upgrade';
174 -
175 115 if ( $action_options['group'] === $id_base ) {
176 - $upgrade_class = str_contains( $action_options['classes'], 'frm_show_upgrade' );
116 + $upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false;
177 117 $action_options['classes'] = $group['icon'];
178 - } elseif ( empty( $action_options['classes'] ) || $upgrade_class ) {
118 + } elseif ( ! isset( $action_options['classes'] ) || empty( $action_options['classes'] ) || $upgrade_class ) {
179 119 $action_options['classes'] = $group['icon'];
180 120 }
181 121
182 122 if ( $upgrade_class ) {
@@ -188,13 +128,8 @@
188 128 }
189 129
190 130 /**
191 131 * @param string $id_base
192 - * @param string $name
193 - * @param array $action_options
194 - * @param array $control_options
195 - *
196 - * @return void
197 132 */
198 133 public function FrmFormAction( $id_base, $name, $action_options = array(), $control_options = array() ) {
199 134 self::__construct( $id_base, $name, $action_options, $control_options );
200 135 }
@@ -201,28 +136,22 @@
201 136
202 137 /**
203 138 * Help to switch old field id by new field id for duplicate form
204 139 *
205 - * @param string $action id of the field that needs to be switched.
140 + * @param string $action id of the field that needs to be switched
206 141 *
207 142 * @return string
208 143 */
209 144 public function maybe_switch_field_ids( $action ) {
210 145 $updated_action = apply_filters( 'frm_maybe_switch_field_ids', $action );
211 -
212 146 if ( $updated_action === $action ) {
213 - return FrmFieldsHelper::switch_field_ids( $action );
147 + $updated_action = FrmFieldsHelper::switch_field_ids( $action );
214 148 }
215 -
216 149 return $updated_action;
217 150 }
218 151
219 152 /**
220 153 * @since 4.0
221 - *
222 - * @param array $action_options
223 - *
224 - * @return array
225 154 */
226 155 protected function get_group( $action_options ) {
227 156 $groups = FrmFormActionsController::form_action_groups();
228 157 $group = 'misc';
@@ -232,9 +161,9 @@
232 161 } elseif ( isset( $groups[ $this->id_base ] ) ) {
233 162 $group = $this->id_base;
234 163 } else {
235 164 foreach ( $groups as $name => $check_group ) {
236 - if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'], true ) ) {
165 + if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'] ) ) {
237 166 $group = $name;
238 167 break;
239 168 }
240 169 }
@@ -248,18 +177,18 @@
248 177 * Constructs name attributes for use in form() fields
249 178 *
250 179 * This function should be used in form() methods to create name attributes for fields to be saved by update()
251 180 *
252 - * @param string $field_name Field name.
253 - * @param string $post_field
181 + * @param string $field_name Field name
254 182 *
255 183 * @return string Name attribute for $field_name
256 184 */
257 185 public function get_field_name( $field_name, $post_field = 'post_content' ) {
258 - $name = $this->option_name . '[' . $this->number . ']';
259 - $name .= $post_field ? '[' . $post_field . ']' : '';
186 + $name = $this->option_name . '[' . $this->number . ']';
187 + $name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' );
188 + $name .= '[' . $field_name . ']';
260 189
261 - return $name . ( '[' . $field_name . ']' );
190 + return $name;
262 191 }
263 192
264 193 /**
265 194 * Constructs id attributes for use in form() fields
@@ -265,9 +194,9 @@
265 194 * Constructs id attributes for use in form() fields
266 195 *
267 196 * This function should be used in form() methods to create id attributes for fields to be saved by update()
268 197 *
269 - * @param string $field_name Field name.
198 + * @param string $field_name Field name
270 199 *
271 200 * @return string ID attribute for $field_name
272 201 */
273 202 public function get_field_id( $field_name ) {
@@ -275,9 +204,8 @@
275 204 }
276 205
277 206 /**
278 207 * @param int|string $number
279 - *
280 208 * @return void
281 209 */
282 210 public function _set( $number ) {
283 211 $this->number = $number;
@@ -284,10 +212,8 @@
284 212 $this->id = $this->id_base . '-' . $number;
285 213 }
286 214
287 215 /**
288 - * @param false|int|string $form_id
289 - *
290 216 * @return object
291 217 */
292 218 public function prepare_new( $form_id = false ) {
293 219 if ( $form_id ) {
@@ -296,9 +222,9 @@
296 222
297 223 $post_content = array();
298 224 $default_values = $this->get_global_defaults();
299 225
300 - // Fill default values
226 + // fill default values
301 227 $post_content = wp_parse_args( $post_content, $default_values );
302 228
303 229 if ( ! isset( $post_content['event'] ) && ! $this->action_options['force_event'] ) {
304 230 $post_content['event'] = array( reset( $this->action_options['event'] ) );
@@ -313,17 +239,13 @@
313 239 'post_type' => FrmFormActionsController::$action_post_type,
314 240 'post_name' => $this->form_id . '_' . $this->id_base . '_' . $this->number,
315 241 'menu_order' => $this->form_id,
316 242 );
243 + unset( $post_content );
317 244
318 245 return (object) $form_action;
319 246 }
320 247
321 - /**
322 - * @param int|string $form_id
323 - *
324 - * @return int|WP_Error
325 - */
326 248 public function create( $form_id ) {
327 249 $this->form_id = $form_id;
328 250
329 251 $action = $this->prepare_new();
@@ -331,17 +253,13 @@
331 253 return $this->save_settings( $action );
332 254 }
333 255
334 256 /**
335 - * @param int|string $form_id
336 - * @param int|string $old_id
337 - *
338 257 * @return void
339 258 */
340 259 public function duplicate_form_actions( $form_id, $old_id ) {
341 - // phpcs:ignore Universal.Operators.StrictComparisons
342 260 if ( $form_id == $old_id ) {
343 - // Don't duplicate the actions if this is a template getting updated
261 + // don't duplicate the actions if this is a template getting updated
344 262 return;
345 263 }
346 264
347 265 $this->form_id = $old_id;
@@ -347,9 +265,8 @@
347 265 $this->form_id = $old_id;
348 266 $actions = $this->get_all( $old_id );
349 267
350 268 $this->form_id = $form_id;
351 -
352 269 foreach ( $actions as $action ) {
353 270 $this->duplicate_one( $action, $form_id );
354 271 unset( $action );
355 272 }
@@ -354,34 +271,31 @@
354 271 unset( $action );
355 272 }
356 273 }
357 274
358 - /**
359 - * Check if imported action should be created or updated.
275 + /* Check if imported action should be created or updated
360 276 *
361 277 * @since 2.0
362 278 *
363 279 * @param array $action
364 - * @param array $forms
365 - *
366 - * @return int Post ID.
280 + * @return integer $post_id
367 281 */
368 282 public function maybe_create_action( $action, $forms ) {
369 - if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] === 'updated' ) {
283 + if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] == 'updated' ) {
370 284 // Update action only
371 285 $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
372 - return $this->save_settings( $action );
286 + $post_id = $this->save_settings( $action );
287 + } else {
288 + // Create action
289 + $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
290 + $post_id = $this->duplicate_one( (object) $action, $action['menu_order'] );
373 291 }
374 - // Create action
375 - $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
376 - return $this->duplicate_one( (object) $action, $action['menu_order'] );
292 +
293 + return $post_id;
377 294 }
378 295
379 296 /**
380 - * @param object $action
381 - * @param int|string $form_id
382 - *
383 - * @return int|WP_Error
297 + * @param object $action
384 298 */
385 299 public function duplicate_one( $action, $form_id ) {
386 300 global $frm_duplicate_ids;
387 301
@@ -393,9 +307,9 @@
393 307 $action->post_content[ $key ] = $frm_duplicate_ids[ $val ];
394 308 } elseif ( ! is_array( $val ) ) {
395 309 $action->post_content[ $key ] = FrmFieldsHelper::switch_field_ids( $val );
396 310 } elseif ( isset( $switch[ $key ] ) && is_array( $switch[ $key ] ) ) {
397 - // Loop through each value if empty
311 + // loop through each value if empty
398 312 if ( empty( $switch[ $key ] ) ) {
399 313 $switch[ $key ] = array_keys( $val );
400 314 }
401 315
@@ -410,15 +324,8 @@
410 324
411 325 return $this->save_settings( $action );
412 326 }
413 327
414 - /**
415 - * @param array $action
416 - * @param array|string $subkey
417 - * @param mixed $val
418 - *
419 - * @return array
420 - */
421 328 private function duplicate_array_walk( $action, $subkey, $val ) {
422 329 global $frm_duplicate_ids;
423 330
424 331 if ( is_array( $subkey ) ) {
@@ -434,15 +341,15 @@
434 341 } else {
435 342 foreach ( (array) $val as $ck => $cv ) {
436 343 if ( is_array( $cv ) ) {
437 344 $action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey, $cv );
438 - } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { // phpcs:ignore Universal.Operators.StrictComparisons
345 + } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) {
439 346 $action[ $ck ] = $frm_duplicate_ids[ $cv ];
440 - } elseif ( $ck == $subkey ) { // phpcs:ignore Universal.Operators.StrictComparisons
347 + } elseif ( $ck == $subkey ) {
441 348 $action[ $ck ] = $this->maybe_switch_field_ids( $action[ $ck ] );
442 349 }
443 350 }
444 - }//end if
351 + }
445 352
446 353 return $action;
447 354 }
448 355
@@ -449,12 +356,8 @@
449 356 /**
450 357 * Deal with changed settings.
451 358 *
452 359 * Do NOT over-ride this function
453 - *
454 - * @param int|string $form_id
455 - *
456 - * @return array|null
457 360 */
458 361 public function update_callback( $form_id ) {
459 362 $this->form_id = $form_id;
460 363
@@ -461,32 +364,32 @@
461 364 $all_instances = $this->get_settings();
462 365
463 366 // We need to update the data
464 367 if ( $this->updated ) {
465 - return null;
368 + return;
466 369 }
467 370
468 371 // phpcs:ignore WordPress.Security.NonceVerification.Missing
469 - if ( ! isset( $_POST[ $this->option_name ] ) || ! is_array( $_POST[ $this->option_name ] ) ) {
470 - return null;
372 + if ( isset( $_POST[ $this->option_name ] ) && is_array( $_POST[ $this->option_name ] ) ) {
373 + // Sanitizing removes scripts and <email> type of values.
374 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
375 + $settings = wp_unslash( $_POST[ $this->option_name ] );
376 + } else {
377 + return;
471 378 }
472 379
473 - // Sanitizing removes scripts and <email> type of values.
474 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
475 - $settings = wp_unslash( $_POST[ $this->option_name ] );
476 -
477 380 $action_ids = array();
478 381
479 382 foreach ( $settings as $number => $new_instance ) {
480 383 $this->_set( $number );
481 384
482 - $old_instance = $all_instances[ $number ] ?? array();
385 + $old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array();
483 386
484 387 if ( ! isset( $new_instance['post_status'] ) ) {
485 388 $new_instance['post_status'] = 'draft';
486 389 }
487 390
488 - // Settings were never opened, so don't update
391 + // settings were never opened, so don't update
489 392 if ( ! isset( $new_instance['post_title'] ) ) {
490 393 $this->maybe_update_status( $new_instance, $old_instance );
491 394 $action_ids[] = $new_instance['ID'];
492 395 $this->updated = true;
@@ -495,10 +398,10 @@
495 398
496 399 $new_instance['post_type'] = FrmFormActionsController::$action_post_type;
497 400 $new_instance['post_name'] = $this->form_id . '_' . $this->id_base . '_' . $this->number;
498 401 $new_instance['menu_order'] = $this->form_id;
499 - $new_instance['post_date'] = $old_instance->post_date ?? '';
500 - $instance = $this->update( $new_instance, $old_instance );
402 + $new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : '';
403 + $instance = $this->update( $new_instance, $old_instance );
501 404
502 405 /**
503 406 * Filter an action's settings before saving.
504 407 *
@@ -506,12 +409,12 @@
506 409 * to update settings.
507 410 *
508 411 * @since 2.0
509 412 *
510 - * @param array $instance The current widget instance's settings.
511 - * @param array $new_instance Array of new widget settings.
512 - * @param array $old_instance Array of old widget settings.
513 - * @param FrmFormAction $form_action FrmFormAction instance.
413 + * @param array $instance The current widget instance's settings.
414 + * @param array $new_instance Array of new widget settings.
415 + * @param array $old_instance Array of old widget settings.
416 + * @param WP_Widget $this The current widget instance.
514 417 */
515 418 $instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this );
516 419
517 420 $instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this );
@@ -523,9 +426,9 @@
523 426
524 427 $action_ids[] = $this->save_settings( $instance );
525 428
526 429 $this->updated = true;
527 - }//end foreach
430 + }
528 431
529 432 return $action_ids;
530 433 }
531 434
@@ -534,10 +437,9 @@
534 437 *
535 438 * @since 3.04
536 439 *
537 440 * @param array $new_instance
538 - * @param array|stdClass $old_instance
539 - *
441 + * @param stdClass|array $old_instance
540 442 * @return void
541 443 */
542 444 protected function maybe_update_status( $new_instance, $old_instance ) {
543 445 if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) {
@@ -552,26 +454,16 @@
552 454 )
553 455 );
554 456 }
555 457
556 - /**
557 - * @param array $settings
558 - *
559 - * @return int|WP_Error
560 - */
561 458 public function save_settings( $settings ) {
562 459 self::clear_cache();
460 +
563 461 return FrmDb::save_settings( $settings, 'frm_actions' );
564 462 }
565 463
566 - /**
567 - * @param int $id
568 - *
569 - * @return object|null
570 - */
571 464 public function get_single_action( $id ) {
572 465 $action = get_post( $id );
573 -
574 466 if ( $action ) {
575 467 $action = $this->prepare_action( $action );
576 468 $this->_set( $id );
577 469 }
@@ -578,43 +470,27 @@
578 470
579 471 return $action;
580 472 }
581 473
582 - /**
583 - * @param int|string $form_id
584 - *
585 - * @return array
586 - */
587 474 public function get_one( $form_id ) {
588 475 return $this->get_all( $form_id, 1 );
589 476 }
590 477
591 - /**
592 - * @param int|string $form_id
593 - * @param string $type
594 - * @param array $atts
595 - *
596 - * @return array
597 - */
598 478 public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) {
599 479 $action_controls = FrmFormActionsController::get_form_actions( $type );
600 -
601 - if ( ! $action_controls ) {
602 - // Don't continue if there are no available actions
480 + if ( empty( $action_controls ) ) {
481 + // don't continue if there are no available actions
603 482 return array();
604 483 }
605 484
606 - if ( 'all' !== $type ) {
607 - if ( is_array( $action_controls ) ) {
608 - return array();
609 - }
610 -
485 + if ( 'all' != $type ) {
611 486 return $action_controls->get_all( $form_id, $atts );
612 487 }
613 488
614 489 self::prepare_get_action( $atts );
615 490
616 - $limit = self::get_action_limit( $form_id, $atts['limit'] );
491 + $limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) );
492 +
617 493 $args = self::action_args( $form_id, $limit );
618 494 $args['post_status'] = $atts['post_status'];
619 495 $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
620 496
@@ -622,11 +498,10 @@
622 498 return array();
623 499 }
624 500
625 501 $settings = array();
626 -
627 502 foreach ( $actions as $action ) {
628 - // Some plugins/themes are formatting the post_excerpt
503 + // some plugins/themes are formatting the post_excerpt
629 504 $action->post_excerpt = sanitize_title( $action->post_excerpt );
630 505
631 506 if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) {
632 507 continue;
@@ -639,43 +514,30 @@
639 514 break;
640 515 }
641 516 }
642 517
643 - return 1 === $limit ? reset( $settings ) : $settings;
644 - }
518 + if ( 1 === $limit ) {
519 + $settings = reset( $settings );
520 + }
645 521
646 - /**
647 - * Get the limit for the number of actions for a single form. By default, this is 99, but
648 - * it can be modified with a code snippet.
649 - *
650 - * @since 6.17 This logic from moved from FrmFormAction::get_action_for_form.
651 - *
652 - * @param int|string $form_id
653 - * @param int|string $limit The unfiltered limit value.
654 - *
655 - * @return int The filtered limit value.
656 - */
657 - public static function get_action_limit( $form_id, $limit = 99 ) {
658 - $type = 'all';
659 - return (int) apply_filters( 'frm_form_action_limit', (int) $limit, compact( 'type', 'form_id' ) );
522 + return $settings;
660 523 }
661 524
662 525 /**
663 526 * @since 3.04
664 527 *
665 - * @param array|string $args
666 - * @param string $default_status
528 + * @param array $args
529 + * @param string $default_status
667 530 *
668 531 * @return void
669 532 */
670 533 protected static function prepare_get_action( &$args, $default_status = 'publish' ) {
671 534 if ( is_numeric( $args ) ) {
672 - // For reverse compatibility. $limit was changed to $args
535 + // for reverse compatibility. $limit was changed to $args
673 536 $args = array(
674 537 'limit' => $args,
675 538 );
676 539 }
677 -
678 540 $defaults = array(
679 541 'limit' => 99,
680 542 'post_status' => $default_status,
681 543 );
@@ -682,21 +544,14 @@
682 544 $args = wp_parse_args( $args, $defaults );
683 545 }
684 546
685 547 /**
686 - * @param int $action_id
687 - * @param string $type
688 - *
689 - * @return bool|object
548 + * @param int $action_id
690 549 */
691 550 public static function get_single_action_type( $action_id, $type ) {
692 551 if ( ! $type ) {
693 552 return false;
694 553 }
695 -
696 - /**
697 - * @var FrmFormAction
698 - */
699 554 $action_control = FrmFormActionsController::get_form_actions( $type );
700 555
701 556 return $action_control->get_single_action( $action_id );
702 557 }
@@ -701,24 +556,18 @@
701 556 return $action_control->get_single_action( $action_id );
702 557 }
703 558
704 559 /**
705 - * @param int $form_id
706 - * @param string $type
560 + * @param int $form_id
707 561 *
708 562 * @return bool
709 563 */
710 564 public static function form_has_action_type( $form_id, $type ) {
711 - $actions = self::get_action_for_form( $form_id, $type );
712 - return ! empty( $actions );
565 + $payment_actions = self::get_action_for_form( $form_id, $type );
566 +
567 + return ! empty( $payment_actions );
713 568 }
714 569
715 - /**
716 - * @param false|int|string $form_id
717 - * @param array $atts
718 - *
719 - * @return array
720 - */
721 570 public function get_all( $form_id = false, $atts = array() ) {
722 571 if ( is_array( $atts ) && ! isset( $atts['limit'] ) && $this->action_options['limit'] > 99 ) {
723 572 $atts['limit'] = $this->action_options['limit'];
724 573 }
@@ -736,10 +585,10 @@
736 585 global $frm_vars;
737 586 $frm_vars['action_type'] = $type;
738 587
739 588 add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
740 - $query = self::action_args( $form_id, $limit );
741 - $query['post_status'] = $atts['post_status'];
589 + $query = self::action_args( $form_id, $limit );
590 + $query['post_status'] = $atts['post_status'];
742 591 $query['suppress_filters'] = false;
743 592
744 593 $actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' );
745 594 unset( $query );
@@ -745,30 +594,31 @@
745 594 unset( $query );
746 595
747 596 remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
748 597
749 - if ( ! $actions ) {
598 + if ( empty( $actions ) ) {
750 599 return array();
751 600 }
752 601
753 602 $settings = array();
754 -
755 603 foreach ( $actions as $action ) {
756 604 if ( count( $settings ) >= $limit ) {
757 605 continue;
758 606 }
759 607
760 - $action = $this->prepare_action( $action );
608 + $action = $this->prepare_action( $action );
609 +
761 610 $settings[ $action->ID ] = $action;
762 611 }
763 612
764 - return 1 === $limit ? reset( $settings ) : $settings;
613 + if ( 1 === $limit ) {
614 + $settings = reset( $settings );
615 + }
616 +
617 + return $settings;
765 618 }
766 619
767 620 /**
768 - * @param int|string $form_id
769 - * @param int $limit
770 - *
771 621 * @return array
772 622 */
773 623 public static function action_args( $form_id = 0, $limit = 99 ) {
774 624 $args = array(
@@ -778,9 +628,9 @@
778 628 'orderby' => 'title',
779 629 'order' => 'ASC',
780 630 );
781 631
782 - if ( $form_id && $form_id !== 'all' ) {
632 + if ( $form_id && $form_id != 'all' ) {
783 633 $args['menu_order'] = $form_id;
784 634 }
785 635
786 636 return $args;
@@ -786,11 +636,9 @@
786 636 return $args;
787 637 }
788 638
789 639 /**
790 - * @param WP_Post $action
791 - *
792 - * @return WP_Post
640 + * @param WP_Post|array $action
793 641 */
794 642 public function prepare_action( $action ) {
795 643 $action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content );
796 644 $action->post_excerpt = sanitize_title( $action->post_excerpt );
@@ -796,20 +644,18 @@
796 644 $action->post_excerpt = sanitize_title( $action->post_excerpt );
797 645
798 646 $default_values = $this->get_global_defaults();
799 647
800 - // Fill default values
648 + // fill default values
801 649 $action->post_content += $default_values;
802 650
803 651 foreach ( $default_values as $k => $vals ) {
804 - if ( ! is_array( $vals ) || ! $vals ) {
805 - continue;
652 + if ( is_array( $vals ) && ! empty( $vals ) ) {
653 + if ( 'event' === $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) {
654 + continue;
655 + }
656 + $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals );
806 657 }
807 -
808 - if ( 'event' === $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) {
809 - continue;
810 - }
811 - $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals );
812 658 }
813 659
814 660 if ( ! is_array( $action->post_content['event'] ) ) {
815 661 $action->post_content['event'] = explode( ',', $action->post_content['event'] );
@@ -818,11 +664,8 @@
818 664 return $action;
819 665 }
820 666
821 667 /**
822 - * @param false|int|string $form_id
823 - * @param string $type
824 - *
825 668 * @return void
826 669 */
827 670 public function destroy( $form_id = false, $type = 'default' ) {
828 671 global $wpdb;
@@ -829,14 +672,12 @@
829 672
830 673 $this->form_id = $form_id;
831 674
832 675 $query = array( 'post_type' => FrmFormActionsController::$action_post_type );
833 -
834 676 if ( $form_id ) {
835 677 $query['menu_order'] = $form_id;
836 678 }
837 -
838 - if ( 'all' !== $type ) {
679 + if ( 'all' != $type ) {
839 680 $query['post_excerpt'] = $this->id_base;
840 681 }
841 682
842 683 $post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' );
@@ -857,11 +698,8 @@
857 698 public static function clear_cache() {
858 699 FrmDb::cache_delete_group( 'frm_actions' );
859 700 }
860 701
861 - /**
862 - * @return array
863 - */
864 702 public function get_settings() {
865 703 return self::get_action_for_form( $this->form_id, $this->id_base );
866 704 }
867 705
@@ -884,43 +722,35 @@
884 722
885 723 return $defaults;
886 724 }
887 725
888 - /**
889 - * @return array
890 - */
891 726 public function get_global_switch_fields() {
892 727 $switch = $this->get_switch_fields();
893 728 $switch['conditions'] = array( 'hide_field' );
894 729
895 - return apply_filters( 'frm_global_switch_fields', $switch );
730 + $switch = apply_filters( 'frm_global_switch_fields', $switch );
731 +
732 + return $switch;
896 733 }
897 734
898 735 /**
899 736 * Migrate settings from form->options into new action.
900 - *
901 - * @param object $form
902 - * @param string $update
903 - *
904 - * @return int|WP_Error
905 737 */
906 738 public function migrate_to_2( $form, $update = 'update' ) {
907 - $action = $this->prepare_new( $form->id );
739 + $action = $this->prepare_new( $form->id );
908 740 FrmAppHelper::unserialize_or_decode( $form->options );
909 741
910 - // Fill with existing options
742 + // fill with existing options
911 743 foreach ( $action->post_content as $name => $val ) {
912 - if ( ! isset( $form->options[ $name ] ) ) {
913 - continue;
744 + if ( isset( $form->options[ $name ] ) ) {
745 + $action->post_content[ $name ] = $form->options[ $name ];
746 + unset( $form->options[ $name ] );
914 747 }
915 -
916 - $action->post_content[ $name ] = $form->options[ $name ];
917 - unset( $form->options[ $name ] );
918 748 }
919 749
920 750 $action = $this->migrate_values( $action, $form );
921 751
922 - // Check if action already exists
752 + // check if action already exists
923 753 $post_id = get_posts(
924 754 array(
925 755 'name' => $action->post_name,
926 756 'post_type' => FrmFormActionsController::$action_post_type,
@@ -928,41 +758,130 @@
928 758 'numberposts' => 1,
929 759 )
930 760 );
931 761
932 - if ( ! $post_id ) {
933 - // Create action now
762 + if ( empty( $post_id ) ) {
763 + // create action now
934 764 $post_id = $this->save_settings( $action );
935 765 }
936 766
937 - if ( ! $post_id || 'update' !== $update ) {
938 - return $post_id;
767 + if ( $post_id && 'update' == $update ) {
768 + global $wpdb;
769 + $form->options = maybe_serialize( $form->options );
770 +
771 + // update form options
772 + $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) );
773 + FrmForm::clear_form_cache();
939 774 }
940 775
941 - global $wpdb;
942 - $form->options = maybe_serialize( $form->options );
776 + return $post_id;
777 + }
943 778
944 - // Update form options
945 - $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) );
946 - FrmForm::clear_form_cache();
779 + public static function action_conditions_met( $action, $entry ) {
780 + if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) {
781 + return FrmProFormActionsController::action_conditions_met( $action, $entry );
782 + }
947 783
948 - return $post_id;
784 + // This is here for reverse compatibility.
785 + $notification = $action->post_content;
786 + $stop = false;
787 + $met = array();
788 +
789 + if ( ! isset( $notification['conditions'] ) || empty( $notification['conditions'] ) ) {
790 + return $stop;
791 + }
792 +
793 + foreach ( $notification['conditions'] as $k => $condition ) {
794 + if ( ! is_numeric( $k ) ) {
795 + continue;
796 + }
797 +
798 + if ( $stop && 'any' == $notification['conditions']['any_all'] && 'stop' == $notification['conditions']['send_stop'] ) {
799 + continue;
800 + }
801 +
802 + self::prepare_logic_value( $condition['hide_opt'], $action, $entry );
803 +
804 + $observed_value = self::get_value_from_entry( $entry, $condition['hide_field'] );
805 +
806 + $stop = FrmFieldsHelper::value_meets_condition( $observed_value, $condition['hide_field_cond'], $condition['hide_opt'] );
807 +
808 + if ( $notification['conditions']['send_stop'] == 'send' ) {
809 + $stop = $stop ? false : true;
810 + }
811 +
812 + $met[ $stop ] = $stop;
813 + }
814 +
815 + if ( $notification['conditions']['any_all'] == 'all' && ! empty( $met ) && isset( $met[0] ) && isset( $met[1] ) ) {
816 + $stop = ( $notification['conditions']['send_stop'] == 'send' );
817 + } elseif ( $notification['conditions']['any_all'] == 'any' && $notification['conditions']['send_stop'] == 'send' && isset( $met[0] ) ) {
818 + $stop = false;
819 + }
820 +
821 + return $stop;
949 822 }
950 823
951 824 /**
952 - * @param WP_Post $action
953 - * @param stdClass $entry
825 + * Prepare the logic value for comparison against the entered value
954 826 *
955 - * @return bool
827 + * @since 2.01.02
828 + *
829 + * @deprecated 4.06.02
830 + *
831 + * @param array|string $logic_value
832 + *
833 + * @return void
956 834 */
957 - public static function action_conditions_met( $action, $entry ) {
958 - if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) {
959 - return FrmProFormActionsController::action_conditions_met( $action, $entry );
835 + private static function prepare_logic_value( &$logic_value, $action, $entry ) {
836 + if ( is_array( $logic_value ) ) {
837 + $logic_value = reset( $logic_value );
960 838 }
961 - return false;
839 +
840 + if ( $logic_value == 'current_user' ) {
841 + $logic_value = get_current_user_id();
842 + }
843 +
844 + $logic_value = apply_filters( 'frm_content', $logic_value, $action->menu_order, $entry );
845 +
846 + /**
847 + * @since 4.04.05
848 + */
849 + $logic_value = apply_filters( 'frm_action_logic_value', $logic_value );
962 850 }
963 851
964 852 /**
853 + * Get the value from a specific field and entry
854 + *
855 + * @since 2.01.02
856 + * @deprecated 4.06.02
857 + *
858 + * @param object $entry
859 + * @param int $field_id
860 + *
861 + * @return array|bool|mixed|string
862 + */
863 + private static function get_value_from_entry( $entry, $field_id ) {
864 + $observed_value = '';
865 +
866 + if ( isset( $entry->metas[ $field_id ] ) ) {
867 + $observed_value = $entry->metas[ $field_id ];
868 + } elseif ( $entry->post_id && FrmAppHelper::pro_is_installed() ) {
869 + $field = FrmField::getOne( $field_id );
870 + $observed_value = FrmProEntryMetaHelper::get_post_or_meta_value(
871 + $entry,
872 + $field,
873 + array(
874 + 'links' => false,
875 + 'truncate' => false,
876 + )
877 + );
878 + }
879 +
880 + return $observed_value;
881 + }
882 +
883 + /**
965 884 * @param string $class
966 885 *
967 886 * @return array
968 887 */
@@ -967,17 +886,14 @@
967 886 * @return array
968 887 */
969 888 public static function default_action_opts( $class = '' ) {
970 889 return array(
971 - 'classes' => 'frmfont ' . $class,
890 + 'classes' => 'frm_icon_font ' . $class,
972 891 'active' => false,
973 892 'limit' => 0,
974 893 );
975 894 }
976 895
977 - /**
978 - * @return array
979 - */
980 896 public static function trigger_labels() {
981 897 $triggers = array(
982 898 'draft' => __( 'Draft is saved', 'formidable' ),
983 899 'create' => __( 'Entry is created', 'formidable' ),
@@ -992,17 +908,15 @@
992 908 /**
993 909 * @return void
994 910 */
995 911 public function render_conditional_logic_call_to_action() {
996 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
997 912 ?>
998 913 <h3>
999 - <a href="javascript:void(0)" class="frm_show_upgrade frm_noallow" data-upgrade="<?php echo esc_attr( $this->get_upgrade_text() ); ?>" data-medium="conditional-<?php echo esc_attr( $this->id_base ); ?>"><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?>
914 + <a href="javascript:void(0)" class="frm_show_upgrade frm_noallow" data-upgrade="<?php echo esc_attr( $this->get_upgrade_text() ); ?>" data-medium="conditional-<?php echo esc_attr( $this->id_base ); ?>">
1000 915 <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?>
1001 916 </a>
1002 917 </h3>
1003 918 <?php
1004 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
1005 919 }
1006 920
1007 921 /**
1008 922 * @return string
@@ -1008,26 +922,6 @@
1008 922 * @return string
1009 923 */
1010 924 protected function get_upgrade_text() {
1011 925 return __( 'Conditional form actions', 'formidable' );
1012 - }
1013 -
1014 - /**
1015 - * Gets form fields for form action settings.
1016 - *
1017 - * @since 6.10
1018 - *
1019 - * @param int $form_id Form ID.
1020 - *
1021 - * @return object[]
1022 - */
1023 - protected function get_form_fields( $form_id ) {
1024 - // Get form fields, include embedded and repeater child fields.
1025 - $form_fields = FrmField::get_all_for_form( $form_id, '', 'include' );
1026 - return array_filter(
1027 - $form_fields,
1028 - function ( $form_field ) {
1029 - return ! FrmField::is_no_save_field( $form_field->type );
1030 - }
1031 - );
1032 926 }
1033 927 }