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
Merge requests
!7
On activation and deactivation, clean up tokens in previous storage
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
On activation and deactivation, clean up tokens in previous storage
add/activation-hooks
into
master
Overview
0
Commits
3
Pipelines
2
Changes
2
Merged
Erick Hitter
requested to merge
add/activation-hooks
into
master
2 years ago
Overview
0
Commits
3
Pipelines
2
Changes
2
Expand
Fixes
#2 (closed)
0
0
Merge request reports
Compare
master
version 1
6ca484f4
2 years ago
master (base)
and
version 1
latest version
4ca06509
3 commits,
2 years ago
version 1
6ca484f4
1 commit,
2 years ago
2 files
+
96
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
inc/class-activation-deactivation-hooks.php
0 → 100644
+
93
−
0
Options
<?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
();
}
}
Loading