Skip to content

Instantly share code, notes, and snippets.

@justingivens
Last active May 10, 2017 18:56
Show Gist options
  • Save justingivens/e45e8a4d2d10b24756dc9c38d3bac7df to your computer and use it in GitHub Desktop.
Save justingivens/e45e8a4d2d10b24756dc9c38d3bac7df to your computer and use it in GitHub Desktop.
Unique file name caching system for beaverbeaver. This is perfect if you use a CDN or strip out url parameters. This class clones the layout files and generates assets.
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( class_exists( 'FLBuilder' ) ) {
/**
* Caching Solutions for _box-Builder
*
* @since 1.1.0
* @package _box_Builder_Modules
* @subpackage _box_Builder_Modules/caching
* @author Image in a Box <support@imageinabox.com>
*/
class _box_Builder_Cache {
/** @var string Meta_Key used for this class */
private $meta_key = '_box_builder_unique_cache_id';
/**
* The single instance of this class
*
* @since 1.0.0
*
* @var _box_Builder_Cache|null
*/
protected static $instance = null;
/**
* _box_Builder_Cache constructor.
*/
public function __construct() {
$this->loader();
}
/**
* Loader functions for all the hooks/filters
*/
public function loader() {
add_action( 'fl_builder_after_save_layout', [ $this, 'generate_unique_assets' ], 999, 2 );
add_action( 'fl_builder_after_render_js', [ $this, 'clone_asset_js' ] );
add_action( 'fl_builder_after_render_css', [ $this, 'clone_asset_css' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'change_paths_as_needed' ], 999 );
}
/**
* Updates the timestamp for the specific post.
*
* @param integer $post_id
* @param boolean $publish
*/
public function generate_unique_assets( $post_id, $publish ) {
if ( $publish && $post_id ) {
$last_unique_time = get_post_meta( $post_id, $this->meta_key, true );
if ( ! empty( $last_unique_time ) ) {
$this->remove_old_cached( $post_id, $last_unique_time );
}
update_post_meta( $post_id, $this->meta_key, time() );
}
}
/**
* Change the enqueued paths to the new cached versions.
*/
public function change_paths_as_needed() {
$post_id = FLBuilderModel::get_post_id();
if ( ! empty( $post_id ) ) {
$wp_styles = wp_styles();
$wp_scripts = wp_scripts();
if ( ! is_admin() ) {
$last_unique_time = get_post_meta( $post_id, $this->meta_key, true );
if ( ! empty( $last_unique_time ) ) {
$handler = 'fl-builder-layout-' . $post_id;
$filepaths = $this->get_asset_names( $post_id, $last_unique_time );
if ( isset( $wp_styles->registered[$handler] ) && file_exists( $filepaths['css'] ) ) {
$wp_styles->registered[$handler]->src = $filepaths['css_url'];
}
if ( isset( $wp_scripts->registered[$handler] ) && file_exists( $filepaths['js'] ) ) {
$wp_scripts->registered[$handler]->src = $filepaths['js_url'];
}
}
}
}
}
/**
* Delete the old cached items
*
* @param $post_id
* @param string $time
*/
public function remove_old_cached( $post_id, $time = '' ) {
if ( empty( $time ) ) {
return;
}
$files = $this->get_asset_names( $post_id, $time );
$paths = array(
$files['css'],
$files['js']
);
foreach ( $paths as $path ) {
if ( file_exists( $path ) ) {
unlink( $path );
}
}
}
/**
* Clone on JS change
*/
public function clone_asset_js() {
$this->clone_asset( 'js' );
}
/**
* Clone on CSS change
*/
public function clone_asset_css() {
$this->clone_asset( 'css' );
}
/**
* Clone the asssets AFTER the builder has generated the new assets.
*
* @param string $type
*/
private function clone_asset( $type = '' ) {
try {
$post_id = FLBuilderModel::get_post_id();
if ( empty( $post_id ) ) {
return;
}
$last_unique_time = get_post_meta( $post_id, $this->meta_key, true );
if ( ! empty( $last_unique_time ) ) {
$filePaths = $this->get_asset_names( $post_id, $last_unique_time );
if( $type == 'css' ) {
$filename = $filePaths['original_css'];
$copyFilename = $filePaths['css'];
} else {
$filename = $filePaths['original_js'];
$copyFilename = $filePaths['js'];
}
if ( file_exists( $filename ) ) {
if ( ! copy( $filename, $copyFilename ) ) {
throw new Exception( 'Could not copy filename from: ' . $filename . ' --- ' . $copyFilename );
}
}
}
}
catch ( Exception $e ) {
error_log( $e );
return;
}
}
/**
* Gets the custom names.
*
* @param int $post_id
* @param string $time
*
* @return array
*/
public function get_asset_names( $post_id = 0, $time = '' ) {
$cache_dir = FLBuilderModel::get_cache_dir();
return [
'original_css' => $cache_dir['path'] . $post_id . '-layout.css',
'css' => $cache_dir['path'] . $post_id . '-layout-' . $time . '.css',
'css_url' => $cache_dir['url'] . $post_id . '-layout-' . $time . '.css',
'original_js' => $cache_dir['path'] . $post_id . '-layout.js',
'js' => $cache_dir['path'] . $post_id . '-layout-' . $time . '.js',
'js_url' => $cache_dir['url'] . $post_id . '-layout-' . $time . '.js',
];
}
/**
* Main _box_Builder_Cache instance
*
* Ensures only one instance of _box_Builder_Cache is loaded or can be loaded
*
* @since 1.0.0
*
* @return _box_Builder_Cache|null
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Global Function if needed
*
* @return _box_Builder_Cache|null
*/
function _box_builder_cache() {
return _box_Builder_Cache::instance();
}
add_action( 'init', '_box_builder_cache' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment