-
-
Save Eran-E/365f10d3f967baaa29589fa1330cc129 to your computer and use it in GitHub Desktop.
WP Trac #42573: Fix for theme template caching. https://core.trac.wordpress.org/ticket/42573
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin name: WP Trac #42573: Fix for theme template file caching. | |
* Description: Flush the theme file cache each time the admin screens are loaded or with admin bar button option | |
* Plugin URI: https://core.trac.wordpress.org/ticket/42573 | |
* Author: Weston Ruter, XWP. | |
* Author URI: https://weston.ruter.net | |
*/ | |
/* choose to flush cache when admin screens are loaded or with admin bar button click, the option is set with WP_42573_USE_ON_LOAD define configuration. | |
if the button option is on, success notice will show up after cache flush. | |
the plugin is running only in the admin screen for the website's client side performance. */ | |
/********* return if it's not admin screen *********/ | |
if ( !is_admin() ) return; | |
/********* config *********/ | |
define( 'WP_42573_USE_ON_LOAD', false ); | |
define( 'WP_42573_FLUSH_TRANSIENT_QSTRING' , 'transient=flush' ); | |
/********* functions *********/ | |
// add notice | |
function wp_42573_notice( $text = 'notice!', $status = 'success', $is_dismissible = true ){ | |
// dismiss button option | |
$dismissible = ( $is_dismissible ) ? ' is-dismissible' : ''; | |
$output = '<div class="notice notice-' . $status . $dismissible . '"> | |
<p>' . $text . '</p> | |
</div>'; | |
echo $output; | |
} | |
// success notice action | |
function wp_42573_action_success_notice(){ | |
// add transient cache flush success notice | |
add_action( 'admin_notices', function(){ | |
$text = __( 'Succeed to flush transient cache.', 'default' ); | |
$status = 'success'; | |
wp_42573_notice( $text, $status ); | |
}); | |
} | |
// check admin screen | |
function wp_42573_screen_check( $current_screen = null ){ | |
// get admin screen object if the parameter is null | |
if ( $current_screen === null ) $current_screen = get_current_screen(); | |
// match admin screen and return true or false | |
return ( in_array( $current_screen->base, array( 'post', 'edit', 'theme-editor' ), true ) ); | |
} | |
// add flush transient cache button to the admin bar | |
function wp_42573_add_items_to_admin_bar( $admin_bar ){ | |
// return if wrong screen | |
if ( ! wp_42573_screen_check() ) return; | |
// get current page | |
global $pagenow; | |
// add flush transient cache button | |
$admin_bar->add_menu( array( | |
'id' => 'flush-transient', | |
'parent' => 'top-secondary', | |
'title' => 'Flush Transient Cache', | |
'href' => wp_nonce_url( network_admin_url( $pagenow . '?' . $_SERVER['QUERY_STRING'] . '&' . WP_42573_FLUSH_TRANSIENT_QSTRING ) ), | |
'meta' => array( | |
'title' => __('Flush Transient Cache'), | |
), | |
) ); | |
} | |
// flush transient cache | |
function wp_42573_fix_template_caching() { | |
// Only if theme object exists | |
$theme = wp_get_theme(); | |
if ( ! $theme->exists() ) return; | |
// flush transient cache | |
$cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() ); | |
$label = sanitize_key( 'files_' . $cache_hash . '-' . $theme->get( 'Version' ) ); | |
$transient_key = substr( $label, 0, 29 ) . md5( $label ); | |
$res = delete_transient( $transient_key ); | |
// return flush result | |
return $res; | |
} | |
function wp_42573_current_screen( WP_Screen $current_screen ) { | |
// flush transient cache on load | |
if ( WP_42573_USE_ON_LOAD ){ | |
// if right screen then flush transient cache | |
if ( wp_42573_screen_check( $current_screen ) ) wp_42573_fix_template_caching(); | |
} | |
else { // flush transient cache with admin bar button | |
// add flush transient cache button to the admin bar if match the correct screens | |
add_action('admin_bar_menu', 'wp_42573_add_items_to_admin_bar'); | |
// return if the url query string not contains the transient cache flush key & value or the cache flush didn't succeed | |
if ( strpos( $_SERVER['QUERY_STRING'], WP_42573_FLUSH_TRANSIENT_QSTRING ) === false || ! wp_42573_fix_template_caching() ) return; | |
// add success notice action | |
wp_42573_action_success_notice(); | |
} | |
} | |
/********* actions *********/ | |
// wp_42573_current_screen function as initial action | |
add_action('current_screen', 'wp_42573_current_screen' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment