PluginProbe
ezCache / 2.5.6
ezCache v2.5.6
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.6, at includes/Settings.php

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