Skip to content

Instantly share code, notes, and snippets.

@herewithme
Last active February 16, 2016 11:47
Show Gist options
  • Save herewithme/5791769e4fcc5b2d0506 to your computer and use it in GitHub Desktop.
Save herewithme/5791769e4fcc5b2d0506 to your computer and use it in GitHub Desktop.
Internal usage -- Purge all varnish when WP changes any content
<?php
/*
Plugin Name: Purge all varnish
Plugin URI: http://www.beapi.fr
Description: Purge all varnish cache when a post is modified/edited
Version: 1.3
Author: BeAPI
Author URI: http://www.beapi.fr
Network: true
*/
class Varnish_Purge_All {
public function __construct() {
// Define registered purge events
$actions = array(
'save_post', // Save a post
'deleted_post', // Delete a post
'trashed_post', // Empty Trashed post
'edit_post', // Edit a post - includes leaving comments
'delete_attachment', // Delete an attachment - includes re-uploading
'switch_theme', // Change theme
);
// Add the action for each event
foreach ( $actions as $event ) {
add_action( $event, array($this, 'purge_all'), 10 );
}
// Add some actions for allow admin bar flush button
if ( is_admin() ) {
add_action( 'admin_bar_menu', array($this, 'admin_bar_menu') );
add_action( 'admin_init', array($this, 'admin_init') );
add_action( 'admin_head', array($this, 'admin_head') );
}
}
public function purge_all() {
global $wpdb, $current_blog;
// Flush original WP domain
$ban = sprintf( 'varnishadm "ban req.http.host == \"%s\" && req.url ~ /"', $current_blog->domain );
$result = shell_exec( $ban );
// Debug
error_log( $ban . ' => ' . $result . PHP_EOL, 3, WP_CONTENT_DIR . '/debug-varnish.log' );
// Get table name for mapping
$wpdb->dmtable = ( isset( $wpdb->base_prefix ) ? $wpdb->base_prefix : $wpdb->prefix ) . 'domain_mapping';
// Flush all mapped domains
$blog_domains = $wpdb->get_col( $wpdb->prepare(
"
SELECT dm.domain
FROM $wpdb->blogs AS b
INNER JOIN $wpdb->dmtable AS dm ON b.blog_id = dm.blog_id
WHERE b.blog_id = %d
",
$current_blog->blog_id
) );
if( empty( $blog_domains ) ) {
return false;
}
foreach( $blog_domains as $blog_domain ) {
$ban = sprintf( 'varnishadm "ban req.http.host == \"%s\" && req.url ~ /"', $blog_domain );
$result = shell_exec( $ban );
// Debug
error_log( $ban . ' => ' . $result . PHP_EOL, 3, WP_CONTENT_DIR . '/debug-varnish.log' );
}
return true;
}
public function admin_bar_menu( $wp_admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
$url = add_query_arg( array('action' => 'flush_varnish_cache'), admin_url( 'index.php' ) );
$wp_admin_bar->add_node( array(
'id' => 'varnish-cache-flusher-button',
'title' => __( 'Flush Cache' ),
'href' => wp_nonce_url( $url, 'varnish-cache-flush' ),
'parent' => 'top-secondary',
) );
return true;
}
public function admin_init() {
if ( empty( $_GET['action'] ) ) {
return false;
}
if ( 'flush_varnish_cache' !== $_GET['action'] ) {
return false;
}
if ( ! check_admin_referer( 'varnish-cache-flush' ) ) {
return false;
}
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
$this->purge_all();
return true;
}
public function admin_head() {
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
echo '<style>#wpadminbar #wp-admin-bar-varnish-cache-flusher-button a{background:#a00}</style>';
return true;
}
}
new Varnish_Purge_All();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment