From 7680bbd12c5d35d5ed578d42bf31076991f00b55 Mon Sep 17 00:00:00 2001 From: Saiful Islam Date: Mon, 29 Apr 2024 18:45:06 +0600 Subject: [PATCH] last tab acive issue fixed. --- admin/post_metabox.php | 50 +++++++++++++++++++++++++++++++++++++ admin/post_metabox_form.php | 19 +++++++++++--- assets/js/admin.js | 35 +++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 5 deletions(-) diff --git a/admin/post_metabox.php b/admin/post_metabox.php index 1849912d..287af117 100644 --- a/admin/post_metabox.php +++ b/admin/post_metabox.php @@ -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 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 + * + * @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 diff --git a/admin/post_metabox_form.php b/admin/post_metabox_form.php index a9952afa..ba8e5b5c 100644 --- a/admin/post_metabox_form.php +++ b/admin/post_metabox_form.php @@ -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 ''; - + ?> + + + + $title) { + $active_tab_content = $tab == $wpt_active_tab ? 'tab-content-active' : ''; echo '
'; echo '
'; @@ -95,7 +107,6 @@ echo '
'; //End of .fieldwrap echo '
'; //End of Tab content div - $active_tab_content = false; //Active tab content only for First } ?> diff --git a/assets/js/admin.js b/assets/js/admin.js index ce070337..74260bed 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -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'); @@ -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 *