forked from woody-wordpress/woody-lib-varnish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Varnish.php
157 lines (131 loc) · 4.9 KB
/
Varnish.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
155
156
157
<?php
/**
* Woody Library Varnish
* @author Léo POIROUX
* @copyright Raccourci Agency 2021
*/
namespace Woody\Lib\Varnish;
use Woody\App\Container;
use Woody\Modules\Module;
use Woody\Services\ParameterManager;
use Woody\Lib\Varnish\Commands\VarnishCommand;
final class Varnish extends Module
{
protected static $key = 'woody_lib_varnish';
protected $status = null;
public function initialize(ParameterManager $parameters, Container $container)
{
define('WOODY_LIB_VARNISH_VERSION', '1.2.4');
define('WOODY_LIB_VARNISH_ROOT', __FILE__);
define('WOODY_LIB_VARNISH_DIR_ROOT', dirname(WOODY_LIB_VARNISH_ROOT));
parent::initialize($parameters, $container);
$this->VarnishManager = $this->container->get('varnish.manager');
require_once WOODY_LIB_VARNISH_DIR_ROOT . '/Helpers/Helpers.php';
}
public function registerCommands()
{
\WP_CLI::add_command('woody:varnish', new VarnishCommand($this->container));
}
public static function dependencyServiceDefinitions()
{
return \Woody\Lib\Varnish\Configurations\Services::loadDefinitions();
}
public function subscribeHooks()
{
register_activation_hook(WOODY_LIB_VARNISH_ROOT, [$this, 'activate']);
register_deactivation_hook(WOODY_LIB_VARNISH_ROOT, [$this, 'deactivate']);
add_action('init', [$this, 'init']);
add_filter('query_vars', [$this, 'queryVars']);
add_action('template_redirect', [$this, 'forceLogout'], 1);
add_action('woody_flush_varnish', [$this, 'flush'], 10, 2);
// Send headers
if (!is_admin() && !defined('WP_CLI')) {
// Redirect HTTP headers and server-specific overrides
add_filter('wp_redirect', [$this->VarnishManager, 'send_redirect_headers']);
// Init Headers xkey
add_action('init', [$this->VarnishManager, 'send_headers']);
add_action('wp', [$this->VarnishManager, 'send_post_headers']);
}
// Logged in cookie
add_action('wp_login', [$this->VarnishManager, 'wp_login'], 1000000);
add_action('wp_logout', [$this->VarnishManager, 'wp_logout'], 1000000);
add_action('save_post', [$this, 'flush'], 10, 2);
}
// ------------------------
// ADMIN BAR
// ------------------------
public function init()
{
if (is_admin()) {
$user = wp_get_current_user();
if (in_array('administrator', $user->roles)) {
add_action('admin_bar_menu', [$this, 'flush_admin_bar_menu'], 100);
if (isset($_GET['flush_admin_varnish']) && check_admin_referer('varnish')) {
$this->flush_admin_varnish();
}
}
}
// Force Logout si il ne reste que le cookies de varnish
add_rewrite_rule('woody-logout', 'index.php?woody_logout=true', 'top');
$this->VarnishManager->force_logout();
}
public function queryVars($qvars)
{
$qvars[] = 'woody_logout';
return $qvars;
}
public function forceLogout($qvars)
{
$woody_logout = get_query_var('woody_logout');
if (!empty($woody_logout)) {
$this->VarnishManager->woody_logout();
}
}
public function flush_admin_bar_menu($admin_bar)
{
$admin_bar->add_menu(array(
'id' => 'flush-varnish',
'title' => '<span class="ab-icon dashicons dashicons-cloud-saved" style="top:2px;" aria-hidden="true"></span> Flush Varnish',
'href' => wp_nonce_url(add_query_arg('flush_admin_varnish', 1), 'varnish'),
'meta' => array(
'title' => 'Flush Varnish',
)
));
}
public function flush_admin_varnish()
{
$this->status = $this->VarnishManager->purge();
add_action('admin_notices', [$this, 'flush_message']);
}
public function flush_message()
{
$purgeme = (!empty($this->status) && $this->status['purgeme']) ? $this->status['purgeme'] : null;
$success = (!empty($this->status) && $this->status['success']) ? true : false;
if ($success) {
$class = 'updated';
$message = 'Varnish is flushed';
} else {
$class = 'error';
$message = 'Varnish not flushed (an error occured)';
}
echo sprintf('<div id="message" class="%s fade"><p><strong>%s</strong> - %s</p></div>', $class, $message, $purgeme);
}
// ------------------------
// PURGE / BAN
// ------------------------
public function flush($xkey = null)
{
$this->purge($xkey);
}
public function purge($xkey = null)
{
if (!empty($xkey) && !empty($xkey->ID)) {
$xkey = $xkey->ID;
}
// If this is just a revision, don't continue
if (wp_is_post_revision($xkey)) {
return;
}
$this->VarnishManager->purge($xkey);
}
}