PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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/helpers/FrmOnSubmitHelper.php +111 -36 6.3 → trunk View file →
@@ -2,8 +2,9 @@
2 2 /**
3 3 * On Submit action helper
4 4 *
5 5 * @package Formidable
6 + *
6 7 * @since 6.0
7 8 */
8 9
9 10 if ( ! defined( 'ABSPATH' ) ) {
@@ -23,13 +24,16 @@
23 24 * @type object $form Form data.
24 25 * @type string $action_key Action key.
25 26 * @type array $values Contains `fields` (form fields) and `id` (form ID).
26 27 * }
28 + *
29 + * @return void
27 30 */
28 31 public static function show_message_settings( $args ) {
29 32 $id_attr = $args['action_control']->get_field_id( 'success_msg' );
33 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
30 34 ?>
31 - <div class="frm_form_field frm_has_shortcodes">
35 + <div class="frm_form_field frm_has_shortcodes frm-mt-sm">
32 36 <label for="<?php echo esc_attr( $id_attr ); ?>" class="screen-reader-text">
33 37 <?php esc_html_e( 'Message on submit', 'formidable' ); ?>
34 38 </label>
35 39
@@ -49,9 +53,9 @@
49 53 <?php
50 54 $id_attr = $args['action_control']->get_field_id( 'show_form' );
51 55 $name_attr = $args['action_control']->get_field_name( 'show_form' );
52 56 ?>
53 - <div class="frm_form_field">
57 + <div class="frm_form_field frm-h-stack-xs">
54 58 <?php
55 59 FrmHtmlHelper::toggle(
56 60 $id_attr,
57 61 $name_attr,
@@ -66,8 +70,9 @@
66 70 <?php esc_html_e( 'Show the form with the confirmation message', 'formidable' ); ?>
67 71 </label>
68 72 </div>
69 73 <?php
74 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
70 75 }
71 76
72 77 /**
73 78 * Shows redirect settings.
@@ -80,22 +85,13 @@
80 85 * @type object $form Form data.
81 86 * @type string $action_key Action key.
82 87 * @type array $values Contains `fields` (form fields) and `id` (form ID).
83 88 * }
89 + *
90 + * @return void
84 91 */
85 92 public static function show_redirect_settings( $args ) {
86 - $id_attr = $args['action_control']->get_field_id( 'success_url' );
87 - ?>
88 - <div class="frm_form_field frm_has_shortcodes">
89 - <label for="<?php echo esc_attr( $id_attr ); ?>"><?php esc_html_e( 'Redirect URL', 'formidable' ); ?></label>
90 - <input
91 - type="text"
92 - id="<?php echo esc_attr( $id_attr ); ?>"
93 - name="<?php echo esc_attr( $args['action_control']->get_field_name( 'success_url' ) ); ?>"
94 - value="<?php echo esc_attr( $args['form_action']->post_content['success_url'] ); ?>"
95 - />
96 - </div>
97 - <?php
93 + include FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/on_submit_redirect_settings.php';
98 94 }
99 95
100 96 /**
101 97 * Shows page settings.
@@ -108,13 +104,19 @@
108 104 * @type object $form Form data.
109 105 * @type string $action_key Action key.
110 106 * @type array $values Contains `fields` (form fields) and `id` (form ID).
111 107 * }
108 + *
109 + * @return void
112 110 */
113 111 public static function show_page_settings( $args ) {
114 112 $name_attr = $args['action_control']->get_field_name( 'success_page_id' );
113 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
115 114 ?>
116 - <div class="frm_form_field">
115 + <div class="frm_form_field frm-mt-xs">
116 + <div class="frm_note_style frm-mt-0 frm-mb-md">
117 + <?php esc_html_e( 'NOTE: The selected page content will be displayed, but the full page will not be loaded. Traditional URL tracking in Google Analytics and similar tools won\'t register a page load event. If precise tracking is essential, consider using the \'Redirect to URL\' option.', 'formidable' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?>
118 + </div>
117 119 <label for="<?php echo esc_attr( $name_attr ); ?>" class="screen-reader-text">
118 120 <?php esc_html_e( 'Select a page', 'formidable' ); ?>
119 121 </label>
120 122 <?php
@@ -127,8 +129,9 @@
127 129 );
128 130 ?>
129 131 </div>
130 132 <?php
133 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
131 134 }
132 135
133 136 /**
134 137 * Gets all active On Submit form actions for form.
@@ -136,13 +139,15 @@
136 139 * action is created during run time. If you use another cache method, please remember to delete the cache in
137 140 * {@see FrmFormAction::save_settings()}.
138 141 *
139 142 * @param int $form_id Form ID.
143 + *
140 144 * @return array
141 145 */
142 146 public static function get_actions( $form_id ) {
143 147 $cache_key = 'frm_on_submit_actions_' . $form_id;
144 148 $actions = wp_cache_get( $cache_key, 'frm_actions' );
149 +
145 150 if ( false !== $actions ) {
146 151 return $actions;
147 152 }
148 153
@@ -154,43 +159,65 @@
154 159 /**
155 160 * Gets On Submit action type (message, redirect or page).
156 161 *
157 162 * @param object $action Form action object.
163 + *
158 164 * @return string
159 165 */
160 166 public static function get_action_type( $action ) {
161 - if ( isset( $action->post_content['success_action'] ) ) {
162 - return $action->post_content['success_action'];
163 - }
164 - return self::get_default_action_type();
167 + return $action->post_content['success_action'] ?? self::get_default_action_type();
165 168 }
166 169
170 + /**
171 + * @return string
172 + */
167 173 public static function get_default_action_type() {
168 174 return 'message';
169 175 }
170 176
177 + /**
178 + * @return string
179 + */
171 180 public static function get_default_msg() {
172 181 $msg = FrmAppHelper::get_settings()->success_msg;
173 182 return $msg ? $msg : __( 'Your responses were successfully submitted. Thank you!', 'formidable' );
174 183 }
175 184
185 + /**
186 + * @return string
187 + */
176 188 public static function get_default_redirect_msg() {
177 189 return __( 'Please wait while you are redirected.', 'formidable' );
178 190 }
179 191
180 192 /**
193 + * Gets the default open in new tab message.
194 + *
195 + * @since 6.3.1
196 + *
197 + * @return string
198 + */
199 + public static function get_default_new_tab_msg() {
200 + return FrmAppHelper::get_settings()->new_tab_msg;
201 + }
202 +
203 + /**
181 204 * Adds the first On Submit action data to the form options to be saved.
182 205 *
183 206 * @param int $form_id Form ID.
207 + *
208 + * @return void
184 209 */
185 210 public static function save_on_submit_settings( $form_id ) {
186 211 $actions = self::get_actions( $form_id );
187 212 $first_create_action = null;
188 213 $first_edit_action = null;
214 +
189 215 foreach ( $actions as $action ) {
190 216 if ( ! $first_create_action && in_array( 'create', $action->post_content['event'], true ) ) {
191 217 $first_create_action = $action;
192 218 }
219 +
193 220 if ( ! $first_edit_action && in_array( 'update', $action->post_content['event'], true ) ) {
194 221 $first_edit_action = $action;
195 222 }
196 223 }
@@ -196,16 +223,18 @@
196 223 }
197 224
198 225 $form_options = array();
199 226 self::populate_on_submit_data( $form_options, $first_create_action );
227 +
200 228 if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() ) {
201 229 $form_editable = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'editable' );
230 +
202 231 if ( $form_editable ) {
203 232 self::populate_on_submit_data( $form_options, $first_edit_action, 'update' );
204 233 }
205 234 }
206 235
207 - if ( ! empty( $form_options ) ) {
236 + if ( $form_options ) {
208 237 $_POST['options'] += $form_options;
209 238 }
210 239 }
211 240
@@ -211,14 +240,17 @@
211 240
212 241 /**
213 242 * Populates the On Submit data to form options.
214 243 *
215 - * @param array $form_options Form options.
216 - * @param object $action Optional. The On Submit action object.
217 - * @param string $event Form event. Default is `create`.
244 + * @param array $form_options Form options.
245 + * @param object|null $action Optional. The On Submit action object.
246 + * @param string $event Form event. Default is `create`.
247 + *
248 + * @return void
218 249 */
219 250 public static function populate_on_submit_data( &$form_options, $action = null, $event = 'create' ) {
220 251 $opt = 'update' === $event ? 'edit_' : 'success_';
252 +
221 253 if ( ! $action || ! is_object( $action ) ) {
222 254 $form_options[ $opt . 'action' ] = self::get_default_action_type();
223 255 $form_options[ $opt . 'msg' ] = self::get_default_msg();
224 256
@@ -224,17 +256,21 @@
224 256
225 257 return;
226 258 }
227 259
228 - $form_options[ $opt . 'action' ] = isset( $action->post_content['success_action'] ) ? $action->post_content['success_action'] : 'message';
260 + $form_options[ $opt . 'action' ] = $action->post_content['success_action'] ?? 'message';
229 261
230 262 switch ( $form_options[ $opt . 'action' ] ) {
231 263 case 'redirect':
232 - $form_options[ $opt . 'url' ] = isset( $action->post_content['success_url'] ) ? $action->post_content['success_url'] : '';
264 + $form_options[ $opt . 'url' ] = $action->post_content['success_url'] ?? '';
265 + $form_options['open_in_new_tab'] = ! empty( $action->post_content['open_in_new_tab'] );
266 + $form_options['redirect_delay'] = ! empty( $action->post_content['redirect_delay'] );
267 + $form_options['redirect_delay_time'] = $action->post_content['redirect_delay_time'];
268 + $form_options['redirect_delay_msg'] = $action->post_content['redirect_delay_msg'];
233 269 break;
234 270
235 271 case 'page':
236 - $form_options[ $opt . 'page_id' ] = isset( $action->post_content['success_page_id'] ) ? $action->post_content['success_page_id'] : '';
272 + $form_options[ $opt . 'page_id' ] = $action->post_content['success_page_id'] ?? '';
237 273 break;
238 274
239 275 default:
240 276 $form_options[ $opt . 'msg' ] = ! empty( $action->post_content['success_msg'] ) ? $action->post_content['success_msg'] : self::get_default_msg();
@@ -248,11 +284,14 @@
248 284 *
249 285 * @since 6.1.1
250 286 *
251 287 * @param int $form_id Form ID.
288 + *
289 + * @return void
252 290 */
253 291 public static function maybe_migrate_submit_settings_to_action( $form_id ) {
254 292 $form = FrmDb::get_row( 'frm_forms', array( 'id' => $form_id ), 'options,editable' );
293 +
255 294 if ( ! $form ) {
256 295 return;
257 296 }
258 297
@@ -261,14 +300,15 @@
261 300 if ( self::form_has_migrated( $form ) ) {
262 301 return;
263 302 }
264 303
265 - $form->id = $form_id;
304 + $form->id = $form_id;
266 305 $action_type = FrmOnSubmitAction::$slug;
267 306
268 307 // Check if form already has form actions to avoid creating duplicates.
269 308 $has_actions = FrmFormAction::form_has_action_type( $form_id, $action_type );
270 - if ( ! empty( $has_actions ) ) {
309 +
310 + if ( $has_actions ) {
271 311 // Don't migrate again.
272 312 self::save_migrated_success_actions( $form );
273 313 return;
274 314 }
@@ -320,27 +360,28 @@
320 360 * @since 6.1.1
321 361 *
322 362 * @param array $form_options Form options.
323 363 * @param string $event Action event. Accepts `create` or `update`. Default is `create`.
364 + *
324 365 * @return array
325 366 */
326 367 private static function get_on_submit_action_data_from_form_options( $form_options, $event = 'create' ) {
327 368 $opt = 'update' === $event ? 'edit_' : 'success_';
328 369 $data = array(
329 - 'success_action' => isset( $form_options[ $opt . 'action' ] ) ? $form_options[ $opt . 'action' ] : self::get_default_action_type(),
370 + 'success_action' => $form_options[ $opt . 'action' ] ?? self::get_default_action_type(),
330 371 );
331 372
332 373 switch ( $data['success_action'] ) {
333 374 case 'redirect':
334 - $data['success_url'] = isset( $form_options[ $opt . 'url' ] ) ? $form_options[ $opt . 'url' ] : '';
375 + $data['success_url'] = $form_options[ $opt . 'url' ] ?? '';
335 376 break;
336 377
337 378 case 'page':
338 - $data['success_page_id'] = isset( $form_options[ $opt . 'page_id' ] ) ? $form_options[ $opt . 'page_id' ] : '';
379 + $data['success_page_id'] = $form_options[ $opt . 'page_id' ] ?? '';
339 380 break;
340 381
341 382 default:
342 - $data['success_msg'] = isset( $form_options[ $opt . 'msg' ] ) ? $form_options[ $opt . 'msg' ] : self::get_default_msg();
383 + $data['success_msg'] = $form_options[ $opt . 'msg' ] ?? self::get_default_msg();
343 384 $data['show_form'] = ! empty( $form_options['show_form'] );
344 385 }
345 386
346 387 return $data;
@@ -351,8 +392,9 @@
351 392 *
352 393 * @since 6.1.1
353 394 *
354 395 * @param object $form Form object.
396 + *
355 397 * @return bool
356 398 */
357 399 public static function form_has_migrated( $form ) {
358 400 return ! empty( $form->options['on_submit_migrated'] );
@@ -361,10 +403,16 @@
361 403 /**
362 404 * @since 6.1.1
363 405 *
364 406 * @param object $form Limited form object.
407 + *
408 + * @return void
365 409 */
366 410 private static function save_migrated_success_actions( $form ) {
411 + // Options may be an empty string.
412 + if ( ! is_array( $form->options ) ) {
413 + $form->options = array();
414 + }
367 415 $form->options['on_submit_migrated'] = 1;
368 416 FrmForm::update( $form->id, array( 'options' => $form->options ) );
369 417 }
370 418
@@ -372,19 +420,27 @@
372 420 * Gets fallback action, used when no actions match.
373 421 *
374 422 * @since 6.1.1
375 423 *
376 - * @param string|array $event Uses 'create' or 'update'.
424 + * @param array|string $event Uses 'create' or 'update'.
377 425 *
378 426 * @return object
379 427 */
380 428 public static function get_fallback_action( $event = 'create' ) {
381 - $action = new stdClass();
429 + $action = new stdClass();
430 + $default_msg = self::get_default_msg();
382 431
432 + if ( current_user_can( 'frm_edit_forms' ) ) {
433 + $default_msg .= '<br />';
434 + $default_msg .= '<span style="font-weight: 600; font-style: italic;">';
435 + $default_msg .= __( 'This is the fallback message. No confirmation actions match your conditional logic, or they are invalid.', 'formidable' );
436 + $default_msg .= '</span>';
437 + }
438 +
383 439 $action->post_content = array(
384 440 'event' => (array) $event,
385 441 'success_action' => 'message',
386 - 'success_msg' => self::get_default_msg(),
442 + 'success_msg' => $default_msg,
387 443 );
388 444
389 445 return $action;
390 446 }
@@ -389,11 +445,30 @@
389 445 return $action;
390 446 }
391 447
392 448 /**
393 - * Check if the current event has been paased. If not, use create actions.
449 + * Gets fallback action after opening the redirect URL in a new tab.
394 450 *
451 + * @since 6.3.1
452 + *
453 + * @param array|string $event Uses 'create' or 'update'.
454 + *
455 + * @return object
456 + */
457 + public static function get_fallback_action_after_open_in_new_tab( $event ) {
458 + $action = self::get_fallback_action( $event );
459 +
460 + $action->post_content['success_msg'] = self::get_default_new_tab_msg();
461 +
462 + return $action;
463 + }
464 +
465 + /**
466 + * Check if the current event has been passed. If not, use create actions.
467 + *
395 468 * @since 6.1.1
469 + *
470 + * @param array $atts
396 471 *
397 472 * @return string
398 473 */
399 474 public static function current_event( $atts ) {