-
Notifications
You must be signed in to change notification settings - Fork 1
/
better-wp-varnish.php
286 lines (202 loc) · 7.59 KB
/
better-wp-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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
Plugin Name: Better WP Varnish
Plugin URI: http://bit51.com/software/better-wp-varnish/
Description: A better solution for clearing Varnish cache with WordPress
Version: 1.0
Text Domain: better-wp-varnish
Domain Path: /languages
Author: Bit51
Author URI: http://bit51.com
License: GPLv2
Copyright 2012 - 2013 Bit51.com (email: info@bit51.com)
*/
//Require common Bit51 library
require_once( plugin_dir_path( __FILE__ ) . 'lib/bit51/bit51.php' );
if ( ! class_exists( 'bit51_bwpv' )) {
class bit51_bwpv extends Bit51 {
public $pluginversion = '0003'; //current plugin version
//important plugin information
public $hook = 'better-wp-varnish';
public $pluginbase = 'better-wp-varnish/better-wp-varnish.php';
public $pluginname = 'Better WP Varnish';
public $homepage = 'http://bit51.com/software/share-center-pro/';
public $supportpage = 'http://wordpress.org/support/plugin/better-wp-varnish';
public $wppage = 'http://wordpress.org/extend/plugins/better-wp-varnish/';
public $accesslvl = 'manage_options';
public $paypalcode = 'EVXJ8EN6YSYFY';
public $plugindata = 'bit51_bwpv_data';
public $primarysettings = 'bit51_bwpv';
public $settings = array(
'bit51_bwpv_options' => array(
'bit51_bwpv' => array(
'callback' => 'bwpv_val_options',
'enabled' => '0',
'address' => '127.0.0.1',
'port' => '80',
'timeout' => '5'
)
)
);
function __construct() {
global $bwpvoptions, $bwpvdata;
$this->pluginname = __( 'Better WP Varnish', 'better_wp_varnish' );
//set path information
if ( ! defined( 'BWPV_PP' ) ) {
define( 'BWPV_PP', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'BWPV_PU' ) ) {
define( 'BWPV_PU', plugin_dir_url( $this->pluginbase, __FILE__ ) );
}
//require admin page
require_once( plugin_dir_path( __FILE__ ) . 'inc/admin.php' );
new bwpv_admin( $this );
//require setup information
require_once( plugin_dir_path( __FILE__ ) . 'inc/setup.php' );
register_activation_hook( __FILE__, array( 'bwpv_setup', 'on_activate' ) );
register_deactivation_hook( __FILE__, array( 'bwpv_setup', 'on_deactivate' ) );
register_uninstall_hook( __FILE__, array( 'bwpv_setup', 'on_uninstall' ) );
$bwpvoptions = get_option( $this->primarysettings );
$bwpvdata = get_option( $this->plugindata );
if ( $bwpvdata['version'] != $this->pluginversion ) {
new bwpv_setup( 'activate' );
}
if ( $bwpvoptions['enabled'] == 1 ) {
add_action( 'edit_post', array( $this, 'purgePost' ), 99 );
add_action( 'edit_post', array( $this, 'purgeCommon' ), 99 );
add_action( 'comment_post', array( $this, 'purgeComment' ), 99 );
add_action( 'edit_comment', array( $this, 'purgeComment' ), 99 );
add_action( 'trashed_comment', array( $this, 'purgeComment' ), 99 );
add_action( 'untrashed_comment', array( $this, 'purgeComment' ), 99 );
add_action( 'deleted_comment', array( $this, 'purgeComment' ), 99 );
add_action( 'deleted_post', array( $this, 'purgePost' ), 99 );
add_action( 'deleted_post', array( $this, 'purgeCommon' ), 99 );
add_action( 'wp_before_admin_bar_render', array( $this, 'adminBar' ) );
}
}
function adminBar() {
global $wp_admin_bar, $post;
if ( current_user_can( $this->accesslvl ) ) {
$nonce = wp_create_nonce( 'bwpv-nonce' );
$wp_admin_bar->add_menu( array(
'parent' => false, // use 'false' for a root menu, or pass the ID of the parent menu
'id' => 'bwpv', // link ID, defaults to a sanitized title value
'title' => __( 'Varnish', 'better_wp_varnish' ), // link title
'href' => admin_url( 'options-general.php?page=better-wp-varnish' ), // name of file
'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
) );
if ( isset( $post->ID ) && $post->ID ) {
$id = $post->ID;
} else {
$id = false;
}
if ( $id !== false ) {
$wp_admin_bar->add_menu( array(
'parent' => 'bwpv', // use 'false' for a root menu, or pass the ID of the parent menu
'id' => 'bwpv-cp', // link ID, defaults to a sanitized title value
'title' => __( 'Clear This Page', 'better_wp_varnish' ), // link title
'href' => admin_url( 'options-general.php?page=better-wp-varnish&flush=current&id=' . $id . '&_wpnonce=' . $nonce ), // name of file
'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
) );
}
$wp_admin_bar->add_menu( array(
'parent' => 'bwpv', // use 'false' for a root menu, or pass the ID of the parent menu
'id' => 'bwpv-ca', // link ID, defaults to a sanitized title value
'title' => __( 'Clear All', 'better_wp_varnish' ), // link title
'href' => admin_url( 'options-general.php?page=better-wp-varnish&flush=all&id=' . ( $id === false ? 'opts' : $id ) . '&_wpnonce=' . $nonce ), // name of file
'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
) );
}
}
/**
* Purge all content from cache
*
**/
function purgeAll() {
if ( $this->purgeVarnish( '(.*)' ) == true ) {
return true;
} else {
return false;
}
}
/**
* Purge common content from cache
*
**/
function purgeCommon() {
$success = $this->purgeVarnish( '/' );
$success = $this->purgeVarnish( '(.*)/feed/(.*)' );
$success = $this->purgeVarnish( '(.*)/trackback/(.*)' );
$success = $this->purgeVarnish( '/page/(.*)' );
if ( $success == true ) {
return true;
} else {
return false;
}
}
/**
* Purges the given post from varnish
*
* @param $postid int post id
**/
function purgePost( $postid ) {
$url = get_permalink( $postid );
$link = str_replace( home_url(), '', $url );
$success = $this->purgeVarnish( $link );
$success = $this->purgeVarnish( $link . 'page/(.*)' );
if ( $success == true ) {
return true;
} else {
return false;
}
}
/**
* Purges the given comment from varnish
*
* @param $commentit int comment id
**/
function purgeComment( $commentid ) {
$comment = get_comment( $commentid );
$approved = $comment->comment_approved;
$success = true;
if ( $approved == 1 || $approved == 'trash') {
$approved = $comment->comment_post_ID;
$success = $this->purgeVarnish( '/\\\?comments_popup=' . $approved );
$success = $this->purgeVarnish( '/\\\?comments_popup=' . $approved . '&(.*)' );
}
if ( $success == true ) {
return true;
} else {
return false;
}
}
/**
* Purges the given url from varnish
*
* @param $target string url of the file to purge
* @return bool true for success, false for failure
**/
function purgeVarnish( $target ) {
global $bwpvoptions;
@ini_set( 'auto_detect_line_endings', true );
$host = parse_url( get_site_url(), PHP_URL_HOST );
$out = 'PURGE ' . $target . ' HTTP/1.0' . PHP_EOL;
$out .= 'Host: ' . $host . PHP_EOL;
$out .= 'Connection: Close' . PHP_EOL . PHP_EOL;
$sock = @fsockopen( $bwpvoptions['address'], $bwpvoptions['port'], $errno, $errstr, $bwpvoptions['timeout'] );
if ( $sock ) {
@fwrite( $sock, $out );
$result = @fread( $sock, 256 );
@fclose( $sock );
}
if ( preg_match( '/200 OK/', $result ) || preg_match( '/200 Purged/', $result ) ) {
return true;
} else {
return false;
}
}
}
}
//create plugin object
global $bit51bwpv;
$bit51bwpv = new bit51_bwpv();