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

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