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

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

452 lines 13.9 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 $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
98 <?php
99 $id_attr = $args['action_control']->get_field_id( 'open_in_new_tab' );
100 $name_attr = $args['action_control']->get_field_name( 'open_in_new_tab' );
101 ?>
102 <div class="frm_form_field">
103 <?php
104 FrmHtmlHelper::toggle(
105 $id_attr,
106 $name_attr,
107 array(
108 'div_class' => 'with_frm_style frm_toggle',
109 'checked' => ! empty( $args['form_action']->post_content['open_in_new_tab'] ),
110 'echo' => true,
111 )
112 );
113 ?>
114 <label for="<?php echo esc_attr( $id_attr ); ?>" <?php FrmAppHelper::maybe_add_tooltip( 'new_tab' ); ?>>
115 <?php esc_html_e( 'Open in new tab', 'formidable' ); ?>
116 </label>
117 </div>
118 <?php
119 }
120
121 /**
122 * Shows page settings.
123 *
124 * @param array $args {
125 * The args.
126 *
127 * @type object $form_action Form action post data.
128 * @type FrmFormAction $action_control Form action object.
129 * @type object $form Form data.
130 * @type string $action_key Action key.
131 * @type array $values Contains `fields` (form fields) and `id` (form ID).
132 * }
133 */
134 public static function show_page_settings( $args ) {
135 $name_attr = $args['action_control']->get_field_name( 'success_page_id' );
136 ?>
137 <div class="frm_form_field">
138 <label for="<?php echo esc_attr( $name_attr ); ?>" class="screen-reader-text">
139 <?php esc_html_e( 'Select a page', 'formidable' ); ?>
140 </label>
141 <?php
142 FrmAppHelper::maybe_autocomplete_pages_options(
143 array(
144 'field_name' => $name_attr,
145 'page_id' => $args['form_action']->post_content['success_page_id'],
146 'placeholder' => __( 'Select a Page', 'formidable' ),
147 )
148 );
149 ?>
150 </div>
151 <?php
152 }
153
154 /**
155 * Gets all active On Submit form actions for form.
156 * This checks and gets data from WordPress cache group `frm_actions` first. It prevents the cache issue if form
157 * action is created during run time. If you use another cache method, please remember to delete the cache in
158 * {@see FrmFormAction::save_settings()}.
159 *
160 * @param int $form_id Form ID.
161 * @return array
162 */
163 public static function get_actions( $form_id ) {
164 $cache_key = 'frm_on_submit_actions_' . $form_id;
165 $actions = wp_cache_get( $cache_key, 'frm_actions' );
166 if ( false !== $actions ) {
167 return $actions;
168 }
169
170 $actions = FrmFormAction::get_action_for_form( $form_id, FrmOnSubmitAction::$slug, array( 'post_status' => 'publish' ) );
171 wp_cache_set( $cache_key, 'frm_actions' );
172 return $actions;
173 }
174
175 /**
176 * Gets On Submit action type (message, redirect or page).
177 *
178 * @param object $action Form action object.
179 * @return string
180 */
181 public static function get_action_type( $action ) {
182 if ( isset( $action->post_content['success_action'] ) ) {
183 return $action->post_content['success_action'];
184 }
185 return self::get_default_action_type();
186 }
187
188 public static function get_default_action_type() {
189 return 'message';
190 }
191
192 public static function get_default_msg() {
193 $msg = FrmAppHelper::get_settings()->success_msg;
194 return $msg ? $msg : __( 'Your responses were successfully submitted. Thank you!', 'formidable' );
195 }
196
197 public static function get_default_redirect_msg() {
198 return __( 'Please wait while you are redirected.', 'formidable' );
199 }
200
201 /**
202 * Gets the default open in new tab message.
203 *
204 * @since 6.3.1
205 *
206 * @return string
207 */
208 public static function get_default_new_tab_msg() {
209 return FrmAppHelper::get_settings()->new_tab_msg;
210 }
211
212 /**
213 * Adds the first On Submit action data to the form options to be saved.
214 *
215 * @param int $form_id Form ID.
216 */
217 public static function save_on_submit_settings( $form_id ) {
218 $actions = self::get_actions( $form_id );
219 $first_create_action = null;
220 $first_edit_action = null;
221 foreach ( $actions as $action ) {
222 if ( ! $first_create_action && in_array( 'create', $action->post_content['event'], true ) ) {
223 $first_create_action = $action;
224 }
225 if ( ! $first_edit_action && in_array( 'update', $action->post_content['event'], true ) ) {
226 $first_edit_action = $action;
227 }
228 }
229
230 $form_options = array();
231 self::populate_on_submit_data( $form_options, $first_create_action );
232 if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() ) {
233 $form_editable = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'editable' );
234 if ( $form_editable ) {
235 self::populate_on_submit_data( $form_options, $first_edit_action, 'update' );
236 }
237 }
238
239 if ( ! empty( $form_options ) ) {
240 $_POST['options'] += $form_options;
241 }
242 }
243
244 /**
245 * Populates the On Submit data to form options.
246 *
247 * @param array $form_options Form options.
248 * @param object $action Optional. The On Submit action object.
249 * @param string $event Form event. Default is `create`.
250 */
251 public static function populate_on_submit_data( &$form_options, $action = null, $event = 'create' ) {
252 $opt = 'update' === $event ? 'edit_' : 'success_';
253 if ( ! $action || ! is_object( $action ) ) {
254 $form_options[ $opt . 'action' ] = self::get_default_action_type();
255 $form_options[ $opt . 'msg' ] = self::get_default_msg();
256
257 return;
258 }
259
260 $form_options[ $opt . 'action' ] = isset( $action->post_content['success_action'] ) ? $action->post_content['success_action'] : 'message';
261
262 switch ( $form_options[ $opt . 'action' ] ) {
263 case 'redirect':
264 $form_options[ $opt . 'url' ] = isset( $action->post_content['success_url'] ) ? $action->post_content['success_url'] : '';
265 $form_options['open_in_new_tab'] = ! empty( $action->post_content['open_in_new_tab'] );
266 break;
267
268 case 'page':
269 $form_options[ $opt . 'page_id' ] = isset( $action->post_content['success_page_id'] ) ? $action->post_content['success_page_id'] : '';
270 break;
271
272 default:
273 $form_options[ $opt . 'msg' ] = ! empty( $action->post_content['success_msg'] ) ? $action->post_content['success_msg'] : self::get_default_msg();
274 $form_options['show_form'] = ! empty( $action->post_content['show_form'] );
275 }
276 }
277
278 /**
279 * Maybe migrate submit settings from the form options to On Submit action.
280 * This is added after On Submit action is released. This might migrate the frontend editing submit settings too.
281 *
282 * @since 6.1.1
283 *
284 * @param int $form_id Form ID.
285 */
286 public static function maybe_migrate_submit_settings_to_action( $form_id ) {
287 $form = FrmDb::get_row( 'frm_forms', array( 'id' => $form_id ), 'options,editable' );
288 if ( ! $form ) {
289 return;
290 }
291
292 FrmAppHelper::unserialize_or_decode( $form->options );
293
294 if ( self::form_has_migrated( $form ) ) {
295 return;
296 }
297
298 $form->id = $form_id;
299 $action_type = FrmOnSubmitAction::$slug;
300
301 // Check if form already has form actions to avoid creating duplicates.
302 $has_actions = FrmFormAction::form_has_action_type( $form_id, $action_type );
303 if ( ! empty( $has_actions ) ) {
304 // Don't migrate again.
305 self::save_migrated_success_actions( $form );
306 return;
307 }
308
309 // Create On Submit action.
310 $base_action = array();
311
312 $base_action['post_type'] = FrmFormActionsController::$action_post_type;
313 $base_action['post_excerpt'] = $action_type;
314 $base_action['post_title'] = FrmOnSubmitAction::get_name();
315 $base_action['menu_order'] = $form_id;
316 $base_action['post_status'] = 'publish';
317 $base_action['post_content'] = array(
318 'event' => array( 'create' ),
319 );
320
321 $action_data = self::get_on_submit_action_data_from_form_options( $form->options );
322
323 // If frontend editing is enabled, migrate its settings too.
324 if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() && $form->editable ) {
325 $edit_data = self::get_on_submit_action_data_from_form_options( $form->options, 'update' );
326
327 if ( $action_data === $edit_data ) {
328 // Just create one action for both create and update if they are the same.
329 $base_action['post_content']['event'][] = 'update';
330 } else {
331 // Create a separate action for update.
332 $edit_action = $base_action;
333 $edit_action['post_content'] += $edit_data;
334 $edit_action['post_content']['event'] = array( 'update' );
335
336 $edit_action['post_content'] = FrmAppHelper::prepare_and_encode( $edit_action['post_content'] );
337 FrmDb::save_json_post( $edit_action );
338 }
339 }
340
341 $action = $base_action;
342 $action['post_content'] += $action_data;
343
344 $action['post_content'] = FrmAppHelper::prepare_and_encode( $action['post_content'] );
345 FrmDb::save_json_post( $action );
346
347 self::save_migrated_success_actions( $form );
348 }
349
350 /**
351 * Gets On Submit action data from form options to be used for the migration.
352 *
353 * @since 6.1.1
354 *
355 * @param array $form_options Form options.
356 * @param string $event Action event. Accepts `create` or `update`. Default is `create`.
357 * @return array
358 */
359 private static function get_on_submit_action_data_from_form_options( $form_options, $event = 'create' ) {
360 $opt = 'update' === $event ? 'edit_' : 'success_';
361 $data = array(
362 'success_action' => isset( $form_options[ $opt . 'action' ] ) ? $form_options[ $opt . 'action' ] : self::get_default_action_type(),
363 );
364
365 switch ( $data['success_action'] ) {
366 case 'redirect':
367 $data['success_url'] = isset( $form_options[ $opt . 'url' ] ) ? $form_options[ $opt . 'url' ] : '';
368 break;
369
370 case 'page':
371 $data['success_page_id'] = isset( $form_options[ $opt . 'page_id' ] ) ? $form_options[ $opt . 'page_id' ] : '';
372 break;
373
374 default:
375 $data['success_msg'] = isset( $form_options[ $opt . 'msg' ] ) ? $form_options[ $opt . 'msg' ] : self::get_default_msg();
376 $data['show_form'] = ! empty( $form_options['show_form'] );
377 }
378
379 return $data;
380 }
381
382 /**
383 * Checks if On Submit settings are migrated.
384 *
385 * @since 6.1.1
386 *
387 * @param object $form Form object.
388 * @return bool
389 */
390 public static function form_has_migrated( $form ) {
391 return ! empty( $form->options['on_submit_migrated'] );
392 }
393
394 /**
395 * @since 6.1.1
396 *
397 * @param object $form Limited form object.
398 */
399 private static function save_migrated_success_actions( $form ) {
400 $form->options['on_submit_migrated'] = 1;
401 FrmForm::update( $form->id, array( 'options' => $form->options ) );
402 }
403
404 /**
405 * Gets fallback action, used when no actions match.
406 *
407 * @since 6.1.1
408 *
409 * @param string|array $event Uses 'create' or 'update'.
410 *
411 * @return object
412 */
413 public static function get_fallback_action( $event = 'create' ) {
414 $action = new stdClass();
415
416 $action->post_content = array(
417 'event' => (array) $event,
418 'success_action' => 'message',
419 'success_msg' => self::get_default_msg(),
420 );
421
422 return $action;
423 }
424
425 /**
426 * Gets fallback action after opening the redirect URL in a new tab.
427 *
428 * @since 6.3.1
429 *
430 * @param string|array $event Uses 'create' or 'update'.
431 * @return object
432 */
433 public static function get_fallback_action_after_open_in_new_tab( $event ) {
434 $action = self::get_fallback_action( $event );
435
436 $action->post_content['success_msg'] = self::get_default_new_tab_msg();
437
438 return $action;
439 }
440
441 /**
442 * Check if the current event has been paased. If not, use create actions.
443 *
444 * @since 6.1.1
445 *
446 * @return string
447 */
448 public static function current_event( $atts ) {
449 return ! empty( $atts['action'] ) ? $atts['action'] : 'create';
450 }
451 }
452