-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhestia-nginx-cache.php
154 lines (131 loc) · 3.6 KB
/
hestia-nginx-cache.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Hestia Nginx Cache
*
* @package Hestia_Nginx_Cache
* @author Jakob Bouchard
* @license GPL-3.0+
*
* @wordpress-plugin
* Plugin Name: Hestia Nginx Cache
* Description: Hestia Nginx Cache Integration for WordPress. Auto-purges the Nginx cache when needed.
* Plugin URI: https://github.com/jakobbouchard/hestia-nginx-cache
* Version: 2.4.1
* Requires at least: 4.8
* Requires PHP: 5.4
* Author: Jakob Bouchard
* Author URI: https://jakobbouchard.dev
* Text Domain: hestia-nginx-cache
* License: GPL v3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
if (!defined('ABSPATH')) {
exit();
}
class Hestia_Nginx_Cache
{
public const NAME = 'hestia-nginx-cache';
public const VERSION = '2.4.1';
private static $instance = null;
public static $plugin_basename = null;
public static $is_configured = false;
public $admin = null;
public $site_health = null;
private $purge = false;
private $events = [
'publish_post',
'edit_post',
'save_post',
'post_updated',
'deleted_post',
'trashed_post',
'wp_trash_post',
'add_attachment',
'edit_attachment',
'attachment_updated',
'publish_phone',
'clean_post_cache',
'comment_post',
'edit_comment',
'delete_comment',
'wp_insert_comment',
'wp_set_comment_status',
'transition_post_status',
'transition_comment_status',
'wp_update_nav_menu',
'switch_theme',
'permalink_structure_changed',
];
private function __construct()
{
$this::$plugin_basename = plugin_basename(__FILE__);
$options = get_option(self::NAME);
if ($options && isset($options['access_key']) && $options['access_key'] != '' && isset($options['secret_key']) && $options['secret_key'] != '') {
$this::$is_configured = true;
}
add_action('init', [$this, 'init']);
add_action('shutdown', [$this, 'purge']);
}
public static function get_instance()
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init()
{
load_plugin_textdomain(self::NAME, false, self::NAME . '/languages');
// Register site_health
if (is_admin()) {
require_once __DIR__ . '/includes/site_health.php';
$this->site_health = new Hestia_Nginx_Cache_Site_Health();
}
// Do not allow logged in users / subscribers to clear cache
if (current_user_can('edit_posts')) {
require_once __DIR__ . '/includes/admin.php';
$this->admin = new Hestia_Nginx_Cache_Admin();
}
foreach ($this->events as $event) {
add_action($event, [$this, 'consolidate_purge']);
}
}
public function consolidate_purge()
{
$this->purge = true;
}
public function purge($force = false)
{
if ($this->purge !== true && !$force) {
return false;
}
if (!$this::$is_configured) {
return false;
}
$options = get_option(self::NAME);
if(key_exists('disable_automatic_purge', $options) && $options['disable_automatic_purge'] && !$force){
return false;
}
// Server credentials
$hostname = $options['host'];
$port = $options['port'];
$access_key = $options['access_key'];
$secret_key = $options['secret_key'];
// Info to purge
$username = $options['user'];
$domain = $options['domain'];
// Prepare POST query
$body = [
'hash' => "$access_key:$secret_key",
'returncode' => 'yes',
'cmd' => 'v-purge-nginx-cache',
'arg1' => $username,
'arg2' => $domain,
];
return wp_remote_post("https://$hostname:$port/api/", ['sslverify' => false, 'timeout' => 60, 'body' => $body]);
}
}
Hestia_Nginx_Cache::get_instance();
if (defined('WP_CLI') && WP_CLI) {
include(__DIR__.'/wp-cli.php');
}