Last active
April 28, 2018 21:30
-
-
Save westonruter/36b9740b35ce4eda91db0d88296bc8b7 to your computer and use it in GitHub Desktop.
This file contains 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: Test $wp_object_cache availability in output buffer callback in shutdown. | |
* Author: Weston Ruter, XWP | |
* Description: Add ?erasable=false to the URL to try calling ob_start() with its erasable=false. | |
*/ | |
if ( ! isset( $_SERVER['REQUEST_URI'] ) || false !== strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) ) { | |
return; | |
} | |
$chunk_size = 0; // The default value. | |
$erasable = ! isset( $_GET['erasable'] ) || (bool) json_decode( $_GET['erasable'] ); | |
$erase_flags = $erasable ? PHP_OUTPUT_HANDLER_STDFLAGS : 0; | |
ob_start( | |
function( $buffer ) use ( $erasable ) { | |
$pass = ! empty( $GLOBALS['wp_object_cache'] ); | |
$buffer = preg_replace( | |
'#(?=</body>)#', | |
sprintf( | |
'<div style="position:fixed; left: 0; bottom: 0; padding: 1em; color: black; text-shadow: 1px 1px 2px white; z-index: 100000000; background-color: %s">%s; buffer erasable: %s, <a href="?erasable=%s" style="text-decoration: underline; color: blue;">Toggle</a>)</div>', | |
$pass ? '#0f0' : '#f00', | |
$pass ? '✅ <code>$wp_object_cache</code> exists' : '🚫 <code>$wp_object_cache</code> empty', | |
json_encode( $erasable ), | |
json_encode( ! $erasable ) | |
), | |
$buffer | |
); | |
return $buffer; | |
}, | |
$chunk_size, | |
$erase_flags | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment