Skip to content
Snippets Groups Projects
Commit 585d31c4 authored by Erick Hitter's avatar Erick Hitter
Browse files

Improve use of global space by declaring a local variable global, rather than...

Improve use of global space by declaring a local variable global, rather than always using the Superglobal.

It's easier to read, especially with the use of the `$_SERVER` superglobal.
parent a9ab06c7
Branches
No related tags found
No related merge requests found
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
/** /**
* GLOBAL CONFIGURATION * GLOBAL CONFIGURATION
*/ */
$GLOBALS['wp_redis_cache_config'] = array( global $wp_redis_cache_config;
$wp_redis_cache_config = array(
'debug' => false, 'debug' => false,
'debug_messages' => '', 'debug_messages' => '',
'cache' => false, 'cache' => false,
...@@ -18,8 +20,8 @@ $GLOBALS['wp_redis_cache_config'] = array( ...@@ -18,8 +20,8 @@ $GLOBALS['wp_redis_cache_config'] = array(
); );
// Uncomment either option below to fix the values here and disable the admin UI // Uncomment either option below to fix the values here and disable the admin UI
// $GLOBALS['wp_redis_cache_config']['cache_duration'] = 43200; // $wp_redis_cache_config['cache_duration'] = 43200;
// $GLOBALS['wp_redis_cache_config']['unlimited'] = false; // $wp_redis_cache_config['unlimited'] = false;
// Modify this function to introduce custom handling when exceptions occur // Modify this function to introduce custom handling when exceptions occur
function wp_redis_cache_exception_handler( $exception ) { function wp_redis_cache_exception_handler( $exception ) {
...@@ -31,11 +33,11 @@ function wp_redis_cache_exception_handler( $exception ) { ...@@ -31,11 +33,11 @@ function wp_redis_cache_exception_handler( $exception ) {
* *
* DO NOT EDIT BELOW THIS LINE! * DO NOT EDIT BELOW THIS LINE!
*/ */
$GLOBALS['wp_redis_cache_config']['current_url'] = wp_redis_cache_get_clean_url( $GLOBALS['wp_redis_cache_config']['secret_string'] ); $wp_redis_cache_config['current_url'] = wp_redis_cache_get_clean_url( $wp_redis_cache_config['secret_string'] );
$GLOBALS['wp_redis_cache_config']['redis_key'] = md5( $GLOBALS['wp_redis_cache_config']['current_url'] ); $wp_redis_cache_config['redis_key'] = md5( $wp_redis_cache_config['current_url'] );
// Start the timer so we can track the page load time // Start the timer so we can track the page load time
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$start = microtime(); $start = microtime();
} }
...@@ -43,7 +45,7 @@ if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { ...@@ -43,7 +45,7 @@ if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
* MOBILE HANDLING * MOBILE HANDLING
*/ */
if ( wp_redis_cache_is_mobile_request() ) { if ( wp_redis_cache_is_mobile_request() ) {
$GLOBALS['wp_redis_cache_config']['redis_key'] = 'MO-' . $GLOBALS['wp_redis_cache_config']['redis_key']; $wp_redis_cache_config['redis_key'] = 'MO-' . $wp_redis_cache_config['redis_key'];
} }
/** /**
...@@ -131,27 +133,29 @@ function wp_redis_cache_is_mobile_request() { ...@@ -131,27 +133,29 @@ function wp_redis_cache_is_mobile_request() {
* @return object * @return object
*/ */
function wp_redis_cache_connect_redis() { function wp_redis_cache_connect_redis() {
global $wp_redis_cache_config;
// check if PECL Extension is available // check if PECL Extension is available
if ( class_exists( 'Redis' ) ) { if ( class_exists( 'Redis' ) ) {
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- Redis PECL module found -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- Redis PECL module found -->\n";
} }
$redis = new Redis(); $redis = new Redis();
// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection // Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
$redis->connect( $GLOBALS['wp_redis_cache_config']['redis_server'], $GLOBALS['wp_redis_cache_config']['redis_port'] ); $redis->connect( $wp_redis_cache_config['redis_server'], $wp_redis_cache_config['redis_port'] );
$redis->select( $GLOBALS['wp_redis_cache_config']['redis_db'] ); $redis->select( $wp_redis_cache_config['redis_db'] );
} else { // Fallback to predis5.2.php } else { // Fallback to predis5.2.php
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- using predis as a backup -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- using predis as a backup -->\n";
} }
include_once dirname( __FILE__ ) . '/wp-content/plugins/wp-redis-cache/predis5.2.php'; //we need this to use Redis inside of PHP include_once dirname( __FILE__ ) . '/wp-content/plugins/wp-redis-cache/predis5.2.php'; //we need this to use Redis inside of PHP
$redis = new Predis_Client( array( $redis = new Predis_Client( array(
'host' => $GLOBALS['wp_redis_cache_config']['redis_server'], 'host' => $wp_redis_cache_config['redis_server'],
'port' => $GLOBALS['wp_redis_cache_config']['redis_port'], 'port' => $wp_redis_cache_config['redis_port'],
'database' => $GLOBALS['wp_redis_cache_config']['redis_db'], 'database' => $wp_redis_cache_config['redis_db'],
) ); ) );
} }
...@@ -181,34 +185,34 @@ try { ...@@ -181,34 +185,34 @@ try {
$is_post = (bool) 'POST' === $_SERVER['REQUEST_METHOD']; $is_post = (bool) 'POST' === $_SERVER['REQUEST_METHOD'];
$logged_in = (bool) preg_match( "#(wordpress_(logged|sec)|comment_author)#", var_export( $_COOKIE, true ) ); $logged_in = (bool) preg_match( "#(wordpress_(logged|sec)|comment_author)#", var_export( $_COOKIE, true ) );
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- POST request: . " . ( $is_post ? 'yes' : 'no' ) . "-->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- POST request: . " . ( $is_post ? 'yes' : 'no' ) . "-->\n";
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- Logged in: . " . ( $logged_in ? 'yes' : 'no' ) . "-->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- Logged in: . " . ( $logged_in ? 'yes' : 'no' ) . "-->\n";
} }
// Refresh request, deletes cache: either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment // Refresh request, deletes cache: either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
if ( wp_redis_cache_refresh_has_secret( $GLOBALS['wp_redis_cache_config']['secret_string'] ) || wp_redis_cache_request_has_secret( $GLOBALS['wp_redis_cache_config']['secret_string'] ) || wp_redis_cache_is_remote_page_load( $GLOBALS['wp_redis_cache_config']['current_url'], $GLOBALS['wp_redis_cache_config']['server_ip'] ) ) { if ( wp_redis_cache_refresh_has_secret( $wp_redis_cache_config['secret_string'] ) || wp_redis_cache_request_has_secret( $wp_redis_cache_config['secret_string'] ) || wp_redis_cache_is_remote_page_load( $wp_redis_cache_config['current_url'], $wp_redis_cache_config['server_ip'] ) ) {
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- manual refresh was required -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- manual refresh was required -->\n";
} }
$redis->del( $GLOBALS['wp_redis_cache_config']['redis_key'] ); $redis->del( $wp_redis_cache_config['redis_key'] );
// This page is cached, the user isn't logged in, and it isn't a POST request, so let's use the cache // This page is cached, the user isn't logged in, and it isn't a POST request, so let's use the cache
} elseif ( ! $is_post && ! $logged_in && $redis->exists( $GLOBALS['wp_redis_cache_config']['redis_key'] ) ) { } elseif ( ! $is_post && ! $logged_in && $redis->exists( $wp_redis_cache_config['redis_key'] ) ) {
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- serving page from cache: key: " . $GLOBALS['wp_redis_cache_config']['redis_key'] . " -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- serving page from cache: key: " . $wp_redis_cache_config['redis_key'] . " -->\n";
} }
// Page is served from cache, so we don't need WP // Page is served from cache, so we don't need WP
$load_wp = false; $load_wp = false;
$GLOBALS['wp_redis_cache_config']['cached'] = true; $wp_redis_cache_config['cached'] = true;
echo trim( $redis->get( $GLOBALS['wp_redis_cache_config']['redis_key'] ) ); echo trim( $redis->get( $wp_redis_cache_config['redis_key'] ) );
// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
} elseif ( $_SERVER['REMOTE_ADDR'] != $GLOBALS['wp_redis_cache_config']['server_ip'] ) { } elseif ( $_SERVER['REMOTE_ADDR'] != $wp_redis_cache_config['server_ip'] ) {
if ( false === strstr( $GLOBALS['wp_redis_cache_config']['current_url'], 'preview=true' ) ) { if ( false === strstr( $wp_redis_cache_config['current_url'], 'preview=true' ) ) {
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- displaying page without cache -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- displaying page without cache -->\n";
} }
// If user isn't logged in and this isn't a post request, render the requested page and cache if appropriate. // If user isn't logged in and this isn't a post request, render the requested page and cache if appropriate.
...@@ -225,29 +229,29 @@ try { ...@@ -225,29 +229,29 @@ try {
// Cache rendered page if appropriate // Cache rendered page if appropriate
if ( ! is_404() && ! is_search() ) { if ( ! is_404() && ! is_search() ) {
// Is unlimited cache life requested? // Is unlimited cache life requested?
if ( isset( $GLOBALS['wp_redis_cache_config']['unlimited'] ) ) { if ( isset( $wp_redis_cache_config['unlimited'] ) ) {
$unlimited = $GLOBALS['wp_redis_cache_config']['unlimited']; $unlimited = $wp_redis_cache_config['unlimited'];
} else { } else {
$unlimited = (bool) get_option( 'wp-redis-cache-debug', false ); $unlimited = (bool) get_option( 'wp-redis-cache-debug', false );
$GLOBALS['wp_redis_cache_config']['unlimited'] = $unlimited; $wp_redis_cache_config['unlimited'] = $unlimited;
} }
// Cache the page for the chosen duration // Cache the page for the chosen duration
if ( $unlimited ) { if ( $unlimited ) {
$redis->set( $GLOBALS['wp_redis_cache_config']['redis_key'], $markup_to_cache ); $redis->set( $wp_redis_cache_config['redis_key'], $markup_to_cache );
} else { } else {
if ( isset( $GLOBALS['wp_redis_cache_config']['cache_duration'] ) ) { if ( isset( $wp_redis_cache_config['cache_duration'] ) ) {
$cache_duration = $GLOBALS['wp_redis_cache_config']['cache_duration']; $cache_duration = $wp_redis_cache_config['cache_duration'];
} else { } else {
$cache_duration = (int) get_option( 'wp-redis-cache-seconds', 43200 ); $cache_duration = (int) get_option( 'wp-redis-cache-seconds', 43200 );
$GLOBALS['wp_redis_cache_config']['cache_duration'] = $cache_duration; $wp_redis_cache_config['cache_duration'] = $cache_duration;
} }
if ( ! is_numeric( $cache_duration ) ) { if ( ! is_numeric( $cache_duration ) ) {
$cache_duration = $GLOBALS['wp_redis_cache_config']['cache_duration'] = 43200; $cache_duration = $wp_redis_cache_config['cache_duration'] = 43200;
} }
$redis->setex( $GLOBALS['wp_redis_cache_config']['redis_key'], $cache_duration, $markup_to_cache ); $redis->setex( $wp_redis_cache_config['redis_key'], $cache_duration, $markup_to_cache );
} }
} }
} }
...@@ -266,19 +270,19 @@ try { ...@@ -266,19 +270,19 @@ try {
/** /**
* DEBUGGING OUTPUT * DEBUGGING OUTPUT
*/ */
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) { if ( $wp_redis_cache_config['debug'] ) {
$end = microtime(); $end = microtime();
$time = @wp_redis_cache_get_micro_time( $end ) - @wp_redis_cache_get_micro_time( $start ); $time = @wp_redis_cache_get_micro_time( $end ) - @wp_redis_cache_get_micro_time( $start );
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- Site was cached = " . $GLOBALS['wp_redis_cache_config']['cached'] . " -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- Site was cached = " . $wp_redis_cache_config['cached'] . " -->\n";
if ( isset( $GLOBALS['wp_redis_cache_config']['cache_duration'] ) ) { if ( isset( $wp_redis_cache_config['cache_duration'] ) ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- wp-redis-cache-seconds = " . $GLOBALS['wp_redis_cache_config']['cache_duration'] . " -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-seconds = " . $wp_redis_cache_config['cache_duration'] . " -->\n";
} }
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- wp-redis-cache-ip = " . $GLOBALS['wp_redis_cache_config']['server_ip'] . "-->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-ip = " . $wp_redis_cache_config['server_ip'] . "-->\n";
if ( isset( $GLOBALS['wp_redis_cache_config']['unlimited'] ) ) { if ( isset( $wp_redis_cache_config['unlimited'] ) ) {
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- wp-redis-cache-unlimited = " . $GLOBALS['wp_redis_cache_config']['unlimited'] . "-->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-unlimited = " . $wp_redis_cache_config['unlimited'] . "-->\n";
} }
$GLOBALS['wp_redis_cache_config']['debug_messages'] .= "<!-- wp-redis-cache-debug = " . $GLOBALS['wp_redis_cache_config']['debug'] . "-->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-debug = " . $wp_redis_cache_config['debug'] . "-->\n";
echo $GLOBALS['wp_redis_cache_config']['debug_messages']; echo $wp_redis_cache_config['debug_messages'];
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment