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