PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.7
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.7
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / cache / class-wpo-cache-config.php

class-wpo-cache-config.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.7, at cache/class-wpo-cache-config.php

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