Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix settings form not redirecting to the correct tab when submitted #7424

Merged
merged 13 commits into from
Jan 10, 2024
1 change: 0 additions & 1 deletion assets/css/admin-custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ a.sensei-custom-navigation__info {

.sensei-custom-navigation__links a {
position: relative;
top: -3px;
}

.sensei-custom-navigation__links a.page-title-action,
Expand Down
82 changes: 76 additions & 6 deletions assets/js/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jQuery( document ).ready( function ( $ ) {
/***** Settings Tabs *****/
const $senseiSettings = $( '#woothemes-sensei.sensei-settings' );
const PREVIOUS_SECTION_ID_KEY = 'sensei-settings-previous-section-id';

function hideAllSections() {
$senseiSettings.find( 'section' ).hide();
Expand All @@ -9,18 +10,34 @@ jQuery( document ).ready( function ( $ ) {

function show( sectionId = '' ) {
$senseiSettings.find( `section#${ sectionId }` ).show();
$senseiSettings
.find( `[href="#${ sectionId }"]` )
.addClass( 'current' );

let $tabLink = $senseiSettings.find(
`a.tab[href="${ window.location.href }"]`
);
if ( ! $tabLink.length ) {
$tabLink = $senseiSettings.find( `a.tab[href$="#${ sectionId }"]` );
}

$tabLink.addClass( 'current' );

sensei_log_event( 'settings_view', { view: sectionId } );
markSectionAsVisited( sectionId );
}

/**
* Get section id from the URL hash.
*
* @returns string|null
*/
function getSectionIdFromUrl() {
return window.location.hash?.replace( '#', '' );
}

// Hide header and submit on page load if needed
hideSettingsFormElements();

function hideSettingsFormElements() {
const urlHashSectionId = window.location.hash?.replace( '#', '' );
const urlHashSectionId = getSectionIdFromUrl();
if ( urlHashSectionId === 'woocommerce-settings' ) {
const formRows = $senseiSettings.find( '#woocommerce-settings tr' );
// Hide header and submit if there is not settings form in section
Expand Down Expand Up @@ -54,12 +71,15 @@ jQuery( document ).ready( function ( $ ) {

window.onhashchange = hideSettingsFormElements;

// Show general settings section if no section is selected in url hasn.
// Show general settings section if no section is selected in url hash.
// Otherwise, show the section from the URL hash or the last visited section.
const defaultSectionId = 'default-settings';
const urlHashSectionId = window.location.hash?.replace( '#', '' );
const urlHashSectionId = getSectionIdFromUrl();
hideAllSections();
if ( urlHashSectionId ) {
show( urlHashSectionId );
} else if ( hasPreviousSectionId() ) {
showPreviousSection();
} else {
show( defaultSectionId );
}
Expand All @@ -81,6 +101,56 @@ jQuery( document ).ready( function ( $ ) {
return false;
} );

// Store the current section id in the session when the form is submitted.
// This is used to redirect back to the last visited section when the user submits the form.
$senseiSettings
.find( '#sensei-settings-form' )
.on( 'submit', storeCurrentSectionId );

/**
* Store the current section id in the session.
*/
function storeCurrentSectionId() {
const sectionId = getSectionIdFromUrl();

if ( sectionId ) {
window.sessionStorage.setItem( PREVIOUS_SECTION_ID_KEY, sectionId );
}
}

/**
* Get the last visited section id from the session.
*
* @returns string|null
*/
function getPreviousSectionId() {
return window.sessionStorage.getItem( PREVIOUS_SECTION_ID_KEY );
}

/**
* Check if the last visited section id is stored in the session.
*
* @returns boolean
*/
function hasPreviousSectionId() {
return !! getPreviousSectionId();
}

/**
* Show the last visited section and update the URL.
*/
function showPreviousSection() {
const previousSectionId = getPreviousSectionId();
if ( ! previousSectionId ) {
return;
}

window.location.hash = '#' + previousSectionId;
show( previousSectionId );

window.sessionStorage.removeItem( PREVIOUS_SECTION_ID_KEY );
}

function markSectionAsVisited( sectionId ) {
const data = new FormData();
data.append( 'action', 'sensei_settings_section_visited' );
Expand Down
4 changes: 4 additions & 0 deletions changelog/fix-settings-form-redirect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Settings form not redirecting to the correct tab when submitted
4 changes: 0 additions & 4 deletions includes/class-sensei-settings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,6 @@ public function settings_tabs() {
foreach ( $this->tabs as $k => $v ) {
$classes = 'tab';

if ( 'default-settings' === $k ) {
$classes .= ' current';
}

$sections[ $k ] = array(
'href' => isset( $v['href'] )
? esc_attr( $v['href'] )
Expand Down
Loading