PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.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
formidable / classes / models / FrmFormAction.php

FrmFormAction.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.3, at classes/models/FrmFormAction.php

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