PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8
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 +206 -293 6.356.8 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,20 +157,18 @@
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 - } elseif ( empty( $action_options['classes'] ) || $upgrade_class ) {
170 + } elseif ( ! isset( $action_options['classes'] ) || empty( $action_options['classes'] ) || $upgrade_class ) {
191 171 $action_options['classes'] = $group['icon'];
192 172 }
193 173
194 174 if ( $upgrade_class ) {
@@ -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 - $name = $this->option_name . '[' . $this->number . ']';
271 - $name .= $post_field ? '[' . $post_field . ']' : '';
238 + $name = $this->option_name . '[' . $this->number . ']';
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 integer $post_id
379 334 */
380 335 public function maybe_create_action( $action, $forms ) {
381 - if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] === 'updated' ) {
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,10 +451,10 @@
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 ?? '';
512 - $instance = $this->update( $new_instance, $old_instance );
455 + $new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : '';
456 + $instance = $this->update( $new_instance, $old_instance );
513 457
514 458 /**
515 459 * Filter an action's settings before saving.
516 460 *
@@ -518,12 +462,11 @@
518 462 * to update settings.
519 463 *
520 464 * @since 2.0
521 465 *
522 - * @param array $instance The current widget instance's settings.
523 - * @param array $new_instance Array of new widget settings.
524 - * @param array $old_instance Array of old widget settings.
525 - * @param FrmFormAction $form_action FrmFormAction instance.
466 + * @param array $instance The current widget instance's settings.
467 + * @param array $new_instance Array of new widget settings.
468 + * @param array $old_instance Array of old widget settings.
526 469 */
527 470 $instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this );
528 471
529 472 $instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this );
@@ -546,10 +489,9 @@
546 489 *
547 490 * @since 3.04
548 491 *
549 492 * @param array $new_instance
550 - * @param array|stdClass $old_instance
551 - *
493 + * @param stdClass|array $old_instance
552 494 * @return void
553 495 */
554 496 protected function maybe_update_status( $new_instance, $old_instance ) {
555 497 if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) {
@@ -564,26 +506,16 @@
564 506 )
565 507 );
566 508 }
567 509
568 - /**
569 - * @param array $settings
570 - *
571 - * @return int|WP_Error
572 - */
573 510 public function save_settings( $settings ) {
574 511 self::clear_cache();
512 +
575 513 return FrmDb::save_settings( $settings, 'frm_actions' );
576 514 }
577 515
578 - /**
579 - * @param int $id
580 - *
581 - * @return object|null
582 - */
583 516 public function get_single_action( $id ) {
584 517 $action = get_post( $id );
585 -
586 518 if ( $action ) {
587 519 $action = $this->prepare_action( $action );
588 520 $this->_set( $id );
589 521 }
@@ -590,43 +522,27 @@
590 522
591 523 return $action;
592 524 }
593 525
594 - /**
595 - * @param int|string $form_id
596 - *
597 - * @return array
598 - */
599 526 public function get_one( $form_id ) {
600 527 return $this->get_all( $form_id, 1 );
601 528 }
602 529
603 - /**
604 - * @param int|string $form_id
605 - * @param string $type
606 - * @param array $atts
607 - *
608 - * @return array
609 - */
610 530 public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) {
611 531 $action_controls = FrmFormActionsController::get_form_actions( $type );
612 -
613 - if ( ! $action_controls ) {
614 - // Don't continue if there are no available actions
532 + if ( empty( $action_controls ) ) {
533 + // don't continue if there are no available actions
615 534 return array();
616 535 }
617 536
618 - if ( 'all' !== $type ) {
619 - if ( is_array( $action_controls ) ) {
620 - return array();
621 - }
622 -
537 + if ( 'all' != $type ) {
623 538 return $action_controls->get_all( $form_id, $atts );
624 539 }
625 540
626 541 self::prepare_get_action( $atts );
627 542
628 - $limit = self::get_action_limit( $form_id, $atts['limit'] );
543 + $limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) );
544 +
629 545 $args = self::action_args( $form_id, $limit );
630 546 $args['post_status'] = $atts['post_status'];
631 547 $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
632 548
@@ -634,11 +550,10 @@
634 550 return array();
635 551 }
636 552
637 553 $settings = array();
638 -
639 554 foreach ( $actions as $action ) {
640 - // Some plugins/themes are formatting the post_excerpt
555 + // some plugins/themes are formatting the post_excerpt
641 556 $action->post_excerpt = sanitize_title( $action->post_excerpt );
642 557
643 558 if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) {
644 559 continue;
@@ -651,43 +566,30 @@
651 566 break;
652 567 }
653 568 }
654 569
655 - return 1 === $limit ? reset( $settings ) : $settings;
656 - }
570 + if ( 1 === $limit ) {
571 + $settings = reset( $settings );
572 + }
657 573
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' ) );
574 + return $settings;
672 575 }
673 576
674 577 /**
675 578 * @since 3.04
676 579 *
677 - * @param array|string $args
678 - * @param string $default_status
580 + * @param array $args
581 + * @param string $default_status
679 582 *
680 583 * @return void
681 584 */
682 585 protected static function prepare_get_action( &$args, $default_status = 'publish' ) {
683 586 if ( is_numeric( $args ) ) {
684 - // For reverse compatibility. $limit was changed to $args
587 + // for reverse compatibility. $limit was changed to $args
685 588 $args = array(
686 589 'limit' => $args,
687 590 );
688 591 }
689 -
690 592 $defaults = array(
691 593 'limit' => 99,
692 594 'post_status' => $default_status,
693 595 );
@@ -694,21 +596,14 @@
694 596 $args = wp_parse_args( $args, $defaults );
695 597 }
696 598
697 599 /**
698 - * @param int $action_id
699 - * @param string $type
700 - *
701 - * @return bool|object
600 + * @param int $action_id
702 601 */
703 602 public static function get_single_action_type( $action_id, $type ) {
704 603 if ( ! $type ) {
705 604 return false;
706 605 }
707 -
708 - /**
709 - * @var FrmFormAction
710 - */
711 606 $action_control = FrmFormActionsController::get_form_actions( $type );
712 607
713 608 return $action_control->get_single_action( $action_id );
714 609 }
@@ -713,24 +608,18 @@
713 608 return $action_control->get_single_action( $action_id );
714 609 }
715 610
716 611 /**
717 - * @param int $form_id
718 - * @param string $type
612 + * @param int $form_id
719 613 *
720 614 * @return bool
721 615 */
722 616 public static function form_has_action_type( $form_id, $type ) {
723 - $actions = self::get_action_for_form( $form_id, $type );
724 - return ! empty( $actions );
617 + $payment_actions = self::get_action_for_form( $form_id, $type );
618 +
619 + return ! empty( $payment_actions );
725 620 }
726 621
727 - /**
728 - * @param false|int|string $form_id
729 - * @param array $atts
730 - *
731 - * @return array
732 - */
733 622 public function get_all( $form_id = false, $atts = array() ) {
734 623 if ( is_array( $atts ) && ! isset( $atts['limit'] ) && $this->action_options['limit'] > 99 ) {
735 624 $atts['limit'] = $this->action_options['limit'];
736 625 }
@@ -748,10 +637,10 @@
748 637 global $frm_vars;
749 638 $frm_vars['action_type'] = $type;
750 639
751 640 add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
752 - $query = self::action_args( $form_id, $limit );
753 - $query['post_status'] = $atts['post_status'];
641 + $query = self::action_args( $form_id, $limit );
642 + $query['post_status'] = $atts['post_status'];
754 643 $query['suppress_filters'] = false;
755 644
756 645 $actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' );
757 646 unset( $query );
@@ -757,30 +646,31 @@
757 646 unset( $query );
758 647
759 648 remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
760 649
761 - if ( ! $actions ) {
650 + if ( empty( $actions ) ) {
762 651 return array();
763 652 }
764 653
765 654 $settings = array();
766 -
767 655 foreach ( $actions as $action ) {
768 656 if ( count( $settings ) >= $limit ) {
769 657 continue;
770 658 }
771 659
772 - $action = $this->prepare_action( $action );
660 + $action = $this->prepare_action( $action );
661 +
773 662 $settings[ $action->ID ] = $action;
774 663 }
775 664
776 - return 1 === $limit ? reset( $settings ) : $settings;
665 + if ( 1 === $limit ) {
666 + $settings = reset( $settings );
667 + }
668 +
669 + return $settings;
777 670 }
778 671
779 672 /**
780 - * @param int|string $form_id
781 - * @param int $limit
782 - *
783 673 * @return array
784 674 */
785 675 public static function action_args( $form_id = 0, $limit = 99 ) {
786 676 $args = array(
@@ -790,9 +680,9 @@
790 680 'orderby' => 'title',
791 681 'order' => 'ASC',
792 682 );
793 683
794 - if ( $form_id && $form_id !== 'all' ) {
684 + if ( $form_id && $form_id != 'all' ) {
795 685 $args['menu_order'] = $form_id;
796 686 }
797 687
798 688 return $args;
@@ -798,11 +688,9 @@
798 688 return $args;
799 689 }
800 690
801 691 /**
802 - * @param WP_Post $action
803 - *
804 - * @return WP_Post
692 + * @param WP_Post|array $action
805 693 */
806 694 public function prepare_action( $action ) {
807 695 $action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content );
808 696 $action->post_excerpt = sanitize_title( $action->post_excerpt );
@@ -808,20 +696,18 @@
808 696 $action->post_excerpt = sanitize_title( $action->post_excerpt );
809 697
810 698 $default_values = $this->get_global_defaults();
811 699
812 - // Fill default values
700 + // fill default values
813 701 $action->post_content += $default_values;
814 702
815 703 foreach ( $default_values as $k => $vals ) {
816 - if ( ! is_array( $vals ) || ! $vals ) {
817 - continue;
704 + if ( is_array( $vals ) && ! empty( $vals ) ) {
705 + if ( 'event' === $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) {
706 + continue;
707 + }
708 + $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals );
818 709 }
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 710 }
825 711
826 712 if ( ! is_array( $action->post_content['event'] ) ) {
827 713 $action->post_content['event'] = explode( ',', $action->post_content['event'] );
@@ -830,11 +716,8 @@
830 716 return $action;
831 717 }
832 718
833 719 /**
834 - * @param false|int|string $form_id
835 - * @param string $type
836 - *
837 720 * @return void
838 721 */
839 722 public function destroy( $form_id = false, $type = 'default' ) {
840 723 global $wpdb;
@@ -841,14 +724,12 @@
841 724
842 725 $this->form_id = $form_id;
843 726
844 727 $query = array( 'post_type' => FrmFormActionsController::$action_post_type );
845 -
846 728 if ( $form_id ) {
847 729 $query['menu_order'] = $form_id;
848 730 }
849 -
850 - if ( 'all' !== $type ) {
731 + if ( 'all' != $type ) {
851 732 $query['post_excerpt'] = $this->id_base;
852 733 }
853 734
854 735 $post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' );
@@ -869,11 +750,8 @@
869 750 public static function clear_cache() {
870 751 FrmDb::cache_delete_group( 'frm_actions' );
871 752 }
872 753
873 - /**
874 - * @return array
875 - */
876 754 public function get_settings() {
877 755 return self::get_action_for_form( $this->form_id, $this->id_base );
878 756 }
879 757
@@ -896,43 +774,35 @@
896 774
897 775 return $defaults;
898 776 }
899 777
900 - /**
901 - * @return array
902 - */
903 778 public function get_global_switch_fields() {
904 779 $switch = $this->get_switch_fields();
905 780 $switch['conditions'] = array( 'hide_field' );
906 781
907 - return apply_filters( 'frm_global_switch_fields', $switch );
782 + $switch = apply_filters( 'frm_global_switch_fields', $switch );
783 +
784 + return $switch;
908 785 }
909 786
910 787 /**
911 788 * Migrate settings from form->options into new action.
912 - *
913 - * @param object $form
914 - * @param string $update
915 - *
916 - * @return int|WP_Error
917 789 */
918 790 public function migrate_to_2( $form, $update = 'update' ) {
919 - $action = $this->prepare_new( $form->id );
791 + $action = $this->prepare_new( $form->id );
920 792 FrmAppHelper::unserialize_or_decode( $form->options );
921 793
922 - // Fill with existing options
794 + // fill with existing options
923 795 foreach ( $action->post_content as $name => $val ) {
924 - if ( ! isset( $form->options[ $name ] ) ) {
925 - continue;
796 + if ( isset( $form->options[ $name ] ) ) {
797 + $action->post_content[ $name ] = $form->options[ $name ];
798 + unset( $form->options[ $name ] );
926 799 }
927 -
928 - $action->post_content[ $name ] = $form->options[ $name ];
929 - unset( $form->options[ $name ] );
930 800 }
931 801
932 802 $action = $this->migrate_values( $action, $form );
933 803
934 - // Check if action already exists
804 + // check if action already exists
935 805 $post_id = get_posts(
936 806 array(
937 807 'name' => $action->post_name,
938 808 'post_type' => FrmFormActionsController::$action_post_type,
@@ -940,41 +810,130 @@
940 810 'numberposts' => 1,
941 811 )
942 812 );
943 813
944 - if ( ! $post_id ) {
945 - // Create action now
814 + if ( empty( $post_id ) ) {
815 + // create action now
946 816 $post_id = $this->save_settings( $action );
947 817 }
948 818
949 - if ( ! $post_id || 'update' !== $update ) {
950 - return $post_id;
819 + if ( $post_id && 'update' == $update ) {
820 + global $wpdb;
821 + $form->options = maybe_serialize( $form->options );
822 +
823 + // update form options
824 + $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) );
825 + FrmForm::clear_form_cache();
951 826 }
952 827
953 - global $wpdb;
954 - $form->options = maybe_serialize( $form->options );
828 + return $post_id;
829 + }
955 830
956 - // Update form options
957 - $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) );
958 - FrmForm::clear_form_cache();
831 + public static function action_conditions_met( $action, $entry ) {
832 + if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) {
833 + return FrmProFormActionsController::action_conditions_met( $action, $entry );
834 + }
959 835
960 - return $post_id;
836 + // This is here for reverse compatibility.
837 + $notification = $action->post_content;
838 + $stop = false;
839 + $met = array();
840 +
841 + if ( ! isset( $notification['conditions'] ) || empty( $notification['conditions'] ) ) {
842 + return $stop;
843 + }
844 +
845 + foreach ( $notification['conditions'] as $k => $condition ) {
846 + if ( ! is_numeric( $k ) ) {
847 + continue;
848 + }
849 +
850 + if ( $stop && 'any' == $notification['conditions']['any_all'] && 'stop' == $notification['conditions']['send_stop'] ) {
851 + continue;
852 + }
853 +
854 + self::prepare_logic_value( $condition['hide_opt'], $action, $entry );
855 +
856 + $observed_value = self::get_value_from_entry( $entry, $condition['hide_field'] );
857 +
858 + $stop = FrmFieldsHelper::value_meets_condition( $observed_value, $condition['hide_field_cond'], $condition['hide_opt'] );
859 +
860 + if ( $notification['conditions']['send_stop'] == 'send' ) {
861 + $stop = $stop ? false : true;
862 + }
863 +
864 + $met[ $stop ] = $stop;
865 + }//end foreach
866 +
867 + if ( $notification['conditions']['any_all'] == 'all' && ! empty( $met ) && isset( $met[0] ) && isset( $met[1] ) ) {
868 + $stop = ( $notification['conditions']['send_stop'] == 'send' );
869 + } elseif ( $notification['conditions']['any_all'] == 'any' && $notification['conditions']['send_stop'] == 'send' && isset( $met[0] ) ) {
870 + $stop = false;
871 + }
872 +
873 + return $stop;
961 874 }
962 875
963 876 /**
964 - * @param WP_Post $action
965 - * @param stdClass $entry
877 + * Prepare the logic value for comparison against the entered value
966 878 *
967 - * @return bool
879 + * @since 2.01.02
880 + *
881 + * @deprecated 4.06.02
882 + *
883 + * @param array|string $logic_value
884 + *
885 + * @return void
968 886 */
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 );
887 + private static function prepare_logic_value( &$logic_value, $action, $entry ) {
888 + if ( is_array( $logic_value ) ) {
889 + $logic_value = reset( $logic_value );
972 890 }
973 - return false;
891 +
892 + if ( $logic_value == 'current_user' ) {
893 + $logic_value = get_current_user_id();
894 + }
895 +
896 + $logic_value = apply_filters( 'frm_content', $logic_value, $action->menu_order, $entry );
897 +
898 + /**
899 + * @since 4.04.05
900 + */
901 + $logic_value = apply_filters( 'frm_action_logic_value', $logic_value );
974 902 }
975 903
976 904 /**
905 + * Get the value from a specific field and entry
906 + *
907 + * @since 2.01.02
908 + * @deprecated 4.06.02
909 + *
910 + * @param object $entry
911 + * @param int $field_id
912 + *
913 + * @return array|bool|mixed|string
914 + */
915 + private static function get_value_from_entry( $entry, $field_id ) {
916 + $observed_value = '';
917 +
918 + if ( isset( $entry->metas[ $field_id ] ) ) {
919 + $observed_value = $entry->metas[ $field_id ];
920 + } elseif ( $entry->post_id && FrmAppHelper::pro_is_installed() ) {
921 + $field = FrmField::getOne( $field_id );
922 + $observed_value = FrmProEntryMetaHelper::get_post_or_meta_value(
923 + $entry,
924 + $field,
925 + array(
926 + 'links' => false,
927 + 'truncate' => false,
928 + )
929 + );
930 + }
931 +
932 + return $observed_value;
933 + }
934 +
935 + /**
977 936 * @param string $class
978 937 *
979 938 * @return array
980 939 */
@@ -979,18 +938,14 @@
979 938 * @return array
980 939 */
981 940 public static function default_action_opts( $class = '' ) {
982 941 return array(
983 - 'classes' => 'frmfont ' . $class,
984 - 'active' => false,
985 - 'limit' => 0,
986 - 'description' => '',
942 + 'classes' => 'frm_icon_font ' . $class,
943 + 'active' => false,
944 + 'limit' => 0,
987 945 );
988 946 }
989 947
990 - /**
991 - * @return array
992 - */
993 948 public static function trigger_labels() {
994 949 $triggers = array(
995 950 'draft' => __( 'Draft is saved', 'formidable' ),
996 951 'create' => __( 'Entry is created', 'formidable' ),
@@ -1002,40 +957,18 @@
1002 957 return apply_filters( 'frm_action_triggers', $triggers );
1003 958 }
1004 959
1005 960 /**
1006 - * @param string $action_key The unique key for this action instance.
1007 - *
1008 961 * @return void
1009 962 */
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 - 'data-content' => 'conditional-logic',
1016 - );
1017 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
963 + public function render_conditional_logic_call_to_action() {
1018 964 ?>
1019 - <div <?php FrmAppHelper::array_to_html_params( $params, true ); ?>>
1020 - <?php
1021 - FrmHtmlHelper::toggle(
1022 - 'frm_logic_cta_' . $action_key,
1023 - 'frm_logic_cta_' . $action_key,
1024 - array(
1025 - 'div_class' => 'with_frm_style frm_toggle',
1026 - 'checked' => false,
1027 - 'echo' => true,
1028 - 'disabled' => true,
1029 - )
1030 - );
1031 - ?>
1032 - <label for="frm_logic_cta_<?php echo esc_attr( $action_key ); ?>" class="frm_noallow">
1033 - <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?>
1034 - </label>
1035 - </div>
965 + <h3>
966 + <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 ); ?>">
967 + <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?>
968 + </a>
969 + </h3>
1036 970 <?php
1037 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
1038 971 }
1039 972
1040 973 /**
1041 974 * @return string
@@ -1041,26 +974,6 @@
1041 974 * @return string
1042 975 */
1043 976 protected function get_upgrade_text() {
1044 977 return __( 'Conditional form actions', 'formidable' );
1045 - }
1046 -
1047 - /**
1048 - * Gets form fields for form action settings.
1049 - *
1050 - * @since 6.10
1051 - *
1052 - * @param int $form_id Form ID.
1053 - *
1054 - * @return object[]
1055 - */
1056 - protected function get_form_fields( $form_id ) {
1057 - // Get form fields, include embedded and repeater child fields.
1058 - $form_fields = FrmField::get_all_for_form( $form_id, '', 'include' );
1059 - return array_filter(
1060 - $form_fields,
1061 - function ( $form_field ) {
1062 - return ! FrmField::is_no_save_field( $form_field->type );
1063 - }
1064 - );
1065 978 }
1066 979 }