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