Skip to content

Commit c04c8b9

Browse files
committed
Enqueue logout script only if logout cookie is set
1 parent 84c468a commit c04c8b9

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Sessions/logout.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
namespace Automattic\Chatrix\Sessions;
44

5+
use const Automattic\Chatrix\LOGOUT_COOKIE_NAME;
6+
use const Automattic\Chatrix\SCRIPT_HANDLE_LOGOUT;
7+
58
function init_logout() {
69
// Set a cookie when user logs out.
710
add_action(
811
'wp_logout',
912
function () {
1013
// Expire in 10 minutes.
1114
$expiration = time() + ( 10 * 60 );
12-
setcookie( 'chatrix-logout', 'true', $expiration, COOKIEPATH, COOKIE_DOMAIN );
15+
setcookie( LOGOUT_COOKIE_NAME, 'true', $expiration, COOKIEPATH, COOKIE_DOMAIN );
16+
17+
// Enqueue the script that will perform logout on the client.
18+
wp_enqueue_script( SCRIPT_HANDLE_LOGOUT );
1319
}
1420
);
1521
}

src/plugin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
const SCRIPT_HANDLE_APP = 'chatrix-app';
1111
const SCRIPT_HANDLE_LOGOUT = 'chatrix-logout';
1212
const CONFIG_VARIABLE = 'ChatrixConfig';
13+
const LOGOUT_COOKIE_NAME = 'chatrix-logout';
1314

1415
function main() {
1516
init_logout();
@@ -51,7 +52,11 @@ function () {
5152
automattic_chatrix_version(),
5253
false
5354
);
54-
wp_enqueue_script( SCRIPT_HANDLE_LOGOUT );
55+
56+
// Enqueue the logout script only if the logout cookie is set.
57+
if ( isset( $_COOKIE[ LOGOUT_COOKIE_NAME ] ) ) {
58+
wp_enqueue_script( SCRIPT_HANDLE_LOGOUT );
59+
}
5560
}
5661
}
5762
);

0 commit comments

Comments
 (0)