-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-warmup.php
More file actions
73 lines (58 loc) · 2.13 KB
/
cache-warmup.php
File metadata and controls
73 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* Plugin Name: WP Engine Cache Warmup
* Plugin URI: https://alphaparticle.com/
* Description: This plugin checks whether the cache has been cleared recently and, if so, uses your sitemap.xml file to loop through relevant URLs on your site and make sure they're added to the cache.
* Author: Alpha Particle
* Author URI: https://alphaparticle.com/
* Version: 1.0.0
* Text Domain: cache-warmup
* Domain Path: /languages
*/
namespace WPE_Cache_Warmup;
class Cache_Warmup {
public function __construct(){
require 'vendor/autoload.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/class-sitemap.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/class-crawler.php';
$this->sitemap = new Sitemap();
$this->crawler = new Crawler();
$this->_setup_hooks();
if( ! get_option( 'cache_last_warmed', false ) ) {
$now = gmdate( 'Y-m-d H:i:s' );
update_option( 'cache_last_warmed', $now);
}
//TO-DO: Admin notice for if this isn't a WP Engine install
//TO-DO: Admin notice if there doesn't appear to be a sitemap
//TO-DO: Option to specify custom sitemap URL
}
public function _setup_hooks(){
add_filter( 'cron_schedules', [ $this, 'add_five_minute_schedule' ] );
add_action( 'cache_warmup', [ $this, 'action_cache_warmup'] );
add_action( 'init', [ $this, 'setup_cron_job' ] );
}
public function setup_cron_job(){
if ( wp_get_schedule( 'cache_warmup' ) !== '5mins' ) {
if ( $time = wp_next_scheduled( 'cache_warmup' ) ) {
wp_unschedule_event( $time, 'cache_warmup' );
}
$response = wp_schedule_event( time(), '5mins', 'cache_warmup' );
}
}
public function add_five_minute_schedule( $schedules ) {
$schedules['5mins'] = array(
'interval' => 5 * 60,
'display' => __('Once every 5 minutes')
);
return $schedules;
}
public function action_cache_warmup() {
$cache_last_cleared = get_option('wpe_cache_last_cleared', true);
$cache_last_warmed = get_option('cache_last_warmed', true);
if($cache_last_cleared >= $cache_last_warmed) {
$links_to_crawl = $this->sitemap->parse();
$this->crawler->crawl( $links_to_crawl );
}
}
}
new Cache_Warmup();