forked from rtCamp/nginx-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx-helper.php
440 lines (363 loc) · 14.6 KB
/
nginx-helper.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/*
Plugin Name: Nginx Helper
Plugin URI: https://rtcamp.com/nginx-helper/
Description: Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things.
Version: 1.9.8
Author: rtCamp
Author URI: https://rtcamp.com
Text Domain: nginx-helper
Requires at least: 3.0
Tested up to: 4.6
*/
namespace rtCamp\WP\Nginx {
define( 'rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH', plugin_dir_path( __FILE__ ) );
define( 'rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_URL', plugin_dir_url( __FILE__ ) );
class Helper {
var $minium_WP = '3.0';
var $options = null;
var $plugin_name = 'nginx-helper';
const WP_CLI_COMMAND = 'nginx-helper';
function __construct()
{
if ( !$this->required_wp_version() )
if ( !$this->required_php_version() )
return;
// Load Plugin Text Domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
$this->load_options();
$this->plugin_name = plugin_basename( __FILE__ );
register_activation_hook( $this->plugin_name, array( &$this, 'activate' ) );
register_deactivation_hook( $this->plugin_name, array( &$this, 'deactivate' ) );
add_action( 'init', array( &$this, 'start_helper' ), 15 );
}
function start_helper()
{
global $rt_wp_nginx_purger;
add_action( 'shutdown', array( &$this, 'add_timestamps' ), 99999 );
add_action( 'add_init', array( &$this, 'update_map' ) );
//add_action( 'save_post', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 );
// add_action( 'publish_post', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 );
// add_action( 'publish_page', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 );
add_action( 'wp_insert_comment', array( &$rt_wp_nginx_purger, 'purgePostOnComment' ), 200, 2 );
add_action( 'transition_comment_status', array( &$rt_wp_nginx_purger, 'purgePostOnCommentChange' ), 200, 3 );
// $args = array( '_builtin' => false );
// $_rt_custom_post_types = get_post_types( $args );
// if ( isset( $post_types ) && !empty( $post_types ) ) {
// if ( $this->options['rt_wp_custom_post_types'] == true ) {
// foreach ( $_rt_custom_post_types as $post_type ) {
// add_action( 'publish_' . trim( $post_type ), array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 );
// }
// }
// }
add_action( 'transition_post_status', array( &$this, 'set_future_post_option_on_future_status' ), 20, 3 );
add_action( 'delete_post', array( &$this, 'unset_future_post_option_on_delete' ), 20, 1 );
add_action( 'nm_check_log_file_size_daily', array( &$rt_wp_nginx_purger, 'checkAndTruncateLogFile' ), 100, 1 );
add_action( 'edit_attachment', array( &$rt_wp_nginx_purger, 'purgeImageOnEdit' ), 100, 1 );
add_action( 'wpmu_new_blog', array( &$this, 'update_new_blog_options' ), 10, 1 );
add_action( 'transition_post_status', array( &$rt_wp_nginx_purger, 'purge_on_post_moved_to_trash' ), 20, 3 );
add_action( 'edit_term', array( &$rt_wp_nginx_purger, 'purge_on_term_taxonomy_edited' ), 20, 3 );
add_action( 'delete_term', array( &$rt_wp_nginx_purger, 'purge_on_term_taxonomy_edited' ), 20, 3 );
add_action( 'check_ajax_referer', array( &$rt_wp_nginx_purger, 'purge_on_check_ajax_referer' ), 20, 2 );
add_action( 'admin_init', array( &$this, 'purge_all' ) );
// expose action to allow other plugins to purge the cache
add_action( 'rt_nginx_helper_purge_all', array( &$this, 'true_purge_all' ) );
// Load WP-CLI command
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once RT_WP_NGINX_HELPER_PATH . 'wp-cli.php';
\WP_CLI::add_command( self::WP_CLI_COMMAND, 'Nginx_Helper_WP_CLI_Command' );
}
}
function activate()
{
$path = $this->functional_asset_path();
if ( !is_dir( $path ) ) {
mkdir( $path );
}
include_once (RT_WP_NGINX_HELPER_PATH . 'admin/install.php');
rt_wp_nginx_helper_install();
}
function deactivate()
{
include_once (RT_WP_NGINX_HELPER_PATH . 'admin/install.php');
rt_wp_nginx_helper_uninstall();
}
function required_wp_version()
{
global $wp_version;
$wp_ok = version_compare( $wp_version, $this->minium_WP, '>=' );
if ( ($wp_ok == FALSE ) ) {
add_action( 'admin_notices', create_function( '', 'global $rt_wp_nginx_helper; printf (\'<div id="message" class="error"><p><strong>\' . __(\'Sorry, Nginx Helper requires WordPress %s or higher\', "nginx-helper" ) . \'</strong></p></div>\', $rt_wp_nginx_helper->minium_WP );' ) );
add_action( 'network_admin_notices', create_function( '', 'global $rt_wp_nginx_helper; printf (\'<div id="message" class="error"><p><strong>\' . __(\'Sorry, Nginx Helper requires WordPress %s or higher\', "nginx-helper" ) . \'</strong></p></div>\', $rt_wp_nginx_helper->minium_WP );' ) );
return false;
}
return true;
}
function load_options()
{
$this->options = get_site_option( 'rt_wp_nginx_helper_options' );
}
function set_future_post_option_on_future_status( $new_status, $old_status, $post )
{
global $blog_id, $rt_wp_nginx_purger;
$purge_status = array( 'publish', 'future' );
if ( !$this->options['enable_purge'] ) {
return;
}
if( in_array( $old_status, $purge_status ) || in_array( $new_status, $purge_status ) ) {
$rt_wp_nginx_purger->log( "Purge post on transition post STATUS from " . $old_status . " to " . $new_status );
$rt_wp_nginx_purger->purgePost( $post->ID );
}
if ( $new_status == 'future' ) {
if ( $post && $post->post_status == 'future' && ( ( $post->post_type == 'post' || $post->post_type == 'page' ) || ( isset( $this->options['custom_post_types_recognized'] ) && in_array( $post->post_type, $this->options['custom_post_types_recognized'] ) ) ) ) {
$rt_wp_nginx_purger->log( "Set/update future_posts option (post id = " . $post->ID . " and blog id = " . $blog_id . ")" );
$this->options['future_posts'][$blog_id][$post->ID] = strtotime( $post->post_date_gmt ) + 60;
update_site_option( "rt_wp_nginx_helper_global_options", $this->options );
}
}
}
function unset_future_post_option_on_delete( $post_id )
{
global $blog_id, $rt_wp_nginx_purger;
if ( !$this->options['enable_purge'] ) {
return;
}
if ( $post_id && !wp_is_post_revision( $post_id ) ) {
if ( isset( $this->options['future_posts'][$blog_id][$post_id] ) && count( $this->options['future_posts'][$blog_id][$post_id] ) ) {
$rt_wp_nginx_purger->log( "Unset future_posts option (post id = " . $post_id . " and blog id = " . $blog_id . ")" );
unset( $this->options['future_posts'][$blog_id][$post_id] );
update_site_option( "rt_wp_nginx_helper_global_options", $this->options );
if ( !count( $this->options['future_posts'][$blog_id] ) ) {
unset( $this->options['future_posts'][$blog_id] );
update_site_option( "rt_wp_nginx_helper_global_options", $this->options );
}
}
}
}
function update_new_blog_options( $blog_id )
{
global $rt_wp_nginx_purger;
include_once (RT_WP_NGINX_HELPER_PATH . 'admin/install.php');
$rt_wp_nginx_purger->log( "New site added (id $blog_id)" );
$this->update_map();
$rt_wp_nginx_purger->log( "New site added to nginx map (id $blog_id)" );
$helper_options = rt_wp_nginx_helper_get_options();
update_blog_option( $blog_id, "rt_wp_nginx_helper_options", $helper_options );
$rt_wp_nginx_purger->log( "Default options updated for the new blog (id $blog_id)" );
}
function get_map()
{
if ( !$this->options['enable_map'] ) {
return;
}
if ( is_multisite() ) {
global $wpdb;
$rt_all_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM " . $wpdb->blogs . " WHERE site_id = %d AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'", $wpdb->siteid ) );
$wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
$rt_domain_map_sites = '';
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->dmtable}'" ) == $wpdb->dmtable ) {
$rt_domain_map_sites = $wpdb->get_results( "SELECT blog_id, domain FROM {$wpdb->dmtable} ORDER BY id DESC" );
}
$rt_nginx_map = "";
$rt_nginx_map_array = array();
if ( $rt_all_blogs )
foreach ( $rt_all_blogs as $blog ) {
if ( SUBDOMAIN_INSTALL == "yes" ) {
$rt_nginx_map_array[$blog->domain] = $blog->blog_id;
} else {
if ( $blog->blog_id != 1 ) {
$rt_nginx_map_array[$blog->path] = $blog->blog_id;
}
}
}
if ( $rt_domain_map_sites ) {
foreach ( $rt_domain_map_sites as $site ) {
$rt_nginx_map_array[$site->domain] = $site->blog_id;
}
}
foreach ( $rt_nginx_map_array as $domain => $domain_id ) {
$rt_nginx_map .= "\t" . $domain . "\t" . $domain_id . ";\n";
}
return $rt_nginx_map;
}
}
function functional_asset_path()
{
$path = WP_CONTENT_DIR . '/uploads/nginx-helper/';
return apply_filters( 'nginx_asset_path', $path );
}
function functional_asset_url()
{
$url = WP_CONTENT_URL . '/uploads/nginx-helper/';
return apply_filters( 'nginx_asset_url', $url );
}
function update_map()
{
if ( is_multisite() ) {
$rt_nginx_map = $this->get_map();
if ( $fp = fopen( $this->functional_asset_path() . 'map.conf', 'w+' ) ) {
fwrite( $fp, $rt_nginx_map );
fclose( $fp );
return true;
}
}
}
function add_timestamps()
{
if ( $this->options['enable_purge'] != 1 )
return;
if ( $this->options['enable_stamp'] != 1 )
return;
if ( is_admin() )
return;
foreach ( headers_list() as $header ) {
list($key, $value) = explode( ':', $header, 2 );
if ( $key == 'Content-Type' && strpos( trim( $value ), 'text/html' ) !== 0 ) {
return;
}
if ( $key == 'Content-Type' )
break;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
if ( defined( 'DOING_CRON' ) && DOING_CRON )
return;
if ( defined( 'WP_CLI' ) && WP_CLI )
return;
$timestamps = "\n<!--" .
"Cached using Nginx-Helper on " . current_time( 'mysql' ) . ". " .
"It took " . get_num_queries() . " queries executed in " . timer_stop() . " seconds." .
"-->\n" .
"<!--Visit http://wordpress.org/extend/plugins/nginx-helper/faq/ for more details-->";
echo $timestamps;
}
function show_notice()
{
echo '<div class="updated"><p>' . __( 'Purge initiated', 'nginx-helper' ) . '</p></div>';
}
function purge_all()
{
if ( !isset( $_REQUEST['nginx_helper_action'] ) )
return;
if ( !current_user_can( 'manage_options' ) )
wp_die( 'Sorry, you do not have the necessary privileges to edit these options.' );
$action = $_REQUEST['nginx_helper_action'];
if ( $action == 'done' ) {
add_action( 'admin_notices', array( &$this, 'show_notice' ) );
add_action( 'network_admin_notices', array( &$this, 'show_notice' ) );
return;
}
check_admin_referer( 'nginx_helper-purge_all' );
switch ( $action ) {
case 'purge':
$this->true_purge_all();
break;
}
wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) );
}
function true_purge_all()
{
global $rt_wp_nginx_purger;
$rt_wp_nginx_purger->true_purge_all();
}
/**
* Load the translation file for current language.
*/
function load_plugin_textdomain()
{
load_plugin_textdomain( 'nginx-helper', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
}
}
namespace {
if ( !defined( 'RT_WP_NGINX_HELPER_CACHE_PATH' ) ) {
define( 'RT_WP_NGINX_HELPER_CACHE_PATH', '/var/run/nginx-cache' );
}
global $current_blog;
if ( is_admin() ) {
require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . '/admin/admin.php');
$rtwpAdminPanel = new \rtCamp\WP\Nginx\Admin();
}
require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'purger.php');
require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'redis-purger.php');
require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'compatibility.php');
global $rt_wp_nginx_helper, $rt_wp_nginx_purger, $rt_wp_nginx_compatibility;
$rt_wp_nginx_helper = new \rtCamp\WP\Nginx\Helper;
if ( !empty( $rt_wp_nginx_helper->options['cache_method'] ) && $rt_wp_nginx_helper->options['cache_method'] == "enable_redis" ) {
$rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Redispurger;
} else {
$rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Purger;
}
$rt_wp_nginx_compatibility = namespace\rtCamp\WP\Nginx\Compatibility::instance();
if ( $rt_wp_nginx_compatibility->haveNginx() && !function_exists( 'wp_redirect' ) ) {
function wp_redirect( $location, $status = 302 )
{
$location = apply_filters( 'wp_redirect', $location, $status );
if ( empty( $location ) ) {
return false;
}
$status = apply_filters( 'wp_redirect_status', $status, $location );
if ( $status < 300 || $status > 399 ) {
$status = 302;
}
if ( function_exists( 'wp_sanitize_redirect' ) ) {
$location = wp_sanitize_redirect( $location );
}
header( 'Location: ' . $location, true, $status );
}
}
// Add settings link on plugin page
function nginx_settings_link( $links )
{
if ( is_network_admin() ) {
$u = 'settings.php';
} else {
$u = 'options-general.php';
}
$settings_link = '<a href="' . $u . '?page=nginx">' . __( 'Settings', 'nginx-helper' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
if ( is_multisite() ) {
add_filter( "network_admin_plugin_action_links_" . plugin_basename( __FILE__ ), 'nginx_settings_link' );
} else {
add_filter( "plugin_action_links_" . plugin_basename( __FILE__ ), 'nginx_settings_link' );
}
function get_feeds( $feed_url = 'http://rtcamp.com/blog/feed/' )
{
// Get RSS Feed(s)
require_once( ABSPATH . WPINC . '/feed.php' );
$maxitems = 0;
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( $feed_url );
if ( !is_wp_error( $rss ) ) { // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity( 5 );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
}
?>
<ul role="list"><?php
if ( $maxitems == 0 ) {
echo '<li role="listitem">' . __( 'No items', 'nginx-helper' ) . '.</li>';
} else {
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) {
?>
<li role="listitem">
<a href='<?php echo $item->get_permalink(); ?>' title='<?php echo __( 'Posted ', 'nginx-helper' ) . $item->get_date( 'j F Y | g:i a' ); ?>'><?php echo $item->get_title(); ?></a>
</li><?php
}
}
?>
</ul><?php
}
function fetch_feeds()
{
if ( isset( $_GET['get_feeds'] ) && $_GET['get_feeds'] == '1' ) {
get_feeds();
die();
}
}
add_action( 'init', 'fetch_feeds' );
}