-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathwp-session-manager.php
156 lines (135 loc) · 5.18 KB
/
wp-session-manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Plugin Name: WP Session Manager
* Plugin URI: https://paypal.me/eam
* Description: Session management for WordPress.
* Version: 4.2.0
* Author: Eric Mann
* Author URI: https://eamann.com
* License: GPLv2+
*
* @package WP Session Manager
*/
if (!defined('WP_SESSION_MINIMUM_PHP_VERSION')) {
define('WP_SESSION_MINIMUM_PHP_VERSION', '7.1.0');
}
$wp_session_messages = [
'bad_php_version' => sprintf(
__(
'WP Session Manager requires PHP %s or newer. Please contact your system administrator to upgrade!',
'wp-session-manager'
),
WP_SESSION_MINIMUM_PHP_VERSION,
PHP_VERSION
),
'multiple_sessions' => __(
'Another plugin is attempting to start a session with WordPress. WP Session Manager will not work!',
'wp-session-manager'
)
];
/**
* Initialize the plugin, bootstrap autoloading, and register default hooks
*/
function wp_session_manager_initialize()
{
$wp_session_autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($wp_session_autoload)) {
require_once $wp_session_autoload;
}
if (!class_exists('EAMann\Sessionz\Manager')) {
exit('WP Session Manager requires Composer autoloading, which is not configured');
}
if (!isset($_SESSION)) {
// Queue up the session stack.
$wp_session_handler = EAMann\Sessionz\Manager::initialize();
// Fall back to database storage where needed.
if (defined('WP_SESSION_USE_OPTIONS') && WP_SESSION_USE_OPTIONS) {
$wp_session_handler->addHandler(new \EAMann\WPSession\OptionsHandler());
} else {
$wp_session_handler->addHandler(new \EAMann\WPSession\DatabaseHandler());
/**
* The database handler can automatically clean up sessions as it goes. By default,
* we'll run the cleanup routine every hour to catch any stale sessions that PHP's
* garbage collector happens to miss. This timeout can be filtered to increase or
* decrease the frequency of the manual purge.
*
* @param string $timeout Interval with which to purge stale sessions
*/
$timeout = apply_filters('wp_session_gc_interval', 'hourly');
if (!wp_next_scheduled('wp_session_database_gc')) {
wp_schedule_event(time(), $timeout, 'wp_session_database_gc');
}
add_action('wp_session_database_gc', ['EAMann\WPSession\DatabaseHandler', 'directClean']);
}
// If we have an external object cache, let's use it!
if (wp_using_ext_object_cache()) {
$wp_session_handler->addHandler(new EAMann\WPSession\CacheHandler());
}
// Decrypt the data surfacing from external storage.
if (defined('WP_SESSION_ENC_KEY') && WP_SESSION_ENC_KEY) {
$wp_session_handler->addHandler(new \EAMann\Sessionz\Handlers\EncryptionHandler(WP_SESSION_ENC_KEY));
}
// Use an in-memory cache for the instance if we can. This will only help in rare cases.
$wp_session_handler->addHandler(new \EAMann\Sessionz\Handlers\MemoryHandler());
$_SESSION['wp_session_manager'] = 'active';
}
if (! isset($_SESSION['wp_session_manager']) || $_SESSION['wp_session_manager'] !== 'active') {
add_action('admin_notices', 'wp_session_manager_multiple_sessions_notice');
return;
}
// Create the required table.
\EAMann\WPSession\DatabaseHandler::createTable();
register_deactivation_hook(__FILE__, function () {
wp_clear_scheduled_hook('wp_session_database_gc');
});
}
/**
* Print an admin notice if too many plugins are manipulating sessions.
*
* @global array $wp_session_messages
*/
function wp_session_manager_multiple_sessions_notice()
{
global $wp_session_messages;
?>
<div class="notice notice-error">
<p><?php echo esc_html($wp_session_messages['multiple_sessions']); ?></p>
</div>
<?php
}
/**
* Print an admin notice if we're on a bad version of PHP.
*
* @global array $wp_session_messages
*/
function wp_session_manager_deactivated_notice()
{
global $wp_session_messages;
?>
<div class="notice notice-error">
<p><?php echo esc_html($wp_session_messages['bad_php_version']); ?></p>
</div>
<?php
}
/**
* If a session hasn't already been started by some external system, start one!
*/
function wp_session_manager_start_session()
{
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
}
// WordPress won't enforce the minimum version of PHP for us, so we need to check.
if (version_compare(PHP_VERSION, WP_SESSION_MINIMUM_PHP_VERSION, '<')) {
add_action('admin_notices', 'wp_session_manager_deactivated_notice');
} else {
// Start up session management, if we're not in the CLI.
if (session_status() !== PHP_SESSION_DISABLED && (!defined('WP_CLI') || false === WP_CLI)) {
add_action('plugins_loaded', 'wp_session_manager_initialize', 1, 0);
// If we're not in a cron, start the session
if (!defined('DOING_CRON') || false === DOING_CRON) {
add_action('plugins_loaded', 'wp_session_manager_start_session', 10, 0);
}
}
}