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