Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a config loader and replace global settings. #728

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions includes/pre-wp-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

function wpsc_load_config() {
global $wpsc_config, $wp_cache_config_file_sample;

if ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) ) {
include WP_CONTENT_DIR . '/wp-cache-config.php';
} else {
include $wp_cache_config_file_sample;
}

$config_variables = array( 'wpsc_version', 'dismiss_gc_warning', 'wpsc_fix_164', 'wp_cache_debug_username', 'cache_domain_mapping', 'wp_cache_mobile_groups', 'cache_page_secret', 'wpsc_cookies', 'wp_cache_home_path', 'wp_cache_slash_check', 'cache_time_interval', 'cache_compression', 'cache_enabled', 'super_cache_enabled', 'cache_max_time', 'use_flock', 'cache_path', 'file_prefix', 'ossdlcdn', 'cache_acceptable_files', 'cache_rejected_uri', 'cache_rejected_user_agent', 'cache_rebuild_files', 'wp_cache_mutex_disabled', 'sem_id', 'wp_cache_mobile', 'wp_cache_mobile_whitelist', 'wp_cache_mobile_browsers', 'wp_cache_plugins_dir', 'wp_cache_shutdown_gc', 'wp_super_cache_late_init', 'wp_super_cache_advanced_debug', 'wp_super_cache_front_page_text', 'wp_super_cache_front_page_clear', 'wp_super_cache_front_page_check', 'wp_super_cache_front_page_notification', 'wp_cache_object_cache', 'wp_cache_anon_only', 'wp_supercache_cache_list', 'wp_cache_debug_to_file', 'wp_super_cache_debug', 'wp_cache_debug_level', 'wp_cache_debug_ip', 'wp_cache_debug_log', 'wp_cache_debug_email', 'wp_cache_pages', 'wp_cache_hide_donation', 'wp_cache_not_logged_in', 'wp_cache_clear_on_post_edit', 'wp_cache_hello_world', 'wp_cache_mobile_enabled', 'wp_cache_cron_check', 'wp_cache_mfunc_enabled', 'wp_cache_make_known_anon', 'wp_cache_refresh_single_only', 'wp_cache_mod_rewrite', 'wp_supercache_304', 'wp_cache_front_page_checks', 'wp_cache_disable_utf8', 'wp_cache_no_cache_for_get', 'cache_scheduled_time', 'wp_cache_preload_interval', 'cache_schedule_type', 'wp_cache_preload_posts', 'wp_cache_preload_on', 'wp_cache_preload_taxonomies', 'wp_cache_preload_email_me', 'wp_cache_preload_email_volume', 'wp_cache_mobile_prefixes', 'cached_direct_pages', 'wpsc_served_header', 'cache_gc_email_me', 'wpsc_save_headers', 'cache_schedule_interval', 'wp_super_cache_comments' );
foreach( $config_variables as $config ) {
$wpsc_config[ $config ] = $$config;
}
}
14 changes: 4 additions & 10 deletions plugins/domain-mapping.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

function domain_mapping_gc_cache( $function, $directory ) {
global $cache_path;

if ( ! function_exists( 'domain_mapping_warning' ) ) {
return;
}
Expand All @@ -20,17 +18,15 @@ function domain_mapping_gc_cache( $function, $directory ) {

switch ( $function ) {
case 'rebuild':
wpsc_rebuild_files( $cache_path . 'supercache/' . $sitedir . $directory );
wpsc_rebuild_files( $GLOBALS['wpsc_config']['cache_path'] . 'supercache/' . $sitedir . $directory );
break;
case 'prune':
wpsc_delete_files( $cache_path . 'supercache/' . $sitedir . $directory );
wpsc_delete_files( $GLOBALS['wpsc_config']['cache_path'] . 'supercache/' . $sitedir . $directory );
break;
}
}

function domain_mapping_supercachedir( $dir ) {
global $cache_path;

if ( ! function_exists( 'domain_mapping_warning' ) ) {
return $dir;
}
Expand All @@ -42,7 +38,7 @@ function domain_mapping_supercachedir( $dir ) {

$sitedir = trailingslashit( preg_replace( '`^(https?:)?//`', '', $siteurl ) );

return trailingslashit( $cache_path . 'supercache/' . $sitedir );
return trailingslashit( $GLOBALS['wpsc_config']['cache_path'] . 'supercache/' . $sitedir );
}

function domain_mapping_actions() {
Expand Down Expand Up @@ -102,9 +98,7 @@ function wp_supercache_domain_mapping_admin() {
add_cacheaction( 'cache_admin_page', 'wp_supercache_domain_mapping_admin' );

function wp_supercache_domain_mapping_notice() {
global $cache_enabled;

if ( $cache_enabled ) {
if ( $GLOBALS['wpsc_config']['cache_enabled'] ) {
echo '<div class="error"><p><strong>' . esc_html__( 'Domain Mapping plugin detected! Please go to the Supercache plugins page and enable the domain mapping helper plugin.', 'wp-super-cache' ) . '</strong></p></div>';
}
}
Expand Down
7 changes: 3 additions & 4 deletions plugins/multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ function wp_super_cache_multisite_notice() {
}

function wp_super_cache_override_on_flag() {
global $cache_enabled, $super_cache_enabled;
if ( true !== $cache_enabled ) {
if ( true !== $GLOBALS['wpsc_config']['cache_enabled'] ) {
return false;
}

if ( 1 === (int) get_option( 'wp_super_cache_disabled' ) ) {
$cache_enabled = false;
$super_cache_enabled = false;
$GLOBALS['wpsc_config']['cache_enabled'] = false;
$GLOBALS['wpsc_config']['super_cache_enabled'] = false;
define( 'DONOTCACHEPAGE', 1 );
define( 'SUBMITDISABLED', 'disabled style="color: #aaa" ' );
if ( is_admin() ) {
Expand Down
3 changes: 1 addition & 2 deletions plugins/wptouch.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ function wp_super_cache_wptouch_admin() {
add_cacheaction( 'cache_admin_page', 'wp_super_cache_wptouch_admin' );

function wp_super_cache_wptouch_notice() {
global $cache_enabled;
if ( $cache_enabled ) {
if ( $GLOBALS['wpsc_config']['cache_enabled'] ) {
echo '<div class="error"><p><strong>' . __( 'WPTouch plugin detected! Please go to the Supercache plugins page and enable the WPTouch helper plugin.', 'wp-super-cache' ) . '</strong></p></div>';
}
}
Expand Down
10 changes: 3 additions & 7 deletions rest/class.wp-super-cache-rest-delete-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ public function callback( $request ) {
wpsc_delete_post_cache( $params['id'] );

} elseif ( !empty( $params['expired'] ) ) {
global $file_prefix;
wp_cache_clean_expired( $file_prefix );
wp_cache_clean_expired( $GLOBALS['wpsc_config']['file_prefix'] );

} elseif ( isset( $params['url'] ) ) {
global $cache_path;

$directory = $cache_path . 'supercache/' . $params[ 'url' ];
$directory = $GLOBALS['wpsc_config']['cache_path'] . 'supercache/' . $params[ 'url' ];
wpsc_delete_files( $directory );
prune_super_cache( $directory . '/page', true );

} else {
global $file_prefix;
wp_cache_clean_cache( $file_prefix, !empty( $params['all'] ) );
wp_cache_clean_cache( $GLOBALS['wpsc_config']['file_prefix'], !empty( $params['all'] ) );
}

return rest_ensure_response( array( 'Cache Cleared' => true ) );
Expand Down
4 changes: 1 addition & 3 deletions rest/class.wp-super-cache-rest-get-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public function get_ossdl_off_blog_url() {
* @return string
*/
public function get_cache_path_url() {
global $cache_path;

return site_url( str_replace( ABSPATH, '', "{$cache_path}" ) );
return site_url( str_replace( ABSPATH, '', "{$GLOBALS['wpsc_config']['cache_path']}" ) );
}

/**
Expand Down
20 changes: 9 additions & 11 deletions rest/class.wp-super-cache-rest-get-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ public function callback( $request ) {
* @param array $status
*/
protected function add_preload_status( & $status ) {
global $wp_cache_config_file;

include( $wp_cache_config_file );
wpsc_load_config();

if ( false == $cache_enabled ) {
if ( false == $GLOBALS['wpsc_config']['cache_enabled'] ) {
$status[ 'preload_disabled_cache_off' ] = true;
}
if ( false == $super_cache_enabled ) {
if ( false == $GLOBALS['wpsc_config']['super_cache_enabled'] ) {
$status[ 'preload_disabled_supercache_off' ] = true;
}
if ( true === defined( 'DISABLESUPERCACHEPRELOADING' ) ) {
Expand All @@ -49,11 +48,10 @@ protected function add_preload_status( & $status ) {
* @param array $status
*/
protected function add_php_mod_rewrite_status( & $status ) {
global $wp_cache_config_file;

include( $wp_cache_config_file );
wpsc_load_config();

if ( $cache_enabled && !$wp_cache_mod_rewrite ) {
if ( $GLOBALS['wpsc_config']['cache_enabled'] && ! $GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] ) {
$scrules = trim( implode( "\n", extract_from_markers( trailingslashit( get_home_path() ) . '.htaccess', 'WPSuperCache' ) ) );
if ( $scrules != '' ) {
$status[ 'php_mod_rewrite' ] = true;
Expand Down Expand Up @@ -87,12 +85,12 @@ protected function add_compression_status( & $status ) {
* @param array $status
*/
protected function add_rewrite_status( & $status ) {
global $home_path, $wp_cache_config_file;
global $home_path;

include( $wp_cache_config_file );
wpsc_load_config();

// Return if the rewrite caching is disabled.
if ( ! $cache_enabled || ! $super_cache_enabled || ! $wp_cache_mod_rewrite ) {
if ( ! $GLOBALS['wpsc_config']['cache_enabled'] || ! $GLOBALS['wpsc_config']['super_cache_enabled'] || ! $GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] ) {
return;
}

Expand All @@ -103,7 +101,7 @@ protected function add_rewrite_status( & $status ) {
$status[ 'mod_rewrite_rules' ] = true;
}
$got_rewrite = apache_mod_loaded( 'mod_rewrite', true );
if ( $wp_cache_mod_rewrite && false == apply_filters( 'got_rewrite', $got_rewrite ) ) {
if ( $GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] && false == apply_filters( 'got_rewrite', $got_rewrite ) ) {
$status[ 'mod_rewrite_missing' ] = true;
}

Expand Down
4 changes: 1 addition & 3 deletions rest/class.wp-super-cache-rest-test-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class WP_Super_Cache_Rest_Test_Cache extends WP_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function callback( $request ) {
global $cache_path;

$url = trailingslashit( get_bloginfo( 'url' ) );

$response = array( 'status' => 'UNKNOWN' );
Expand All @@ -24,7 +22,7 @@ public function callback( $request ) {
$page[ $c ] = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );

if ( ! is_wp_error( $page[ $c ] ) ) {
$fp = fopen( $cache_path . $c . ".html", "w" );
$fp = fopen( $GLOBALS['wpsc_config']['cache_path'] . $c . ".html", "w" );
fwrite( $fp, $page[ $c ][ 'body' ] );
fclose( $fp );
}
Expand Down
53 changes: 24 additions & 29 deletions rest/class.wp-super-cache-rest-update-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ protected function set_global( $global_name, $value ) {
* @param mixed $value
*/
protected function set_wp_cache_location( $value ) {
global $cache_path;

if ( $value != '' && ( ! isset( $cache_path ) || $value != $cache_path ) ) {
if ( $value != '' && ( ! isset( $GLOBALS['wpsc_config']['cache_path'] ) || $value != $GLOBALS['wpsc_config']['cache_path'] ) ) {
$dir = realpath( trailingslashit( dirname( $value ) ) );
if ( $dir == false ) {
$dir = WP_CONTENT_DIR . '/cache/';
Expand All @@ -159,13 +157,13 @@ protected function set_wp_cache_location( $value ) {
$new_cache_path = WP_CONTENT_DIR . '/cache/';
}

if ( $new_cache_path != $cache_path ) {
if ( $new_cache_path != $GLOBALS['wpsc_config']['cache_path'] ) {
if ( file_exists( $new_cache_path ) == false ) {
rename( $cache_path, $new_cache_path );
rename( $GLOBALS['wpsc_config']['cache_path'], $new_cache_path );
}

$cache_path = $new_cache_path;
wp_cache_setting( 'cache_path', $cache_path );
$GLOBALS['wpsc_config']['cache_path'] = $new_cache_path;
wp_cache_setting( 'cache_path', $GLOBALS['wpsc_config']['cache_path'] );
}
}

Expand Down Expand Up @@ -194,7 +192,6 @@ protected function set_lock_down( $value ) {
* @param mixed $value
*/
protected function set_super_cache_enabled( $value ) {
global $wp_cache_mod_rewrite;

if ( is_numeric( $value ) == false ) {
$types = array( 'wpcache' => 0, 'mod_rewrite' => 1, 'PHP' => 2 );
Expand All @@ -210,19 +207,19 @@ protected function set_super_cache_enabled( $value ) {

} else {
wp_super_cache_enable();
$wp_cache_mod_rewrite = 0; // PHP recommended
$GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] = 0; // PHP recommended

if ( $value == 1 ) { // mod_rewrite
$wp_cache_mod_rewrite = 1;
$GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] = 1;
add_mod_rewrite_rules();

} elseif( $value == 2 ) { // PHP
$wp_cache_mod_rewrite = 0;
$GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] = 0;
remove_mod_rewrite_rules();

}

wp_cache_setting( 'wp_cache_mod_rewrite', $wp_cache_mod_rewrite );
wp_cache_setting( 'wp_cache_mod_rewrite', $GLOBALS['wpsc_config']['wp_cache_mod_rewrite'] );
}
return true;
}
Expand All @@ -231,11 +228,11 @@ protected function set_super_cache_enabled( $value ) {
* @param mixed $value
*/
protected function set_wp_cache_not_logged_in( $value ) {
global $wp_cache_not_logged_in, $cache_path;
global $wp_cache_not_logged_in;

if ( 0 != $value ) {
if ( 0 == $wp_cache_not_logged_in && function_exists( 'prune_super_cache' ) ) {
prune_super_cache( $cache_path, true );
prune_super_cache( $GLOBALS['wpsc_config']['cache_path'], true );
}

$wp_cache_not_logged_in = (int) $value;
Expand All @@ -251,11 +248,11 @@ protected function set_wp_cache_not_logged_in( $value ) {
* @param mixed $value
*/
protected function set_wp_cache_make_known_anon( $value ) {
global $wp_cache_make_known_anon, $cache_path;
global $wp_cache_make_known_anon;

if ( 1 == $value ) {
if ( $wp_cache_make_known_anon == 0 && function_exists( 'prune_super_cache' ) ) {
prune_super_cache( $cache_path, true );
prune_super_cache( $GLOBALS['wpsc_config']['cache_path'], true );
}

$wp_cache_make_known_anon = 1;
Expand All @@ -271,15 +268,15 @@ protected function set_wp_cache_make_known_anon( $value ) {
* @param mixed $value
*/
protected function set_wp_cache_object_cache( $value ) {
global $_wp_using_ext_object_cache, $wp_cache_object_cache, $cache_path;
global $_wp_using_ext_object_cache, $wp_cache_object_cache;

if ( ! $_wp_using_ext_object_cache ) {
return;
}

if ( $value == 0 ) {
if ( function_exists( 'prune_super_cache' ) ) {
prune_super_cache( $cache_path, true );
prune_super_cache( $GLOBALS['wpsc_config']['cache_path'], true );
}

$wp_cache_object_cache = 1;
Expand All @@ -297,12 +294,10 @@ protected function set_wp_cache_object_cache( $value ) {
* @return null|string
*/
protected function set_cache_compression( $value ) {
global $cache_compression, $cache_path;

$new_cache_compression = 0;
if ( defined( 'WPSC_DISABLE_COMPRESSION' ) ) {
$cache_compression = 0;
wp_cache_setting( 'cache_compression', $cache_compression );
$GLOBALS['wpsc_config']['cache_compression'] = 0;
wp_cache_setting( 'cache_compression', $GLOBALS['wpsc_config']['cache_compression'] );

} else {
if ( 1 == $value ) {
Expand All @@ -313,11 +308,11 @@ protected function set_cache_compression( $value ) {
return __( "You attempted to enable compression but `zlib.output_compression` is enabled. See #21 in the Troubleshooting section of the readme file.", 'wp-super-cache' );
}

if ( $new_cache_compression != $cache_compression ) {
$cache_compression = $new_cache_compression;
wp_cache_setting( 'cache_compression', $cache_compression );
if ( $new_cache_compression != $GLOBALS['wpsc_config']['cache_compression'] ) {
$GLOBALS['wpsc_config']['cache_compression'] = $new_cache_compression;
wp_cache_setting( 'cache_compression', $GLOBALS['wpsc_config']['cache_compression'] );
if ( function_exists( 'prune_super_cache' ) ) {
prune_super_cache( $cache_path, true );
prune_super_cache( $GLOBALS['wpsc_config']['cache_path'], true );
}

delete_option( 'super_cache_meta' );
Expand Down Expand Up @@ -447,7 +442,7 @@ protected function set_preload_setting( $value, $name ) {
* @param bool $enabled
*/
protected function toggle_easy_caching( $enabled = true ) {
global $cache_path, $wp_cache_shutdown_gc, $cache_schedule_type;
global $cache_schedule_type;
if ( $enabled ) {
$settings = array(
'wp_cache_mobile_enabled' => 1,
Expand Down Expand Up @@ -475,8 +470,8 @@ protected function toggle_easy_caching( $enabled = true ) {
$this->set_value_by_key( $value, $key );
}

if ( $cache_path != WP_CONTENT_DIR . '/cache/' ) {
$this->set_value_by_key( $cache_path, 'wp_cache_location' );
if ( $GLOBALS['wpsc_config']['cache_path'] != WP_CONTENT_DIR . '/cache/' ) {
$this->set_value_by_key( $GLOBALS['wpsc_config']['cache_path'], 'wp_cache_location' );
}

$advanced_settings = array(
Expand Down
8 changes: 4 additions & 4 deletions wp-cache-base.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
global $WPSC_HTTP_HOST, $cache_enabled, $cache_path, $blogcacheid, $blog_cache_dir;
global $wpsc_config, $WPSC_HTTP_HOST, $blogcacheid, $blog_cache_dir;

if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
$WPSC_HTTP_HOST = function_exists( 'mb_strtolower' ) ? mb_strtolower( $_SERVER['HTTP_HOST'] ) : strtolower( $_SERVER['HTTP_HOST'] );
$WPSC_HTTP_HOST = htmlentities( $WPSC_HTTP_HOST );
} elseif ( PHP_SAPI === 'cli' && function_exists( 'get_option' ) ) {
$WPSC_HTTP_HOST = (string) parse_url( get_option( 'home' ), PHP_URL_HOST );
} else {
$cache_enabled = false;
$GLOBALS['wpsc_config']['cache_enabled'] = false;
$WPSC_HTTP_HOST = '';
}

// We want to be able to identify each blog in a WordPress MU install
$blogcacheid = '';
$blog_cache_dir = $cache_path;
$blog_cache_dir = $GLOBALS['wpsc_config']['cache_path'];

if ( is_multisite() ) {
global $current_blog;
Expand Down Expand Up @@ -43,5 +43,5 @@
if ( empty( $blogcacheid ) ) {
$blogcacheid = 'blog';
}
$blog_cache_dir = str_replace( '//', '/', $cache_path . 'blogs/' . $blogcacheid . '/' );
$blog_cache_dir = str_replace( '//', '/', $GLOBALS['wpsc_config']['cache_path'] . 'blogs/' . $blogcacheid . '/' );
}
Loading