Commit 2feae9d4 authored by Rolf Forst's avatar Rolf Forst
Browse files

Add Cache class (Main class refactoring)

parent 63207ff2
<?php
namespace RRZE\Cache;
defined('ABSPATH') || exit;
class Cache
{
/**
* [protected description]
* @var object
*/
protected $options;
/**
* [protected description]
* @var object
*/
protected $siteOptions;
/**
* [__construct description]
*/
public function __construct()
{
$this->options = Options::getOptions();
$this->siteOptions = Options::getSiteOptions();
}
public function onLoaded()
{
$this->setCacheFiles();
add_action('init', [$this, 'add_publish_hooks'], 99);
add_action('pre_comment_approved', [$this, 'pre_comment_approved'], 99, 2);
add_action('transition_comment_status', [$this, 'transition_comment_status'], 10, 3);
add_action('edit_comment', [$this, 'edit_comment']);
add_action('post_submitbox_misc_actions', [$this, 'post_cache_submitbox'], 99);
add_action('template_redirect', [$this, 'template_redirect'], 0);
add_action('rrzecache_flush_cache_post_id', [__NAMESPACE__ . '\Flush', 'flushCacheByPostId']);
add_action('rrzecache_flush_cache_url', [__NAMESPACE__ . '\Flush', 'flushCacheByUrl']);
add_action('rrzecache_flush_cache', [__NAMESPACE__ . '\Flush', 'flushCache']);
add_action('_core_updated_successfully', [__NAMESPACE__ . '\Flush', 'flushAllCache']);
add_action('switch_theme', [__NAMESPACE__ . '\Flush', 'flushCache']);
add_action('customize_save_after', [__NAMESPACE__ . '\Flush', 'flushCache']);
add_action('wp_update_nav_menu', [__NAMESPACE__ . '\Flush', 'flushCache']);
add_action('wp_trash_post', [__NAMESPACE__ . '\Flush', 'flushCache']);
}
protected function setCacheFiles()
{
File::createCacheDir();
File::createCacheOldDir();
if (!file_exists(File::getTimeoutFile())) {
File::storeTimeout($this->siteOptions->cache_expires);
}
}
public function add_publish_hooks()
{
$post_types = get_post_types(['public' => true]);
if (empty($post_types)) {
return;
}
foreach ($post_types as $post_type) {
add_action('publish_' . $post_type, [$this, 'publish_post_types'], 10, 2);
add_action('publish_future_' . $post_type, [__NAMESPACE__ . '\Flush', 'flushCache']);
}
}
public function publish_post_types($post_ID, $post)
{
if (empty($post_ID) || empty($post)) {
return;
}
if (!in_array($post->post_status, ['publish', 'future'])) {
return;
}
if (!current_user_can('publish_posts')) {
return;
}
if ($this->options->reset_on_publish) {
Flush::flushCache();
} else {
Flush::flushCacheByPostId($post_ID);
}
if (isset($_POST['post_cache_submitbox_nonce']) && wp_verify_nonce($_POST['post_cache_submitbox_nonce'], 'post_cache_submitbox')) {
if ($_POST['rrze_cache_select'] == 'disabled') {
update_post_meta($post_ID, RRZECACHE_META_KEY, 'disabled');
} else {
delete_post_meta($post_ID, RRZECACHE_META_KEY);
}
}
// rrze-ac plugin
if (is_plugin_active('rrze-ac/rrze-ac.php') && isset($_POST['post_protection_submitbox_nonce']) && wp_verify_nonce($_POST['post_protection_submitbox_nonce'], 'post_protection_submitbox')) {
if ($_POST['access_permission_select'] != 'all') {
update_post_meta($post_ID, RRZECACHE_META_KEY, 'disabled');
} elseif ($_POST['access_permission_select'] == 'all' && !get_post_meta($post_ID, RRZECACHE_META_KEY, true)) {
delete_post_meta($post_ID, RRZECACHE_META_KEY);
}
}
}
public function pre_comment_approved($approved, $comment)
{
if ($approved === 1) {
if ($this->options->reset_on_comment) {
Flush::flushCache();
} else {
Flush::flushCacheByPostId($comment['comment_post_ID']);
}
}
return $approved;
}
public function transition_comment_status($new_status, $old_status, $comment)
{
if ($new_status != $old_status) {
if ($this->options->reset_on_comment) {
Flush::flushCache();
} else {
Flush::flushCacheByPostId($comment->comment_post_ID);
}
}
}
public function edit_comment($id)
{
if ($this->options->reset_on_comment) {
Flush::flushCache();
} else {
Flush::flushCacheByPostId(get_comment($id)->comment_post_ID);
}
}
public function post_cache_submitbox()
{
global $post;
wp_nonce_field('post_cache_submitbox', 'post_cache_submitbox_nonce');
$cache = get_post_meta($post->ID, RRZECACHE_META_KEY, true) ? 'disabled' : 'enabled';
// rrze-ac plugin
if (is_plugin_active('rrze-ac/rrze-ac.php')) {
$permission = get_post_meta($post->ID, ACCESS_PERMISSION_META_KEY, true);
if (!empty($permission) && $permission != 'all') {
$cache = 'disabled';
}
}
$label = $cache == 'disabled' ? __('Disabled', 'rrze-cache') : __('Enabled', 'rrze-cache');
$class = $cache == 'disabled' ? 'cache-disabled-icon' : 'cache-icon'; ?>
<div id="post-cache-wrap" class="misc-pub-section">
<span>
<span id="cache-icon" class="<?php echo $class; ?> dashicons dashicons-cloud"></span>
<?php _e('Cache:', 'rrze-cache'); ?>
<b id="post-cache-label"><?php echo $label; ?></b>
</span>
<?php if (empty($permission) || $permission == 'all') : ?>
<a href="#" id="edit-post-cache" class="edit-post-cache hide-if-no-js">
<span aria-hidden="true"><?php _e('Edit', 'rrze-cache'); ?></span>
<span class="screen-reader-text"><?php _e('Edit Cache', 'rrze-cache'); ?></span>
</a>
<div id="post-cache-field" class="hide-if-js">
<select id="rrze-cache-select" name="rrze_cache_select">
<option value="enabled" <?php selected($cache, 'enabled'); ?>>
<?php echo sanitize_text_field(__('Enable', 'rrze-cache')); ?>
</option>
<option value="disabled" <?php selected($cache, 'disabled'); ?>>
<?php echo sanitize_text_field(__('Disable', 'rrze-cache')); ?>
</option>
</select>
<a href="#" class="save-post-cache hide-if-no-js button"><?php _e('OK', 'rrze-cache'); ?></a>
<a href="#" class="cancel-post-cache hide-if-no-js button-cancel"><?php _e('Cancel', 'rrze-cache'); ?></a>
</div>
<?php endif ?>
</div>
<?php
}
public function template_redirect()
{
if ($this->skip_cache()) {
return;
}
$cached = File::getItem($this->get_cache_expires());
if (!$cached) {
ob_start([$this, 'storeCache']);
}
}
public function get_cache_expires()
{
if (is_multisite()) {
return $this->siteOptions->cache_expires;
}
return $this->options->cache_expires;
}
public function storeCache($data)
{
if (empty($data)) {
return '';
}
File::storeItem($data);
return $data;
}
protected function is_logged_in()
{
if (is_user_logged_in()) {
return true;
}
if (empty($_COOKIE)) {
return false;
}
foreach ($_COOKIE as $k => $v) {
if (preg_match('/^(wp-postpass|wordpress_logged_in|comment_author)_/', $k)) {
return true;
}
}
}
protected function is_wp_sitemap()
{
global $wp;
if (preg_match('/^wp-sitemap/', $wp->request)) {
return true;
}
return false;
}
protected function is_mobile()
{
$templatedir = get_template_directory();
return (strpos($templatedir, 'wptouch') || strpos($templatedir, 'carrington') || strpos($templatedir, 'jetpack') || strpos($templatedir, 'handheld'));
}
protected function skip_cache()
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != 'GET') {
return true;
}
if (!empty($_SERVER['QUERY_STRING'])) {
return true;
}
if (basename($_SERVER['SCRIPT_NAME']) != 'index.php') {
return true;
}
if ($this->is_logged_in()) {
return true;
}
if (apply_filters('rrzecache_skip_cache', false)) {
return true;
}
if (is_search() || is_404() || is_feed() || is_trackback() || is_robots() || is_preview() || post_password_required()) {
return true;
}
if (defined('DONOTCACHEPAGE') && DONOTCACHEPAGE) {
return true;
}
if ($this->is_mobile()) {
return true;
}
if ($this->is_wp_sitemap()) {
return true;
}
if (is_singular()) {
$post_ID = $GLOBALS['wp_query']->get_queried_object_id();
$cache = get_post_meta($post_ID, RRZECACHE_META_KEY, true) ? 'disabled' : 'enabled';
if ($cache == 'disabled') {
return true;
}
}
return false;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment