| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles cache configuration and related I/O |
| 7 |
*/ |
| 8 |
|
| 9 |
if (!class_exists('WPO_Cache_Config')) : |
| 10 |
|
| 11 |
class WPO_Cache_Config { |
| 12 |
|
| 13 |
/** |
| 14 |
* Defaults |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
public $defaults; |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance of this class |
| 22 |
* |
| 23 |
* @var mixed |
| 24 |
*/ |
| 25 |
public static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
public $config; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Set config defaults |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->defaults = $this->get_defaults(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get config from file or cache |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function get() { |
| 46 |
|
| 47 |
if (is_multisite()) { |
| 48 |
$config = get_site_option('wpo_cache_config', $this->get_defaults()); |
| 49 |
} else { |
| 50 |
$config = get_option('wpo_cache_config', $this->get_defaults()); |
| 51 |
} |
| 52 |
|
| 53 |
return wp_parse_args($config, $this->get_defaults()); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get a specific configuration option |
| 58 |
* |
| 59 |
* @param string $option_key The option identifier |
| 60 |
* @param boolean $default Default value if the option doesn't exist (Default to false) |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function get_option($option_key, $default = false) { |
| 64 |
$options = $this->get(); |
| 65 |
return apply_filters("wpo_option_key_{$option_key}", (isset($options[$option_key]) ? $options[$option_key] : $default)); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Updates the given config object in file and DB |
| 70 |
* |
| 71 |
* @param array $config - the cache configuration |
| 72 |
* @param boolean $skip_disk_if_not_yet_present - only write the configuration file to disk if it already exists. This presents PHP notices if the cache has never been on, and settings are saved. |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function update($config, $skip_disk_if_not_yet_present = false) { |
| 77 |
$config = wp_parse_args($config, $this->get_defaults()); |
| 78 |
|
| 79 |
$config['page_cache_length_value'] = intval($config['page_cache_length_value']); |
| 80 |
$config['page_cache_length'] = $this->calculate_page_cache_length($config['page_cache_length_value'], $config['page_cache_length_unit']); |
| 81 |
|
| 82 |
/** |
| 83 |
* Filters the cookies used to set cache file names |
| 84 |
* |
| 85 |
* @param array $cookies - The cookies |
| 86 |
* @param array $config - The new config |
| 87 |
*/ |
| 88 |
$wpo_cache_cookies = apply_filters('wpo_cache_cookies', array(), $config); |
| 89 |
sort($wpo_cache_cookies); |
| 90 |
|
| 91 |
/** |
| 92 |
* Filters the query variables used to set cache file names |
| 93 |
* |
| 94 |
* @param array $wpo_query_variables - The variables |
| 95 |
* @param array $config - The new config |
| 96 |
*/ |
| 97 |
$wpo_query_variables = apply_filters('wpo_cache_query_variables', array(), $config); |
| 98 |
sort($wpo_query_variables); |
| 99 |
|
| 100 |
$config['wpo_cache_cookies'] = $wpo_cache_cookies; |
| 101 |
$config['wpo_cache_query_variables'] = $wpo_query_variables; |
| 102 |
|
| 103 |
$config = apply_filters('wpo_cache_update_config', $config); |
| 104 |
|
| 105 |
if (is_multisite()) { |
| 106 |
update_site_option('wpo_cache_config', $config); |
| 107 |
} else { |
| 108 |
update_option('wpo_cache_config', $config); |
| 109 |
} |
| 110 |
|
| 111 |
do_action('wpo_cache_config_updated', $config); |
| 112 |
|
| 113 |
return $this->write($config, $skip_disk_if_not_yet_present); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Calculate cache expiration value in seconds. |
| 118 |
* |
| 119 |
* @param int $value |
| 120 |
* @param string $unit ( hours | days | months ) |
| 121 |
* |
| 122 |
* @return int |
| 123 |
*/ |
| 124 |
private function calculate_page_cache_length($value, $unit) { |
| 125 |
$cache_length_units = array( |
| 126 |
'hours' => 3600, |
| 127 |
'days' => 86400, |
| 128 |
'months' => 2629800, // 365.25 * 86400 / 12 |
| 129 |
); |
| 130 |
|
| 131 |
return $value * $cache_length_units[$unit]; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Deletes config files and options |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public function delete() { |
| 140 |
|
| 141 |
if (is_multisite()) { |
| 142 |
delete_site_option('wpo_cache_config'); |
| 143 |
} else { |
| 144 |
delete_option('wpo_cache_config'); |
| 145 |
} |
| 146 |
|
| 147 |
if (!WPO_Page_Cache::delete(WPO_CACHE_CONFIG_DIR)) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Writes config to file |
| 156 |
* |
| 157 |
* @param array $config - Configuration array. |
| 158 |
* @param boolean $only_if_present - only writes to the disk if the configuration file already exists |
| 159 |
* |
| 160 |
* @return boolean - returns false if an attempt to write failed |
| 161 |
*/ |
| 162 |
public function write($config, $only_if_present = false) { |
| 163 |
|
| 164 |
$config_file = WPO_CACHE_CONFIG_DIR.'/'.$this->get_cache_config_filename(); |
| 165 |
|
| 166 |
$this->config = wp_parse_args($config, $this->get_defaults()); |
| 167 |
|
| 168 |
// from 3.0.17 we use more secure way to store cache config files. |
| 169 |
$advanced_cache_version = WPO_Page_Cache::instance()->get_advanced_cache_version(); |
| 170 |
// if advanced-cache.php exists and has at least 3.0.17 version or |
| 171 |
// advanced-cache.php doesn't exist then |
| 172 |
// we write the cache config in a new format. |
| 173 |
if (($advanced_cache_version && (version_compare($advanced_cache_version, '3.0.17', '>='))) || !$advanced_cache_version) { |
| 174 |
// Apply the encoding required for placing within PHP single quotes - https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single |
| 175 |
$json_encoded_string = str_replace(array('\\', "'"), array('\\\\', '\\\''), json_encode($this->config)); |
| 176 |
|
| 177 |
$config_content = '<?php' . "\n" |
| 178 |
. 'if (!defined(\'ABSPATH\')) die(\'No direct access allowed\');' . "\n\n" |
| 179 |
. '$GLOBALS[\'wpo_cache_config\'] = json_decode(\'' . $json_encoded_string . '\', true);' . "\n"; |
| 180 |
} else { |
| 181 |
$config_content = json_encode($this->config); |
| 182 |
} |
| 183 |
|
| 184 |
if ((!$only_if_present || file_exists($config_file)) && !file_put_contents($config_file, $config_content)) { |
| 185 |
return new WP_Error('write_cache_config', sprintf(__('The cache configuration file could not be saved to the disk; please check the file/folder permissions of %s .', 'wp-optimize'), $config_file)); |
| 186 |
} |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Verify we can write to the file system |
| 193 |
* |
| 194 |
* @since 1.0 |
| 195 |
* @return boolean |
| 196 |
*/ |
| 197 |
public function verify_file_access() { |
| 198 |
if (function_exists('clearstatcache')) { |
| 199 |
clearstatcache(); |
| 200 |
} |
| 201 |
|
| 202 |
// First check wp-config.php. |
| 203 |
if (!is_writable(ABSPATH . 'wp-config.php') && !is_writable(ABSPATH . '../wp-config.php')) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// Now check wp-content. We need to be able to create files of the same user as this file. |
| 208 |
if (!$this->_is_dir_writable(untrailingslashit(WP_CONTENT_DIR))) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
// If the cache and config directories exist, make sure they're writeable |
| 213 |
if (file_exists(untrailingslashit(WP_CONTENT_DIR) . '/wpo-cache')) { |
| 214 |
|
| 215 |
if (file_exists(WPO_CACHE_DIR)) { |
| 216 |
if (!$this->_is_dir_writable(WPO_CACHE_DIR)) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (file_exists(WPO_CACHE_CONFIG_DIR)) { |
| 222 |
if (!$this->_is_dir_writable(WPO_CACHE_CONFIG_DIR)) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Return defaults |
| 233 |
* |
| 234 |
* @return array |
| 235 |
*/ |
| 236 |
public function get_defaults() { |
| 237 |
|
| 238 |
$defaults = array( |
| 239 |
'enable_page_caching' => false, |
| 240 |
'page_cache_length_value' => 24, |
| 241 |
'page_cache_length_unit' => 'hours', |
| 242 |
'page_cache_length' => 86400, |
| 243 |
'cache_exception_conditional_tags' => array(), |
| 244 |
'cache_exception_urls' => array(), |
| 245 |
'cache_exception_cookies' => array(), |
| 246 |
'cache_exception_browser_agents' => array(), |
| 247 |
'enable_sitemap_preload' => false, |
| 248 |
'enable_schedule_preload' => false, |
| 249 |
'preload_schedule_type' => '', |
| 250 |
'enable_mobile_caching' => false, |
| 251 |
'enable_user_caching' => false, |
| 252 |
'site_url' => network_home_url('/'), |
| 253 |
'enable_cache_per_country' => false, |
| 254 |
'permalink_structure' => get_option('permalink_structure'), |
| 255 |
'uploads' => wp_normalize_path(wp_upload_dir()['basedir']), |
| 256 |
'gmt_offset' => get_option('gmt_offset'), |
| 257 |
'timezone_string' => get_option('timezone_string'), |
| 258 |
'date_format' => get_option('date_format'), |
| 259 |
'time_format' => get_option('time_format'), |
| 260 |
'use_webp_images' => false, |
| 261 |
); |
| 262 |
|
| 263 |
return apply_filters('wpo_cache_defaults', $defaults); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get advanced-cache.php file name with full path. |
| 268 |
* |
| 269 |
* @return string |
| 270 |
*/ |
| 271 |
public function get_cache_config_filename() { |
| 272 |
$url = parse_url(network_site_url()); |
| 273 |
|
| 274 |
if (isset($url['port']) && '' != $url['port'] && 80 != $url['port']) { |
| 275 |
return 'config-'.strtolower($url['host']).'-port'.$url['port'].'.php'; |
| 276 |
} else { |
| 277 |
return 'config-'.strtolower($url['host']).'.php'; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Return an instance of the current class, create one if it doesn't exist |
| 283 |
* |
| 284 |
* @since 1.0 |
| 285 |
* @return WPO_Cache_Config |
| 286 |
*/ |
| 287 |
public static function instance() { |
| 288 |
|
| 289 |
if (!self::$instance) { |
| 290 |
self::$instance = new self(); |
| 291 |
} |
| 292 |
|
| 293 |
return self::$instance; |
| 294 |
} |
| 295 |
} |
| 296 |
endif; |
| 297 |
|