PluginProbe
ezCache / 1.2.4
ezCache v1.2.4
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 1.2.4, at includes/Settings.php

117 lines 3.1 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 protected 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 'separate_mobile_cache' => true,
20 'cache_lifetime' => 86400,
21 'cache_expiry_interval' => 3600,
22
23 'optimize_google_fonts' => false,
24 'minify_html' => false,
25 'minify_inline_js' => false,
26 'minify_inline_css' => false,
27 'minify_js' => false,
28 'combine_head_js' => false,
29 'combine_body_js' => false,
30 'minify_css' => false,
31 'combine_css' => false,
32 'enable_webp_support' => true,
33
34 'no_cache_query_params' => false,
35 'cache_clear_on_post_edit' => true,
36 'bypass_cache' => [
37 'single' => false,
38 'pages' => false,
39 'frontpage' => false,
40 'home' => false,
41 'archives' => false,
42 'tag' => false,
43 'category' => false,
44 'feed' => false,
45 'search' => false,
46 'author' => false,
47 ],
48 'rejected_uri' => "/cart\n/checkout",
49 'rejected_user_agent' => '',
50 ];
51 }
52
53 /**
54 * Get the settings stored in the config file
55 * @return object
56 */
57 public static function get_settings() {
58
59 if ( self::$settings ) {
60 return self::$settings;
61 }
62
63 $file = self::settings_file_path();
64 $default_settings = self::get_default_settings();
65
66 if ( ! file_exists( $file ) || ! $json = json_decode( file_get_contents( $file ) ) ) {
67 self::$settings = $default_settings;
68 file_put_contents( $file, json_encode( self::$settings ) );
69
70 return self::$settings;
71 }
72
73 self::$settings = (object) array_merge( (array) $default_settings, (array) $json );
74 return self::$settings;
75 }
76
77 /**
78 * Merge new settings into the settings file
79 * @param array $new_settings
80 * @return object
81 */
82 public static function set_settings( $new_settings=[] ) {
83 $settings = self::get_settings();
84
85 foreach( $new_settings as $key => $value ) {
86 $settings->{$key} = $value;
87 }
88
89 self::maybe_update_cronjobs( $settings );
90
91 file_put_contents( self::settings_file_path(), json_encode( $settings ) );
92 self::$settings = $settings;
93 return $settings;
94 }
95
96 /**
97 * @param $settings
98 */
99 public static function maybe_update_cronjobs( $settings ) {
100 if ( wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
101 wp_clear_scheduled_hook( 'ezcache_clear_expired_cache' );
102 }
103
104 if ( $settings->cache_lifetime > 0 ) {
105 $schedules = wp_get_schedules();
106 foreach( $schedules as $key=>$s ) {
107 if( $s['interval'] == $settings->cache_expiry_interval ) {
108 if ( ! wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
109 wp_schedule_event( time(), $key, 'ezcache_clear_expired_cache' );
110 }
111 break;
112 }
113 }
114 }
115 }
116 }
117