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