Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Redis User Session Storage
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP Plugins
Redis User Session Storage
Commits
6ca484f4
Commit
6ca484f4
authored
2 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
On activation and deactivation, clean up tokens in previous storage
Fixes
#2
parent
34b7ca48
Loading
Loading
1 merge request
!7
On activation and deactivation, clean up tokens in previous storage
Pipeline
#5054
failed
2 years ago
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inc/class-activation-deactivation-hooks.php
+93
-0
93 additions, 0 deletions
inc/class-activation-deactivation-hooks.php
redis-user-session-storage.php
+3
-0
3 additions, 0 deletions
redis-user-session-storage.php
with
96 additions
and
0 deletions
inc/class-activation-deactivation-hooks.php
0 → 100644
+
93
−
0
View file @
6ca484f4
<?php
/**
* Plugin activation hooks to migrate and clean up data.
*
* @package Redis_User_Session_Storage
*/
namespace
Redis_User_Session_Storage
;
/**
* Class Activation_Deactivation_Hooks.
*/
final
class
Activation_Deactivation_Hooks
{
/**
* Cron hook for usermeta session cleanup.
*
* @var string
*/
private
$cron_hook
=
'redis_user_session_storage_clean_usermeta_storage'
;
/**
* Activation_Hooks constructor.
*
* @param string $plugin_file Path to plugin's main file.
*/
public
function
__construct
(
$plugin_file
)
{
register_activation_hook
(
$plugin_file
,
array
(
$this
,
'clean_usermeta_storage'
)
);
register_deactivation_hook
(
$plugin_file
,
array
(
$this
,
'clean_redis_storage'
)
);
add_action
(
$this
->
cron_hook
,
array
(
$this
,
'do_scheduled_cleanup'
)
);
}
/**
* Remove all sessions from usermeta on activation.
*
* @return void
*/
public
function
clean_usermeta_storage
()
{
wp_schedule_single_event
(
time
()
+
600
,
'redis_user_session_storage_clean_usermeta_storage'
);
}
/**
* Remove session data from usermeta using cron to avoid excessive load.
*
* While this could use `WP_User_Meta_Session_Tokens::drop_sessions()`, this
* approach is safer for large sites.
*
* @return void
*/
public
function
do_scheduled_cleanup
()
{
global
$wpdb
;
$this
->
clean_usermeta_storage
();
$key
=
'session_tokens'
;
$count
=
$wpdb
->
get_var
(
"SELECT COUNT(*) FROM
$wpdb->usermeta
WHERE meta_key = '
$key
'"
);
if
(
!
$count
)
{
wp_clear_scheduled_hook
(
$this
->
cron_hook
);
}
$wpdb
->
query
(
"DELETE FROM
$wpdb->usermeta
WHERE meta_key = '
$key
' LIMIT 500"
);
}
/**
* Remove all sessions from Redis on deactivation.
*
* @return void
*/
public
function
clean_redis_storage
()
{
wp_clear_scheduled_hook
(
$this
->
cron_hook
);
Plugin
::
drop_sessions
();
}
}
This diff is collapsed.
Click to expand it.
redis-user-session-storage.php
+
3
−
0
View file @
6ca484f4
...
@@ -47,6 +47,9 @@ function load() {
...
@@ -47,6 +47,9 @@ function load() {
}
}
require_once
__DIR__
.
'/inc/class-plugin.php'
;
require_once
__DIR__
.
'/inc/class-plugin.php'
;
require_once
__DIR__
.
'/inc/class-activation-deactivation-hooks.php'
;
new
Activation_Deactivation_Hooks
(
__FILE__
);
// Hooked at 9 in case old plugin is also active.
// Hooked at 9 in case old plugin is also active.
add_filter
(
add_filter
(
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment