| @@ -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,43 +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 | - | |
| 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 | |
| 93 | + include FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/on_submit_redirect_settings.php'; | |
| 119 | 94 | } |
| 120 | 95 | |
| 121 | 96 | /** |
| 122 | 97 | * Shows page settings. |
| @@ -129,13 +104,19 @@ | ||
| 129 | 104 | * @type object $form Form data. |
| 130 | 105 | * @type string $action_key Action key. |
| 131 | 106 | * @type array $values Contains `fields` (form fields) and `id` (form ID). |
| 132 | 107 | * } |
| 108 | + * | |
| 109 | + * @return void | |
| 133 | 110 | */ |
| 134 | 111 | public static function show_page_settings( $args ) { |
| 135 | 112 | $name_attr = $args['action_control']->get_field_name( 'success_page_id' ); |
| 113 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 136 | 114 | ?> |
| 137 | - <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> | |
| 138 | 119 | <label for="<?php echo esc_attr( $name_attr ); ?>" class="screen-reader-text"> |
| 139 | 120 | <?php esc_html_e( 'Select a page', 'formidable' ); ?> |
| 140 | 121 | </label> |
| 141 | 122 | <?php |
| @@ -148,8 +129,9 @@ | ||
| 148 | 129 | ); |
| 149 | 130 | ?> |
| 150 | 131 | </div> |
| 151 | 132 | <?php |
| 133 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 152 | 134 | } |
| 153 | 135 | |
| 154 | 136 | /** |
| 155 | 137 | * Gets all active On Submit form actions for form. |
| @@ -157,13 +139,15 @@ | ||
| 157 | 139 | * action is created during run time. If you use another cache method, please remember to delete the cache in |
| 158 | 140 | * {@see FrmFormAction::save_settings()}. |
| 159 | 141 | * |
| 160 | 142 | * @param int $form_id Form ID. |
| 143 | + * | |
| 161 | 144 | * @return array |
| 162 | 145 | */ |
| 163 | 146 | public static function get_actions( $form_id ) { |
| 164 | 147 | $cache_key = 'frm_on_submit_actions_' . $form_id; |
| 165 | 148 | $actions = wp_cache_get( $cache_key, 'frm_actions' ); |
| 149 | + | |
| 166 | 150 | if ( false !== $actions ) { |
| 167 | 151 | return $actions; |
| 168 | 152 | } |
| 169 | 153 | |
| @@ -175,26 +159,33 @@ | ||
| 175 | 159 | /** |
| 176 | 160 | * Gets On Submit action type (message, redirect or page). |
| 177 | 161 | * |
| 178 | 162 | * @param object $action Form action object. |
| 163 | + * | |
| 179 | 164 | * @return string |
| 180 | 165 | */ |
| 181 | 166 | 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(); | |
| 167 | + return $action->post_content['success_action'] ?? self::get_default_action_type(); | |
| 186 | 168 | } |
| 187 | 169 | |
| 170 | + /** | |
| 171 | + * @return string | |
| 172 | + */ | |
| 188 | 173 | public static function get_default_action_type() { |
| 189 | 174 | return 'message'; |
| 190 | 175 | } |
| 191 | 176 | |
| 177 | + /** | |
| 178 | + * @return string | |
| 179 | + */ | |
| 192 | 180 | public static function get_default_msg() { |
| 193 | 181 | $msg = FrmAppHelper::get_settings()->success_msg; |
| 194 | 182 | return $msg ? $msg : __( 'Your responses were successfully submitted. Thank you!', 'formidable' ); |
| 195 | 183 | } |
| 196 | 184 | |
| 185 | + /** | |
| 186 | + * @return string | |
| 187 | + */ | |
| 197 | 188 | public static function get_default_redirect_msg() { |
| 198 | 189 | return __( 'Please wait while you are redirected.', 'formidable' ); |
| 199 | 190 | } |
| 200 | 191 | |
| @@ -200,9 +191,9 @@ | ||
| 200 | 191 | |
| 201 | 192 | /** |
| 202 | 193 | * Gets the default open in new tab message. |
| 203 | 194 | * |
| 204 | - * @since 6.x.x | |
| 195 | + * @since 6.3.1 | |
| 205 | 196 | * |
| 206 | 197 | * @return string |
| 207 | 198 | */ |
| 208 | 199 | public static function get_default_new_tab_msg() { |
| @@ -212,17 +203,21 @@ | ||
| 212 | 203 | /** |
| 213 | 204 | * Adds the first On Submit action data to the form options to be saved. |
| 214 | 205 | * |
| 215 | 206 | * @param int $form_id Form ID. |
| 207 | + * | |
| 208 | + * @return void | |
| 216 | 209 | */ |
| 217 | 210 | public static function save_on_submit_settings( $form_id ) { |
| 218 | 211 | $actions = self::get_actions( $form_id ); |
| 219 | 212 | $first_create_action = null; |
| 220 | 213 | $first_edit_action = null; |
| 214 | + | |
| 221 | 215 | foreach ( $actions as $action ) { |
| 222 | 216 | if ( ! $first_create_action && in_array( 'create', $action->post_content['event'], true ) ) { |
| 223 | 217 | $first_create_action = $action; |
| 224 | 218 | } |
| 219 | + | |
| 225 | 220 | if ( ! $first_edit_action && in_array( 'update', $action->post_content['event'], true ) ) { |
| 226 | 221 | $first_edit_action = $action; |
| 227 | 222 | } |
| 228 | 223 | } |
| @@ -228,16 +223,18 @@ | ||
| 228 | 223 | } |
| 229 | 224 | |
| 230 | 225 | $form_options = array(); |
| 231 | 226 | self::populate_on_submit_data( $form_options, $first_create_action ); |
| 227 | + | |
| 232 | 228 | if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() ) { |
| 233 | 229 | $form_editable = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'editable' ); |
| 230 | + | |
| 234 | 231 | if ( $form_editable ) { |
| 235 | 232 | self::populate_on_submit_data( $form_options, $first_edit_action, 'update' ); |
| 236 | 233 | } |
| 237 | 234 | } |
| 238 | 235 | |
| 239 | - if ( ! empty( $form_options ) ) { | |
| 236 | + if ( $form_options ) { | |
| 240 | 237 | $_POST['options'] += $form_options; |
| 241 | 238 | } |
| 242 | 239 | } |
| 243 | 240 | |
| @@ -243,14 +240,17 @@ | ||
| 243 | 240 | |
| 244 | 241 | /** |
| 245 | 242 | * Populates the On Submit data to form options. |
| 246 | 243 | * |
| 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`. | |
| 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 | |
| 250 | 249 | */ |
| 251 | 250 | public static function populate_on_submit_data( &$form_options, $action = null, $event = 'create' ) { |
| 252 | 251 | $opt = 'update' === $event ? 'edit_' : 'success_'; |
| 252 | + | |
| 253 | 253 | if ( ! $action || ! is_object( $action ) ) { |
| 254 | 254 | $form_options[ $opt . 'action' ] = self::get_default_action_type(); |
| 255 | 255 | $form_options[ $opt . 'msg' ] = self::get_default_msg(); |
| 256 | 256 | |
| @@ -256,18 +256,21 @@ | ||
| 256 | 256 | |
| 257 | 257 | return; |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | - $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'; | |
| 261 | 261 | |
| 262 | 262 | switch ( $form_options[ $opt . 'action' ] ) { |
| 263 | 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'] ); | |
| 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']; | |
| 266 | 269 | break; |
| 267 | 270 | |
| 268 | 271 | case 'page': |
| 269 | - $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'] ?? ''; | |
| 270 | 273 | break; |
| 271 | 274 | |
| 272 | 275 | default: |
| 273 | 276 | $form_options[ $opt . 'msg' ] = ! empty( $action->post_content['success_msg'] ) ? $action->post_content['success_msg'] : self::get_default_msg(); |
| @@ -281,11 +284,14 @@ | ||
| 281 | 284 | * |
| 282 | 285 | * @since 6.1.1 |
| 283 | 286 | * |
| 284 | 287 | * @param int $form_id Form ID. |
| 288 | + * | |
| 289 | + * @return void | |
| 285 | 290 | */ |
| 286 | 291 | public static function maybe_migrate_submit_settings_to_action( $form_id ) { |
| 287 | 292 | $form = FrmDb::get_row( 'frm_forms', array( 'id' => $form_id ), 'options,editable' ); |
| 293 | + | |
| 288 | 294 | if ( ! $form ) { |
| 289 | 295 | return; |
| 290 | 296 | } |
| 291 | 297 | |
| @@ -294,14 +300,15 @@ | ||
| 294 | 300 | if ( self::form_has_migrated( $form ) ) { |
| 295 | 301 | return; |
| 296 | 302 | } |
| 297 | 303 | |
| 298 | - $form->id = $form_id; | |
| 304 | + $form->id = $form_id; | |
| 299 | 305 | $action_type = FrmOnSubmitAction::$slug; |
| 300 | 306 | |
| 301 | 307 | // Check if form already has form actions to avoid creating duplicates. |
| 302 | 308 | $has_actions = FrmFormAction::form_has_action_type( $form_id, $action_type ); |
| 303 | - if ( ! empty( $has_actions ) ) { | |
| 309 | + | |
| 310 | + if ( $has_actions ) { | |
| 304 | 311 | // Don't migrate again. |
| 305 | 312 | self::save_migrated_success_actions( $form ); |
| 306 | 313 | return; |
| 307 | 314 | } |
| @@ -353,27 +360,28 @@ | ||
| 353 | 360 | * @since 6.1.1 |
| 354 | 361 | * |
| 355 | 362 | * @param array $form_options Form options. |
| 356 | 363 | * @param string $event Action event. Accepts `create` or `update`. Default is `create`. |
| 364 | + * | |
| 357 | 365 | * @return array |
| 358 | 366 | */ |
| 359 | 367 | private static function get_on_submit_action_data_from_form_options( $form_options, $event = 'create' ) { |
| 360 | 368 | $opt = 'update' === $event ? 'edit_' : 'success_'; |
| 361 | 369 | $data = array( |
| 362 | - '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(), | |
| 363 | 371 | ); |
| 364 | 372 | |
| 365 | 373 | switch ( $data['success_action'] ) { |
| 366 | 374 | case 'redirect': |
| 367 | - $data['success_url'] = isset( $form_options[ $opt . 'url' ] ) ? $form_options[ $opt . 'url' ] : ''; | |
| 375 | + $data['success_url'] = $form_options[ $opt . 'url' ] ?? ''; | |
| 368 | 376 | break; |
| 369 | 377 | |
| 370 | 378 | case 'page': |
| 371 | - $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' ] ?? ''; | |
| 372 | 380 | break; |
| 373 | 381 | |
| 374 | 382 | default: |
| 375 | - $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(); | |
| 376 | 384 | $data['show_form'] = ! empty( $form_options['show_form'] ); |
| 377 | 385 | } |
| 378 | 386 | |
| 379 | 387 | return $data; |
| @@ -384,8 +392,9 @@ | ||
| 384 | 392 | * |
| 385 | 393 | * @since 6.1.1 |
| 386 | 394 | * |
| 387 | 395 | * @param object $form Form object. |
| 396 | + * | |
| 388 | 397 | * @return bool |
| 389 | 398 | */ |
| 390 | 399 | public static function form_has_migrated( $form ) { |
| 391 | 400 | return ! empty( $form->options['on_submit_migrated'] ); |
| @@ -394,10 +403,16 @@ | ||
| 394 | 403 | /** |
| 395 | 404 | * @since 6.1.1 |
| 396 | 405 | * |
| 397 | 406 | * @param object $form Limited form object. |
| 407 | + * | |
| 408 | + * @return void | |
| 398 | 409 | */ |
| 399 | 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 | + } | |
| 400 | 415 | $form->options['on_submit_migrated'] = 1; |
| 401 | 416 | FrmForm::update( $form->id, array( 'options' => $form->options ) ); |
| 402 | 417 | } |
| 403 | 418 | |
| @@ -405,19 +420,27 @@ | ||
| 405 | 420 | * Gets fallback action, used when no actions match. |
| 406 | 421 | * |
| 407 | 422 | * @since 6.1.1 |
| 408 | 423 | * |
| 409 | - * @param string|array $event Uses 'create' or 'update'. | |
| 424 | + * @param array|string $event Uses 'create' or 'update'. | |
| 410 | 425 | * |
| 411 | 426 | * @return object |
| 412 | 427 | */ |
| 413 | 428 | public static function get_fallback_action( $event = 'create' ) { |
| 414 | - $action = new stdClass(); | |
| 429 | + $action = new stdClass(); | |
| 430 | + $default_msg = self::get_default_msg(); | |
| 415 | 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 | + | |
| 416 | 439 | $action->post_content = array( |
| 417 | 440 | 'event' => (array) $event, |
| 418 | 441 | 'success_action' => 'message', |
| 419 | - 'success_msg' => self::get_default_msg(), | |
| 442 | + 'success_msg' => $default_msg, | |
| 420 | 443 | ); |
| 421 | 444 | |
| 422 | 445 | return $action; |
| 423 | 446 | } |
| @@ -424,11 +447,12 @@ | ||
| 424 | 447 | |
| 425 | 448 | /** |
| 426 | 449 | * Gets fallback action after opening the redirect URL in a new tab. |
| 427 | 450 | * |
| 428 | - * @since 6.x.x | |
| 451 | + * @since 6.3.1 | |
| 429 | 452 | * |
| 430 | - * @param string|array $event Uses 'create' or 'update'. | |
| 453 | + * @param array|string $event Uses 'create' or 'update'. | |
| 454 | + * | |
| 431 | 455 | * @return object |
| 432 | 456 | */ |
| 433 | 457 | public static function get_fallback_action_after_open_in_new_tab( $event ) { |
| 434 | 458 | $action = self::get_fallback_action( $event ); |
| @@ -438,11 +462,13 @@ | ||
| 438 | 462 | return $action; |
| 439 | 463 | } |
| 440 | 464 | |
| 441 | 465 | /** |
| 442 | - * Check if the current event has been paased. If not, use create actions. | |
| 466 | + * Check if the current event has been passed. If not, use create actions. | |
| 443 | 467 | * |
| 444 | 468 | * @since 6.1.1 |
| 469 | + * | |
| 470 | + * @param array $atts | |
| 445 | 471 | * |
| 446 | 472 | * @return string |
| 447 | 473 | */ |
| 448 | 474 | public static function current_event( $atts ) { |