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

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