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