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

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