Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions classes/controllers/FrmFormsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2386,7 +2386,7 @@ public static function maybe_trigger_redirect( $form, $params, $args ) {
*/
public static function maybe_trigger_redirect_with_action( $conf_method, $form, $params, $args ) {
if ( is_array( $conf_method ) && 1 === count( $conf_method ) ) {
if ( 'redirect' === FrmOnSubmitHelper::get_action_type( $conf_method[0] ) ) {
if ( 'redirect' === FrmOnSubmitHelper::get_action_type( $conf_method[0] ) && empty( $conf_method[0]->post_content['redirect_delay'] ) ) {
$event = FrmOnSubmitHelper::current_event( $params );
FrmOnSubmitHelper::populate_on_submit_data( $form->options, $conf_method[0], $event );
$conf_method = 'redirect';
Expand Down Expand Up @@ -2723,7 +2723,7 @@ private static function redirect_after_submit( $args ) {
wp_die();
}

if ( ! headers_sent() && empty( $args['force_delay_redirect'] ) ) {
if ( ! headers_sent() && empty( $args['force_delay_redirect'] ) && empty( $args['form']->options['redirect_delay'] ) ) {
// Not AJAX submit, no headers sent, and there is just one Redirect action runs.
if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) {
self::print_open_in_new_tab_js_with_fallback_handler( $success_url, $args );
Expand Down Expand Up @@ -2772,25 +2772,33 @@ private static function print_open_in_new_tab_js_with_fallback_handler( $success
private static function get_ajax_redirect_response_data( $args ) {
$response_data = array( 'redirect' => $args['success_url'] );

if ( ! empty( $args['form']->options['redirect_delay'] ) ) {
$response_data['delay'] = $args['form']->options['redirect_delay_time'];
$response_data['content'] .= self::get_redirect_message( $args['success_url'], $args['form']->options['redirect_delay_msg'], $args );
}

if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) {
$response_data['openInNewTab'] = 1;

$args['message'] = FrmOnSubmitHelper::get_default_new_tab_msg();
// Only show open in new tab text if there is no delay.
if ( empty( $response_data['content'] ) ) {
$args['message'] = FrmOnSubmitHelper::get_default_new_tab_msg();

$args['form']->options['success_msg'] = $args['message'];
$args['form']->options['edit_msg'] = $args['message'];
if ( ! isset( $args['fields'] ) ) {
$args['fields'] = FrmField::get_all_for_form( $args['form']->id );
}
$args['form']->options['success_msg'] = $args['message'];
$args['form']->options['edit_msg'] = $args['message'];
if ( ! isset( $args['fields'] ) ) {
$args['fields'] = FrmField::get_all_for_form( $args['form']->id );
}

$args['message'] = self::prepare_submit_message( $args['form'], $args['entry_id'], $args );
$args['message'] = self::prepare_submit_message( $args['form'], $args['entry_id'], $args );

ob_start();
self::show_lone_success_message( $args );
$response_data['content'] = ob_get_clean();
ob_start();
self::show_lone_success_message( $args );
$response_data['content'] .= ob_get_clean();
Comment thread
truongwp marked this conversation as resolved.
}//end if

$response_data['fallbackMsg'] = self::get_redirect_fallback_message( $args['success_url'], $args );
}
}//end if

return $response_data;
}
Expand All @@ -2803,7 +2811,7 @@ private static function get_ajax_redirect_response_data( $args ) {
* @param array $args See {@see FrmFormsController::run_success_action()}.
*/
private static function redirect_after_submit_using_js( $args ) {
$success_msg = FrmOnSubmitHelper::get_default_redirect_msg();
$success_msg = $args['form']->options['redirect_delay_msg'];
$redirect_msg = self::get_redirect_message( $args['success_url'], $success_msg, $args );
$success_url = esc_url_raw( $args['success_url'] );

Expand All @@ -2814,7 +2822,7 @@ private static function redirect_after_submit_using_js( $args ) {
*
* @param int $delay_time Delay time in milliseconds.
*/
$delay_time = apply_filters( 'frm_redirect_delay_time', 8000 );
$delay_time = apply_filters( 'frm_redirect_delay_time', 1000 * $args['form']->options['redirect_delay_time'] );

if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) {
$redirect_js = 'window.open("' . $success_url . '", "_blank")';
Expand Down Expand Up @@ -2845,6 +2853,8 @@ private static function redirect_after_submit_using_js( $args ) {
* @param array $args
*/
private static function get_redirect_message( $success_url, $success_msg, $args ) {
$success_msg = apply_filters( 'frm_content', $success_msg, $args['form'], $args['entry_id'] );
$success_msg = do_shortcode( FrmAppHelper::use_wpautop( $success_msg ) );
$redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg" role="status">' . $success_msg . '<br/>' .
self::get_redirect_fallback_message( $success_url, $args ) .
'</div></div>';
Expand Down
51 changes: 15 additions & 36 deletions classes/helpers/FrmOnSubmitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,7 @@ public static function show_message_settings( $args ) {
* }
*/
public static function show_redirect_settings( $args ) {
$id_attr = $args['action_control']->get_field_id( 'success_url' );
?>
<div class="frm_form_field frm_has_shortcodes">
<label for="<?php echo esc_attr( $id_attr ); ?>"><?php esc_html_e( 'Redirect URL', 'formidable' ); ?></label>
<input
type="text"
id="<?php echo esc_attr( $id_attr ); ?>"
name="<?php echo esc_attr( $args['action_control']->get_field_name( 'success_url' ) ); ?>"
value="<?php echo esc_attr( $args['form_action']->post_content['success_url'] ); ?>"
/>
</div>

<?php
$id_attr = $args['action_control']->get_field_id( 'open_in_new_tab' );
$name_attr = $args['action_control']->get_field_name( 'open_in_new_tab' );
?>
<div class="frm_form_field">
<?php
FrmHtmlHelper::toggle(
$id_attr,
$name_attr,
array(
'div_class' => 'with_frm_style frm_toggle',
'checked' => ! empty( $args['form_action']->post_content['open_in_new_tab'] ),
'echo' => true,
)
);
?>
<label for="<?php echo esc_attr( $id_attr ); ?>" <?php FrmAppHelper::maybe_add_tooltip( 'new_tab' ); ?>>
<?php esc_html_e( 'Open in new tab', 'formidable' ); ?>
</label>
</div>
<?php
include FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/on_submit_redirect_settings.php';
}

/**
Expand Down Expand Up @@ -264,8 +232,11 @@ public static function populate_on_submit_data( &$form_options, $action = null,

switch ( $form_options[ $opt . 'action' ] ) {
case 'redirect':
$form_options[ $opt . 'url' ] = isset( $action->post_content['success_url'] ) ? $action->post_content['success_url'] : '';
$form_options['open_in_new_tab'] = ! empty( $action->post_content['open_in_new_tab'] );
$form_options[ $opt . 'url' ] = isset( $action->post_content['success_url'] ) ? $action->post_content['success_url'] : '';
$form_options['open_in_new_tab'] = ! empty( $action->post_content['open_in_new_tab'] );
$form_options['redirect_delay'] = ! empty( $action->post_content['redirect_delay'] );
$form_options['redirect_delay_time'] = $action->post_content['redirect_delay_time'];
$form_options['redirect_delay_msg'] = $action->post_content['redirect_delay_msg'];
break;

case 'page':
Expand Down Expand Up @@ -421,10 +392,18 @@ private static function save_migrated_success_actions( $form ) {
public static function get_fallback_action( $event = 'create' ) {
$action = new stdClass();

$default_msg = self::get_default_msg();
if ( current_user_can( 'frm_edit_forms' ) ) {
$default_msg .= '<br />';
$default_msg .= '<span style="font-weight: 600; font-style: italic;">';
$default_msg .= __( 'This is the fallback message. No confirmation actions that match your conditional logic, or they are invalid.', 'formidable' );
$default_msg .= '</span>';
}

$action->post_content = array(
'event' => (array) $event,
'success_action' => 'message',
'success_msg' => self::get_default_msg(),
'success_msg' => $default_msg,
);

return $action;
Expand Down
16 changes: 10 additions & 6 deletions classes/models/FrmOnSubmitAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ public function form( $instance, $args = array() ) {

public function get_defaults() {
return array(
'success_action' => FrmOnSubmitHelper::get_default_action_type(),
'success_msg' => FrmOnSubmitHelper::get_default_msg(),
'show_form' => '',
'success_url' => '',
'success_page_id' => '',
'open_in_new_tab' => '',
'success_action' => FrmOnSubmitHelper::get_default_action_type(),
'success_msg' => FrmOnSubmitHelper::get_default_msg(),
'show_form' => '',
'success_url' => '',
'success_page_id' => '',
'open_in_new_tab' => '',
'redirect_delay' => '',
// Value in second.
'redirect_delay_time' => 8,
'redirect_delay_msg' => FrmOnSubmitHelper::get_default_redirect_msg(),
);
}

Expand Down
121 changes: 121 additions & 0 deletions classes/views/frm-form-actions/on_submit_redirect_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* On Submit redirect settings
*
* @package Formidable
* @since x.x
*
* @var array $args See {@see FrmOnSubmitHelper::show_redirect_settings()}.
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}

$id_attr = $args['action_control']->get_field_id( 'success_url' );
?>
<div class="frm_form_field frm_has_shortcodes">
<label for="<?php echo esc_attr( $id_attr ); ?>"><?php esc_html_e( 'Redirect URL', 'formidable' ); ?></label>
<input
type="text"
id="<?php echo esc_attr( $id_attr ); ?>"
name="<?php echo esc_attr( $args['action_control']->get_field_name( 'success_url' ) ); ?>"
value="<?php echo esc_attr( $args['form_action']->post_content['success_url'] ); ?>"
/>
</div>

<?php
$id_attr = $args['action_control']->get_field_id( 'open_in_new_tab' );
$name_attr = $args['action_control']->get_field_name( 'open_in_new_tab' );
?>
<div class="frm_form_field">
<?php
FrmHtmlHelper::toggle(
$id_attr,
$name_attr,
array(
'div_class' => 'with_frm_style frm_toggle',
'checked' => ! empty( $args['form_action']->post_content['open_in_new_tab'] ),
'echo' => true,
)
);
?>
<label for="<?php echo esc_attr( $id_attr ); ?>" <?php FrmAppHelper::maybe_add_tooltip( 'new_tab' ); ?>>
<?php esc_html_e( 'Open in new tab', 'formidable' ); ?>
</label>
</div>

<?php
$id_attr = $args['action_control']->get_field_id( 'redirect_delay' );
$redirect_delay = ! empty( $args['form_action']->post_content['redirect_delay'] );
?>
<div class="frm_form_field">
<?php
FrmHtmlHelper::toggle(
$id_attr,
$args['action_control']->get_field_name( 'redirect_delay' ),
array(
'div_class' => 'with_frm_style frm_toggle',
'checked' => $redirect_delay,
'echo' => true,
'input_html' => array(
'data-toggleclass' => 'frm_redirect_delay_settings',
),
)
);
?>
<label for="<?php echo esc_attr( $id_attr ); ?>">
<?php esc_html_e( 'Delay redirect and show message', 'formidable' ); ?>
</label>
</div>

<?php
$css_class = 'frm_redirect_delay_settings';
if ( ! $redirect_delay ) {
$css_class .= ' frm_hidden';
}
?>
<div class="<?php echo esc_attr( $css_class ); ?>">
<?php
$id_attr = $args['action_control']->get_field_id( 'redirect_delay_time' );
$delay_time = intval( $args['form_action']->post_content['redirect_delay_time'] );
if ( $delay_time < 1 ) {
$delay_time = 8;
}
?>
<div class="frm_form_field">
<label for="<?php echo esc_attr( $id_attr ); ?>"><?php esc_html_e( 'Delay time', 'formidable' ); ?></label>
<span class="frm_input_with_suffix">
<input
type="number"
min="1"
step="1"
id="<?php echo esc_attr( $id_attr ); ?>"
name="<?php echo esc_attr( $args['action_control']->get_field_name( 'redirect_delay_time' ) ); ?>"
value="<?php echo intval( $delay_time ); ?>"
style="width:60px;"
/><span class="frm_suffix"><?php esc_html_e( 'seconds', 'formidable' ); ?></span>
</span>
</div>

<?php
$id_attr = $args['action_control']->get_field_id( 'redirect_delay_msg' );
?>
<div class="frm_form_field frm_has_shortcodes">
<label for="<?php echo esc_attr( $id_attr ); ?>" class="screen-reader-text">
<?php esc_html_e( 'Redirect message', 'formidable' ); ?>
</label>

<?php
wp_editor(
$args['form_action']->post_content['redirect_delay_msg'],
$id_attr,
array(
'textarea_name' => $args['action_control']->get_field_name( 'redirect_delay_msg' ),
'textarea_rows' => 4,
'editor_class' => 'frm_not_email_message',
)
);
?>
</div>
</div>
14 changes: 13 additions & 1 deletion css/frm_admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -8221,6 +8221,18 @@ button.frm_choose_image_box,
line-height: var(--leading);
}

.frm_input_with_suffix {
border-radius: var(--small-radius);
border: 1px solid var(--grey-300);
display: inline-block;
padding-right: 14px;
}

.frm_input_with_suffix input {
border: 0 !important;
padding-right: 5px !important;
}

.frm_wrap ::-webkit-datetime-edit {
line-height: var(--leading);
}
Expand Down Expand Up @@ -9345,7 +9357,7 @@ Responsive Design

/* On Submit action */
.frm_single_on_submit_settings .frm_form_field {
margin: 8px 0;
margin: 22px 0;
}

.frm_on_submit_type_setting {
Expand Down
36 changes: 23 additions & 13 deletions js/formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,25 @@ function frmFrontFormJS() {
data = jQuery( object ).serialize() + '&action=frm_entries_' + action + '&nonce=' + frm_js.nonce; // eslint-disable-line camelcase
shouldTriggerEvent = object.classList.contains( 'frm_trigger_event_on_submit' );

const doRedirect = response => {
jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ]);

if ( ! response.openInNewTab ) {
// We return here because we're redirecting there is no need to update content.
window.location = response.redirect;
return;
}

// We don't return here because we're opening in a new tab, the old tab will still update.
const newTab = window.open( response.redirect, '_blank' );
if ( ! newTab && response.fallbackMsg && response.content ) {
response.content = response.content.trim().replace( /(<\/div><\/div>)$/, ' ' + response.fallbackMsg + '</div></div>' );
}
};

success = function( response ) {
let defaultResponse, formID, replaceContent, pageOrder, formReturned, contSubmit, delay,
$fieldCont, key, inCollapsedSection, frmTrigger, newTab;
$fieldCont, key, inCollapsedSection, frmTrigger;

defaultResponse = {
content: '',
Expand All @@ -832,18 +848,12 @@ function frmFrontFormJS() {
return;
}

jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ]);

if ( ! response.openInNewTab ) {
// We return here because we're redirecting there is no need to update content.
window.location = response.redirect;
return;
}

// We don't return here because we're opening in a new tab, the old tab will still update.
newTab = window.open( response.redirect, '_blank' );
if ( ! newTab && response.fallbackMsg && response.content ) {
response.content = response.content.trim().replace( /(<\/div><\/div>)$/, ' ' + response.fallbackMsg + '</div></div>' );
if ( response.delay ) {
setTimeout( function() {
doRedirect( response );
}, 1000 * response.delay );
} else {
doRedirect( response );
}
}

Expand Down
Loading