PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.5.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.5.3
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
← All changes | classes/models/FrmFormAction.php +207 -361 6.285.5.3 View file →
@@ -4,71 +4,19 @@
4 4 }
5 5
6 6 class FrmFormAction {
7 7
8 - /**
9 - * Root id for all actions of this type.
10 - *
11 - * @var string
12 - */
13 - public $id_base;
14 -
15 - /**
16 - * Name for this action type.
17 - *
18 - * @var string
19 - */
20 - public $name;
21 -
22 - /**
23 - * Name of the option used to store the settings for this action type.
24 - *
25 - * @var string
26 - */
8 + public $id_base; // Root id for all actions of this type.
9 + public $name; // Name for this action type.
27 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()
28 13
29 - /**
30 - * Option array passed to wp_register_sidebar_widget().
31 - *
32 - * @var array
33 - */
34 - public $action_options;
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.
35 18
36 - /**
37 - * Option array passed to wp_register_widget_control().
38 - *
39 - * @var array
40 - */
41 - public $control_options;
42 -
43 - /**
44 - * The ID of the form to evaluate.
45 - *
46 - * @var int
47 - */
48 - public $form_id;
49 -
50 - /**
51 - * Unique ID number of the current instance.
52 - *
53 - * @var int
54 - */
55 - public $number = false;
56 -
57 - /**
58 - * Unique ID string of the current instance (id_base-number).
59 - *
60 - * @var string
61 - */
62 - public $id = '';
63 -
64 - /**
65 - * Set true when we update the data after a POST submit - makes sure we don't do it twice.
66 - *
67 - * @var bool
68 - */
69 - public $updated = false;
70 -
71 19 // Member functions that you must over-ride.
72 20
73 21 /**
74 22 * This function should check that $new_instance is set correctly.
@@ -74,10 +22,10 @@
74 22 * This function should check that $new_instance is set correctly.
75 23 * The newly calculated value of $instance should be returned.
76 24 * If "false" is returned, the instance won't be saved/updated.
77 25 *
78 - * @param array $new_instance New settings for this instance as input by the user via form().
79 - * @param array $old_instance Old settings for this instance.
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
80 28 *
81 29 * @return array Settings to save or bool false to cancel saving
82 30 */
83 31 public function update( $new_instance, $old_instance ) {
@@ -86,15 +34,13 @@
86 34
87 35 /**
88 36 * Echo the settings update form
89 37 *
90 - * @param WP_Post $instance Current settings.
91 - * @param array $args
92 - *
93 - * @return string
38 + * @param array $instance Current settings
94 39 */
95 40 public function form( $instance, $args = array() ) {
96 41 echo '<p class="no-options-widget">' . esc_html__( 'There are no options for this action.', 'formidable' ) . '</p>';
42 +
97 43 return 'noform';
98 44 }
99 45
100 46 /**
@@ -103,21 +49,12 @@
103 49 public function get_defaults() {
104 50 return array();
105 51 }
106 52
107 - /**
108 - * @return array
109 - */
110 53 public function get_switch_fields() {
111 54 return array();
112 55 }
113 56
114 - /**
115 - * @param object $action
116 - * @param object $form
117 - *
118 - * @return object
119 - */
120 57 public function migrate_values( $action, $form ) {
121 58 return $action;
122 59 }
123 60
@@ -128,14 +65,14 @@
128 65 *
129 66 * @param string $id_base Optional Base ID for the widget, lower case,
130 67 * if left empty a portion of the widget's class name will be used. Has to be unique.
131 68 * @param string $name Name for the widget displayed on the configuration page.
132 - * @param array $action_options Optional Passed to wp_register_sidebar_widget().
133 - * - description: shown on the configuration page.
134 - * - classname.
135 - * @param array $control_options Optional Passed to wp_register_widget_control().
136 - * - width: required if more than 250px.
137 - * - height: currently not used but may be needed in the future.
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
138 75 */
139 76 public function __construct( $id_base, $name, $action_options = array(), $control_options = array() ) {
140 77 if ( ! defined( 'ABSPATH' ) ) {
141 78 die( 'You are not allowed to call this page directly.' );
@@ -156,9 +93,8 @@
156 93 'plugin' => $this->id_base,
157 94 'tooltip' => $name,
158 95 'group' => $id_base,
159 96 'color' => '',
160 - 'keywords' => '',
161 97 );
162 98
163 99 $action_options = apply_filters( 'frm_' . $id_base . '_action_options', $action_options );
164 100 $group = $this->get_group( $action_options );
@@ -170,13 +106,12 @@
170 106 $action_options['color'] = 'var(--' . reset( $colors ) . ')';
171 107 }
172 108
173 109 $upgrade_class = isset( $action_options['classes'] ) && $action_options['classes'] === 'frm_show_upgrade';
174 -
175 110 if ( $action_options['group'] === $id_base ) {
176 - $upgrade_class = str_contains( $action_options['classes'], 'frm_show_upgrade' );
111 + $upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false;
177 112 $action_options['classes'] = $group['icon'];
178 - } elseif ( empty( $action_options['classes'] ) || $upgrade_class ) {
113 + } elseif ( ! isset( $action_options['classes'] ) || empty( $action_options['classes'] ) || $upgrade_class ) {
179 114 $action_options['classes'] = $group['icon'];
180 115 }
181 116
182 117 if ( $upgrade_class ) {
@@ -188,13 +123,8 @@
188 123 }
189 124
190 125 /**
191 126 * @param string $id_base
192 - * @param string $name
193 - * @param array $action_options
194 - * @param array $control_options
195 - *
196 - * @return void
197 127 */
198 128 public function FrmFormAction( $id_base, $name, $action_options = array(), $control_options = array() ) {
199 129 self::__construct( $id_base, $name, $action_options, $control_options );
200 130 }
@@ -201,28 +131,22 @@
201 131
202 132 /**
203 133 * Help to switch old field id by new field id for duplicate form
204 134 *
205 - * @param string $action id of the field that needs to be switched.
135 + * @param string $action id of the field that needs to be switched
206 136 *
207 137 * @return string
208 138 */
209 139 public function maybe_switch_field_ids( $action ) {
210 140 $updated_action = apply_filters( 'frm_maybe_switch_field_ids', $action );
211 -
212 141 if ( $updated_action === $action ) {
213 - return FrmFieldsHelper::switch_field_ids( $action );
142 + $updated_action = FrmFieldsHelper::switch_field_ids( $action );
214 143 }
215 -
216 144 return $updated_action;
217 145 }
218 146
219 147 /**
220 148 * @since 4.0
221 - *
222 - * @param array $action_options
223 - *
224 - * @return array
225 149 */
226 150 protected function get_group( $action_options ) {
227 151 $groups = FrmFormActionsController::form_action_groups();
228 152 $group = 'misc';
@@ -232,9 +156,9 @@
232 156 } elseif ( isset( $groups[ $this->id_base ] ) ) {
233 157 $group = $this->id_base;
234 158 } else {
235 159 foreach ( $groups as $name => $check_group ) {
236 - if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'], true ) ) {
160 + if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'] ) ) {
237 161 $group = $name;
238 162 break;
239 163 }
240 164 }
@@ -248,18 +172,18 @@
248 172 * Constructs name attributes for use in form() fields
249 173 *
250 174 * This function should be used in form() methods to create name attributes for fields to be saved by update()
251 175 *
252 - * @param string $field_name Field name.
253 - * @param string $post_field
176 + * @param string $field_name Field name
254 177 *
255 178 * @return string Name attribute for $field_name
256 179 */
257 180 public function get_field_name( $field_name, $post_field = 'post_content' ) {
258 - $name = $this->option_name . '[' . $this->number . ']';
259 - $name .= $post_field ? '[' . $post_field . ']' : '';
181 + $name = $this->option_name . '[' . $this->number . ']';
182 + $name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' );
183 + $name .= '[' . $field_name . ']';
260 184
261 - return $name . ( '[' . $field_name . ']' );
185 + return $name;
262 186 }
263 187
264 188 /**
265 189 * Constructs id attributes for use in form() fields
@@ -265,9 +189,9 @@
265 189 * Constructs id attributes for use in form() fields
266 190 *
267 191 * This function should be used in form() methods to create id attributes for fields to be saved by update()
268 192 *
269 - * @param string $field_name Field name.
193 + * @param string $field_name Field name
270 194 *
271 195 * @return string ID attribute for $field_name
272 196 */
273 197 public function get_field_id( $field_name ) {
@@ -273,23 +197,15 @@
273 197 public function get_field_id( $field_name ) {
274 198 return $field_name . '_' . $this->number;
275 199 }
276 200
277 - /**
278 - * @param int|string $number
279 - *
280 - * @return void
281 - */
201 + // Private Function. Don't worry about this.
202 +
282 203 public function _set( $number ) {
283 204 $this->number = $number;
284 205 $this->id = $this->id_base . '-' . $number;
285 206 }
286 207
287 - /**
288 - * @param false|int|string $form_id
289 - *
290 - * @return object
291 - */
292 208 public function prepare_new( $form_id = false ) {
293 209 if ( $form_id ) {
294 210 $this->form_id = $form_id;
295 211 }
@@ -296,9 +212,9 @@
296 212
297 213 $post_content = array();
298 214 $default_values = $this->get_global_defaults();
299 215
300 - // Fill default values
216 + // fill default values
301 217 $post_content = wp_parse_args( $post_content, $default_values );
302 218
303 219 if ( ! isset( $post_content['event'] ) && ! $this->action_options['force_event'] ) {
304 220 $post_content['event'] = array( reset( $this->action_options['event'] ) );
@@ -313,17 +229,13 @@
313 229 'post_type' => FrmFormActionsController::$action_post_type,
314 230 'post_name' => $this->form_id . '_' . $this->id_base . '_' . $this->number,
315 231 'menu_order' => $this->form_id,
316 232 );
233 + unset( $post_content );
317 234
318 235 return (object) $form_action;
319 236 }
320 237
321 - /**
322 - * @param int|string $form_id
323 - *
324 - * @return int|WP_Error
325 - */
326 238 public function create( $form_id ) {
327 239 $this->form_id = $form_id;
328 240
329 241 $action = $this->prepare_new();
@@ -330,18 +242,11 @@
330 242
331 243 return $this->save_settings( $action );
332 244 }
333 245
334 - /**
335 - * @param int|string $form_id
336 - * @param int|string $old_id
337 - *
338 - * @return void
339 - */
340 246 public function duplicate_form_actions( $form_id, $old_id ) {
341 - // phpcs:ignore Universal.Operators.StrictComparisons
342 247 if ( $form_id == $old_id ) {
343 - // Don't duplicate the actions if this is a template getting updated
248 + // don't duplicate the actions if this is a template getting updated
344 249 return;
345 250 }
346 251
347 252 $this->form_id = $old_id;
@@ -347,9 +252,8 @@
347 252 $this->form_id = $old_id;
348 253 $actions = $this->get_all( $old_id );
349 254
350 255 $this->form_id = $form_id;
351 -
352 256 foreach ( $actions as $action ) {
353 257 $this->duplicate_one( $action, $form_id );
354 258 unset( $action );
355 259 }
@@ -354,35 +258,29 @@
354 258 unset( $action );
355 259 }
356 260 }
357 261
358 - /**
359 - * Check if imported action should be created or updated.
262 + /* Check if imported action should be created or updated
360 263 *
361 264 * @since 2.0
362 265 *
363 266 * @param array $action
364 - * @param array $forms
365 - *
366 - * @return int Post ID.
267 + * @return integer $post_id
367 268 */
368 269 public function maybe_create_action( $action, $forms ) {
369 - if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] === 'updated' ) {
270 + if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] == 'updated' ) {
370 271 // Update action only
371 272 $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
372 - return $this->save_settings( $action );
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'] );
373 278 }
374 - // Create action
375 - $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] );
376 - return $this->duplicate_one( (object) $action, $action['menu_order'] );
279 +
280 + return $post_id;
377 281 }
378 282
379 - /**
380 - * @param object $action
381 - * @param int|string $form_id
382 - *
383 - * @return int|WP_Error
384 - */
385 283 public function duplicate_one( $action, $form_id ) {
386 284 global $frm_duplicate_ids;
387 285
388 286 $action->menu_order = $form_id;
@@ -393,9 +291,9 @@
393 291 $action->post_content[ $key ] = $frm_duplicate_ids[ $val ];
394 292 } elseif ( ! is_array( $val ) ) {
395 293 $action->post_content[ $key ] = FrmFieldsHelper::switch_field_ids( $val );
396 294 } elseif ( isset( $switch[ $key ] ) && is_array( $switch[ $key ] ) ) {
397 - // Loop through each value if empty
295 + // loop through each value if empty
398 296 if ( empty( $switch[ $key ] ) ) {
399 297 $switch[ $key ] = array_keys( $val );
400 298 }
401 299
@@ -410,15 +308,8 @@
410 308
411 309 return $this->save_settings( $action );
412 310 }
413 311
414 - /**
415 - * @param array $action
416 - * @param array|string $subkey
417 - * @param mixed $val
418 - *
419 - * @return array
420 - */
421 312 private function duplicate_array_walk( $action, $subkey, $val ) {
422 313 global $frm_duplicate_ids;
423 314
424 315 if ( is_array( $subkey ) ) {
@@ -434,15 +325,15 @@
434 325 } else {
435 326 foreach ( (array) $val as $ck => $cv ) {
436 327 if ( is_array( $cv ) ) {
437 328 $action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey, $cv );
438 - } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { // phpcs:ignore Universal.Operators.StrictComparisons
329 + } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) {
439 330 $action[ $ck ] = $frm_duplicate_ids[ $cv ];
440 - } elseif ( $ck == $subkey ) { // phpcs:ignore Universal.Operators.StrictComparisons
331 + } elseif ( $ck == $subkey ) {
441 332 $action[ $ck ] = $this->maybe_switch_field_ids( $action[ $ck ] );
442 333 }
443 334 }
444 - }//end if
335 + }
445 336
446 337 return $action;
447 338 }
448 339
@@ -449,12 +340,8 @@
449 340 /**
450 341 * Deal with changed settings.
451 342 *
452 343 * Do NOT over-ride this function
453 - *
454 - * @param int|string $form_id
455 - *
456 - * @return array|null
457 344 */
458 345 public function update_callback( $form_id ) {
459 346 $this->form_id = $form_id;
460 347
@@ -461,32 +348,32 @@
461 348 $all_instances = $this->get_settings();
462 349
463 350 // We need to update the data
464 351 if ( $this->updated ) {
465 - return null;
352 + return;
466 353 }
467 354
468 355 // phpcs:ignore WordPress.Security.NonceVerification.Missing
469 - if ( ! isset( $_POST[ $this->option_name ] ) || ! is_array( $_POST[ $this->option_name ] ) ) {
470 - return null;
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;
471 362 }
472 363
473 - // Sanitizing removes scripts and <email> type of values.
474 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
475 - $settings = wp_unslash( $_POST[ $this->option_name ] );
476 -
477 364 $action_ids = array();
478 365
479 366 foreach ( $settings as $number => $new_instance ) {
480 367 $this->_set( $number );
481 368
482 - $old_instance = $all_instances[ $number ] ?? array();
369 + $old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array();
483 370
484 371 if ( ! isset( $new_instance['post_status'] ) ) {
485 372 $new_instance['post_status'] = 'draft';
486 373 }
487 374
488 - // Settings were never opened, so don't update
375 + // settings were never opened, so don't update
489 376 if ( ! isset( $new_instance['post_title'] ) ) {
490 377 $this->maybe_update_status( $new_instance, $old_instance );
491 378 $action_ids[] = $new_instance['ID'];
492 379 $this->updated = true;
@@ -495,10 +382,10 @@
495 382
496 383 $new_instance['post_type'] = FrmFormActionsController::$action_post_type;
497 384 $new_instance['post_name'] = $this->form_id . '_' . $this->id_base . '_' . $this->number;
498 385 $new_instance['menu_order'] = $this->form_id;
499 - $new_instance['post_date'] = $old_instance->post_date ?? '';
500 - $instance = $this->update( $new_instance, $old_instance );
386 + $new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : '';
387 + $instance = $this->update( $new_instance, $old_instance );
501 388
502 389 /**
503 390 * Filter an action's settings before saving.
504 391 *
@@ -506,12 +393,12 @@
506 393 * to update settings.
507 394 *
508 395 * @since 2.0
509 396 *
510 - * @param array $instance The current widget instance's settings.
511 - * @param array $new_instance Array of new widget settings.
512 - * @param array $old_instance Array of old widget settings.
513 - * @param FrmFormAction $form_action FrmFormAction instance.
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.
514 401 */
515 402 $instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this );
516 403
517 404 $instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this );
@@ -523,9 +410,9 @@
523 410
524 411 $action_ids[] = $this->save_settings( $instance );
525 412
526 413 $this->updated = true;
527 - }//end foreach
414 + }
528 415
529 416 return $action_ids;
530 417 }
531 418
@@ -534,10 +421,9 @@
534 421 *
535 422 * @since 3.04
536 423 *
537 424 * @param array $new_instance
538 - * @param array|stdClass $old_instance
539 - *
425 + * @param stdClass|array $old_instance
540 426 * @return void
541 427 */
542 428 protected function maybe_update_status( $new_instance, $old_instance ) {
543 429 if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) {
@@ -552,26 +438,16 @@
552 438 )
553 439 );
554 440 }
555 441
556 - /**
557 - * @param array $settings
558 - *
559 - * @return int|WP_Error
560 - */
561 442 public function save_settings( $settings ) {
562 443 self::clear_cache();
444 +
563 445 return FrmDb::save_settings( $settings, 'frm_actions' );
564 446 }
565 447
566 - /**
567 - * @param int $id
568 - *
569 - * @return object|null
570 - */
571 448 public function get_single_action( $id ) {
572 449 $action = get_post( $id );
573 -
574 450 if ( $action ) {
575 451 $action = $this->prepare_action( $action );
576 452 $this->_set( $id );
577 453 }
@@ -578,43 +454,27 @@
578 454
579 455 return $action;
580 456 }
581 457
582 - /**
583 - * @param int|string $form_id
584 - *
585 - * @return array
586 - */
587 458 public function get_one( $form_id ) {
588 459 return $this->get_all( $form_id, 1 );
589 460 }
590 461
591 - /**
592 - * @param int|string $form_id
593 - * @param string $type
594 - * @param array $atts
595 - *
596 - * @return array
597 - */
598 462 public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) {
599 463 $action_controls = FrmFormActionsController::get_form_actions( $type );
600 -
601 - if ( ! $action_controls ) {
602 - // Don't continue if there are no available actions
464 + if ( empty( $action_controls ) ) {
465 + // don't continue if there are no available actions
603 466 return array();
604 467 }
605 468
606 - if ( 'all' !== $type ) {
607 - if ( is_array( $action_controls ) ) {
608 - return array();
609 - }
610 -
469 + if ( 'all' != $type ) {
611 470 return $action_controls->get_all( $form_id, $atts );
612 471 }
613 472
614 473 self::prepare_get_action( $atts );
615 474
616 - $limit = self::get_action_limit( $form_id, $atts['limit'] );
475 + $limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) );
476 +
617 477 $args = self::action_args( $form_id, $limit );
618 478 $args['post_status'] = $atts['post_status'];
619 479 $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
620 480
@@ -622,11 +482,10 @@
622 482 return array();
623 483 }
624 484
625 485 $settings = array();
626 -
627 486 foreach ( $actions as $action ) {
628 - // Some plugins/themes are formatting the post_excerpt
487 + // some plugins/themes are formatting the post_excerpt
629 488 $action->post_excerpt = sanitize_title( $action->post_excerpt );
630 489
631 490 if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) {
632 491 continue;
@@ -639,43 +498,27 @@
639 498 break;
640 499 }
641 500 }
642 501
643 - return 1 === $limit ? reset( $settings ) : $settings;
644 - }
502 + if ( 1 === $limit ) {
503 + $settings = reset( $settings );
504 + }
645 505
646 - /**
647 - * Get the limit for the number of actions for a single form. By default, this is 99, but
648 - * it can be modified with a code snippet.
649 - *
650 - * @since 6.17 This logic from moved from FrmFormAction::get_action_for_form.
651 - *
652 - * @param int|string $form_id
653 - * @param int|string $limit The unfiltered limit value.
654 - *
655 - * @return int The filtered limit value.
656 - */
657 - public static function get_action_limit( $form_id, $limit = 99 ) {
658 - $type = 'all';
659 - return (int) apply_filters( 'frm_form_action_limit', (int) $limit, compact( 'type', 'form_id' ) );
506 + return $settings;
660 507 }
661 508
662 509 /**
663 510 * @since 3.04
664 - *
665 - * @param array|string $args
666 - * @param string $default_status
667 - *
668 - * @return void
511 + * @param array $args
512 + * @param string $default_status
669 513 */
670 514 protected static function prepare_get_action( &$args, $default_status = 'publish' ) {
671 515 if ( is_numeric( $args ) ) {
672 - // For reverse compatibility. $limit was changed to $args
516 + // for reverse compatibility. $limit was changed to $args
673 517 $args = array(
674 518 'limit' => $args,
675 519 );
676 520 }
677 -
678 521 $defaults = array(
679 522 'limit' => 99,
680 523 'post_status' => $default_status,
681 524 );
@@ -682,21 +525,14 @@
682 525 $args = wp_parse_args( $args, $defaults );
683 526 }
684 527
685 528 /**
686 - * @param int $action_id
687 - * @param string $type
688 - *
689 - * @return bool|object
529 + * @param int $action_id
690 530 */
691 531 public static function get_single_action_type( $action_id, $type ) {
692 532 if ( ! $type ) {
693 533 return false;
694 534 }
695 -
696 - /**
697 - * @var FrmFormAction
698 - */
699 535 $action_control = FrmFormActionsController::get_form_actions( $type );
700 536
701 537 return $action_control->get_single_action( $action_id );
702 538 }
@@ -701,31 +537,20 @@
701 537 return $action_control->get_single_action( $action_id );
702 538 }
703 539
704 540 /**
705 - * @param int $form_id
706 - * @param string $type
541 + * @param int $form_id
707 542 *
708 543 * @return bool
709 544 */
710 545 public static function form_has_action_type( $form_id, $type ) {
711 - $actions = self::get_action_for_form( $form_id, $type );
712 - return ! empty( $actions );
546 + $payment_actions = self::get_action_for_form( $form_id, $type );
547 +
548 + return ! empty( $payment_actions );
713 549 }
714 550
715 - /**
716 - * @param false|int|string $form_id
717 - * @param array $atts
718 - *
719 - * @return array
720 - */
721 551 public function get_all( $form_id = false, $atts = array() ) {
722 - if ( is_array( $atts ) && ! isset( $atts['limit'] ) && $this->action_options['limit'] > 99 ) {
723 - $atts['limit'] = $this->action_options['limit'];
724 - }
725 -
726 552 self::prepare_get_action( $atts, 'any' );
727 -
728 553 $limit = $atts['limit'];
729 554
730 555 if ( $form_id ) {
731 556 $this->form_id = $form_id;
@@ -736,10 +561,10 @@
736 561 global $frm_vars;
737 562 $frm_vars['action_type'] = $type;
738 563
739 564 add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
740 - $query = self::action_args( $form_id, $limit );
741 - $query['post_status'] = $atts['post_status'];
565 + $query = self::action_args( $form_id, $limit );
566 + $query['post_status'] = $atts['post_status'];
742 567 $query['suppress_filters'] = false;
743 568
744 569 $actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' );
745 570 unset( $query );
@@ -745,32 +570,30 @@
745 570 unset( $query );
746 571
747 572 remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' );
748 573
749 - if ( ! $actions ) {
574 + if ( empty( $actions ) ) {
750 575 return array();
751 576 }
752 577
753 578 $settings = array();
754 -
755 579 foreach ( $actions as $action ) {
756 580 if ( count( $settings ) >= $limit ) {
757 581 continue;
758 582 }
759 583
760 - $action = $this->prepare_action( $action );
584 + $action = $this->prepare_action( $action );
585 +
761 586 $settings[ $action->ID ] = $action;
762 587 }
763 588
764 - return 1 === $limit ? reset( $settings ) : $settings;
589 + if ( 1 === $limit ) {
590 + $settings = reset( $settings );
591 + }
592 +
593 + return $settings;
765 594 }
766 595
767 - /**
768 - * @param int|string $form_id
769 - * @param int $limit
770 - *
771 - * @return array
772 - */
773 596 public static function action_args( $form_id = 0, $limit = 99 ) {
774 597 $args = array(
775 598 'post_type' => FrmFormActionsController::$action_post_type,
776 599 'post_status' => 'publish',
@@ -778,9 +601,9 @@
778 601 'orderby' => 'title',
779 602 'order' => 'ASC',
780 603 );
781 604
782 - if ( $form_id && $form_id !== 'all' ) {
605 + if ( $form_id && $form_id != 'all' ) {
783 606 $args['menu_order'] = $form_id;
784 607 }
785 608
786 609 return $args;
@@ -785,13 +608,8 @@
785 608
786 609 return $args;
787 610 }
788 611
789 - /**
790 - * @param WP_Post $action
791 - *
792 - * @return WP_Post
793 - */
794 612 public function prepare_action( $action ) {
795 613 $action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content );
796 614 $action->post_excerpt = sanitize_title( $action->post_excerpt );
797 615
@@ -796,14 +614,14 @@
796 614 $action->post_excerpt = sanitize_title( $action->post_excerpt );
797 615
798 616 $default_values = $this->get_global_defaults();
799 617
800 - // Fill default values
618 + // fill default values
801 619 $action->post_content += $default_values;
802 620
803 621 foreach ( $default_values as $k => $vals ) {
804 - if ( is_array( $vals ) && $vals ) {
805 - if ( 'event' === $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) {
622 + if ( is_array( $vals ) && ! empty( $vals ) ) {
623 + if ( 'event' == $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) {
806 624 continue;
807 625 }
808 626 $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals );
809 627 }
@@ -815,14 +633,8 @@
815 633
816 634 return $action;
817 635 }
818 636
819 - /**
820 - * @param false|int|string $form_id
821 - * @param string $type
822 - *
823 - * @return void
824 - */
825 637 public function destroy( $form_id = false, $type = 'default' ) {
826 638 global $wpdb;
827 639
828 640 $this->form_id = $form_id;
@@ -827,14 +639,12 @@
827 639
828 640 $this->form_id = $form_id;
829 641
830 642 $query = array( 'post_type' => FrmFormActionsController::$action_post_type );
831 -
832 643 if ( $form_id ) {
833 644 $query['menu_order'] = $form_id;
834 645 }
835 -
836 - if ( 'all' !== $type ) {
646 + if ( 'all' != $type ) {
837 647 $query['post_excerpt'] = $this->id_base;
838 648 }
839 649
840 650 $post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' );
@@ -848,25 +658,17 @@
848 658 /**
849 659 * Delete the action cache when a form action is created, deleted, or updated
850 660 *
851 661 * @since 2.0.5
852 - *
853 - * @return void
854 662 */
855 663 public static function clear_cache() {
856 664 FrmDb::cache_delete_group( 'frm_actions' );
857 665 }
858 666
859 - /**
860 - * @return array
861 - */
862 667 public function get_settings() {
863 668 return self::get_action_for_form( $this->form_id, $this->id_base );
864 669 }
865 670
866 - /**
867 - * @return array
868 - */
869 671 public function get_global_defaults() {
870 672 $defaults = $this->get_defaults();
871 673
872 674 if ( ! isset( $defaults['event'] ) ) {
@@ -882,31 +684,25 @@
882 684
883 685 return $defaults;
884 686 }
885 687
886 - /**
887 - * @return array
888 - */
889 688 public function get_global_switch_fields() {
890 689 $switch = $this->get_switch_fields();
891 690 $switch['conditions'] = array( 'hide_field' );
892 691
893 - return apply_filters( 'frm_global_switch_fields', $switch );
692 + $switch = apply_filters( 'frm_global_switch_fields', $switch );
693 +
694 + return $switch;
894 695 }
895 696
896 697 /**
897 698 * Migrate settings from form->options into new action.
898 - *
899 - * @param object $form
900 - * @param string $update
901 - *
902 - * @return int|WP_Error
903 699 */
904 700 public function migrate_to_2( $form, $update = 'update' ) {
905 - $action = $this->prepare_new( $form->id );
701 + $action = $this->prepare_new( $form->id );
906 702 FrmAppHelper::unserialize_or_decode( $form->options );
907 703
908 - // Fill with existing options
704 + // fill with existing options
909 705 foreach ( $action->post_content as $name => $val ) {
910 706 if ( isset( $form->options[ $name ] ) ) {
911 707 $action->post_content[ $name ] = $form->options[ $name ];
912 708 unset( $form->options[ $name ] );
@@ -914,9 +710,9 @@
914 710 }
915 711
916 712 $action = $this->migrate_values( $action, $form );
917 713
918 - // Check if action already exists
714 + // check if action already exists
919 715 $post_id = get_posts(
920 716 array(
921 717 'name' => $action->post_name,
922 718 'post_type' => FrmFormActionsController::$action_post_type,
@@ -924,56 +720,134 @@
924 720 'numberposts' => 1,
925 721 )
926 722 );
927 723
928 - if ( ! $post_id ) {
929 - // Create action now
724 + if ( empty( $post_id ) ) {
725 + // create action now
930 726 $post_id = $this->save_settings( $action );
931 727 }
932 728
933 - if ( ! $post_id || 'update' !== $update ) {
934 - return $post_id;
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();
935 736 }
936 737
937 - global $wpdb;
938 - $form->options = maybe_serialize( $form->options );
738 + return $post_id;
739 + }
939 740
940 - // Update form options
941 - $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) );
942 - FrmForm::clear_form_cache();
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 + }
943 745
944 - return $post_id;
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;
945 784 }
946 785
947 786 /**
948 - * @param WP_Post $action
949 - * @param stdClass $entry
787 + * Prepare the logic value for comparison against the entered value
950 788 *
951 - * @return bool
789 + * @since 2.01.02
790 + * @deprecated 4.06.02
791 + *
792 + * @param array|string $logic_value
952 793 */
953 - public static function action_conditions_met( $action, $entry ) {
954 - if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) {
955 - return FrmProFormActionsController::action_conditions_met( $action, $entry );
794 + private static function prepare_logic_value( &$logic_value, $action, $entry ) {
795 + if ( is_array( $logic_value ) ) {
796 + $logic_value = reset( $logic_value );
956 797 }
957 - return false;
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 );
958 809 }
959 810
960 811 /**
961 - * @param string $class
812 + * Get the value from a specific field and entry
962 813 *
963 - * @return array
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
964 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 +
965 842 public static function default_action_opts( $class = '' ) {
966 843 return array(
967 - 'classes' => 'frmfont ' . $class,
844 + 'classes' => 'frm_icon_font ' . $class,
968 845 'active' => false,
969 846 'limit' => 0,
970 847 );
971 848 }
972 849
973 - /**
974 - * @return array
975 - */
976 850 public static function trigger_labels() {
977 851 $triggers = array(
978 852 'draft' => __( 'Draft is saved', 'formidable' ),
979 853 'create' => __( 'Entry is created', 'formidable' ),
@@ -984,46 +858,18 @@
984 858
985 859 return apply_filters( 'frm_action_triggers', $triggers );
986 860 }
987 861
988 - /**
989 - * @return void
990 - */
991 862 public function render_conditional_logic_call_to_action() {
992 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
993 863 ?>
994 864 <h3>
995 - <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 ); ?>"><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?>
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 ); ?>">
996 866 <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?>
997 867 </a>
998 868 </h3>
999 869 <?php
1000 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
1001 870 }
1002 871
1003 - /**
1004 - * @return string
1005 - */
1006 872 protected function get_upgrade_text() {
1007 873 return __( 'Conditional form actions', 'formidable' );
1008 - }
1009 -
1010 - /**
1011 - * Gets form fields for form action settings.
1012 - *
1013 - * @since 6.10
1014 - *
1015 - * @param int $form_id Form ID.
1016 - *
1017 - * @return object[]
1018 - */
1019 - protected function get_form_fields( $form_id ) {
1020 - // Get form fields, include embedded and repeater child fields.
1021 - $form_fields = FrmField::get_all_for_form( $form_id, '', 'include' );
1022 - return array_filter(
1023 - $form_fields,
1024 - function ( $form_field ) {
1025 - return ! FrmField::is_no_save_field( $form_field->type );
1026 - }
1027 - );
1028 874 }
1029 875 }