Skip to content

Commit cf4ba56

Browse files
committed
fix: Fixes assumption that CACHE_PORT & CACHE_PASSWORD are Set.
* Fixes #359 * Falls back on port 6379 if the CACHE_PORT is not configured. * Doesn't require a CACHE_PASSWORD to be set when it isn't used, or can't be. * Improves code quality by reducing Redis default port & databasei duplicate values to a share variables. * Fixes invalid code changes made in PR [#400 ](#400) to the core plugin connectivity testing that prevent connectivty checks if the port/password/database aren't explicitly defined.
1 parent 402461b commit cf4ba56

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

object-cache.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,29 +1238,32 @@ public function check_client_dependencies() {
12381238
* with defaults applied.
12391239
*/
12401240
public function build_client_parameters( $redis_server ) {
1241+
// Default Redis port.
1242+
$port = 6379;
1243+
// Default Redis database number.
1244+
$database = 0;
1245+
12411246
if ( empty( $redis_server ) ) {
12421247
// Attempt to automatically load Pantheon's Redis config from the env.
12431248
if ( isset( $_SERVER['CACHE_HOST'] ) ) {
12441249
$redis_server = [
12451250
'host' => wp_strip_all_tags( $_SERVER['CACHE_HOST'] ),
1246-
'port' => isset( $_SERVER['CACHE_PORT'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PORT'] ) : 0,
1247-
'auth' => isset( $_SERVER['CACHE_PASSWORD'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PASSWORD'] ) : '',
1248-
'database' => isset( $_SERVER['CACHE_DB'] ) ? wp_strip_all_tags( $_SERVER['CACHE_DB'] ) : 0,
1251+
'port' => isset( $_SERVER['CACHE_PORT'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PORT'] ) : $port,
1252+
'auth' => isset( $_SERVER['CACHE_PASSWORD'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PASSWORD'] ) : null,
1253+
'database' => isset( $_SERVER['CACHE_DB'] ) ? wp_strip_all_tags( $_SERVER['CACHE_DB'] ) : $database,
12491254
];
12501255
} else {
12511256
$redis_server = [
12521257
'host' => '127.0.0.1',
1253-
'port' => 6379,
1254-
'database' => 0,
1258+
'port' => $port,
1259+
'database' => $database,
12551260
];
12561261
}
12571262
}
12581263

12591264
if ( file_exists( $redis_server['host'] ) && 'socket' === filetype( $redis_server['host'] ) ) { // unix socket connection.
12601265
// port must be null or socket won't connect.
12611266
$port = null;
1262-
} else { // tcp connection.
1263-
$port = ! empty( $redis_server['port'] ) ? $redis_server['port'] : 6379;
12641267
}
12651268

12661269
$defaults = [
@@ -1470,9 +1473,9 @@ protected function _exception_handler( $exception ) {
14701473
try {
14711474
$this->last_triggered_error = 'WP Redis: ' . $exception->getMessage();
14721475
// Be friendly to developers debugging production servers by triggering an error.
1473-
1476+
14741477
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error,WordPress.Security.EscapeOutput.OutputNotEscaped
1475-
trigger_error( $this->last_triggered_error, E_USER_WARNING );
1478+
trigger_error( $this->last_triggered_error, E_USER_WARNING );
14761479
} catch ( PHPUnit_Framework_Error_Warning $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
14771480
// PHPUnit throws an Exception when `trigger_error()` is called. To ensure our tests (which expect Exceptions to be caught) continue to run, we catch the PHPUnit exception and inspect the RedisException message.
14781481
}

wp-redis.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ function wp_redis_get_info() {
3838

3939
if ( empty( $redis_server ) ) {
4040
// Attempt to automatically load Pantheon's Redis config from the env.
41-
if ( isset( $_SERVER['CACHE_HOST'] ) && isset( $_SERVER['CACHE_PORT'] ) && isset( $_SERVER['CACHE_PASSWORD'] ) && isset( $_SERVER['CACHE_DB'] ) ) {
41+
if ( isset( $_SERVER['CACHE_HOST'] ) ) {
4242
$redis_server = [
4343
'host' => sanitize_text_field( $_SERVER['CACHE_HOST'] ),
44-
'port' => sanitize_text_field( $_SERVER['CACHE_PORT'] ),
45-
'auth' => sanitize_text_field( $_SERVER['CACHE_PASSWORD'] ),
46-
'database' => sanitize_text_field( $_SERVER['CACHE_DB'] ),
44+
'port' => isset( $_SERVER['CACHE_PORT'] ) ? sanitize_text_field( $_SERVER['CACHE_PORT'] ) : 6379,
45+
'auth' => isset( $_SERVER['CACHE_PASSWORD'] ) ? sanitize_text_field( $_SERVER['CACHE_PASSWORD'] ) : null,
46+
'database' => isset( $_SERVER['CACHE_DB'] ) ? sanitize_text_field( $_SERVER['CACHE_DB'] ) : 0,
4747
];
4848
} else {
4949
$redis_server = [

0 commit comments

Comments
 (0)