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

291 lines 8.9 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 WPO_Cache_Config | null
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->defaults);
49 } else {
50 $config = get_option('wpo_cache_config', $this->defaults);
51 }
52
53 return wp_parse_args($config, $this->defaults);
54 }
55
56 /**
57 * Get a specific configuration option
58 *
59 * @param string $option_key The option identifier
60 * @param mixed $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}", ($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 true|WP_Error - returns true on success or WP_Error if the config cannot be written to disk
75 */
76 public function update($config, $skip_disk_if_not_yet_present = false) {
77 $config = wp_parse_args($config, $this->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 $fields_to_trim = array('cache_exception_conditional_tags', 'cache_exception_urls', 'cache_ignore_query_variables', 'cache_exception_cookies', 'cache_exception_browser_agents');
83 foreach ($fields_to_trim as $field) {
84 if (!empty($config[$field]) && is_array($config[$field])) {
85 $config[$field] = array_map('trim', $config[$field]);
86 }
87 }
88
89 /**
90 * Filters the cookies used to set cache file names
91 *
92 * @param array $cookies - The cookies
93 * @param array $config - The new config
94 */
95 $wpo_cache_cookies = apply_filters('wpo_cache_cookies', array(), $config);
96 sort($wpo_cache_cookies);
97
98 /**
99 * Filters the query variables used to set cache file names
100 *
101 * @param array $wpo_query_variables - The variables
102 * @param array $config - The new config
103 */
104 $wpo_query_variables = apply_filters('wpo_cache_query_variables', array(), $config);
105 sort($wpo_query_variables);
106
107 $config['wpo_cache_cookies'] = $wpo_cache_cookies;
108 $config['wpo_cache_query_variables'] = $wpo_query_variables;
109
110 $config = apply_filters('wpo_cache_update_config', $config);
111
112 if (is_multisite()) {
113 update_site_option('wpo_cache_config', $config);
114 } else {
115 update_option('wpo_cache_config', $config);
116 }
117
118 do_action('wpo_cache_config_updated', $config);
119
120 return $this->write($config, $skip_disk_if_not_yet_present);
121 }
122
123 /**
124 * Calculate cache expiration value in seconds.
125 *
126 * @param int $value
127 * @param string $unit ( hours | days | months )
128 *
129 * @return int
130 */
131 private function calculate_page_cache_length($value, $unit) {
132 $cache_length_units = array(
133 'hours' => 3600,
134 'days' => 86400,
135 'months' => 2629800, // 365.25 * 86400 / 12
136 );
137
138 return $value * $cache_length_units[$unit];
139 }
140
141 /**
142 * Deletes config files and options
143 *
144 * @return bool
145 */
146 public function delete() {
147
148 if (is_multisite()) {
149 delete_site_option('wpo_cache_config');
150 } else {
151 delete_option('wpo_cache_config');
152 }
153
154 if (!WPO_Page_Cache::delete(WPO_CACHE_CONFIG_DIR)) {
155 return false;
156 }
157
158 return true;
159 }
160
161 /**
162 * Writes config to file
163 *
164 * @param array $config - Configuration array.
165 * @param boolean $only_if_present - only writes to the disk if the configuration file already exists
166 *
167 * @return true|WP_Error - returns true on success or WP_Error if an attempt to write failed
168 */
169 public function write($config, $only_if_present = false) {
170
171 $config_file = $this->get_config_file_path();
172
173 $this->config = wp_parse_args($config, $this->defaults);
174
175 // from 3.0.17 we use more secure way to store cache config files.
176 $advanced_cache_version = WPO_Page_Cache::instance()->get_advanced_cache_version();
177 // if advanced-cache.php exists and has at least 3.0.17 version or
178 // advanced-cache.php doesn't exist then
179 // we write the cache config in a new format.
180 if (($advanced_cache_version && (version_compare($advanced_cache_version, '3.0.17', '>='))) || !$advanced_cache_version) {
181 // 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
182 $json_encoded_string = str_replace(array('\\', "'"), array('\\\\', '\\\''), wp_json_encode($this->config));
183
184 $config_content = '<?php' . "\n"
185 . 'if (!defined(\'ABSPATH\')) die(\'No direct access allowed\');' . "\n\n"
186 . '$GLOBALS[\'wpo_cache_config\'] = json_decode(\'' . $json_encoded_string . '\', true);' . "\n";
187 } else {
188 $config_content = wp_json_encode($this->config);
189 }
190
191 if ((!$only_if_present || file_exists($config_file)) && (!wp_is_writable(WPO_CACHE_CONFIG_DIR) || !file_put_contents($config_file, $config_content))) {
192 // If translations are already loaded, use the translated string
193 if (is_textdomain_loaded('wp-optimize')) {
194 /* translators: %s is the path to the cache config file */
195 $message = sprintf(__('The cache configuration file could not be saved to the disk; please check the file/folder permissions of %s .', 'wp-optimize'), $config_file);
196 } else {
197 // Fallback to plain English string to avoid triggering premature translation loading
198 $message = sprintf('The cache configuration file could not be saved to the disk; please check the file/folder permissions of %s .', $config_file);
199 }
200
201 return new WP_Error('write_cache_config', $message);
202 }
203
204 return true;
205 }
206
207 /**
208 * Get config file name with full path.
209 *
210 * @return string
211 */
212 public function get_config_file_path() {
213 return WPO_CACHE_CONFIG_DIR . '/' . $this->get_cache_config_filename();
214 }
215
216 /**
217 * Return defaults
218 *
219 * @return array
220 */
221 public function get_defaults() {
222
223 $defaults = array(
224 'enable_page_caching' => false,
225 'page_cache_length_value' => 24,
226 'page_cache_length_unit' => 'hours',
227 'page_cache_length' => 86400,
228 'cache_exception_conditional_tags' => array(),
229 'cache_specific_urls_only' => false,
230 'cache_include_urls' => array(),
231 'cache_exception_urls' => array(),
232 'cache_ignore_query_variables' => array(),
233 'cache_exception_cookies' => array(),
234 'cache_exception_browser_agents' => array(),
235 'enable_sitemap_preload' => false,
236 'enable_schedule_preload' => false,
237 'preload_schedule_type' => '',
238 'enable_mobile_caching' => false,
239 'enable_user_caching' => false,
240 'site_url' => network_home_url('/'),
241 'enable_cache_per_country' => false,
242 'enable_cache_aelia_currency' => false,
243 'permalink_structure' => get_option('permalink_structure'),
244 'uploads' => wp_normalize_path(wp_upload_dir()['basedir']),
245 'gmt_offset' => get_option('gmt_offset'),
246 'timezone_string' => get_option('timezone_string'),
247 'date_format' => get_option('date_format'),
248 'time_format' => get_option('time_format'),
249 'use_webp_images' => false,
250 'host_gravatars_locally' => 0,
251 'auto_preload_purged_contents' => true,
252 'enable_rest_caching' => false,
253 'last_cleared' => time(),
254 'lcp_preload_enable' => false,
255 );
256
257 return apply_filters('wpo_cache_defaults', $defaults);
258 }
259
260 /**
261 * Get advanced-cache.php file name with full path.
262 *
263 * @return string
264 */
265 public function get_cache_config_filename() {
266 $url = wp_parse_url(network_site_url());
267
268 if (isset($url['port']) && '' !== (string) $url['port'] && 80 !== $url['port']) {
269 return 'config-'.strtolower($url['host']).'-port'.$url['port'].'.php';
270 } else {
271 return 'config-'.strtolower($url['host']).'.php';
272 }
273 }
274
275 /**
276 * Return an instance of the current class, create one if it doesn't exist
277 *
278 * @since 1.0
279 * @return WPO_Cache_Config
280 */
281 public static function instance() {
282
283 if (!self::$instance) {
284 self::$instance = new self();
285 }
286
287 return self::$instance;
288 }
289 }
290 endif;
291