Completed
Pull Request — master (#201)
by Stephanie
02:43
created

FrmFormAction::get_global_defaults()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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