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