| @@ -5,16 +5,15 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmFormsController { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Track the form that opened the redirect URL in a new tab. This is used to check if we should show the default | |
| 10 | - * message in the currect tab. | |
| 9 | + * Track the form and action that ran frm_redirect_url filter. Each item in array is {form_id}_create or {form_id}_update. | |
| 11 | 10 | * |
| 12 | 11 | * @since 6.2 |
| 13 | 12 | * |
| 14 | - * @var array Keys are form IDs and values are 1. | |
| 13 | + * @var array | |
| 15 | 14 | */ |
| 16 | - private static $redirected_in_new_tab = array(); | |
| 15 | + private static $ran_redirect_url_filter = array(); | |
| 17 | 16 | |
| 18 | 17 | public static function menu() { |
| 19 | 18 | $menu_label = __( 'Forms', 'formidable' ); |
| 20 | 19 | if ( ! FrmAppHelper::pro_is_installed() ) { |
| @@ -1615,17 +1614,13 @@ | ||
| 1615 | 1614 | * Replace any [form_name] shortcodes in a string. |
| 1616 | 1615 | * |
| 1617 | 1616 | * @since 5.5 |
| 1618 | 1617 | * |
| 1619 | - * @param string|array $string | |
| 1618 | + * @param string $string | |
| 1620 | 1619 | * @param stdClass|string|int $form |
| 1621 | - * @return string|array | |
| 1620 | + * @return string | |
| 1622 | 1621 | */ |
| 1623 | 1622 | public static function replace_form_name_shortcodes( $string, $form ) { |
| 1624 | - if ( ! is_string( $string ) ) { | |
| 1625 | - return $string; | |
| 1626 | - } | |
| 1627 | - | |
| 1628 | 1623 | if ( false === strpos( $string, '[form_name]' ) ) { |
| 1629 | 1624 | return $string; |
| 1630 | 1625 | } |
| 1631 | 1626 | |
| @@ -2275,13 +2270,8 @@ | ||
| 2275 | 2270 | if ( ! FrmOnSubmitHelper::form_has_migrated( $args['form'] ) ) { |
| 2276 | 2271 | return array(); |
| 2277 | 2272 | } |
| 2278 | 2273 | |
| 2279 | - // If a redirect action has already opened the URL in a new tab, we show the default message in the currect tab. | |
| 2280 | - if ( ! empty( self::$redirected_in_new_tab[ $args['form']->id ] ) ) { | |
| 2281 | - return array( FrmOnSubmitHelper::get_fallback_action_after_open_in_new_tab( $event ) ); | |
| 2282 | - } | |
| 2283 | - | |
| 2284 | 2274 | $entry = FrmEntry::getOne( $args['entry_id'], true ); |
| 2285 | 2275 | $actions = FrmOnSubmitHelper::get_actions( $args['form']->id ); |
| 2286 | 2276 | $met_actions = array(); |
| 2287 | 2277 | $has_redirect = false; |
| @@ -2300,11 +2290,18 @@ | ||
| 2300 | 2290 | if ( 'redirect' === $action_type ) { |
| 2301 | 2291 | if ( $has_redirect ) { // Do not process because we run the first redirect action only. |
| 2302 | 2292 | continue; |
| 2303 | 2293 | } |
| 2294 | + | |
| 2295 | + // Run through frm_redirect_url filter. This is used for the valid action check. | |
| 2296 | + $action->post_content['success_url'] = self::run_redirect_url_filter( | |
| 2297 | + $action->post_content['success_url'], | |
| 2298 | + $args['form'], | |
| 2299 | + $args + array( 'action' => $event ) | |
| 2300 | + ); | |
| 2304 | 2301 | } |
| 2305 | 2302 | |
| 2306 | - if ( ! self::is_valid_on_submit_action( $action, $args, $event ) ) { | |
| 2303 | + if ( ! self::is_valid_on_submit_action( $action ) ) { | |
| 2307 | 2304 | continue; |
| 2308 | 2305 | } |
| 2309 | 2306 | |
| 2310 | 2307 | if ( 'redirect' === $action_type ) { |
| @@ -2334,30 +2331,45 @@ | ||
| 2334 | 2331 | return $met_actions; |
| 2335 | 2332 | } |
| 2336 | 2333 | |
| 2337 | 2334 | /** |
| 2335 | + * Runs frm_redirect_url filter and prepare the URL. | |
| 2336 | + * This ensures that filter just fires once per form and action. | |
| 2337 | + * | |
| 2338 | + * @since 6.2 | |
| 2339 | + * | |
| 2340 | + * @param string $url The URL. | |
| 2341 | + * @param object $form Form object. | |
| 2342 | + * @param array $args Args from {@see FrmFormsController::run_success_action()}. `$args['action']` is required. | |
| 2343 | + */ | |
| 2344 | + private static function run_redirect_url_filter( $url, $form, $args ) { | |
| 2345 | + if ( empty( $args['action'] ) || ! is_string( $args['action'] ) ) { | |
| 2346 | + return $url; | |
| 2347 | + } | |
| 2348 | + | |
| 2349 | + if ( in_array( $form->id . '_' . $args['action'], self::$ran_redirect_url_filter, true ) ) { | |
| 2350 | + return $url; | |
| 2351 | + } | |
| 2352 | + | |
| 2353 | + self::$ran_redirect_url_filter[] = $form->id . '_' . $args['action']; | |
| 2354 | + | |
| 2355 | + add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' ); | |
| 2356 | + return apply_filters( 'frm_redirect_url', $url, $form, $args ); | |
| 2357 | + } | |
| 2358 | + | |
| 2359 | + /** | |
| 2338 | 2360 | * Checks if a Confirmation action has the valid data. |
| 2339 | 2361 | * |
| 2340 | 2362 | * @since 6.1.2 |
| 2341 | 2363 | * |
| 2342 | 2364 | * @param object $action Form action object. |
| 2343 | - * @param array $args See {@see FrmFormsController::run_success_action()}. | |
| 2344 | - * @param string $event Form event. Default is `create`. | |
| 2345 | 2365 | * @return bool |
| 2346 | 2366 | */ |
| 2347 | - private static function is_valid_on_submit_action( $action, $args, $event = 'create' ) { | |
| 2367 | + private static function is_valid_on_submit_action( $action ) { | |
| 2348 | 2368 | $action_type = FrmOnSubmitHelper::get_action_type( $action ); |
| 2349 | 2369 | |
| 2350 | - if ( 'redirect' === $action_type ) { | |
| 2351 | - // Run through frm_redirect_url filter. This is used for the valid action check. | |
| 2352 | - $action->post_content['success_url'] = apply_filters( | |
| 2353 | - 'frm_redirect_url', | |
| 2354 | - $action->post_content['success_url'], | |
| 2355 | - $args['form'], | |
| 2356 | - $args + array( 'action' => $event ) | |
| 2357 | - ); | |
| 2358 | - | |
| 2359 | - return ! empty( $action->post_content['success_url'] ); | |
| 2370 | + if ( 'redirect' === $action_type && empty( $action->post_content['success_url'] ) ) { | |
| 2371 | + return false; | |
| 2360 | 2372 | } |
| 2361 | 2373 | |
| 2362 | 2374 | if ( 'page' === $action_type ) { |
| 2363 | 2375 | if ( empty( $action->post_content['success_page_id'] ) ) { |
| @@ -2521,25 +2533,18 @@ | ||
| 2521 | 2533 | |
| 2522 | 2534 | $args['id'] = $args['entry_id']; |
| 2523 | 2535 | FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args ); |
| 2524 | 2536 | |
| 2525 | - add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' ); | |
| 2526 | - $success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args ); | |
| 2537 | + $success_url = self::run_redirect_url_filter( $success_url, $args['form'], $args ); | |
| 2527 | 2538 | |
| 2528 | 2539 | $doing_ajax = FrmAppHelper::doing_ajax(); |
| 2529 | 2540 | |
| 2530 | 2541 | if ( ! empty( $args['ajax'] ) && $doing_ajax && empty( $args['force_delay_redirect'] ) ) { // Is AJAX submit and there is just one Redirect action runs. |
| 2531 | - echo json_encode( self::get_ajax_redirect_response_data( $args + compact( 'success_url' ) ) ); | |
| 2542 | + echo json_encode( array( 'redirect' => $success_url ) ); | |
| 2532 | 2543 | wp_die(); |
| 2533 | 2544 | } |
| 2534 | 2545 | |
| 2535 | 2546 | if ( ! headers_sent() && empty( $args['force_delay_redirect'] ) ) { // Not AJAX submit, no headers sent, and there is just one Redirect action runs. |
| 2536 | - if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { | |
| 2537 | - self::print_open_in_new_tab_js_with_fallback_handler( $success_url, $args ); | |
| 2538 | - self::$redirected_in_new_tab[ $args['form']->id ] = 1; | |
| 2539 | - return; | |
| 2540 | - } | |
| 2541 | - | |
| 2542 | 2547 | wp_redirect( esc_url_raw( $success_url ) ); |
| 2543 | 2548 | die(); // do not use wp_die or redirect fails |
| 2544 | 2549 | } |
| 2545 | 2550 | |
| @@ -2547,63 +2552,8 @@ | ||
| 2547 | 2552 | self::redirect_after_submit_using_js( $args + compact( 'success_url', 'doing_ajax' ) ); |
| 2548 | 2553 | } |
| 2549 | 2554 | |
| 2550 | 2555 | /** |
| 2551 | - * Prints open in new tab js with fallback handler. | |
| 2552 | - * | |
| 2553 | - * @since 6.3.1 | |
| 2554 | - * | |
| 2555 | - * @param string $success_url Success URL. | |
| 2556 | - * @param array $args See {@see FrmFormsController::redirect_after_submit()}. | |
| 2557 | - */ | |
| 2558 | - private static function print_open_in_new_tab_js_with_fallback_handler( $success_url, $args ) { | |
| 2559 | - echo '<script>var newTab = window.open("' . esc_url_raw( $success_url ) . '", "_blank");'; | |
| 2560 | - echo 'if ( ! newTab ) {'; | |
| 2561 | - | |
| 2562 | - $data = array( | |
| 2563 | - 'formId' => intval( $args['form']->id ), | |
| 2564 | - 'message' => self::get_redirect_fallback_message( $success_url, $args ), | |
| 2565 | - ); | |
| 2566 | - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 2567 | - echo 'frmShowNewTabFallback = ' . FrmAppHelper::maybe_json_encode( $data ) . ';'; | |
| 2568 | - echo '}</script>'; | |
| 2569 | - } | |
| 2570 | - | |
| 2571 | - /** | |
| 2572 | - * Gets response data for redirect action when AJAX submitting. | |
| 2573 | - * | |
| 2574 | - * @since 6.3.1 | |
| 2575 | - * | |
| 2576 | - * @param array $args See {@see FrmFormsController::run_success_action()}. | |
| 2577 | - * @return array | |
| 2578 | - */ | |
| 2579 | - private static function get_ajax_redirect_response_data( $args ) { | |
| 2580 | - $response_data = array( 'redirect' => $args['success_url'] ); | |
| 2581 | - | |
| 2582 | - if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { | |
| 2583 | - $response_data['openInNewTab'] = 1; | |
| 2584 | - | |
| 2585 | - $args['message'] = FrmOnSubmitHelper::get_default_new_tab_msg(); | |
| 2586 | - | |
| 2587 | - $args['form']->options['success_msg'] = $args['message']; | |
| 2588 | - $args['form']->options['edit_msg'] = $args['message']; | |
| 2589 | - if ( ! isset( $args['fields'] ) ) { | |
| 2590 | - $args['fields'] = FrmField::get_all_for_form( $args['form']->id ); | |
| 2591 | - } | |
| 2592 | - | |
| 2593 | - $args['message'] = self::prepare_submit_message( $args['form'], $args['entry_id'], $args ); | |
| 2594 | - | |
| 2595 | - ob_start(); | |
| 2596 | - self::show_lone_success_messsage( $args ); | |
| 2597 | - $response_data['content'] = ob_get_clean(); | |
| 2598 | - | |
| 2599 | - $response_data['fallbackMsg'] = self::get_redirect_fallback_message( $args['success_url'], $args ); | |
| 2600 | - } | |
| 2601 | - | |
| 2602 | - return $response_data; | |
| 2603 | - } | |
| 2604 | - | |
| 2605 | - /** | |
| 2606 | 2556 | * Redirects after submitting using JS. This is used when showing message before redirecting. |
| 2607 | 2557 | * |
| 2608 | 2558 | * @since 6.0 |
| 2609 | 2559 | * |
| @@ -2611,9 +2561,8 @@ | ||
| 2611 | 2561 | */ |
| 2612 | 2562 | private static function redirect_after_submit_using_js( $args ) { |
| 2613 | 2563 | $success_msg = FrmOnSubmitHelper::get_default_redirect_msg(); |
| 2614 | 2564 | $redirect_msg = self::get_redirect_message( $args['success_url'], $success_msg, $args ); |
| 2615 | - $success_url = esc_url_raw( $args['success_url'] ); | |
| 2616 | 2565 | |
| 2617 | 2566 | /** |
| 2618 | 2567 | * Filters the delay time before redirecting when On Submit Redirect action is delayed. |
| 2619 | 2568 | * |
| @@ -2622,14 +2571,8 @@ | ||
| 2622 | 2571 | * @param int $delay_time Delay time in miliseconds. |
| 2623 | 2572 | */ |
| 2624 | 2573 | $delay_time = apply_filters( 'frm_redirect_delay_time', 8000 ); |
| 2625 | 2574 | |
| 2626 | - if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { | |
| 2627 | - $redirect_js = 'window.open("' . $success_url . '", "_blank")'; | |
| 2628 | - } else { | |
| 2629 | - $redirect_js = 'window.location="' . $success_url . '";'; | |
| 2630 | - } | |
| 2631 | - | |
| 2632 | 2575 | add_filter( 'frm_use_wpautop', '__return_true' ); |
| 2633 | 2576 | |
| 2634 | 2577 | echo FrmAppHelper::maybe_kses( $redirect_msg ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 2635 | 2578 | echo '<script>'; |
| @@ -2635,9 +2578,9 @@ | ||
| 2635 | 2578 | echo '<script>'; |
| 2636 | 2579 | if ( empty( $args['doing_ajax'] ) ) { // Not AJAX submit, delay JS until window.load. |
| 2637 | 2580 | echo 'window.onload=function(){'; |
| 2638 | 2581 | } |
| 2639 | - echo 'setTimeout(function(){' . $redirect_js . '}, ' . intval( $delay_time ) . ');'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 2582 | + echo 'setTimeout(function(){window.location="' . esc_url_raw( $args['success_url'] ) . '";}, ' . intval( $delay_time ) . ');'; | |
| 2640 | 2583 | if ( empty( $args['doing_ajax'] ) ) { |
| 2641 | 2584 | echo '};'; |
| 2642 | 2585 | } |
| 2643 | 2586 | echo '</script>'; |
| @@ -2651,9 +2594,10 @@ | ||
| 2651 | 2594 | * @param array $args |
| 2652 | 2595 | */ |
| 2653 | 2596 | private static function get_redirect_message( $success_url, $success_msg, $args ) { |
| 2654 | 2597 | $redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg" role="status">' . $success_msg . '<br/>' . |
| 2655 | - self::get_redirect_fallback_message( $success_url, $args ) . | |
| 2598 | + /* translators: %1$s: Start link HTML, %2$s: End link HTML */ | |
| 2599 | + sprintf( __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '<a href="' . esc_url( $success_url ) . '">', '</a>' ) . | |
| 2656 | 2600 | '</div></div>'; |
| 2657 | 2601 | |
| 2658 | 2602 | $redirect_args = array( |
| 2659 | 2603 | 'entry_id' => $args['entry_id'], |
| @@ -2661,31 +2605,8 @@ | ||
| 2661 | 2605 | 'form' => $args['form'], |
| 2662 | 2606 | ); |
| 2663 | 2607 | |
| 2664 | 2608 | return apply_filters( 'frm_redirect_msg', $redirect_msg, $redirect_args ); |
| 2665 | - } | |
| 2666 | - | |
| 2667 | - /** | |
| 2668 | - * Gets fallback message when redirecting failed. | |
| 2669 | - * | |
| 2670 | - * @since 6.3.1 | |
| 2671 | - * | |
| 2672 | - * @param string $success_url Redirect URL. | |
| 2673 | - * @param array $args Contains `form` object. | |
| 2674 | - * @return string | |
| 2675 | - */ | |
| 2676 | - private static function get_redirect_fallback_message( $success_url, $args ) { | |
| 2677 | - $target = ''; | |
| 2678 | - if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { | |
| 2679 | - $target = ' target="_blank"'; | |
| 2680 | - } | |
| 2681 | - | |
| 2682 | - return sprintf( | |
| 2683 | - /* translators: %1$s: Start link HTML, %2$s: End link HTML */ | |
| 2684 | - __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), | |
| 2685 | - '<a href="' . esc_url( $success_url ) . '"' . $target . '>', | |
| 2686 | - '</a>' | |
| 2687 | - ); | |
| 2688 | 2609 | } |
| 2689 | 2610 | |
| 2690 | 2611 | /** |
| 2691 | 2612 | * Prepare to show the success message and empty form after submit |