Skip to content

Commit e123ced

Browse files
committed
fix: Fixes incorrect order of array_replace_recursive arguments & other issues
* Fixes pantheon-systems#433 * Fixes $432 * Fixes pantheon-systems#431 * Further clean-up & standardization between object-cache.php & wp-redis.php. * Fixes incorrect order of array_replace_recursive arguments. * Addresses issue with port still not being null for socket connections due to defaults array_repalce_recursive use.
1 parent bf8e124 commit e123ced

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

object-cache.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,10 +1247,11 @@ public function build_client_parameters( $redis_server ) {
12471247
// Attempt to automatically load Pantheon's Redis config from the env.
12481248
if ( isset( $_SERVER['CACHE_HOST'] ) ) {
12491249
$redis_server = [
1250-
'host' => wp_strip_all_tags( $_SERVER['CACHE_HOST'] ),
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,
1250+
'host' => strip_tags( $_SERVER['CACHE_HOST'] ),
1251+
'port' => ! empty( $_SERVER['CACHE_PORT'] ) ? strip_tags( $_SERVER['CACHE_PORT'] ) : $port,
1252+
// Don't attempt to sanitize passwords as this can break authentication.
1253+
'auth' => ! empty( $_SERVER['CACHE_PASSWORD'] ) ? $_SERVER['CACHE_PASSWORD'] : null,
1254+
'database' => ! empty( $_SERVER['CACHE_DB'] ) ? strip_tags( $_SERVER['CACHE_DB'] ) : $database,
12541255
];
12551256
} else {
12561257
$redis_server = [
@@ -1263,6 +1264,7 @@ public function build_client_parameters( $redis_server ) {
12631264

12641265
if ( file_exists( $redis_server['host'] ) && 'socket' === filetype( $redis_server['host'] ) ) { // unix socket connection.
12651266
// port must be null or socket won't connect.
1267+
unset( $redis_server['port'] );
12661268
$port = null;
12671269
}
12681270

@@ -1275,7 +1277,7 @@ public function build_client_parameters( $redis_server ) {
12751277
// 1s timeout, 100ms delay between reconnections.
12761278

12771279
// merging the defaults with the original $redis_server enables any custom parameters to get sent downstream to the redis client.
1278-
return array_replace_recursive( $redis_server, $defaults );
1280+
return array_replace_recursive( $defaults, $redis_server );
12791281
}
12801282

12811283
/**

wp-redis.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@
3535
*/
3636
function wp_redis_get_info() {
3737
global $wp_object_cache, $redis_server;
38+
// Default Redis port.
39+
$port = 6379;
40+
// Default Redis database number.
41+
$database = 0;
3842

3943
if ( empty( $redis_server ) ) {
4044
// Attempt to automatically load Pantheon's Redis config from the env.
4145
if ( isset( $_SERVER['CACHE_HOST'] ) ) {
4246
$redis_server = [
43-
'host' => sanitize_text_field( $_SERVER['CACHE_HOST'] ),
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,
47+
'host' => strip_tags( $_SERVER['CACHE_HOST'] ),
48+
'port' => ! empty( $_SERVER['CACHE_PORT'] ) ? strip_tags( $_SERVER['CACHE_PORT'] ) : $port,
49+
// Don't attempt to sanitize passwords as this can break authentication.
50+
'auth' => ! empty( $_SERVER['CACHE_PASSWORD'] ) ? $_SERVER['CACHE_PASSWORD'] : null,
51+
'database' => ! empty( $_SERVER['CACHE_DB'] ) ? strip_tags( $_SERVER['CACHE_DB'] ) : $database,
4752
];
4853
} else {
4954
$redis_server = [
5055
'host' => '127.0.0.1',
51-
'port' => 6379,
52-
'database' => 0,
56+
'port' => $port,
57+
'database' => $database,
5358
];
5459
}
5560
}
@@ -73,7 +78,7 @@ function wp_redis_get_info() {
7378
} else {
7479
$uptime_in_days .= ' days';
7580
}
76-
$database = ! empty( $redis_server['database'] ) ? $redis_server['database'] : 0;
81+
$database = ! empty( $redis_server['database'] ) ? $redis_server['database'] : $database;
7782
$key_count = 0;
7883
if ( isset( $info[ 'db' . $database ] ) && preg_match( '#keys=([\d]+)#', $info[ 'db' . $database ], $matches ) ) {
7984
$key_count = $matches[1];

0 commit comments

Comments
 (0)