PluginProbe
ezCache / 2.6.5
ezCache v2.6.5
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 / ezcache.php

ezcache.php in ezCache 2.6.5, at ezcache.php

324 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: ezCache
4 Description: 🚀 ezCache 2026 — Lightning-fast WordPress caching with CSS/JS optimization, WebP images, preloading, database cleanup &amp; more. Free &amp; Pro.
5 Plugin URI: https://ezcache-wp.com
6 Version: 2.6.5
7 Author: uPress
8 Author URI: https://www.upress.io
9 Text Domain: ezcache
10 Domain Path: /languages/
11 License: GPLv2 or later
12 License URI: http://www.gnu.org/licenses/gpl-2.0.html
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 namespace {
30 if ( ! defined( 'ABSPATH' ) ) {
31 die( 'NO direct access!' );
32 }
33
34 // Freemius SDK removed — Pro is unlocked for everyone, no licensing layer.
35 define( 'EZCACHE_DIR', __DIR__ );
36 define( 'EZCACHE_FILE', __FILE__ );
37 define( 'EZCACHE_URL', plugin_dir_url( __FILE__ ) );
38 define( 'EZCACHE_BASEBANE', basename( __FILE__ ) );
39 define( 'EZCACHE_VERSION', '2.6.5' );
40 define( 'EZCACHE_SETTINGS_KEY', 'ezcache' );
41
42 register_activation_hook( EZCACHE_FILE, 'upress_ezcache_activation_hook' );
43 register_deactivation_hook( EZCACHE_FILE, 'upress_ezcache_deactivation_hook' );
44
45 function upress_ezcache_activation_hook() {
46 $ezcache = Upress\EzCache\Plugin::initialize();
47 $ezcache->activation_hook();
48 }
49
50 function upress_ezcache_deactivation_hook() {
51 $ezcache = Upress\EzCache\Plugin::initialize();
52 $ezcache->deactivation_hook();
53 }
54 }
55
56 namespace Upress\EzCache {
57 class Plugin {
58 private static $instance;
59 public $plugin_dir;
60 public $plugin_file;
61 public $plugin_url;
62 public $plugin_basename;
63 public $plugin_version;
64 public $plugin_settings_key;
65 /** @var Cache $ezcache */
66 public $ezcache;
67
68 /**
69 * @return Plugin
70 */
71 public static function initialize() {
72 if ( ! self::$instance ) {
73 self::$instance = new self;
74 }
75
76 return self::$instance;
77 }
78
79
80 private function __construct() {
81 $this->plugin_dir = EZCACHE_DIR;
82 $this->plugin_file = EZCACHE_FILE;
83 $this->plugin_url = EZCACHE_URL;
84 $this->plugin_basename = EZCACHE_BASEBANE;
85 $this->plugin_version = EZCACHE_VERSION;
86 $this->plugin_settings_key = EZCACHE_SETTINGS_KEY;
87
88 $this->load_dependencies();
89 $this->initialize_plugin();
90 }
91
92 /**
93 * Import required files
94 */
95 protected function load_dependencies() {
96 require_once EZCACHE_DIR . '/vendor/autoload.php';
97 }
98
99 /**
100 * Init all the plugin parts
101 */
102 protected function initialize_plugin() {
103 $this->ezcache = Cache::instance();
104
105 add_action( 'init', [ $this, 'load_translation' ] );
106 add_filter( 'cron_schedules', [ $this, 'add_cron_schedules' ] );
107 add_action( 'init', [ $this, 'maybe_repair_installation' ] );
108 // Self-heal DB tables after file-replacement updates (no activation hook).
109 // maybe_upgrade() runs version migrations; ensure_tables() runs unconditionally
110 // so a site already on the current version but missing a table still recovers.
111 add_action( 'admin_init', [ '\Upress\EzCache\Updater', 'maybe_upgrade' ] );
112 add_action( 'admin_init', [ '\Upress\EzCache\Updater', 'ensure_tables' ] );
113
114 // Background WebP conversion cron — resilient, self-rescheduling queue drain.
115 add_action( 'ezcache_webp_process_batch', [ '\Upress\EzCache\Rest\WebpController', 'cron_process' ] );
116
117 new RestApi( $this );
118 new Admin( $this );
119 new AdvancedSettingsPage();
120 new Optimizations();
121 Preload::instance();
122
123 // v2.2.0 features
124 RedisObjectCache::init();
125 SpeculativeLoading::instance()->register();
126 EarlyHints::instance()->register();
127 CriticalCSS::instance()->register();
128
129 add_action( 'ezcache_clear_expired_cache', [ $this->ezcache, 'clear_expired_cache' ] );
130 add_action( 'ezcache_db_cleanup', function () {
131 ( new DatabaseOptimizer() )->clean();
132 } );
133 }
134
135 function load_translation() {
136 $locale = apply_filters( 'plugin_locale', get_locale(), 'ezcache' );
137 load_textdomain( 'ezcache', EZCACHE_DIR . "/languages/ezcache-{$locale}.mo" );
138 }
139
140 function get_wp_config_path() {
141 if ( file_exists( ABSPATH . 'wp-config.php' ) ) {
142 return ABSPATH . 'wp-config.php';
143 }
144
145 return dirname( ABSPATH ) . '/wp-config.php';
146 }
147
148 function activation_hook() {
149 set_transient( 'ezcache_activating', true );
150
151 // copy advanced-cache
152 copy( EZCACHE_DIR . '/advanced-cache.php', WP_CONTENT_DIR . '/advanced-cache.php' );
153
154 if ( ! wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
155 Settings::maybe_update_cronjobs( Settings::get_settings() );
156 }
157 Updater::upgrade();
158
159 $this->update_wp_config_const( [ 'WP_CACHE' => true ] );
160
161 $this->ezcache->preload_homepage();
162
163 delete_transient( 'ezcache_activating' );
164 }
165
166 function deactivation_hook() {
167 set_transient( 'ezcache_deactivating', true );
168
169 // delete advanced-cache
170 if ( file_exists( WP_CONTENT_DIR . '/advanced-cache.php' ) ) {
171 unlink( WP_CONTENT_DIR . '/advanced-cache.php' );
172 }
173
174 if ( wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
175 wp_clear_scheduled_hook( 'ezcache_clear_expired_cache' );
176 }
177
178 delete_site_option( 'ezcache_first_run' );
179
180 $this->update_wp_config_const( [ 'WP_CACHE' => false ] );
181
182 $this->ezcache->clear_cache();
183
184 delete_transient( 'ezcache_deactivating' );
185 }
186
187 /**
188 * Update the wp-config.php file with the specified values
189 *
190 * @param array $args Assoc array of key-value, key is the define to update with the value
191 */
192 function update_wp_config_const( $args ) {
193 if ( ! is_array( $args ) ) {
194 return;
195 }
196
197 $wp_config_path = $this->get_wp_config_path();
198
199 copy( $wp_config_path, $wp_config_path . '.backup' );
200
201 $contents = file_get_contents( $wp_config_path );
202 $contents = preg_replace( "/\r\n|\r|\n/", "\n", $contents ); // normalize line-breaks
203
204 // update wp-config define
205 foreach ( $args as $const => $value ) {
206 if ( is_string( $value ) ) {
207 $value = "'" . str_replace( "'", "\'", $value ) . "'";
208 } elseif ( is_bool( $value ) ) {
209 $value = $value ? 'true' : 'false'; // simply casting to string will result in '1' and ''
210 } else {
211 continue;
212 }
213
214 if ( preg_match( '/define\s*?\(\s*?[\']' . $const . '[\'"]/', $contents ) ) {
215 $contents = preg_replace( '/define\s*?\(\s*?([\'"])' . $const . '\1,\s*(([\'"]).+\3|.+?)\s*\)/', "define( '{$const}', {$value} )", $contents );
216 } else {
217 $contents = preg_replace( '/(<\?php)/', "$1\ndefine( '{$const}', {$value} );", $contents );
218 }
219 }
220
221 if ( "\n" !== PHP_EOL ) {
222 // update line-breaks to platform defaults
223 $contents = preg_replace( "/\n/", PHP_EOL, $contents );
224 }
225
226 file_put_contents( $wp_config_path, trim( $contents ) );
227
228 if ( filesize( $wp_config_path ) <= 0 ) {
229 // operation failed, revert the file
230 copy( $wp_config_path . '.backup', $wp_config_path );
231 }
232
233 unlink( $wp_config_path . '.backup' );
234 }
235
236 function add_cron_schedules( $schedules ) {
237 if ( ! isset( $schedules['every_10_minutes'] ) ) {
238 $schedules['every_10_minutes'] = [
239 'interval' => 600,
240 'display' => __( 'Every 10 Minutes', 'ezcache' ),
241 ];
242 }
243 if ( ! isset( $schedules['hourly'] ) ) {
244 $schedules['hourly'] = [
245 'interval' => 3600,
246 'display' => __( 'Hourly', 'ezcache' ),
247 ];
248 }
249 if ( ! isset( $schedules['every_3_hours'] ) ) {
250 $schedules['every_3_hours'] = [
251 'interval' => 10800,
252 'display' => __( 'Every 3 Hours', 'ezcache' ),
253 ];
254 }
255 if ( ! isset( $schedules['every_6_hours'] ) ) {
256 $schedules['every_6_hours'] = [
257 'interval' => 21600,
258 'display' => __( 'Every 6 Hours', 'ezcache' ),
259 ];
260 }
261 if ( ! isset( $schedules['every_12_hours'] ) ) {
262 $schedules['every_12_hours'] = [
263 'interval' => 43200,
264 'display' => __( 'Every 12 Hours', 'ezcache' ),
265 ];
266 }
267 if ( ! isset( $schedules['daily'] ) ) {
268 $schedules['daily'] = [
269 'interval' => 86400,
270 'display' => __( 'Daily', 'ezcache' ),
271 ];
272 }
273 if ( ! isset( $schedules['every_3_days'] ) ) {
274 $schedules['every_3_days'] = [
275 'interval' => 259200,
276 'display' => __( 'Every 3 Days', 'ezcache' ),
277 ];
278 }
279 if ( ! isset( $schedules['every_5_days'] ) ) {
280 $schedules['every_5_days'] = [
281 'interval' => 432000,
282 'display' => __( 'Every 5 Days', 'ezcache' ),
283 ];
284 }
285 if ( ! isset( $schedules['every_7_days'] ) ) {
286 $schedules['every_7_days'] = [
287 'interval' => 604800,
288 'display' => __( 'Every 7 Days', 'ezcache' ),
289 ];
290 }
291
292 return $schedules;
293 }
294
295 /**
296 * Repaire the plugin installation when POST variable is set
297 */
298 function maybe_repair_installation() {
299 $repaired = get_transient( 'ezcache_repaired' );
300 delete_transient( 'ezcache_repaired' );
301
302 if ( $repaired || get_transient( 'ezcache_deactivating' ) || get_transient( 'ezcache_activating' ) ) {
303 return;
304 }
305
306 if ( ! wp_next_scheduled( 'ezcache_clear_expired_cache' ) ) {
307 Settings::maybe_update_cronjobs( Settings::get_settings() );
308 }
309
310 $status = $this->ezcache->get_status();
311 if ( $status['cache_enabled'] && $status['adv_cache_exists'] && $status['adv_cache_exists'] && $status['correct_cache_exists'] && $status['webp_table_exists'] ) {
312 return;
313 }
314
315 $this->activation_hook();
316
317 set_transient( 'ezcache_repaired', true );
318 }
319 }
320
321 add_action( 'plugins_loaded', '\Upress\EzCache\Plugin::initialize' );
322 add_action( 'plugins_loaded', function() { \Upress\EzCache\PremiumFeatures::maybe_start_trial(); }, 20 );
323 }
324