PluginProbe
ezCache / 2.6.3
ezCache v2.6.3
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / Settings.php

Settings.php in ezCache 2.6.3, at includes/Settings.php

196 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Upress\EzCache;
3
4 class Settings {
5 /** @var object */
6 protected static $settings;
7
8 /**
9 * Get the path to the config file
10 * @return string
11 */
12 public static function settings_file_path() {
13 return WP_CONTENT_DIR . '/ezcache-config.json';
14 }
15
16 public static function get_default_settings() {
17 return (object) [
18 'no_cache_known_users' => true,
19 'no_cache_comment_authors' => true,
20 'separate_mobile_cache' => true,
21 'cache_lifetime' => 604800,
22 'cache_expiry_interval' => 10800,
23
24 'disable_wp_emoji' => false,
25 'optimize_google_fonts' => false,
26 'minify_html' => false,
27 'minify_html_comments' => true,
28 'minify_inline_js' => false,
29 'minify_inline_css' => false,
30 'minify_js' => false,
31 'combine_head_js' => false,
32 'combine_body_js' => false,
33 'combine_head_inline_js' => true,
34 'combine_body_inline_js' => true,
35 'minify_css' => false,
36 'combine_css' => false,
37 'combine_css_footer' => false,
38 'critical_css' => '',
39 'enable_webp_support' => false,
40
41 'no_cache_query_params' => false,
42 'ignore_query_params' => false,
43 'ignored_query_params_list' => "gclid\ngbraid\nwbraid\nfbclid\nmsclkid\ndclid\nyclid\ntwclid\nutm_*\nmc_cid\nmc_eid\n_ga\n_gl",
44 'cache_clear_on_post_edit' => true,
45 'cache_clear_home_on_post_edit' => true,
46 'enable_varnish_purge' => true,
47 'bypass_cache' => [
48 'single' => false,
49 'pages' => false,
50 'frontpage' => false,
51 'home' => false,
52 'archives' => false,
53 'tag' => false,
54 'category' => false,
55 'feed' => false,
56 'search' => false,
57 'author' => false,
58 ],
59 'rejected_uri' => "/cart\n/checkout",
60 'rejected_user_agent' => '',
61 'rejected_cookies' => '',
62 'excluded_minify_files' => '',
63
64 // Preload (1.7.0+)
65 'enable_preload' => false,
66 'preload_on_cache_clear' => true,
67 'preload_sitemap_url' => '',
68 'preload_batch_size' => 5,
69 'preload_crawl_homepage_links' => true,
70
71 // Front-end optimizations (1.7.0+)
72 'lazy_load_images' => false,
73 'lazy_load_iframes' => false,
74 'remove_query_strings' => false,
75 'defer_js' => false,
76 'defer_js_exclusions' => '',
77 'dns_prefetch' => '',
78 'preconnect' => '',
79
80 // Heartbeat (1.7.0+)
81 'heartbeat_control' => false,
82 'heartbeat_mode' => 'reduce',
83
84 // CDN (1.7.0+)
85 'cdn_enabled' => false,
86 'cdn_url' => '',
87
88 // Database cleanup (1.7.0+)
89 'db_cleanup_revisions' => false,
90 'db_cleanup_auto_drafts' => false,
91 'db_cleanup_trashed_posts' => false,
92 'db_cleanup_spam_comments' => false,
93 'db_cleanup_trashed_comments' => false,
94 'db_cleanup_expired_transients' => false,
95 'db_cleanup_orphan_postmeta' => false,
96 'db_optimize_tables' => false,
97 'db_cleanup_schedule' => 'never',
98
99 // Redis Object Cache (v2.2.0+)
100 'enable_redis_object_cache' => false,
101 'enable_redis_fullpage' => false,
102
103 // Critical CSS (v2.2.0+)
104 'enable_critical_css' => false,
105
106 // Speculative Loading (v2.2.0+)
107 'enable_speculative_loading' => false,
108 'speculative_mode' => 'moderate',
109
110 // 103 Early Hints (v2.2.0+)
111 'enable_early_hints' => false,
112 ];
113 }
114
115 /**
116 * Get the settings stored in the config file
117 * @return object
118 */
119 public static function get_settings() {
120
121 if ( self::$settings ) {
122 return self::$settings;
123 }
124
125 $file = self::settings_file_path();
126 $default_settings = self::get_default_settings();
127
128 if ( ! file_exists( $file ) || ! $json = json_decode( file_get_contents( $file ) ) ) {
129 self::$settings = $default_settings;
130 file_put_contents( $file, json_encode( self::$settings ) );
131
132 return self::$settings;
133 }
134
135 self::$settings = (object) array_merge( (array) $default_settings, (array) $json );
136 return self::$settings;
137 }
138
139 /**
140 * Merge new settings into the settings file
141 * @param array $new_settings
142 * @return object
143 */
144 public static function set_settings( $new_settings=[] ) {
145 $settings = self::get_settings();
146
147 foreach( $new_settings as $key => $value ) {
148 $settings->{$key} = $value;
149 }
150
151 self::maybe_update_cronjobs( $settings );
152
153 $file_path = self::settings_file_path();
154 $json = json_encode( $settings );
155 $written = file_put_contents( $file_path, $json );
156
157 if ( $written === false ) {
158 error_log( '[EzCache] Failed to write settings to ' . $file_path );
159 return new \WP_Error( 'ezcache_settings_write_failed', 'Failed to save settings file. Check file permissions.', [ 'status' => 500 ] );
160 }
161
162 self::$settings = $settings;
163
164 // Refresh schedules for the new modules.
165 if ( class_exists( '\\Upress\\EzCache\\Preload' ) ) {
166 Preload::instance()->maybe_schedule_cron();
167 }
168 if ( class_exists( '\\Upress\\EzCache\\DatabaseOptimizer' ) ) {
169 ( new DatabaseOptimizer() )->schedule();
170 }
171
172 return $settings;
173 }
174
175 /**
176 * @param $settings
177 */
178 public static function maybe_update_cronjobs( $settings ) {
179 if ( wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
180 wp_clear_scheduled_hook( 'ezcache_clear_expired_cache' );
181 }
182
183 if ( $settings->cache_lifetime > 0 ) {
184 $schedules = wp_get_schedules();
185 foreach( $schedules as $key=>$s ) {
186 if( $s['interval'] == $settings->cache_expiry_interval ) {
187 if ( ! wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
188 wp_schedule_event( time(), $key, 'ezcache_clear_expired_cache' );
189 }
190 break;
191 }
192 }
193 }
194 }
195 }
196