Completed
Push — master ( 709374...172c6c )
by Stephanie
03:07 queued 10s
created

FrmFormAction::get_upgrade_text()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 = $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
	 * @since 4.0
134
	 */
135
	protected function get_group( $action_options ) {
136
		$groups = FrmFormActionsController::form_action_groups();
137
		$group  = 'misc';
138
139
		if ( isset( $action_options['group'] ) && isset( $groups[ $action_options['group'] ] ) ) {
140
			$group = $action_options['group'];
141
		} elseif ( isset( $groups[ $this->id_base ] ) ) {
142
			$group = $this->id_base;
143
		} else {
144
			foreach ( $groups as $name => $check_group ) {
145
				if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'] ) ) {
146
					$group = $name;
147
					break;
148
				}
149
			}
150
		}
151
152
		$groups[ $group ]['id'] = $group;
153
		return $groups[ $group ];
154
	}
155
156
	/**
157
	 * Constructs name attributes for use in form() fields
158
	 *
159
	 * This function should be used in form() methods to create name attributes for fields to be saved by update()
160
	 *
161
	 * @param string $field_name Field name
162
	 *
163
	 * @return string Name attribute for $field_name
164
	 */
165
	public function get_field_name( $field_name, $post_field = 'post_content' ) {
166
		$name = $this->option_name . '[' . $this->number . ']';
167
		$name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' );
168
		$name .= '[' . $field_name . ']';
169
170
		return $name;
171
	}
172
173
	/**
174
	 * Constructs id attributes for use in form() fields
175
	 *
176
	 * This function should be used in form() methods to create id attributes for fields to be saved by update()
177
	 *
178
	 * @param string $field_name Field name
179
	 *
180
	 * @return string ID attribute for $field_name
181
	 */
182
	public function get_field_id( $field_name ) {
183
		return $field_name . '_' . $this->number;
184
	}
185
186
	// Private Function. Don't worry about this.
187
188
	public function _set( $number ) {
189
		$this->number = $number;
190
		$this->id     = $this->id_base . '-' . $number;
191
	}
192
193
	public function prepare_new( $form_id = false ) {
194
		if ( $form_id ) {
195
			$this->form_id = $form_id;
196
		}
197
198
		$post_content   = array();
199
		$default_values = $this->get_global_defaults();
200
201
		// fill default values
202
		$post_content = wp_parse_args( $post_content, $default_values );
203
204
		if ( ! isset( $post_content['event'] ) && ! $this->action_options['force_event'] ) {
205
			$post_content['event'] = array( reset( $this->action_options['event'] ) );
206
		}
207
208
		$form_action = array(
209
			'post_title'   => $this->name,
210
			'post_content' => $post_content,
211
			'post_excerpt' => $this->id_base,
212
			'ID'           => '',
213
			'post_status'  => 'publish',
214
			'post_type'    => FrmFormActionsController::$action_post_type,
215
			'post_name'    => $this->form_id . '_' . $this->id_base . '_' . $this->number,
216
			'menu_order'   => $this->form_id,
217
		);
218
		unset( $post_content );
219
220
		return (object) $form_action;
221
	}
222
223
	public function create( $form_id ) {
224
		$this->form_id = $form_id;
225
226
		$action = $this->prepare_new();
227
228
		return $this->save_settings( $action );
229
	}
230
231
	public function duplicate_form_actions( $form_id, $old_id ) {
232
		if ( $form_id == $old_id ) {
233
			// don't duplicate the actions if this is a template getting updated
234
			return;
235
		}
236
237
		$this->form_id = $old_id;
238
		$actions       = $this->get_all( $old_id );
239
240
		$this->form_id = $form_id;
241
		foreach ( $actions as $action ) {
242
			$this->duplicate_one( $action, $form_id );
243
			unset( $action );
244
		}
245
	}
246
247
	/* Check if imported action should be created or updated
248
	 *
249
	 * @since 2.0
250
	 *
251
	 * @param array $action
252
	 * @return integer $post_id
253
	 */
254
	public function maybe_create_action( $action, $forms ) {
255
		if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] == 'updated' ) {
256
			// Update action only
257
			$action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
258
			$post_id                = $this->save_settings( $action );
259
		} else {
260
			// Create action
261
			$action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
262
			$post_id                = $this->duplicate_one( (object) $action, $action['menu_order'] );
263
		}
264
265
		return $post_id;
266
	}
267
268
	public function duplicate_one( $action, $form_id ) {
269
		global $frm_duplicate_ids;
270
271
		$action->menu_order = $form_id;
272
		$switch             = $this->get_global_switch_fields();
273
		foreach ( (array) $action->post_content as $key => $val ) {
274
			if ( is_numeric( $val ) && isset( $frm_duplicate_ids[ $val ] ) ) {
275
				$action->post_content[ $key ] = $frm_duplicate_ids[ $val ];
276
			} elseif ( ! is_array( $val ) ) {
277
				$action->post_content[ $key ] = FrmFieldsHelper::switch_field_ids( $val );
278
			} elseif ( isset( $switch[ $key ] ) && is_array( $switch[ $key ] ) ) {
279
				// loop through each value if empty
280
				if ( empty( $switch[ $key ] ) ) {
281
					$switch[ $key ] = array_keys( $val );
282
				}
283
284
				foreach ( $switch[ $key ] as $subkey ) {
285
					$action->post_content[ $key ] = $this->duplicate_array_walk( $action->post_content[ $key ], $subkey, $val );
286
				}
287
			}
288
289
			unset( $key, $val );
290
		}
291
		unset( $action->ID );
292
293
		return $this->save_settings( $action );
294
	}
295
296
	private function duplicate_array_walk( $action, $subkey, $val ) {
297
		global $frm_duplicate_ids;
298
299
		if ( is_array( $subkey ) ) {
300
			foreach ( $subkey as $subkey2 ) {
301
				foreach ( (array) $val as $ck => $cv ) {
302
					if ( is_array( $cv ) ) {
303
						$action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey2, $cv );
304
					} elseif ( isset( $cv[ $subkey ] ) && is_numeric( $cv[ $subkey ] ) && isset( $frm_duplicate_ids[ $cv[ $subkey ] ] ) ) {
305
						$action[ $ck ][ $subkey ] = $frm_duplicate_ids[ $cv[ $subkey ] ];
306
					}
307
				}
308
			}
309
		} else {
310
			foreach ( (array) $val as $ck => $cv ) {
311
				if ( is_array( $cv ) ) {
312
					$action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey, $cv );
313
				} elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) {
314
					$action[ $ck ] = $frm_duplicate_ids[ $cv ];
315
				} elseif ( $ck == $subkey ) {
316
					$action[ $ck ] = FrmFieldsHelper::switch_field_ids( $action[ $ck ] );
317
				}
318
			}
319
		}
320
321
		return $action;
322
	}
323
324
	/**
325
	 * Deal with changed settings.
326
	 *
327
	 * Do NOT over-ride this function
328
	 */
329
	public function update_callback( $form_id ) {
330
		$this->form_id = $form_id;
331
332
		$all_instances = $this->get_settings();
333
334
		// We need to update the data
335
		if ( $this->updated ) {
336
			return;
337
		}
338
339
		if ( isset( $_POST[ $this->option_name ] ) && is_array( $_POST[ $this->option_name ] ) ) {
340
			// Sanitizing removes scripts and <email> type of values.
341
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
342
			$settings = wp_unslash( $_POST[ $this->option_name ] );
343
		} else {
344
			return;
345
		}
346
347
		$action_ids = array();
348
349
		foreach ( $settings as $number => $new_instance ) {
350
			$this->_set( $number );
351
352
			$old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array();
353
354
			if ( ! isset( $new_instance['post_status'] ) ) {
355
				$new_instance['post_status'] = 'draft';
356
			}
357
358
			// settings were never opened, so don't update
359
			if ( ! isset( $new_instance['post_title'] ) ) {
360
				$this->maybe_update_status( $new_instance, $old_instance );
361
				$action_ids[]  = $new_instance['ID'];
362
				$this->updated = true;
363
				continue;
364
			}
365
366
			$new_instance['post_type']  = FrmFormActionsController::$action_post_type;
367
			$new_instance['post_name']  = $this->form_id . '_' . $this->id_base . '_' . $this->number;
368
			$new_instance['menu_order'] = $this->form_id;
369
			$new_instance['post_date']  = isset( $old_instance->post_date ) ? $old_instance->post_date : '';
370
371
			$instance = $this->update( $new_instance, $old_instance );
372
373
			/**
374
			 * Filter an action's settings before saving.
375
			 *
376
			 * Returning false will effectively short-circuit the widget's ability
377
			 * to update settings.
378
			 *
379
			 * @since 2.0
380
			 *
381
			 * @param array $instance The current widget instance's settings.
382
			 * @param array $new_instance Array of new widget settings.
383
			 * @param array $old_instance Array of old widget settings.
384
			 * @param WP_Widget $this The current widget instance.
385
			 */
386
			$instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this );
387
388
			$instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this );
389
			$instance['post_content'] = apply_filters( 'frm_before_save_' . $this->id_base . '_action', $new_instance['post_content'], $instance, $new_instance, $old_instance, $this );
390
391
			if ( false !== $instance ) {
392
				$all_instances[ $number ] = $instance;
393
			}
394
395
			$action_ids[] = $this->save_settings( $instance );
396
397
			$this->updated = true;
398
		}
399
400
		return $action_ids;
401
	}
402
403
	/**
404
	 * If the status of the action has changed, update it
405
	 *
406
	 * @since 3.04
407
	 */
408
	protected function maybe_update_status( $new_instance, $old_instance ) {
409
		if ( $new_instance['post_status'] !== $old_instance->post_status ) {
410
			self::clear_cache();
411
			wp_update_post(
412
				array(
413
					'ID'          => $new_instance['ID'],
414
					'post_status' => $new_instance['post_status'],
415
				)
416
			);
417
		}
418
	}
419
420
	public function save_settings( $settings ) {
421
		self::clear_cache();
422
423
		return FrmDb::save_settings( $settings, 'frm_actions' );
424
	}
425
426
	public function get_single_action( $id ) {
427
		$action = get_post( $id );
428
		if ( $action ) {
429
			$action = $this->prepare_action( $action );
430
			$this->_set( $id );
431
		}
432
433
		return $action;
434
	}
435
436
	public function get_one( $form_id ) {
437
		return $this->get_all( $form_id, 1 );
438
	}
439
440
	public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) {
441
		$action_controls = FrmFormActionsController::get_form_actions( $type );
442
		if ( empty( $action_controls ) ) {
443
			// don't continue if there are no available actions
444
			return array();
445
		}
446
447
		if ( 'all' != $type ) {
448
			return $action_controls->get_all( $form_id, $atts );
449
		}
450
451
		self::prepare_get_action( $atts );
452
453
		$limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) );
454
455
		$args                = self::action_args( $form_id, $limit );
456
		$args['post_status'] = $atts['post_status'];
457
		$actions             = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
458
459
		if ( ! $actions ) {
460
			return array();
461
		}
462
463
		$settings = array();
464
		foreach ( $actions as $action ) {
465
			// some plugins/themes are formatting the post_excerpt
466
			$action->post_excerpt = sanitize_title( $action->post_excerpt );
467
468
			if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) {
469
				continue;
470
			}
471
472
			$action                  = $action_controls[ $action->post_excerpt ]->prepare_action( $action );
473
			$settings[ $action->ID ] = $action;
474
475
			if ( count( $settings ) >= $limit ) {
476
				break;
477
			}
478
		}
479
480
		if ( 1 === $limit ) {
481
			$settings = reset( $settings );
482
		}
483
484
		return $settings;
485
	}
486
487
	/**
488
	 * @since 3.04
489
	 * @param array  $args
490
	 * @param string $default_status
491
	 */
492
	protected static function prepare_get_action( &$args, $default_status = 'publish' ) {
493
		if ( is_numeric( $args ) ) {
494
			// for reverse compatibility. $limit was changed to $args
495
			$args = array(
496
				'limit' => $args,
497
			);
498
		}
499
		$defaults = array(
500
			'limit'       => 99,
501
			'post_status' => $default_status,
502
		);
503
		$args     = wp_parse_args( $args, $defaults );
504
	}
505
506
	/**
507
	 * @param int $action_id
508
	 */
509
	public static function get_single_action_type( $action_id, $type ) {
510
		if ( ! $type ) {
511
			return false;
512
		}
513
		$action_control = FrmFormActionsController::get_form_actions( $type );
514
515
		return $action_control->get_single_action( $action_id );
516
	}
517
518
	/**
519
	 * @param int $form_id
520
	 *
521
	 * @return bool
522
	 */
523
	public static function form_has_action_type( $form_id, $type ) {
524
		$payment_actions = self::get_action_for_form( $form_id, $type );
525
526
		return ! empty( $payment_actions );
527
	}
528
529
	public function get_all( $form_id = false, $atts = array() ) {
530
		self::prepare_get_action( $atts, 'any' );
531
		$limit = $atts['limit'];
532
533
		if ( $form_id ) {
534
			$this->form_id = $form_id;
535
		}
536
537
		$type = $this->id_base;
538
539
		global $frm_vars;
540
		$frm_vars['action_type'] = $type;
541
542
		add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
543
		$query = self::action_args( $form_id, $limit );
544
		$query['post_status'] = $atts['post_status'];
545
		$query['suppress_filters'] = false;
546
547
		$actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' );
548
		unset( $query );
549
550
		remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
551
552
		if ( empty( $actions ) ) {
553
			return array();
554
		}
555
556
		$settings = array();
557
		foreach ( $actions as $action ) {
558
			if ( count( $settings ) >= $limit ) {
559
				continue;
560
			}
561
562
			$action = $this->prepare_action( $action );
563
564
			$settings[ $action->ID ] = $action;
565
		}
566
567
		if ( 1 === $limit ) {
568
			$settings = reset( $settings );
569
		}
570
571
		return $settings;
572
	}
573
574
	public static function action_args( $form_id = 0, $limit = 99 ) {
575
		$args = array(
576
			'post_type'   => FrmFormActionsController::$action_post_type,
577
			'post_status' => 'publish',
578
			'numberposts' => $limit,
579
			'orderby'     => 'title',
580
			'order'       => 'ASC',
581
		);
582
583
		if ( $form_id && $form_id != 'all' ) {
584
			$args['menu_order'] = $form_id;
585
		}
586
587
		return $args;
588
	}
589
590
	public function prepare_action( $action ) {
591
		$action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content );
592
		$action->post_excerpt = sanitize_title( $action->post_excerpt );
593
594
		$default_values = $this->get_global_defaults();
595
596
		// fill default values
597
		$action->post_content += $default_values;
598
599
		foreach ( $default_values as $k => $vals ) {
600
			if ( is_array( $vals ) && ! empty( $vals ) ) {
601
				if ( 'event' == $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) {
602
					continue;
603
				}
604
				$action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals );
605
			}
606
		}
607
608
		if ( ! is_array( $action->post_content['event'] ) ) {
609
			$action->post_content['event'] = explode( ',', $action->post_content['event'] );
610
		}
611
612
		return $action;
613
	}
614
615
	public function destroy( $form_id = false, $type = 'default' ) {
616
		global $wpdb;
617
618
		$this->form_id = $form_id;
619
620
		$query = array( 'post_type' => FrmFormActionsController::$action_post_type );
621
		if ( $form_id ) {
622
			$query['menu_order'] = $form_id;
623
		}
624
		if ( 'all' != $type ) {
625
			$query['post_excerpt'] = $this->id_base;
626
		}
627
628
		$post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' );
629
630
		foreach ( $post_ids as $id ) {
0 ignored issues
show
Bug introduced by
The expression $post_ids of type array|null|string|object is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
631
			wp_delete_post( $id );
632
		}
633
		self::clear_cache();
634
	}
635
636
	/**
637
	 * Delete the action cache when a form action is created, deleted, or updated
638
	 *
639
	 * @since 2.0.5
640
	 */
641
	public static function clear_cache() {
642
		FrmDb::cache_delete_group( 'frm_actions' );
643
	}
644
645
	public function get_settings() {
646
		return self::get_action_for_form( $this->form_id, $this->id_base );
647
	}
648
649
	public function get_global_defaults() {
650
		$defaults = $this->get_defaults();
651
652
		if ( ! isset( $defaults['event'] ) ) {
653
			$defaults['event'] = array( 'create' );
654
		}
655
656
		if ( ! isset( $defaults['conditions'] ) ) {
657
			$defaults['conditions'] = array(
658
				'send_stop' => '',
659
				'any_all'   => '',
660
			);
661
		}
662
663
		return $defaults;
664
	}
665
666
	public function get_global_switch_fields() {
667
		$switch               = $this->get_switch_fields();
668
		$switch['conditions'] = array( 'hide_field' );
669
670
		return $switch;
671
	}
672
673
	/**
674
	 * Migrate settings from form->options into new action.
675
	 */
676
	public function migrate_to_2( $form, $update = 'update' ) {
677
		$action        = $this->prepare_new( $form->id );
678
		FrmAppHelper::unserialize_or_decode( $form->options );
679
680
		// fill with existing options
681
		foreach ( $action->post_content as $name => $val ) {
682
			if ( isset( $form->options[ $name ] ) ) {
683
				$action->post_content[ $name ] = $form->options[ $name ];
684
				unset( $form->options[ $name ] );
685
			}
686
		}
687
688
		$action = $this->migrate_values( $action, $form );
689
690
		// check if action already exists
691
		$post_id = get_posts(
692
			array(
693
				'name'        => $action->post_name,
694
				'post_type'   => FrmFormActionsController::$action_post_type,
695
				'post_status' => $action->post_status,
696
				'numberposts' => 1,
697
			)
698
		);
699
700
		if ( empty( $post_id ) ) {
701
			// create action now
702
			$post_id = $this->save_settings( $action );
703
		}
704
705
		if ( $post_id && 'update' == $update ) {
706
			global $wpdb;
707
			$form->options = maybe_serialize( $form->options );
708
709
			// update form options
710
			$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) );
711
			FrmForm::clear_form_cache();
712
		}
713
714
		return $post_id;
715
	}
716
717
	public static function action_conditions_met( $action, $entry ) {
718
		if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) {
719
			return FrmProFormActionsController::action_conditions_met( $action, $entry );
720
		}
721
722
		// This is here for reverse compatibility.
723
		$notification = $action->post_content;
724
		$stop         = false;
725
		$met          = array();
726
727
		if ( ! isset( $notification['conditions'] ) || empty( $notification['conditions'] ) ) {
728
			return $stop;
729
		}
730
731
		foreach ( $notification['conditions'] as $k => $condition ) {
732
			if ( ! is_numeric( $k ) ) {
733
				continue;
734
			}
735
736
			if ( $stop && 'any' == $notification['conditions']['any_all'] && 'stop' == $notification['conditions']['send_stop'] ) {
737
				continue;
738
			}
739
740
			self::prepare_logic_value( $condition['hide_opt'], $action, $entry );
0 ignored issues
show
Deprecated Code introduced by
The method FrmFormAction::prepare_logic_value() has been deprecated with message: 4.06.02

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
741
742
			$observed_value = self::get_value_from_entry( $entry, $condition['hide_field'] );
0 ignored issues
show
Deprecated Code introduced by
The method FrmFormAction::get_value_from_entry() has been deprecated with message: 4.06.02

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
743
744
			$stop = FrmFieldsHelper::value_meets_condition( $observed_value, $condition['hide_field_cond'], $condition['hide_opt'] );
745
746
			if ( $notification['conditions']['send_stop'] == 'send' ) {
747
				$stop = $stop ? false : true;
748
			}
749
750
			$met[ $stop ] = $stop;
751
		}
752
753
		if ( $notification['conditions']['any_all'] == 'all' && ! empty( $met ) && isset( $met[0] ) && isset( $met[1] ) ) {
754
			$stop = ( $notification['conditions']['send_stop'] == 'send' );
755
		} elseif ( $notification['conditions']['any_all'] == 'any' && $notification['conditions']['send_stop'] == 'send' && isset( $met[0] ) ) {
756
			$stop = false;
757
		}
758
759
		return $stop;
760
	}
761
762
	/**
763
	 * Prepare the logic value for comparison against the entered value
764
	 *
765
	 * @since 2.01.02
766
	 * @deprecated 4.06.02
767
	 *
768
	 * @param array|string $logic_value
769
	 */
770
	private static function prepare_logic_value( &$logic_value, $action, $entry ) {
771
		if ( is_array( $logic_value ) ) {
772
			$logic_value = reset( $logic_value );
773
		}
774
775
		if ( $logic_value == 'current_user' ) {
776
			$logic_value = get_current_user_id();
777
		}
778
779
		$logic_value = apply_filters( 'frm_content', $logic_value, $action->menu_order, $entry );
780
781
		/**
782
		 * @since 4.04.05
783
		 */
784
		$logic_value = apply_filters( 'frm_action_logic_value', $logic_value );
785
	}
786
787
	/**
788
	 * Get the value from a specific field and entry
789
	 *
790
	 * @since 2.01.02
791
	 * @deprecated 4.06.02
792
	 *
793
	 * @param object $entry
794
	 * @param int $field_id
795
	 *
796
	 * @return array|bool|mixed|string
797
	 */
798
	private static function get_value_from_entry( $entry, $field_id ) {
799
		$observed_value = '';
800
801
		if ( isset( $entry->metas[ $field_id ] ) ) {
802
			$observed_value = $entry->metas[ $field_id ];
803
		} elseif ( $entry->post_id && FrmAppHelper::pro_is_installed() ) {
804
			$field          = FrmField::getOne( $field_id );
805
			$observed_value = FrmProEntryMetaHelper::get_post_or_meta_value(
806
				$entry,
807
				$field,
808
				array(
809
					'links'    => false,
810
					'truncate' => false,
811
				)
812
			);
813
		}
814
815
		return $observed_value;
816
	}
817
818
	public static function default_action_opts( $class = '' ) {
819
		return array(
820
			'classes' => 'frm_icon_font ' . $class,
821
			'active'  => false,
822
			'limit'   => 0,
823
		);
824
	}
825
826
	public static function trigger_labels() {
827
		$triggers = array(
828
			'draft'  => __( 'Draft is saved', 'formidable' ),
829
			'create' => __( 'Entry is created', 'formidable' ),
830
			'update' => __( 'Entry is updated', 'formidable' ),
831
			'delete' => __( 'Entry is deleted', 'formidable' ),
832
			'import' => __( 'Entry is imported', 'formidable' ),
833
		);
834
835
		return apply_filters( 'frm_action_triggers', $triggers );
836
	}
837
838
	public function render_conditional_logic_call_to_action() {
839
		?>
840
			<h3>
841
				<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 ); ?>">
842
					<?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?>
843
				</a>
844
			</h3>
845
		<?php
846
	}
847
848
	protected function get_upgrade_text() {
849
		return __( 'Conditional form actions', 'formidable' );
850
	}
851
}
852