Skip to content

Commit

Permalink
last tab acive issue fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
codersaiful committed Apr 29, 2024
1 parent f0f4e26 commit 7680bbd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
50 changes: 50 additions & 0 deletions admin/post_metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,56 @@ function wpt_array_filter_recursive($array) {
return $array;
}

add_filter('redirect_post_location', 'wpt_redirect_after_save', 10, 2);

/**
* actually redirect to last active tab, we will use this.
* See from post_metabox.php file and admin.js file
* using: setLastActiveTab(tabName); from js code
*
* value taken from <input type="text" name="wpt_last_active_tab" id="wpt-last-active-tab" value=""> file post_metabox_form.php
*
* @since 3.4.8.0
* @link https://github.com/codersaiful/woo-product-table/issues/321
*
* Redirects the user after saving a post based on specific conditions.
* @author Saiful Islam <[email protected]>
*
* @param string $location The URL to redirect to.
* @param int $post_id The ID of the post being saved.
* @return string The updated redirect URL.
*/
function wpt_redirect_after_save($location, $post_id) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/

if ( ! isset( $_POST['wpt_shortcode_nonce_value'] ) ) { // Check if our nonce is set.
return;
}

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if( ! wp_verify_nonce( $_POST['wpt_shortcode_nonce_value'], plugin_basename(__FILE__) ) ) {
return;
}

$wpt_last_active_tab = $_POST['wpt_last_active_tab'] ?? 'column_settings';
if( empty( $wpt_last_active_tab ) ){
$wpt_last_active_tab = 'column_settings';
}

// Check if it's the desired post type
if (get_post_type($post_id) == 'wpt_product_table') {
// Append the desired anchor to the redirect location
$location = add_query_arg('message', 'updated', $location) . '#' . $wpt_last_active_tab;
$location = add_query_arg('wpt_active_tab', $wpt_last_active_tab, $location);
}
return $location;
}


if( ! function_exists( 'wpt_shortcode_configuration_metabox_save_meta' ) ){

function wpt_shortcode_configuration_metabox_save_meta( $post_id, $post ) { // save the data
Expand Down
19 changes: 15 additions & 4 deletions admin/post_metabox_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,29 @@
);
$additional_data = apply_filters( 'wpto_additional_variable', $additional_variable, $post );

$wpt_active_tab = $_GET['wpt_active_tab'] ?? 'column_settings';
if( empty( $wpt_active_tab ) ){
$wpt_active_tab = 'column_settings';
}
echo '<nav class="nav-tab-wrapper">';
$active_nav = 'nav-tab-active';

foreach ($tab_array as $nav => $title) {
$active_nav = $nav == $wpt_active_tab ? 'nav-tab-active' : '';
echo "<a href='#{$nav}' data-tab='{$nav}' class='wpt_nav_for_{$nav} wpt_nav_tab nav-tab " . esc_attr( $active_nav ) . "'>" . wp_kses_post( $title ). "</a>";
$active_nav = false;
}
echo '</nav>';

?>
<!-- actually to store last active tab, we will use this.
See from post_metabox.php file and admin.js file
using: setLastActiveTab(tabName); from js code
-->
<!-- add_filter('redirect_post_location', 'wpt_redirect_after_save', 10, 2); see from post_metabox.php file -->
<input type="hidden" name="wpt_last_active_tab" id="wpt-last-active-tab" value="">
<?php
//Now start for Tab Content
$active_tab_content = 'tab-content-active';
foreach ($tab_array as $tab => $title) {
$active_tab_content = $tab == $wpt_active_tab ? 'tab-content-active' : '';
echo '<div class="wpt_tab_content tab-content ' . esc_attr( $active_tab_content ) . '" id="' . esc_attr( $tab ) . '">';
echo '<div class="fieldwrap">';

Expand Down Expand Up @@ -95,7 +107,6 @@

echo '</div>'; //End of .fieldwrap
echo '</div>'; //End of Tab content div
$active_tab_content = false; //Active tab content only for First
}
?>

Expand Down
35 changes: 34 additions & 1 deletion assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ jQuery.fn.extend({
var selectLinkTab = $(selectLinkTabSelector);
var selectTabContent = $(selectTabContentSelector);
var tabName = window.location.hash.substr(1);
setLastActiveTab(tabName);
if (tabName) {
removingActiveClass();
$('body.wpt_admin_body #wpt_configuration_form #' + tabName).addClass('tab-content-active');
Expand All @@ -398,17 +399,49 @@ jQuery.fn.extend({
$('body.wpt_admin_body').on('click','#wpt_configuration_form a.wpt_nav_tab',function(e){
e.preventDefault(); //Than prevent for click action of hash keyword
var targetTabContent = $(this).data('tab');//getting data value from data-tab attribute

setLastActiveTab(targetTabContent);
// Detect if pushState is available
if(history.pushState) {
history.pushState(null, null, $(this).attr('href'));
}

removingActiveClass();
$(this).addClass('nav-tab-active');
$('body.wpt_admin_body #wpt_configuration_form #' + targetTabContent).addClass('tab-content-active');
return false;
});

/**
* Sets the value of the input element with the ID 'wpt-last-active-tab' to the provided tabName.
*
* related code:
* 1. post_metabox.php
* 2. post_metabox_form.php
*
* Hooked: add_filter('redirect_post_location', 'wpt_redirect_after_save', 10, 2);
*
* @param {string} tabName - The name of the tab to set as the last active tab.
* @return {void} This function does not return anything.
*/
function setLastActiveTab(tabName) {

$('input#wpt-last-active-tab').attr('value',tabName);
//and
if( tabName == '' ){
return;
}
// Get the current URL
var currentUrl = window.location.href;

// Replace the value of 'wpt_active_tab'
var newUrl = currentUrl.replace(/(wpt_active_tab=)[^\&]+/, '$1' + tabName);

// Replace the current URL with the new URL
// Change the URL without reloading the page
history.replaceState(null, null, newUrl);
}


/**
* Removing current active nav_tab and tab_content element
*
Expand Down

0 comments on commit 7680bbd

Please sign in to comment.