Skip to content

Commit

Permalink
clear values on logout
Browse files Browse the repository at this point in the history
ajayadav09 committed Oct 9, 2024
1 parent f9c7da3 commit ea5c0a2
Showing 2 changed files with 28 additions and 20 deletions.
29 changes: 28 additions & 1 deletion includes/HelpCenter.php
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ public function register_assets() {
\wp_register_script(
self::$slug,
NFD_HELPCENTER_PLUGIN_URL . 'vendor/newfold-labs/wp-module-help-center/build/index.js',
array_merge( $asset['dependencies'], array() ),
array_merge( $asset['dependencies'], array(), array( 'jquery', 'heartbeat' ) ),
$asset['version'],
true
);
@@ -130,6 +130,33 @@ public function register_assets() {
) . ';',
'before'
);

/* Remove values on log out */
$logout_listener_js = <<<JS
jQuery(document).ready(function ($) {
$('a[href*="wp-login.php?action=logout"]').on('click', function () {
localStorage.removeItem('helpResultContent');
localStorage.removeItem('searchInput');
localStorage.removeItem('helpVisible');
});
});
JS;

\wp_add_inline_script( self::$slug, $logout_listener_js );

/* Remove values when the user is logged out */
$session_expiration_js = <<<JS
jQuery(document).on('heartbeat-tick', function (event, data) {
if (data.hasOwnProperty('wp-auth-check') && data['wp-auth-check'] === false) {
localStorage.removeItem('helpResultContent');
localStorage.removeItem('searchInput');
localStorage.removeItem('helpVisible');
}
});
JS;

\wp_add_inline_script( self::$slug, $session_expiration_js );

}
}
}
19 changes: 0 additions & 19 deletions src/components/HelpCenter.js
Original file line number Diff line number Diff line change
@@ -33,25 +33,6 @@ const HelpCenter = ( props ) => {
};
}, [] );

useEffect( () => {
const handleHeartbeat = ( event, data ) => {
if (
data.hasOwnProperty( 'wp-auth-check' ) &&
! data[ 'wp-auth-check' ]
) {
// WordPress auth check failed, session is expired, Clear relevant localStorage data
LocalStorageUtils.clear();
}
};

// Listening to the WordPress Heartbeat tick event to check user session
window.jQuery( document ).on( 'heartbeat-tick', handleHeartbeat );

return () => {
window.jQuery( document ).off( 'heartbeat-tick', handleHeartbeat );
};
}, [] );

if ( ! helpEnabled || ! visible ) {
return <></>;
}

0 comments on commit ea5c0a2

Please sign in to comment.