<?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;