Improved handling of removable query args

This commit is contained in:
Tetrakern 2023-05-23 00:57:30 +02:00
parent 0d06212127
commit 09a6d0446c
6 changed files with 49 additions and 31 deletions

View File

@ -449,6 +449,16 @@ Filters the arguments to query the recommendations in the `recommendations.php`
---
### `apply_filters( 'fictioneer_filter_removable_query_args', $query_args )`
Filters the array of query args to be removed after being parsed on page load.
**$query_args:**
* 0 `'success'`
* 1 `'failure'`
* 3 `'fictioneer_nonce'`
---
### `apply_filters( 'fictioneer_filter_root_attributes', $attributes )`
Filters the intermediate output array of the `fictioneer_root_attributes()` function in the `header.php` template before it is looped and rendered. Note that the array keys are used as attribute names and include hyphens.

View File

@ -623,6 +623,19 @@ function fictioneer_add_custom_scripts() {
wp_localize_script( 'fictioneer-application-scripts', 'fictioneer_fonts', fictioneer_get_fonts() );
wp_localize_script( 'fictioneer-application-scripts', 'fictioneer_font_colors', fictioneer_get_font_colors() );
// Append JS snippets
$removable_query_args = ['success', 'failure', 'fictioneer_nonce'];
$removable_query_args = apply_filters( 'fictioneer_filter_removable_query_args', $removable_query_args );
$extra_scripts = "
function fcn_removeQueryArgs() {
history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[?&](" . implode( '|', $removable_query_args ) . ")=[^&]+/g, '').replace(/^[?&]/, '?') + location.hash);
}
";
// Localize the script
wp_add_inline_script( 'fictioneer-application-scripts', $extra_scripts );
// Enqueue mobile menu
wp_enqueue_script( 'fictioneer-mobile-menu-scripts' );
@ -1037,4 +1050,26 @@ add_action( 'admin_notices', 'fictioneer_admin_update_notice' );
add_post_type_support( 'page', 'excerpt' );
// =============================================================================
// ADD REMOVABLE QUERY ARGS
// =============================================================================
/**
* Modifies the list of removable query arguments (admin panel only)
*
* @since Fictioneer 5.2.5
*
* @param array $args The list of removable query arguments.
*
* @return array The modified list of removable query arguments.
*/
function fictioneer_removable_args( $args ) {
$args[] = 'success';
$args[] = 'failure';
$args[] = 'fictioneer_nonce';
return $args;
}
add_filter( 'removable_query_args', 'fictioneer_removable_args' );
?>

View File

@ -812,31 +812,7 @@ if ( ! function_exists( 'fictioneer_clean_actions_from_url' ) ) {
*/
function fictioneer_clean_actions_from_url() {
echo "<script>history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[\?&](action=)[^&]+/, '').replace(/[\?&](fictioneer_nonce=)[^&]+/, '').replace(/^&/, '?') + location.hash);</script>";
}
}
if ( ! function_exists( 'fictioneer_clean_failures_from_url' ) ) {
/**
* Output script to remove error parameters from URL
*
* @since Fictioneer 5.0
*/
function fictioneer_clean_failures_from_url() {
echo "<script>history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[\?&]failure=[^&]+/, '').replace(/^&/, '?') + location.hash);</script>";
}
}
if ( ! function_exists( 'fictioneer_clean_successes_from_url' ) ) {
/**
* Output script to remove success parameters from URL
*
* @since Fictioneer 5.0
*/
function fictioneer_clean_successes_from_url() {
echo "<script>history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[\?&]success=[^&]+/, '').replace(/^&/, '?') + location.hash);</script>";
echo "<script>history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[\?&](action=)[^&]+/, '').replace(/^&/, '?') + location.hash);</script>";
}
}

View File

@ -152,8 +152,6 @@ function fictioneer_custom_profile_fields( $profile_user ) {
// Remove action query args
fictioneer_clean_actions_from_url();
fictioneer_clean_failures_from_url();
fictioneer_clean_successes_from_url();
}
add_action( 'show_user_profile', 'fictioneer_custom_profile_fields', 20 );
add_action( 'edit_user_profile', 'fictioneer_custom_profile_fields', 20 );

File diff suppressed because one or more lines are too long

View File

@ -34,9 +34,8 @@ if (!fcn_isLoggedIn && !fcn_isAjaxAuth) {
// Terminate ongoing text-to-speech when page is reloaded
if (typeof speechSynthesis !== 'undefined') window.speechSynthesis.cancel();
// Remove success and failure params from URL (after they have been extracted)
history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[\?&]failure=[^&]+/, '').replace(/^&/, '?') + location.hash);
history.replaceState && history.replaceState(null, '', location.pathname + location.search.replace(/[\?&]success=[^&]+/, '').replace(/^&/, '?') + location.hash);
// Remove query args (defined in _theme_setup.php)
fcn_removeQueryArgs();
// =============================================================================
// LOCAL STORAGE CLEANUP