public
Last active

Theme Napkin Wordpress Plugin - Safeguard unintended output generated by theme bootstrap code (functions.php).

  • Download Gist
theme-napkin.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?php
/**
* Theme Napkin Wordpress Plugin
*
* Safeguard unintended output generated by theme bootstrap code (functions.php).
*
* Copyright 2011 by hakre <http::/hakre.wordpress.com>, some rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Wordpress Plugin Header:
*
* Plugin Name: Theme Napkin
* Plugin URI: http://core.trac.wordpress.org/ticket/16485
* Version: 0.1
* Author: hakre
* Author URI: http://hakre.wordpress.com
* Description: Some themes leak, Safeguard Theme Output puts a napkin around them. In case WP_DEBUG is on, you can use it to debug your environment for theme leaking issues.
* Tags: Output, Theme, function.php, #16485
* Min WP Version: 3.0
* Requires at least: 3.0
* Tested up to: 3.1-RC4
* Stable tag: 0.1
*/
 
return themeNapkinPlugin::bootstrap();
 
class themeNapkinPlugin {
/** @var int */
private $outputLength = 0;
/** @var safeguardThemeOutputPlugin */
static $instance;
static function bootstrap() {
if (null === self::$instance)
self::$instance = new self();
return self::$instance;
}
public function count() {
return $this->outputLength;
}
public function __construct() {
foreach(array('setup_theme', 'after_setup_theme') as $hook)
add_filter($hook, array($this, $hook));
}
public function setup_theme() {
ob_start();
}
public function after_setup_theme() {
$buffer = ob_get_clean();
$this->outputLength = strlen($buffer);
if (strlen($buffer) && WP_DEBUG && apply_filters('leaking_theme_trigger_error', true, $buffer)) {
trigger_error( __('The theme\'s functions.php generated unexpected output.'));
}
}
}
 
#EOF;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.