diff --git a/Gruntfile.js b/Gruntfile.js index 7a1e8fa771d008e2f8c6c371e6c52da97e1eb1d2..1d96d8e31eaa41a04c45ae6ccd500a7cc11fbd0b 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -9,7 +9,7 @@ module.exports = function( grunt ) { addtextdomain: { options: { - textdomain: 'wp_redis_user_session_storage', + textdomain: 'redis-user-session-storage', }, update_all_domains: { options: { @@ -32,8 +32,8 @@ module.exports = function( grunt ) { options: { domainPath: '/languages', exclude: [ '\.git/*', 'bin/*', 'node_modules/*', 'tests/*' ], - mainFile: 'wp-redis-user-session-storage.php', - potFilename: 'wp-redis-user-session-storage.pot', + mainFile: 'redis-user-session-storage.php', + potFilename: 'redis-user-session-storage.pot', potHeaders: { poedit: true, 'x-poedit-keywordslist': true diff --git a/README.md b/README.md index 464144f5d419314080b55f10fa0ddaa2a692a4b7..1980ae69c3476175f90dc248c85de6de149f016e 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# WP Redis User Session Storage # +# Redis User Session Storage # **Contributors:** [ethitter](https://profiles.wordpress.org/ethitter/) **Donate link:** https://ethitter.com/donate/ **Tags:** user sessions, session tokens, session storage **Requires at least:** 4.0 -**Tested up to:** 5.2 -**Stable tag:** 0.1 -**Requires PHP:** 5.3 +**Tested up to:** 6.0 +**Stable tag:** 0.2 +**Requires PHP:** 5.6 **License:** GPLv2 or later **License URI:** http://www.gnu.org/licenses/gpl-2.0.html @@ -15,21 +15,39 @@ Store WordPress session tokens in Redis rather than the usermeta table. Store WordPress user session tokens in Redis rather than the usermeta table. +This plugin was previously known as `WP Redis User Session Storage` and was renamed to comply with WordPress.org naming constraints. + ## Installation ## -1. Install and configure Redis. There is a good tutorial [here](http://www.saltwebsites.com/2012/install-redis-245-service-centos-6). +1. Install and configure Redis. 2. Install the [Redis PECL module](http://pecl.php.net/package/redis). 3. Activate the plugin network-wide or by placing it in `mu-plugins`. -4. By default, the script will connect to Redis at 127.0.0.1:6379. See the *Connecting to Redis* section for further options. +4. By default, the script will connect to Redis at `127.0.0.1:6379`. See the *Connecting to Redis* section for further options. ## Frequently Asked Questions ## ### Connecting to Redis ### -By default, the plugin uses `127.0.0.1` and `6379` as the default host and port when creating a new client instance; the default database of `0` is also used. Three constants are provided to override these default values. +By default, the plugin uses `127.0.0.1` and `6379` as the default host and port, respectively, when creating a new client instance; the default database of `0` is also used. + +Specify any of the following constants to set the necessary, non-default connection values for your Redis instance: + +* `WP_REDIS_USER_SESSION_HOST` - Hostname or IP of the Redis server, defaults to `127.0.0.1`. +* `WP_REDIS_USER_SESSION_PORT` - Port of the Redis server, defaults to `6379`. +* `WP_REDIS_USER_SESSION_SOCKET` - Path to a Unix socket file for the Redis server, if available. Takes precedence over the port value when set. +* `WP_REDIS_USER_SESSION_AUTH` - Password for the Redis server, if required. +* `WP_REDIS_USER_SESSION_DB` - Database number to use for the Redis server, defaults to `0`. +* `WP_REDIS_USER_SESSION_SERIALIZER` - Serializer to use for the Redis server, defaults to `Redis::SERIALIZER_PHP`. -Specify `WP_REDIS_USER_SESSION_HOST`, `WP_REDIS_USER_SESSION_PORT`, and `WP_REDIS_USER_SESSION_DB` to set the necessary, non-default connection values for your Redis instance. +### How do I upgrade from WP Redis User Session Storage? ### + +Install and activate this plugin, then deactivate the old plugin. Both plugins can safely be activated together as long as no additional classes extend the `WP_Redis_User_Session_Storage` class. After activating this plugin, deactivate the `WP Redis User Session Storage` plugin and remove it. ## Changelog ## +### 0.2 ### +* Rename plugin to `Redis User Session Storage` to submit to WordPress.org plugins repository. +* Allow two versions of this plugin to co-exist safely to support seamless migration. +* Changes plugin class name to `Redis_User_Session_Storage` from `WP_Redis_User_Session_Storage`. + ### 0.1 ### * Initial public release diff --git a/composer.json b/composer.json index 0d2630636eceef236f01866e5379ca6b7b29e2a2..83e178436ae5beb2b51c024d51a7751c580a8316 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name" : "ethitter/wp-redis-user-session-storage", + "name" : "ethitter/redis-user-session-storage", "description" : "Store WordPress session tokens in Redis rather than the usermeta table. Requires the Redis PECL extension.", "homepage" : "https://ethitter.com/", "type" : "wordpress-plugin", diff --git a/inc/class-wp-redis-user-session-storage.php b/inc/class-plugin.php similarity index 92% rename from inc/class-wp-redis-user-session-storage.php rename to inc/class-plugin.php index d7c70a1966bd69731422222f631cac1622b01fac..e6b2e07f009c3e62f3ea83ebe154317e702623a8 100644 --- a/inc/class-wp-redis-user-session-storage.php +++ b/inc/class-plugin.php @@ -2,22 +2,21 @@ /** * Offload session storage to Redis. * - * @package WP_Redis_User_Session_Storage + * @package Redis_User_Session_Storage */ -/** - * Don't load in contexts that lack the WP_Session_Tokens class - */ -if ( ! class_exists( 'WP_Session_Tokens' ) ) { - return; -} +namespace Redis_User_Session_Storage; + +use Redis; +use RedisException; +use WP_Session_Tokens; /** * Redis-based user sessions token manager. * * @since 0.1 */ -class WP_Redis_User_Session_Storage extends WP_Session_Tokens { +class Plugin extends WP_Session_Tokens { /** * Holds the Redis client. * @@ -270,13 +269,3 @@ class WP_Redis_User_Session_Storage extends WP_Session_Tokens { return $this->prefix . ':' . $this->user_id; } } - -/** - * Override Core's default usermeta-based token storage - * - * @return string - */ -function wp_redis_user_session_storage() { - return 'WP_Redis_User_Session_Storage'; -} -add_filter( 'session_token_manager', 'wp_redis_user_session_storage' ); diff --git a/languages/wp-redis-user-session-storage.pot b/languages/redis-user-session-storage.pot similarity index 66% rename from languages/wp-redis-user-session-storage.pot rename to languages/redis-user-session-storage.pot index 73f402997e8b5a4aac1cf389a654fc204d4c3ce5..0eb5f2db532cb812d135870b68889897a138b321 100644 --- a/languages/wp-redis-user-session-storage.pot +++ b/languages/redis-user-session-storage.pot @@ -1,11 +1,11 @@ # Copyright (C) 2022 Erick Hitter -# This file is distributed under the same license as the WP Redis User Session Storage package. +# This file is distributed under the same license as the Redis User Session Storage package. msgid "" msgstr "" -"Project-Id-Version: WP Redis User Session Storage 0.1\n" +"Project-Id-Version: Redis User Session Storage 0.2\n" "Report-Msgid-Bugs-To: " -"https://wordpress.org/support/plugin/wp-redis-user-session-storage\n" -"POT-Creation-Date: 2022-07-01 04:12:36+00:00\n" +"https://wordpress.org/support/plugin/redis-user-session-storage\n" +"POT-Creation-Date: 2022-07-09 03:45:24+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -25,12 +25,19 @@ msgstr "" "X-Textdomain-Support: yes\n" "X-Generator: grunt-wp-i18n 1.0.3\n" +#: redis-user-session-storage.php:90 +#. translators: 1: New plugin name, 2: Old plugin name +msgid "" +"%1$s: An outdated version of this plugin, %2$s, is active. Please " +"deactivate it to use the current version." +msgstr "" + #. Plugin Name of the plugin/theme -msgid "WP Redis User Session Storage" +msgid "Redis User Session Storage" msgstr "" #. Plugin URI of the plugin/theme -msgid "https://ethitter.com/plugins/wp-redis-user-session-storage/" +msgid "https://ethitter.com/plugins/redis-user-session-storage/" msgstr "" #. Description of the plugin/theme diff --git a/package.json b/package.json index bac1b9e7fb18c8799e85e846730a56985b93d3a3..e4845f358ae1098a0f25a60a600cda856f0099d6 100755 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "wp-redis-user-session-storage", + "name": "redis-user-session-storage", "version": "0.1.0", "main": "Gruntfile.js", "author": "Erick Hitter", diff --git a/phpcs.xml b/phpcs.xml index 9a198a11bc5c5d73dac420379452204dc63d2582..e41bee18e8bcd39874a217fd866ec735c07980f2 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<ruleset name="WP Redis User Session Storage"> +<ruleset name="Redis User Session Storage"> <description>Generally-applicable sniffs for WordPress plugins.</description> <!-- What to scan --> @@ -18,7 +18,7 @@ <!-- Rules: Check PHP version compatibility --> <!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions --> - <config name="testVersion" value="5.3-"/> + <config name="testVersion" value="5.6-"/> <!-- https://github.com/PHPCompatibility/PHPCompatibilityWP --> <rule ref="PHPCompatibilityWP"/> @@ -32,13 +32,13 @@ <rule ref="WordPress.NamingConventions.PrefixAllGlobals"> <properties> <!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. --> - <property name="prefixes" type="array" value="wp_redis_user_session_storage"/> + <property name="prefixes" type="array" value="redis_user_session_storage,wp_redis_user_session_storage"/> </properties> </rule> <rule ref="WordPress.WP.I18n"> <properties> <!-- Value: replace the text domain used. --> - <property name="text_domain" type="array" value="wp_redis_user_session_storage"/> + <property name="text_domain" type="array" value="redis-user-session-storage"/> </properties> </rule> <rule ref="WordPress.WhiteSpace.ControlStructureSpacing"> diff --git a/readme.txt b/readme.txt index bf9e5f39d6fc3c9962346891a9d1f09366171881..3ec3b397dcfc1475ae433f23544b0b8f1389b8a1 100644 --- a/readme.txt +++ b/readme.txt @@ -1,11 +1,11 @@ -=== WP Redis User Session Storage === +=== Redis User Session Storage === Contributors: ethitter Donate link: https://ethitter.com/donate/ Tags: user sessions, session tokens, session storage Requires at least: 4.0 -Tested up to: 5.2 -Stable tag: 0.1 -Requires PHP: 5.3 +Tested up to: 6.0 +Stable tag: 0.2 +Requires PHP: 5.6 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -15,21 +15,39 @@ Store WordPress session tokens in Redis rather than the usermeta table. Store WordPress user session tokens in Redis rather than the usermeta table. +This plugin was previously known as `WP Redis User Session Storage` and was renamed to comply with WordPress.org naming constraints. + == Installation == -1. Install and configure Redis. There is a good tutorial [here](http://www.saltwebsites.com/2012/install-redis-245-service-centos-6). +1. Install and configure Redis. 2. Install the [Redis PECL module](http://pecl.php.net/package/redis). 3. Activate the plugin network-wide or by placing it in `mu-plugins`. -4. By default, the script will connect to Redis at 127.0.0.1:6379. See the *Connecting to Redis* section for further options. +4. By default, the script will connect to Redis at `127.0.0.1:6379`. See the *Connecting to Redis* section for further options. == Frequently Asked Questions == = Connecting to Redis = -By default, the plugin uses `127.0.0.1` and `6379` as the default host and port when creating a new client instance; the default database of `0` is also used. Three constants are provided to override these default values. +By default, the plugin uses `127.0.0.1` and `6379` as the default host and port, respectively, when creating a new client instance; the default database of `0` is also used. + +Specify any of the following constants to set the necessary, non-default connection values for your Redis instance: + +* `WP_REDIS_USER_SESSION_HOST` - Hostname or IP of the Redis server, defaults to `127.0.0.1`. +* `WP_REDIS_USER_SESSION_PORT` - Port of the Redis server, defaults to `6379`. +* `WP_REDIS_USER_SESSION_SOCKET` - Path to a Unix socket file for the Redis server, if available. Takes precedence over the port value when set. +* `WP_REDIS_USER_SESSION_AUTH` - Password for the Redis server, if required. +* `WP_REDIS_USER_SESSION_DB` - Database number to use for the Redis server, defaults to `0`. +* `WP_REDIS_USER_SESSION_SERIALIZER` - Serializer to use for the Redis server, defaults to `Redis::SERIALIZER_PHP`. -Specify `WP_REDIS_USER_SESSION_HOST`, `WP_REDIS_USER_SESSION_PORT`, and `WP_REDIS_USER_SESSION_DB` to set the necessary, non-default connection values for your Redis instance. += How do I upgrade from WP Redis User Session Storage? = + +Install and activate this plugin, then deactivate the old plugin. Both plugins can safely be activated together as long as no additional classes extend the `WP_Redis_User_Session_Storage` class. After activating this plugin, deactivate the `WP Redis User Session Storage` plugin and remove it. == Changelog == += 0.2 = +* Rename plugin to `Redis User Session Storage` to submit to WordPress.org plugins repository. +* Allow two versions of this plugin to co-exist safely to support seamless migration. +* Changes plugin class name to `Redis_User_Session_Storage` from `WP_Redis_User_Session_Storage`. + = 0.1 = * Initial public release diff --git a/redis-user-session-storage.php b/redis-user-session-storage.php new file mode 100644 index 0000000000000000000000000000000000000000..7aaccbed04af8caa173c17ef0ead94a4a0ce0f44 --- /dev/null +++ b/redis-user-session-storage.php @@ -0,0 +1,98 @@ +<?php +/** + * Plugin Name: Redis User Session Storage + * Plugin URI: https://ethitter.com/plugins/redis-user-session-storage/ + * Description: Store WordPress session tokens in Redis rather than the usermeta table. Requires the Redis PECL extension. + * Version: 0.2 + * Author: Erick Hitter + * Author URI: https://ethitter.com/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * @package Redis_User_Session_Storage + */ + +namespace Redis_User_Session_Storage; + +use Redis; +use WP_Redis_User_Session_Storage; +use WP_Session_Tokens; + +/** + * Load plugin when safe to do so, accounting for previous plugin name. + * + * WordPress.org no longer accepts plugins beginning with the `WP` prefix, so + * this was renamed to comply. + * + * @return void + */ +function load() { + if ( + ! class_exists( Redis::class, false ) + || ! class_exists( WP_Session_Tokens::class, false ) + ) { + return; + } + + require_once __DIR__ . '/inc/class-plugin.php'; + + // Hooked at 9 in case old plugin is also active. + add_filter( + 'session_token_manager', + __NAMESPACE__ . '\set_session_token_manager', + 9 + ); +} +load(); + +/** + * Override Core's default usermeta-based token storage. + * + * @param string $manager Name of session-manager class. + * @return string + */ +function set_session_token_manager( $manager ) { + if ( class_exists( WP_Redis_User_Session_Storage::class, false ) ) { + add_action( 'admin_notices', __NAMESPACE__ . '\admin_notice' ); + } + + return Plugin::class; +} + +/** + * Show admin notice to certain users when older version is active. + * + * @return void + */ +function admin_notice() { + if ( ! current_user_can( 'activate_plugins' ) ) { + return; + } + + ?> + <div id="message" class="error"> + <p> + <?php + printf( + /* translators: 1: New plugin name, 2: Old plugin name */ + esc_html__( '%1$s: An outdated version of this plugin, %2$s, is active. Please deactivate it to use the current version.', 'redis-user-session-storage' ), + '<strong>Redis User Session Storage</strong>', + '<em>WP Redis User Session Storage</em>' + ); + ?> + </p> + </div> + <?php +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e32bcc744ed5f44da6dc87b495c196a48f87a56b..f838a17324fb9d11ace8eb00e2902b3cd4b8acfd 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,39 +5,27 @@ * @package WP_Revisions_Control */ -$wp_redis_user_session_storage = getenv( 'WP_TESTS_DIR' ); +$redis_user_session_storage = getenv( 'WP_TESTS_DIR' ); -if ( ! $wp_redis_user_session_storage ) { - $wp_redis_user_session_storage = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; +if ( ! $redis_user_session_storage ) { + $redis_user_session_storage = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; } -if ( ! file_exists( $wp_redis_user_session_storage . '/includes/functions.php' ) ) { - echo "Could not find $wp_redis_user_session_storage/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // WPCS: XSS ok. +if ( ! file_exists( $redis_user_session_storage . '/includes/functions.php' ) ) { + echo "Could not find $redis_user_session_storage/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // WPCS: XSS ok. exit( 1 ); } // Give access to tests_add_filter() function. -require_once $wp_redis_user_session_storage . '/includes/functions.php'; - -/** - * Stub admin-only function not needed for testing. - */ -// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -if ( ! function_exists( 'post_revisions_meta_box' ) ) { - /** - * Stub for Core's revisions meta box. - */ - function post_revisions_meta_box() {} -} -// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound +require_once $redis_user_session_storage . '/includes/functions.php'; /** * Manually load the plugin being tested. */ -function wp_redis_user_session_storage_tests_manually_load_plugin() { - require dirname( dirname( __FILE__ ) ) . '/wp-redis-user-session-storage.php'; +function redis_user_session_storage_tests_manually_load_plugin() { + require dirname( dirname( __FILE__ ) ) . '/redis-user-session-storage.php'; } -tests_add_filter( 'muplugins_loaded', 'wp_redis_user_session_storage_tests_manually_load_plugin' ); +tests_add_filter( 'muplugins_loaded', 'redis_user_session_storage_tests_manually_load_plugin' ); // Start up the WP testing environment. -require $wp_redis_user_session_storage . '/includes/bootstrap.php'; +require $redis_user_session_storage . '/includes/bootstrap.php'; diff --git a/tests/test-sample.php b/tests/test-sample.php index ccdf95bc181e52eca521bf967e65133f03095a4b..c8f05ed5bd8bfe159a2d1b5efe3a96e2dbf30198 100755 --- a/tests/test-sample.php +++ b/tests/test-sample.php @@ -2,7 +2,7 @@ /** * Class SampleTest * - * @package WP_Redis_User_Session_Storage + * @package Redis_User_Session_Storage */ /** diff --git a/wp-redis-user-session-storage.php b/wp-redis-user-session-storage.php deleted file mode 100644 index 789c3300dcab5b8a806fa42467e74b624d7a6a98..0000000000000000000000000000000000000000 --- a/wp-redis-user-session-storage.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php -/** - * Load plugin. - * - * @package WP_Redis_User_Session_Storage - */ - -/** - * Plugin Name: WP Redis User Session Storage - * Plugin URI: https://ethitter.com/plugins/wp-redis-user-session-storage/ - * Description: Store WordPress session tokens in Redis rather than the usermeta table. Requires the Redis PECL extension. - * Version: 0.1 - * Author: Erick Hitter - * Author URI: https://ethitter.com/ - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -require_once __DIR__ . '/inc/class-wp-redis-user-session-storage.php';