Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gMagicScott/5400268 to your computer and use it in GitHub Desktop.
Save gMagicScott/5400268 to your computer and use it in GitHub Desktop.
Bustin' up cache's in WordPress. Doing it H5BP style, and without a build script.
<?php
/**
* Plugin Name: Filename Based Cache Busting
* Plugin URI: https://gist.github.com/gMagicScott/5400268
* Description: Bustin' up cache's in WordPress. Doing it H5BP style, and without a build script.
* Author: Scott Lesovic, forked from Ben Sampo
* Author URI:
* Version: 0.2-dev
*/
class FilenameBasedCacheBusting {
/**
* Hold and protect options
*
* @var array
*/
private $data;
/** Singleton *************************************************************/
/**
* @var FilenameBasedCacheBusting The one true FilenameBasedCacheBusting
*/
private static $instance;
/**
* Main FilenameBasedCacheBusting Instance
*
* Ensures that only one instance exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new FilenameBasedCacheBusting;
self::$instance->setup_globals();
self::$instance->setup_actions();
}
return self::$instance;
}
/**
* A dummy constructor to prevent class from being loaded more than once.
*/
private function __construct() {
/* Do nothing here */
}
/** Private Methods *******************************************************/
/**
* Set some smart defaults to class variables. Allow some of them to be
* filtered to allow for early overriding.
*/
private function setup_globals() {
/** Version ***********************************************************/
$this->version = '0.2-dev'; // plugin version
/** Paths *************************************************************/
// Setup some base path and URL information
$this->file = __FILE__;
$this->basename = apply_filters( 'FilenameBasedCacheBusting_plugin_basenname', plugin_basename( $this->file ) );
// Grab Options from DB
$this->data = get_option( $this->basename . '-options', array() );
}
/**
* Setup the default hooks and actions
*/
private function setup_actions() {
add_action( 'generate_rewrite_rules', array( $this, 'action_generate_rewrite_rules' ) );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_filter( 'script_loader_src', array( $this, 'filter_script_style_loader_src'), 10, 1 );
add_filter( 'style_loader_src', array( $this, 'filter_script_style_loader_src'), 10, 1 );
do_action_ref_array( 'FilenameBasedCacheBusting_after_setup_actions', array( &$this ) );
}
/**
* Install / Upgrade routine
*
* Avoiding standard WordPress Activation/Deactivation method to make plugin
* work as 'must-use' dropin.
*/
public function action_admin_init() {
$saved_version = $this->get( 'version' );
if ( $this->version !== $saved_version ) {
$this->flush_rewrite();
$this->set( 'version', $this->version );
}
}
public function filter_script_style_loader_src( $src ) {
// Don't touch admin scripts.
if ( is_admin() ) {
return $src;
}
// Only for scripts on current domain
$domain = get_site_url();
if ( strpos( $src, $domain) === false ) {
return $src;
}
// Remove version string from the source.
if ( strpos( $src, 'ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
}
// Get the sources local file path
$src_local_path = str_replace( get_site_url(), ABSPATH, $src );
// Get the files last updated timestamp
$timestamp = filemtime( $src_local_path );
// Get the extension of the file, can be '.js' or '.css'.
$ext = '.' . pathinfo( $src, PATHINFO_EXTENSION );
// Build the new source.
$src = str_replace( $ext, '.' . $timestamp . '.fbcb' . $ext, $src );
// Return the new source.
return $src;
}
private function flush_rewrite () {
global $wp_rewrite;
$wp_rewrite->flush_rules( true );
}
public function action_generate_rewrite_rules () {
global $wp_rewrite;
$new_non_wp_rules = array(
'^(.+)\.(.+)\.fbcb\.(js|css)$' => '$1.$3',
);
$wp_rewrite->non_wp_rules = $new_non_wp_rules + $wp_rewrite->non_wp_rules;
}
private function get( $key ) {
return isset( $this->data[$key] ) ? $this->data[$key] : null;
}
private function set( $key, $value ) {
$this->data[$key] = $value;
update_option( $this->basename . '-options', $this->data );
}
public static function uninstall() {
$plug = new FilenameBasedCacheBusting;
$plug->setup_globals();
delete_option( plugin_basename( __FILE__ ) . '-options');
global $wp_rewrite;
$wp_rewrite->flush_rules( true );
}
}
/**
* The main function responsible for returning the one true theme instance
* to functions everywhere.
*/
function FilenameBasedCacheBusting() {
return FilenameBasedCacheBusting::instance();
}
add_action( 'plugins_loaded', 'FilenameBasedCacheBusting' );
/**
* Uninstall the plugin
*
* 1. Comment out the line above
* 2. Un-comment the line below.
* 3. Refresh the admin screen.
*/
// add_action( 'admin_init', 'FilenameBasedCacheBusting::uninstall' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment