PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
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.3, at cache/class-wpo-cache-config.php

272 lines 7.2 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 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 private function write($config, $only_if_present = false) {
158
159 $url = parse_url(network_site_url());
160
161 if (isset($url['port']) && '' != $url['port'] && 80 != $url['port']) {
162 $config_file = WPO_CACHE_CONFIG_DIR.'/config-'.$url['host'].'-port'.$url['port'].'.php';
163 } else {
164 $config_file = WPO_CACHE_CONFIG_DIR.'/config-'.$url['host'].'.php';
165 }
166
167 $this->config = wp_parse_args($config, $this->get_defaults());
168
169 // from 3.0.17 we use more secure way to store cache config files.
170 $advanced_cache_version = WPO_Page_Cache::instance()->get_advanced_cache_version();
171 // if advanced-cache.php exists and has at least 3.0.17 version or
172 // advanced-cache.php doesn't exist then
173 // we write the cache config in a new format.
174 if (($advanced_cache_version && (version_compare($advanced_cache_version, '3.0.17', '>='))) || !$advanced_cache_version) {
175 $config_content = '<?php' . "\n"
176 . 'if (!defined(\'ABSPATH\')) die(\'No direct access allowed\');' . "\n\n"
177 . '$GLOBALS[\'wpo_cache_config\'] = json_decode(\'' . json_encode($this->config) . '\', true);' . "\n";
178 } else {
179 $config_content = json_encode($this->config);
180 }
181
182 if ((!$only_if_present || file_exists($config_file)) && !file_put_contents($config_file, $config_content)) {
183 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));
184 }
185
186 return true;
187 }
188
189 /**
190 * Verify we can write to the file system
191 *
192 * @since 1.0
193 * @return boolean
194 */
195 public function verify_file_access() {
196 if (function_exists('clearstatcache')) {
197 clearstatcache();
198 }
199
200 // First check wp-config.php.
201 if (!is_writable(ABSPATH . 'wp-config.php') && !is_writable(ABSPATH . '../wp-config.php')) {
202 return false;
203 }
204
205 // Now check wp-content. We need to be able to create files of the same user as this file.
206 if (!$this->_is_dir_writable(untrailingslashit(WP_CONTENT_DIR))) {
207 return false;
208 }
209
210 // If the cache and config directories exist, make sure they're writeable
211 if (file_exists(untrailingslashit(WP_CONTENT_DIR) . '/wpo-cache')) {
212
213 if (file_exists(WPO_CACHE_DIR)) {
214 if (!$this->_is_dir_writable(WPO_CACHE_DIR)) {
215 return false;
216 }
217 }
218
219 if (file_exists(WPO_CACHE_CONFIG_DIR)) {
220 if (!$this->_is_dir_writable(WPO_CACHE_CONFIG_DIR)) {
221 return false;
222 }
223 }
224 }
225
226 return true;
227 }
228
229 /**
230 * Return defaults
231 *
232 * @return array
233 */
234 public function get_defaults() {
235
236 $defaults = array(
237 'enable_page_caching' => false,
238 'page_cache_length_value' => 24,
239 'page_cache_length_unit' => 'hours',
240 'page_cache_length' => 86400,
241 'cache_exception_urls' => array(),
242 'cache_exception_cookies' => array(),
243 'cache_exception_browser_agents' => array(),
244 'enable_sitemap_preload' => false,
245 'enable_schedule_preload' => false,
246 'preload_schedule_type' => '',
247 'enable_mobile_caching' => false,
248 'enable_user_caching' => false,
249 'site_url' => network_site_url('/'),
250 'enable_cache_per_country' => false,
251 );
252
253 return apply_filters('wpo_cache_defaults', $defaults);
254 }
255
256 /**
257 * Return an instance of the current class, create one if it doesn't exist
258 *
259 * @since 1.0
260 * @return WPO_Cache_Config
261 */
262 public static function instance() {
263
264 if (!self::$instance) {
265 self::$instance = new self();
266 }
267
268 return self::$instance;
269 }
270 }
271 endif;
272