| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Cache Warmer |
| 4 |
* Description: Visits website pages to warm (create) the cache if you have any caching solutions configured. |
| 5 |
* Version: 1.3.7 |
| 6 |
* Text Domain: cache-warmer |
| 7 |
* Author: TMM Technology |
| 8 |
* Author URI: https://tmm.ventures/ |
| 9 |
* Requires PHP: 7.4 |
| 10 |
* License: GPLv3 |
| 11 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 12 |
* |
| 13 |
* @package Cache-Warmer |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace Cache_Warmer; |
| 17 |
|
| 18 |
/** |
| 19 |
* Main Cache_Warmer class. |
| 20 |
*/ |
| 21 |
final class Cache_Warmer { |
| 22 |
|
| 23 |
/** |
| 24 |
* Cache warmer process hook name. |
| 25 |
*/ |
| 26 |
const HOOK_NAME = 'cache_warmer_process'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Cache warmer process hook name for interval. |
| 30 |
*/ |
| 31 |
const INTERVAL_HOOK_NAME = 'cache_warmer_process_interval'; |
| 32 |
|
| 33 |
/** |
| 34 |
* AJAX class. |
| 35 |
* |
| 36 |
* @var AJAX |
| 37 |
*/ |
| 38 |
public static $ajax; |
| 39 |
|
| 40 |
/** |
| 41 |
* Options. |
| 42 |
* |
| 43 |
* @var Options |
| 44 |
*/ |
| 45 |
public static $options; |
| 46 |
|
| 47 |
/** |
| 48 |
* Plugin name. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
public static $name; |
| 53 |
|
| 54 |
/** |
| 55 |
* Plugin slug. |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
public static $slug; |
| 60 |
|
| 61 |
/** |
| 62 |
* Plugin version. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
public static $version; |
| 67 |
|
| 68 |
/** |
| 69 |
* The list of extensions that are polyfilled and therefore not required to run the plugin. |
| 70 |
* |
| 71 |
* Some of them can be fakely polyfilled (just to allow to start the plugin). |
| 72 |
* |
| 73 |
* @var string[] |
| 74 |
*/ |
| 75 |
public static $polyfilled_extensions = [ |
| 76 |
// Faked. |
| 77 |
'simplexml', |
| 78 |
]; |
| 79 |
|
| 80 |
/** |
| 81 |
* Constructor. |
| 82 |
*/ |
| 83 |
public function __construct() { |
| 84 |
$this->define_constants(); |
| 85 |
$this->import_plugin_files(); |
| 86 |
|
| 87 |
// Schedule intervals. |
| 88 |
new Intervals_Scheduler(); |
| 89 |
|
| 90 |
add_action( |
| 91 |
'plugins_loaded', |
| 92 |
function() { |
| 93 |
new \WP_Plugins_Core\WP_Plugins_Core( $this, false ); |
| 94 |
|
| 95 |
// Load translations. |
| 96 |
$this->load_plugin_textdomain(); |
| 97 |
|
| 98 |
// Options. |
| 99 |
self::$options = new Options(); |
| 100 |
|
| 101 |
// Handle version update. |
| 102 |
$this->handle_version_update(); |
| 103 |
|
| 104 |
// The plugin is being updated notice. |
| 105 |
if ( self::$version !== self::$options->get( 'last-handled-version-update' ) ) { |
| 106 |
add_action( |
| 107 |
'admin_notices', |
| 108 |
function() { |
| 109 |
?> |
| 110 |
<div class="notice notice-info"> |
| 111 |
<p> |
| 112 |
<b><?php echo esc_html( self::$name ); ?>: </b> |
| 113 |
<?php |
| 114 |
esc_html_e( |
| 115 |
'The plugin is being updated in the background.', |
| 116 |
'cache-warmer' |
| 117 |
); |
| 118 |
?> |
| 119 |
</p> |
| 120 |
</div> |
| 121 |
<?php |
| 122 |
} |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
// Interval. |
| 127 |
new Interval(); |
| 128 |
|
| 129 |
// Posts Warming Interval. |
| 130 |
new Posts_Warming_Interval(); |
| 131 |
|
| 132 |
// Clear old actions. |
| 133 |
new Clear_Old_Actions(); |
| 134 |
|
| 135 |
// Assets. |
| 136 |
new Assets\Assets(); |
| 137 |
|
| 138 |
// Post publish box. |
| 139 |
new Representation\Publish_Box(); |
| 140 |
|
| 141 |
// Server IP detection logic. |
| 142 |
new Server_IP_Detection(); |
| 143 |
|
| 144 |
// Adds dashboard widget. |
| 145 |
add_action( 'wp_dashboard_setup', [ $this, 'setup_dashboard_widget' ] ); |
| 146 |
|
| 147 |
// Adds plugin settings links to plugins admin screen. |
| 148 |
add_filter( 'plugin_action_links_' . CACHE_WARMER_BASENAME, [ $this, 'plugin_action_links' ] ); |
| 149 |
|
| 150 |
add_action( self::HOOK_NAME, [ 'Cache_Warmer\Warm_Up', 'process' ] ); |
| 151 |
add_action( self::INTERVAL_HOOK_NAME, [ 'Cache_Warmer\AJAX', 'start_warm_up' ] ); |
| 152 |
|
| 153 |
add_action( |
| 154 |
'init', |
| 155 |
function () { |
| 156 |
|
| 157 |
// Posts enqueue. |
| 158 |
new Posts_Enqueue(); |
| 159 |
|
| 160 |
// External warmer. |
| 161 |
new External_Warmer(); |
| 162 |
|
| 163 |
// Menu. |
| 164 |
new Admin_Menu(); |
| 165 |
|
| 166 |
// AJAX Handler. |
| 167 |
self::$ajax = new AJAX(); |
| 168 |
|
| 169 |
// Extend WP CLI. |
| 170 |
new Extend_WP_CLI(); |
| 171 |
} |
| 172 |
); |
| 173 |
} |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Defines constants. |
| 179 |
*/ |
| 180 |
private function define_constants() { |
| 181 |
require_once __DIR__ . '/data/constants.php'; |
| 182 |
|
| 183 |
/** |
| 184 |
* Plugin name. |
| 185 |
*/ |
| 186 |
self::$name = get_file_data( CACHE_WARMER_FILE, [ 'Plugin Name' ] )[0]; |
| 187 |
|
| 188 |
/** |
| 189 |
* Plugin slug. |
| 190 |
*/ |
| 191 |
$dir_parts = explode( DIRECTORY_SEPARATOR, CACHE_WARMER_DIR ); |
| 192 |
self::$slug = $dir_parts[ array_search( 'plugins', $dir_parts, true ) + 1 ]; |
| 193 |
|
| 194 |
/** |
| 195 |
* Plugin version. |
| 196 |
*/ |
| 197 |
self::$version = CACHE_WARMER_VERSION; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Imports plugin files. |
| 202 |
*/ |
| 203 |
private function import_plugin_files() { |
| 204 |
$src_files = [ |
| 205 |
'assets/class-assets', |
| 206 |
'assets/screens/class-assets-logs-screen', |
| 207 |
'assets/screens/class-assets-main-screen', |
| 208 |
'assets/screens/class-assets-settings-screen', |
| 209 |
'assets/screens/class-assets-post-screen', |
| 210 |
'assets/screens/class-assets-dashboard', |
| 211 |
'class-admin-menu', |
| 212 |
'class-warm-up', |
| 213 |
'class-ajax', |
| 214 |
'class-summary', |
| 215 |
'class-logging', |
| 216 |
'class-databases', |
| 217 |
'class-options', |
| 218 |
'class-settings-export', |
| 219 |
'class-interval', |
| 220 |
'class-content-parsing', |
| 221 |
'class-tree', |
| 222 |
'class-leaf-only-subtree', |
| 223 |
'class-url-formatting', |
| 224 |
'class-url-parsing', |
| 225 |
'class-url-validation', |
| 226 |
'class-debug', |
| 227 |
'class-utils', |
| 228 |
'class-object-cache', |
| 229 |
'class-server-ip-detection', |
| 230 |
'class-migrations', |
| 231 |
'class-clear-old-actions', |
| 232 |
'class-external-warmer', |
| 233 |
'class-intervals-scheduler', |
| 234 |
'class-extend-wp-cli', |
| 235 |
'posts-warming/class-posts-enqueue', |
| 236 |
'posts-warming/class-posts-warming-interval', |
| 237 |
'integrations/class-cache-plugins-integration', |
| 238 |
'integrations/class-wp-super-cache', |
| 239 |
'representation/class-publish-box', |
| 240 |
]; |
| 241 |
foreach ( $src_files as $file ) { |
| 242 |
require_once CACHE_WARMER_DIR . 'src/' . $file . '.php'; |
| 243 |
} |
| 244 |
|
| 245 |
$files = [ |
| 246 |
'libs/phpuri/phpuri', |
| 247 |
'vendor/autoload_packages', |
| 248 |
'vendor/tmmtech/wp-plugins-core/wp-plugins-core', |
| 249 |
'vendor/woocommerce/action-scheduler/action-scheduler', |
| 250 |
]; |
| 251 |
foreach ( $files as $file ) { |
| 252 |
require_once CACHE_WARMER_DIR . $file . '.php'; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Loads textdomain. |
| 258 |
*/ |
| 259 |
private function load_plugin_textdomain() { |
| 260 |
load_plugin_textdomain( |
| 261 |
'cache-warmer', |
| 262 |
false, |
| 263 |
dirname( CACHE_WARMER_BASENAME ) . '/languages' |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Show settings link on the plugin screen. |
| 269 |
* |
| 270 |
* @param mixed $links Plugin Action links. |
| 271 |
* |
| 272 |
* @return array |
| 273 |
*/ |
| 274 |
public function plugin_action_links( $links ) { |
| 275 |
$action_links = array( |
| 276 |
'settings' => '<a href="' . admin_url( 'admin.php?page=cache-warmer-settings' ) . '" aria-label="' . |
| 277 |
esc_attr__( 'View Cache Warmer settings', 'cache-warmer' ) . '">' . esc_html__( 'Settings', 'cache-warmer' ) . '</a>', |
| 278 |
); |
| 279 |
|
| 280 |
return array_merge( $action_links, $links ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Handles version update. |
| 285 |
*/ |
| 286 |
private function handle_version_update() { |
| 287 |
if ( time() - MINUTE_IN_SECONDS > (int) get_option( 'cache-warmer-updating' ) ) { |
| 288 |
$last_handled_version_update = self::$options->get( 'last-handled-version-update' ); |
| 289 |
if ( self::$version !== $last_handled_version_update ) { |
| 290 |
update_option( 'cache-warmer-updating', time() ); |
| 291 |
|
| 292 |
DB::do_migrations(); |
| 293 |
new Migrations( $last_handled_version_update ); |
| 294 |
|
| 295 |
self::$options->set( 'last-handled-version-update', self::$version ); |
| 296 |
delete_option( 'cache-warmer-updating' ); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Adds the dashboard metrics widget. |
| 303 |
*/ |
| 304 |
public function setup_dashboard_widget() { |
| 305 |
wp_add_dashboard_widget( |
| 306 |
'dashboard_cache_warmer', |
| 307 |
__( 'Cache Warmer', 'cache-warmer' ), |
| 308 |
[ $this, 'show_dashboard_widget' ], |
| 309 |
null, |
| 310 |
null, |
| 311 |
'normal', |
| 312 |
'high' |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Displays the dashboard widget |
| 318 |
* |
| 319 |
* @return void |
| 320 |
*/ |
| 321 |
public function show_dashboard_widget() { |
| 322 |
require_once CACHE_WARMER_DIR . 'src/templates/admin/widget.php'; |
| 323 |
} |
| 324 |
} |
| 325 |
new Cache_Warmer(); |
| 326 |
|