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