PluginProbe
ezCache / 1.3.10
ezCache v1.3.10
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 1.3.10, at ezcache.php

245 lines 7.5 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 is an easy and innovative cache plugin that will help you significantly improve your site speed.
5 Plugin URI: https://ezcache.app
6 Version: 1.3.10
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 define( 'EZCACHE_DIR', __DIR__ );
35 define( 'EZCACHE_FILE', __FILE__ );
36 define( 'EZCACHE_URL', plugin_dir_url( __FILE__ ) );
37 define( 'EZCACHE_BASEBANE', basename( __FILE__ ) );
38 define( 'EZCACHE_VERSION', '1.3.10' );
39 define( 'EZCACHE_SETTINGS_KEY', 'ezcache' );
40
41 register_activation_hook( EZCACHE_FILE, 'upress_ezcache_activation_hook' );
42 register_deactivation_hook( EZCACHE_FILE, 'upress_ezcache_deactivation_hook' );
43
44 function upress_ezcache_activation_hook() {
45 $ezcache = Upress\EzCache\Plugin::initialize();
46 $ezcache->activation_hook();
47 }
48
49 function upress_ezcache_deactivation_hook() {
50 $ezcache = Upress\EzCache\Plugin::initialize();
51 $ezcache->deactivation_hook();
52 }
53 }
54
55 namespace Upress\EzCache {
56
57 use WP_Post;
58
59 class Plugin {
60 private static $instance;
61 public $plugin_dir;
62 public $plugin_file;
63 public $plugin_url;
64 public $plugin_basename;
65 public $plugin_version;
66 public $plugin_settings_key;
67 /** @var Cache $ezcache */
68 public $ezcache;
69
70 /**
71 * @return Plugin
72 */
73 public static function initialize() {
74 if ( ! self::$instance ) {
75 self::$instance = new self;
76 }
77
78 return self::$instance;
79 }
80
81
82 private function __construct() {
83 $this->plugin_dir = EZCACHE_DIR;
84 $this->plugin_file = EZCACHE_FILE;
85 $this->plugin_url = EZCACHE_URL;
86 $this->plugin_basename = EZCACHE_BASEBANE;
87 $this->plugin_version = EZCACHE_VERSION;
88 $this->plugin_settings_key = EZCACHE_SETTINGS_KEY;
89
90 $this->load_dependencies();
91 $this->initialize_plugin();
92 }
93
94 /**
95 * Import required files
96 */
97 protected function load_dependencies() {
98 require_once EZCACHE_DIR . '/vendor/autoload.php';
99 }
100
101 /**
102 * Init all the plugin parts
103 */
104 protected function initialize_plugin() {
105 $this->ezcache = Cache::instance();
106
107 add_action( 'init', [ $this, 'load_translation' ] );
108 add_action( 'edit_post', [ $this, 'maybe_clear_cache_on_post_update' ], 10, 2 );
109 add_filter( 'cron_schedules', [ $this, 'add_cron_schedules' ] );
110
111 new RestApi($this);
112 new Admin($this);
113
114 add_action( 'ezcache_clear_expired_cache', [ $this->ezcache, 'clear_expired_cache' ] );
115 }
116
117 function load_translation() {
118 load_textdomain( 'ezcache', sprintf( '%1$s/%2$s/%2$s-%3$s.mo', WP_LANG_DIR, 'ezcache', get_locale() ) );
119 load_plugin_textdomain( 'ezcache', false, basename( dirname( EZCACHE_FILE ) ) . '/languages' );
120 }
121
122 function get_wp_config_path() {
123 if ( file_exists( ABSPATH . 'wp-config.php') ) {
124 return ABSPATH . 'wp-config.php';
125 }
126
127 return dirname(ABSPATH) . '/wp-config.php';
128 }
129
130 function activation_hook() {
131 $path = $this->get_wp_config_path();
132 $contents = file_get_contents( $path );
133
134 // update wp-config define
135 if ( preg_match( '/define\s*?\(\s*?[\']WP_CACHE[\'"]/', $contents) ) {
136 $contents = preg_replace( '/define\s*?\(\s*?[\'"]WP_CACHE[\'"],\s*?(true|false)\s*?\)/', "define( 'WP_CACHE', true )", $contents );
137 } else {
138 $contents = preg_replace( '/(<\?php)/', "$1\ndefine( 'WP_CACHE', true );", $contents );
139 }
140
141 file_put_contents( $path, $contents );
142
143 // if the user currently has an advanced-cache file rename it as a backup
144 if ( file_exists( WP_CONTENT_DIR . '/advanced-cache.php' ) && false === strpos( file_get_contents( WP_CONTENT_DIR . '/advanced-cache.php' ), 'ezCache Advanced Cache' ) ) {
145 rename( WP_CONTENT_DIR . '/advanced-cache.php', WP_CONTENT_DIR . '/advanced-cache.php.ezcache-backup' );
146 }
147 // copy advanced-cache
148 copy( EZCACHE_DIR . '/advanced-cache.php', WP_CONTENT_DIR . '/advanced-cache.php' );
149
150 Settings::maybe_update_cronjobs( Settings::get_settings() );
151 Updater::upgrade();
152
153 $this->ezcache->preload_homepage();
154 }
155
156 function deactivation_hook() {
157 $path = $this->get_wp_config_path();
158 $contents = file_get_contents( $path );
159
160 // update wp-config define
161 // we don't need to do anything if the constant is not defined
162 if ( preg_match( '/define\s*?\(\s*?[\']WP_CACHE[\'"]/', $contents) ) {
163 $contents = preg_replace( '/define\s*?\(\s*?[\'"]WP_CACHE[\'"],\s*?(true|false)\s*?\)/', "define( 'WP_CACHE', false )", $contents );
164 }
165
166 file_put_contents( $path, $contents );
167
168 // delete advanced-cache
169 if ( file_exists( WP_CONTENT_DIR . '/advanced-cache.php' ) ) {
170 unlink( WP_CONTENT_DIR . '/advanced-cache.php' );
171 }
172 // if we have a backup of an older advanced-cache file rename it back
173 if ( file_exists( WP_CONTENT_DIR . '/advanced-cache.php.ezcache-backup' ) ) {
174 rename( WP_CONTENT_DIR . '/advanced-cache.php.ezcache-backup', WP_CONTENT_DIR . '/advanced-cache.php' );
175 }
176
177 if ( wp_next_scheduled ( 'ezcache_clear_expired_cache' ) ) {
178 wp_clear_scheduled_hook( 'ezcache_clear_expired_cache' );
179 }
180
181 delete_site_option( 'ezcache_first_run' );
182
183 $this->ezcache->clear_cache();
184 }
185
186 /**
187 * Runs when a post is created, updated, or a comment is left on it
188 * @param int $post_id
189 * @param WP_Post $post
190 */
191 function maybe_clear_cache_on_post_update( $post_id, $post ) {
192 $settings = Settings::get_settings();
193
194 if ( ! $settings->cache_clear_on_post_edit ) {
195 return;
196 }
197
198 $this->ezcache->clear_cache_single( $post_id );
199 }
200
201 function add_cron_schedules( $schedules ) {
202 if ( ! isset( $schedules['every_10_minutes'] ) ) {
203 $schedules['every_10_minutes'] = array(
204 'interval' => 600,
205 'display' => __( 'Every 10 Minutes', 'ezcache' ),
206 );
207 }
208 if ( ! isset( $schedules['hourly'] ) ) {
209 $schedules['hourly'] = array(
210 'interval' => 3600,
211 'display' => __( 'Hourly', 'ezcache' ),
212 );
213 }
214 if ( ! isset( $schedules['every_3_hours'] ) ) {
215 $schedules['every_3_hours'] = array(
216 'interval' => 10800,
217 'display' => __( 'Every 3 Hours', 'ezcache' ),
218 );
219 }
220 if ( ! isset( $schedules['every_6_hours'] ) ) {
221 $schedules['every_6_hours'] = array(
222 'interval' => 21600,
223 'display' => __( 'Every 6 Hours', 'ezcache' ),
224 );
225 }
226 if ( ! isset( $schedules['every_12_hours'] ) ) {
227 $schedules['every_12_hours'] = array(
228 'interval' => 43200,
229 'display' => __( 'Every 12 Hours', 'ezcache' ),
230 );
231 }
232 if ( ! isset( $schedules['daily'] ) ) {
233 $schedules['daily'] = array(
234 'interval' => 86400,
235 'display' => __( 'Daily', 'ezcache' ),
236 );
237 }
238
239 return $schedules;
240 }
241 }
242
243 Plugin::initialize();
244 }
245