| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Page caching rules and exceptions |
| 7 |
*/ |
| 8 |
|
| 9 |
if (!class_exists('WPO_Cache_Config')) require_once('class-wpo-cache-config.php'); |
| 10 |
|
| 11 |
if (!class_exists('WPO_Cache_Rules')) : |
| 12 |
|
| 13 |
class WPO_Cache_Rules { |
| 14 |
|
| 15 |
/** |
| 16 |
* Cache config object |
| 17 |
* |
| 18 |
* @var mixed |
| 19 |
*/ |
| 20 |
public $config; |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance of this class |
| 24 |
* |
| 25 |
* @var mixed |
| 26 |
*/ |
| 27 |
public static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Class constructor |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$this->config = WPO_Cache_Config::instance()->get(); |
| 34 |
$this->setup_hooks(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Setup hooks/filters |
| 39 |
*/ |
| 40 |
public function setup_hooks() { |
| 41 |
add_action('pre_post_update', array($this, 'purge_post_on_update'), 10, 1); |
| 42 |
add_action('save_post', array($this, 'purge_post_on_update'), 10, 1); |
| 43 |
add_action('wp_trash_post', array($this, 'purge_post_on_update'), 10, 1); |
| 44 |
add_action('wp_set_comment_status', array($this, 'purge_post_on_comment_status_change'), 10); |
| 45 |
add_action('set_comment_cookies', array($this, 'set_comment_cookie_exceptions'), 10); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* When user posts a comment, set a cookie so we don't show them page cache |
| 50 |
* |
| 51 |
* @param WP_Comment $comment Comment to check. |
| 52 |
*/ |
| 53 |
public function set_comment_cookie_exceptions($comment) { |
| 54 |
|
| 55 |
if (empty($this->config['enable_page_caching'])) return; |
| 56 |
|
| 57 |
$path = $this->get_post_path($comment->comment_post_ID); |
| 58 |
|
| 59 |
$this->purge_from_cache($path); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Purge files for a particular path from the cache |
| 64 |
* |
| 65 |
* @param String $path - the path |
| 66 |
*/ |
| 67 |
public function purge_from_cache($path) { |
| 68 |
WPO_Page_Cache::delete(untrailingslashit($path) . '/index.html'); |
| 69 |
WPO_Page_Cache::delete(untrailingslashit($path) . '/index.gzip.html'); |
| 70 |
|
| 71 |
if (!empty($this->config['enable_mobile_caching'])) { |
| 72 |
WPO_Page_Cache::delete(untrailingslashit($path) . '/mobile.index.html'); |
| 73 |
WPO_Page_Cache::delete(untrailingslashit($path) . '/mobile.index.gzip.html'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Get the cache path for a given post |
| 80 |
* |
| 81 |
* @param Integer $post_id - WP post ID |
| 82 |
* |
| 83 |
* @return String |
| 84 |
*/ |
| 85 |
private function get_post_path($post_id) { |
| 86 |
return WPO_CACHE_DIR . preg_replace('#^https?://#i', '', get_permalink($post_id)); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Every time a comment's status changes, purge it's parent posts cache |
| 91 |
* |
| 92 |
* @param Integer $comment_id Comment ID. |
| 93 |
*/ |
| 94 |
public function purge_post_on_comment_status_change($comment_id) { |
| 95 |
|
| 96 |
if (empty($this->config['enable_page_caching'])) return; |
| 97 |
|
| 98 |
$comment = get_comment($comment_id); |
| 99 |
|
| 100 |
$path = $this->get_post_path($comment->comment_post_ID); |
| 101 |
|
| 102 |
$this->purge_from_cache($path); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Automatically purge all file based page cache on post changes |
| 108 |
* We want the whole cache purged here as different parts |
| 109 |
* of the site could potentially change on post updates |
| 110 |
* |
| 111 |
* @param Integer $post_id WordPress post id |
| 112 |
*/ |
| 113 |
public function purge_post_on_update($post_id) { |
| 114 |
$post_type = get_post_type($post_id); |
| 115 |
|
| 116 |
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type) { |
| 117 |
return; |
| 118 |
} elseif (!current_user_can('edit_post', $post_id) && (!defined('DOING_CRON') || !DOING_CRON)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
if (!empty($this->config['enable_page_caching'])) { |
| 123 |
wpo_cache_flush(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns an instance of the current class, creates one if it doesn't exist |
| 129 |
* |
| 130 |
* @return WPO_Cache_Rules |
| 131 |
*/ |
| 132 |
public static function instance() { |
| 133 |
if (empty(self::$instance)) { |
| 134 |
self::$instance = new self(); |
| 135 |
} |
| 136 |
return self::$instance; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
endif; |
| 141 |
|