PluginProbe
ezCache / 2.5.2
ezCache v2.5.2
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.5.2, at includes/Settings.php

193 lines 5.8 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 'cache_clear_on_post_edit' => true,
43 'cache_clear_home_on_post_edit' => true,
44 'bypass_cache' => [
45 'single' => false,
46 'pages' => false,
47 'frontpage' => false,
48 'home' => false,
49 'archives' => false,
50 'tag' => false,
51 'category' => false,
52 'feed' => false,
53 'search' => false,
54 'author' => false,
55 ],
56 'rejected_uri' => "/cart\n/checkout",
57 'rejected_user_agent' => '',
58 'rejected_cookies' => '',
59 'excluded_minify_files' => '',
60
61 // Preload (1.7.0+)
62 'enable_preload' => false,
63 'preload_on_cache_clear' => true,
64 'preload_sitemap_url' => '',
65 'preload_batch_size' => 5,
66 'preload_crawl_homepage_links' => true,
67
68 // Front-end optimizations (1.7.0+)
69 'lazy_load_images' => false,
70 'lazy_load_iframes' => false,
71 'remove_query_strings' => false,
72 'defer_js' => false,
73 'defer_js_exclusions' => '',
74 'dns_prefetch' => '',
75 'preconnect' => '',
76
77 // Heartbeat (1.7.0+)
78 'heartbeat_control' => false,
79 'heartbeat_mode' => 'reduce',
80
81 // CDN (1.7.0+)
82 'cdn_enabled' => false,
83 'cdn_url' => '',
84
85 // Database cleanup (1.7.0+)
86 'db_cleanup_revisions' => false,
87 'db_cleanup_auto_drafts' => false,
88 'db_cleanup_trashed_posts' => false,
89 'db_cleanup_spam_comments' => false,
90 'db_cleanup_trashed_comments' => false,
91 'db_cleanup_expired_transients' => false,
92 'db_cleanup_orphan_postmeta' => false,
93 'db_optimize_tables' => false,
94 'db_cleanup_schedule' => 'never',
95
96 // Redis Object Cache (v2.2.0+)
97 'enable_redis_object_cache' => false,
98 'enable_redis_fullpage' => false,
99
100 // Critical CSS (v2.2.0+)
101 'enable_critical_css' => false,
102
103 // Speculative Loading (v2.2.0+)
104 'enable_speculative_loading' => false,
105 'speculative_mode' => 'moderate',
106
107 // 103 Early Hints (v2.2.0+)
108 'enable_early_hints' => false,
109 ];
110 }
111
112 /**
113 * Get the settings stored in the config file
114 * @return object
115 */
116 public static function get_settings() {
117
118 if ( self::$settings ) {
119 return self::$settings;
120 }
121
122 $file = self::settings_file_path();
123 $default_settings = self::get_default_settings();
124
125 if ( ! file_exists( $file ) || ! $json = json_decode( file_get_contents( $file ) ) ) {
126 self::$settings = $default_settings;
127 file_put_contents( $file, json_encode( self::$settings ) );
128
129 return self::$settings;
130 }
131
132 self::$settings = (object) array_merge( (array) $default_settings, (array) $json );
133 return self::$settings;
134 }
135
136 /**
137 * Merge new settings into the settings file
138 * @param array $new_settings
139 * @return object
140 */
141 public static function set_settings( $new_settings=[] ) {
142 $settings = self::get_settings();
143
144 foreach( $new_settings as $key => $value ) {
145 $settings->{$key} = $value;
146 }
147
148 self::maybe_update_cronjobs( $settings );
149
150 $file_path = self::settings_file_path();
151 $json = json_encode( $settings );
152 $written = file_put_contents( $file_path, $json );
153
154 if ( $written === false ) {
155 error_log( '[EzCache] Failed to write settings to ' . $file_path );
156 return new \WP_Error( 'ezcache_settings_write_failed', 'Failed to save settings file. Check file permissions.', [ 'status' => 500 ] );
157 }
158
159 self::$settings = $settings;
160
161 // Refresh schedules for the new modules.
162 if ( class_exists( '\\Upress\\EzCache\\Preload' ) ) {
163 Preload::instance()->maybe_schedule_cron();
164 }
165 if ( class_exists( '\\Upress\\EzCache\\DatabaseOptimizer' ) ) {
166 ( new DatabaseOptimizer() )->schedule();
167 }
168
169 return $settings;
170 }
171
172 /**
173 * @param $settings
174 */
175 public static function maybe_update_cronjobs( $settings ) {
176 if ( wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
177 wp_clear_scheduled_hook( 'ezcache_clear_expired_cache' );
178 }
179
180 if ( $settings->cache_lifetime > 0 ) {
181 $schedules = wp_get_schedules();
182 foreach( $schedules as $key=>$s ) {
183 if( $s['interval'] == $settings->cache_expiry_interval ) {
184 if ( ! wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
185 wp_schedule_event( time(), $key, 'ezcache_clear_expired_cache' );
186 }
187 break;
188 }
189 }
190 }
191 }
192 }
193