PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.22.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.22.3
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmOnSubmitHelper.php

FrmOnSubmitHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.22.3, at classes/helpers/FrmOnSubmitHelper.php

439 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * On Submit action helper
4 *
5 * @package Formidable
6 * @since 6.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die( 'You are not allowed to call this page directly.' );
11 }
12
13 class FrmOnSubmitHelper {
14
15 /**
16 * Shows message settings.
17 *
18 * @param array $args {
19 * The args.
20 *
21 * @type object $form_action Form action post data.
22 * @type FrmFormAction $action_control Form action object.
23 * @type object $form Form data.
24 * @type string $action_key Action key.
25 * @type array $values Contains `fields` (form fields) and `id` (form ID).
26 * }
27 */
28 public static function show_message_settings( $args ) {
29 $id_attr = $args['action_control']->get_field_id( 'success_msg' );
30 ?>
31 <div class="frm_form_field frm_has_shortcodes">
32 <label for="<?php echo esc_attr( $id_attr ); ?>" class="screen-reader-text">
33 <?php esc_html_e( 'Message on submit', 'formidable' ); ?>
34 </label>
35
36 <?php
37 wp_editor(
38 $args['form_action']->post_content['success_msg'],
39 $id_attr,
40 array(
41 'textarea_name' => $args['action_control']->get_field_name( 'success_msg' ),
42 'textarea_rows' => 4,
43 'editor_class' => 'frm_not_email_message',
44 )
45 );
46 ?>
47 </div>
48
49 <?php
50 $id_attr = $args['action_control']->get_field_id( 'show_form' );
51 $name_attr = $args['action_control']->get_field_name( 'show_form' );
52 ?>
53 <div class="frm_form_field">
54 <?php
55 FrmHtmlHelper::toggle(
56 $id_attr,
57 $name_attr,
58 array(
59 'div_class' => 'with_frm_style frm_toggle',
60 'checked' => ! empty( $args['form_action']->post_content['show_form'] ),
61 'echo' => true,
62 )
63 );
64 ?>
65 <label for="<?php echo esc_attr( $id_attr ); ?>">
66 <?php esc_html_e( 'Show the form with the confirmation message', 'formidable' ); ?>
67 </label>
68 </div>
69 <?php
70 }
71
72 /**
73 * Shows redirect settings.
74 *
75 * @param array $args {
76 * The args.
77 *
78 * @type object $form_action Form action post data.
79 * @type FrmFormAction $action_control Form action object.
80 * @type object $form Form data.
81 * @type string $action_key Action key.
82 * @type array $values Contains `fields` (form fields) and `id` (form ID).
83 * }
84 */
85 public static function show_redirect_settings( $args ) {
86 include FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/on_submit_redirect_settings.php';
87 }
88
89 /**
90 * Shows page settings.
91 *
92 * @param array $args {
93 * The args.
94 *
95 * @type object $form_action Form action post data.
96 * @type FrmFormAction $action_control Form action object.
97 * @type object $form Form data.
98 * @type string $action_key Action key.
99 * @type array $values Contains `fields` (form fields) and `id` (form ID).
100 * }
101 */
102 public static function show_page_settings( $args ) {
103 $name_attr = $args['action_control']->get_field_name( 'success_page_id' );
104 ?>
105 <div class="frm_form_field">
106 <div class="frm_note_style">
107 <?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 ?>
108 </div>
109 <label for="<?php echo esc_attr( $name_attr ); ?>" class="screen-reader-text">
110 <?php esc_html_e( 'Select a page', 'formidable' ); ?>
111 </label>
112 <?php
113 FrmAppHelper::maybe_autocomplete_pages_options(
114 array(
115 'field_name' => $name_attr,
116 'page_id' => $args['form_action']->post_content['success_page_id'],
117 'placeholder' => __( 'Select a Page', 'formidable' ),
118 )
119 );
120 ?>
121 </div>
122 <?php
123 }
124
125 /**
126 * Gets all active On Submit form actions for form.
127 * This checks and gets data from WordPress cache group `frm_actions` first. It prevents the cache issue if form
128 * action is created during run time. If you use another cache method, please remember to delete the cache in
129 * {@see FrmFormAction::save_settings()}.
130 *
131 * @param int $form_id Form ID.
132 * @return array
133 */
134 public static function get_actions( $form_id ) {
135 $cache_key = 'frm_on_submit_actions_' . $form_id;
136 $actions = wp_cache_get( $cache_key, 'frm_actions' );
137 if ( false !== $actions ) {
138 return $actions;
139 }
140
141 $actions = FrmFormAction::get_action_for_form( $form_id, FrmOnSubmitAction::$slug, array( 'post_status' => 'publish' ) );
142 wp_cache_set( $cache_key, 'frm_actions' );
143 return $actions;
144 }
145
146 /**
147 * Gets On Submit action type (message, redirect or page).
148 *
149 * @param object $action Form action object.
150 * @return string
151 */
152 public static function get_action_type( $action ) {
153 if ( isset( $action->post_content['success_action'] ) ) {
154 return $action->post_content['success_action'];
155 }
156 return self::get_default_action_type();
157 }
158
159 public static function get_default_action_type() {
160 return 'message';
161 }
162
163 public static function get_default_msg() {
164 $msg = FrmAppHelper::get_settings()->success_msg;
165 return $msg ? $msg : __( 'Your responses were successfully submitted. Thank you!', 'formidable' );
166 }
167
168 public static function get_default_redirect_msg() {
169 return __( 'Please wait while you are redirected.', 'formidable' );
170 }
171
172 /**
173 * Gets the default open in new tab message.
174 *
175 * @since 6.3.1
176 *
177 * @return string
178 */
179 public static function get_default_new_tab_msg() {
180 return FrmAppHelper::get_settings()->new_tab_msg;
181 }
182
183 /**
184 * Adds the first On Submit action data to the form options to be saved.
185 *
186 * @param int $form_id Form ID.
187 */
188 public static function save_on_submit_settings( $form_id ) {
189 $actions = self::get_actions( $form_id );
190 $first_create_action = null;
191 $first_edit_action = null;
192 foreach ( $actions as $action ) {
193 if ( ! $first_create_action && in_array( 'create', $action->post_content['event'], true ) ) {
194 $first_create_action = $action;
195 }
196 if ( ! $first_edit_action && in_array( 'update', $action->post_content['event'], true ) ) {
197 $first_edit_action = $action;
198 }
199 }
200
201 $form_options = array();
202 self::populate_on_submit_data( $form_options, $first_create_action );
203 if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() ) {
204 $form_editable = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'editable' );
205 if ( $form_editable ) {
206 self::populate_on_submit_data( $form_options, $first_edit_action, 'update' );
207 }
208 }
209
210 if ( ! empty( $form_options ) ) {
211 $_POST['options'] += $form_options;
212 }
213 }
214
215 /**
216 * Populates the On Submit data to form options.
217 *
218 * @param array $form_options Form options.
219 * @param object $action Optional. The On Submit action object.
220 * @param string $event Form event. Default is `create`.
221 */
222 public static function populate_on_submit_data( &$form_options, $action = null, $event = 'create' ) {
223 $opt = 'update' === $event ? 'edit_' : 'success_';
224 if ( ! $action || ! is_object( $action ) ) {
225 $form_options[ $opt . 'action' ] = self::get_default_action_type();
226 $form_options[ $opt . 'msg' ] = self::get_default_msg();
227
228 return;
229 }
230
231 $form_options[ $opt . 'action' ] = isset( $action->post_content['success_action'] ) ? $action->post_content['success_action'] : 'message';
232
233 switch ( $form_options[ $opt . 'action' ] ) {
234 case 'redirect':
235 $form_options[ $opt . 'url' ] = isset( $action->post_content['success_url'] ) ? $action->post_content['success_url'] : '';
236 $form_options['open_in_new_tab'] = ! empty( $action->post_content['open_in_new_tab'] );
237 $form_options['redirect_delay'] = ! empty( $action->post_content['redirect_delay'] );
238 $form_options['redirect_delay_time'] = $action->post_content['redirect_delay_time'];
239 $form_options['redirect_delay_msg'] = $action->post_content['redirect_delay_msg'];
240 break;
241
242 case 'page':
243 $form_options[ $opt . 'page_id' ] = isset( $action->post_content['success_page_id'] ) ? $action->post_content['success_page_id'] : '';
244 break;
245
246 default:
247 $form_options[ $opt . 'msg' ] = ! empty( $action->post_content['success_msg'] ) ? $action->post_content['success_msg'] : self::get_default_msg();
248 $form_options['show_form'] = ! empty( $action->post_content['show_form'] );
249 }
250 }
251
252 /**
253 * Maybe migrate submit settings from the form options to On Submit action.
254 * This is added after On Submit action is released. This might migrate the frontend editing submit settings too.
255 *
256 * @since 6.1.1
257 *
258 * @param int $form_id Form ID.
259 */
260 public static function maybe_migrate_submit_settings_to_action( $form_id ) {
261 $form = FrmDb::get_row( 'frm_forms', array( 'id' => $form_id ), 'options,editable' );
262 if ( ! $form ) {
263 return;
264 }
265
266 FrmAppHelper::unserialize_or_decode( $form->options );
267
268 if ( self::form_has_migrated( $form ) ) {
269 return;
270 }
271
272 $form->id = $form_id;
273 $action_type = FrmOnSubmitAction::$slug;
274
275 // Check if form already has form actions to avoid creating duplicates.
276 $has_actions = FrmFormAction::form_has_action_type( $form_id, $action_type );
277 if ( ! empty( $has_actions ) ) {
278 // Don't migrate again.
279 self::save_migrated_success_actions( $form );
280 return;
281 }
282
283 // Create On Submit action.
284 $base_action = array();
285
286 $base_action['post_type'] = FrmFormActionsController::$action_post_type;
287 $base_action['post_excerpt'] = $action_type;
288 $base_action['post_title'] = FrmOnSubmitAction::get_name();
289 $base_action['menu_order'] = $form_id;
290 $base_action['post_status'] = 'publish';
291 $base_action['post_content'] = array(
292 'event' => array( 'create' ),
293 );
294
295 $action_data = self::get_on_submit_action_data_from_form_options( $form->options );
296
297 // If frontend editing is enabled, migrate its settings too.
298 if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() && $form->editable ) {
299 $edit_data = self::get_on_submit_action_data_from_form_options( $form->options, 'update' );
300
301 if ( $action_data === $edit_data ) {
302 // Just create one action for both create and update if they are the same.
303 $base_action['post_content']['event'][] = 'update';
304 } else {
305 // Create a separate action for update.
306 $edit_action = $base_action;
307 $edit_action['post_content'] += $edit_data;
308 $edit_action['post_content']['event'] = array( 'update' );
309
310 $edit_action['post_content'] = FrmAppHelper::prepare_and_encode( $edit_action['post_content'] );
311 FrmDb::save_json_post( $edit_action );
312 }
313 }
314
315 $action = $base_action;
316 $action['post_content'] += $action_data;
317
318 $action['post_content'] = FrmAppHelper::prepare_and_encode( $action['post_content'] );
319 FrmDb::save_json_post( $action );
320
321 self::save_migrated_success_actions( $form );
322 }
323
324 /**
325 * Gets On Submit action data from form options to be used for the migration.
326 *
327 * @since 6.1.1
328 *
329 * @param array $form_options Form options.
330 * @param string $event Action event. Accepts `create` or `update`. Default is `create`.
331 * @return array
332 */
333 private static function get_on_submit_action_data_from_form_options( $form_options, $event = 'create' ) {
334 $opt = 'update' === $event ? 'edit_' : 'success_';
335 $data = array(
336 'success_action' => isset( $form_options[ $opt . 'action' ] ) ? $form_options[ $opt . 'action' ] : self::get_default_action_type(),
337 );
338
339 switch ( $data['success_action'] ) {
340 case 'redirect':
341 $data['success_url'] = isset( $form_options[ $opt . 'url' ] ) ? $form_options[ $opt . 'url' ] : '';
342 break;
343
344 case 'page':
345 $data['success_page_id'] = isset( $form_options[ $opt . 'page_id' ] ) ? $form_options[ $opt . 'page_id' ] : '';
346 break;
347
348 default:
349 $data['success_msg'] = isset( $form_options[ $opt . 'msg' ] ) ? $form_options[ $opt . 'msg' ] : self::get_default_msg();
350 $data['show_form'] = ! empty( $form_options['show_form'] );
351 }
352
353 return $data;
354 }
355
356 /**
357 * Checks if On Submit settings are migrated.
358 *
359 * @since 6.1.1
360 *
361 * @param object $form Form object.
362 * @return bool
363 */
364 public static function form_has_migrated( $form ) {
365 return ! empty( $form->options['on_submit_migrated'] );
366 }
367
368 /**
369 * @since 6.1.1
370 *
371 * @param object $form Limited form object.
372 * @return void
373 */
374 private static function save_migrated_success_actions( $form ) {
375 // Options may be an empty string.
376 if ( ! is_array( $form->options ) ) {
377 $form->options = array();
378 }
379 $form->options['on_submit_migrated'] = 1;
380 FrmForm::update( $form->id, array( 'options' => $form->options ) );
381 }
382
383 /**
384 * Gets fallback action, used when no actions match.
385 *
386 * @since 6.1.1
387 *
388 * @param array|string $event Uses 'create' or 'update'.
389 *
390 * @return object
391 */
392 public static function get_fallback_action( $event = 'create' ) {
393 $action = new stdClass();
394
395 $default_msg = self::get_default_msg();
396 if ( current_user_can( 'frm_edit_forms' ) ) {
397 $default_msg .= '<br />';
398 $default_msg .= '<span style="font-weight: 600; font-style: italic;">';
399 $default_msg .= __( 'This is the fallback message. No confirmation actions match your conditional logic, or they are invalid.', 'formidable' );
400 $default_msg .= '</span>';
401 }
402
403 $action->post_content = array(
404 'event' => (array) $event,
405 'success_action' => 'message',
406 'success_msg' => $default_msg,
407 );
408
409 return $action;
410 }
411
412 /**
413 * Gets fallback action after opening the redirect URL in a new tab.
414 *
415 * @since 6.3.1
416 *
417 * @param array|string $event Uses 'create' or 'update'.
418 * @return object
419 */
420 public static function get_fallback_action_after_open_in_new_tab( $event ) {
421 $action = self::get_fallback_action( $event );
422
423 $action->post_content['success_msg'] = self::get_default_new_tab_msg();
424
425 return $action;
426 }
427
428 /**
429 * Check if the current event has been passed. If not, use create actions.
430 *
431 * @since 6.1.1
432 *
433 * @return string
434 */
435 public static function current_event( $atts ) {
436 return ! empty( $atts['action'] ) ? $atts['action'] : 'create';
437 }
438 }
439