<?php
/*
Plugin Name: HeFo
Plugin URI: http://kaloyan.info/
Description: This plugin is designed to inject HTML snippets into the header and the footer of WordPress pages.
Author: Kaloyan K. Tsvetkov
Version: 0.1
Author URI: http://kaloyan.info/
*/

/////////////////////////////////////////////////////////////////////////////

/**
* @internal prevent from direct calls
*/
if (!defined('ABSPATH')) {
	return ;
	}

/**
* @internal prevent from second inclusion
*/
if (!class_exists('hefo')) {

/////////////////////////////////////////////////////////////////////////////

/**
* "HeFo" WordPress Plugin
*/
Class hefo {

	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

	/**
	* Constructor
	*/
	function hefo() {

		// attach the handler
		//
		add_action('wp_head',
			array($this, 'wp_head'));
		add_action('wp_footer',
			array($this, 'wp_footer'));


		// attach to admin menu
		//
		if (is_admin()) {
			add_action('admin_menu',
				array(&$this, '_menu')
				);
			}
		
		// attach to plugin installation
		//
		add_action(
			'activate_' . str_replace(
				DIRECTORY_SEPARATOR, '/',
				str_replace(
					realpath(ABSPATH . PLUGINDIR) . DIRECTORY_SEPARATOR,
						'', __FILE__
					)
				),
			array(&$this, 'install')
			);
		}
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	
	/**
	* Inject the wp_head related snippets
	* @return string
	*/
	function wp_head() {
		return $this->heforize(__FUNCTION__);
		}

	/**
	* Inject the wp_footer related snippets
	* @return string
	*/
	function wp_footer() {
		return $this->heforize(__FUNCTION__);
		}

	/**
	* Inject the wp_footer related snippets
	* @return string
	*/
	function heforize($tag) {
		$hefo_settings = (array) get_option('hefo_settings');
		if (isset($hefo_settings['snippets'][$tag])) {
			echo $hefo_settings['snippets'][$tag];
			}
		}

	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

	/**
	* Performs the routines required at plugin installation: 
	* in general introducing the settings array
	*/	
	function install() {
		add_option(
			'hefo_settings',
				array(
					'snippets' => array(
						'wp_head' => '',
						'wp_footer' => '',
						)
				)
			);
		}

	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	
	/**
	* Attach the menu page to the `Options` tab
	*/
	function _menu() {
		add_submenu_page('options-general.php',
			 'HeFo',
			 'HeFo', 8,
			 __FILE__,
			 array($this, 'menu')
			);
		}
		
	/**
	* Handles and renders the menu page
	*/
	function menu() {

		// sanitize referrer
		//
		$_SERVER['HTTP_REFERER'] = preg_replace(
			'~&saved=.*$~Uis','', $_SERVER['HTTP_REFERER']
			);
		
		// information updated ?
		//
		if ($_POST['submit']) {

			$_ = $_POST['hefo_settings'];
			$_['snippets'] = array_map('stripCSlashes', $_['snippets']);
			
			// save
			//
			update_option(
				'hefo_settings',
				$_
				);

			die("<script>document.location.href = '{$_SERVER['HTTP_REFERER']}&saved=settings:" . time() . "';</script>");
			}

		// operation report detected
		//
		if (@$_GET['saved']) {
			
			list($saved, $ts) = explode(':', $_GET['saved']);
			if (time() - $ts < 10) {
				echo '<div class="updated"><p>';
	
				switch ($saved) {
					case 'settings' :
						echo 'Settings saved.';
						break;
					}
	
				echo '</p></div>';
				}
			}

		// read the settings
		//
		$hefo_settings = (array) get_option('hefo_settings');

?>
<div class="wrap">
	<h2>HeFo</h2>
	<p>
	This plugin is designed to help you inject portions of HTML code (or as 
	we call them "HTML snippets") into your blog without having to modify 
	the theme you are using.
	</p>

	<form method="post">
	<fieldset class="options">

		<label for="wp_head_html">Header:</label><br/>
		<textarea name="hefo_settings[snippets][wp_head]" style="width:90%; height:120px;"
			id="wp_head_html"><?php echo $hefo_settings['snippets']['wp_head']; ?></textarea><br/><br/>

		<label for="wp_footer_html">Footer:</label><br/>
		<textarea name="hefo_settings[snippets][wp_footer]" style="width:90%; height:120px;"
			id="wp_footer_html"><?php echo $hefo_settings['snippets']['wp_footer']; ?></textarea>

		<p class="submit" style="text-align:left;"><input type="submit" name="submit" value="Update &raquo;" /></p>
	</fieldset>
	</form>
</div>
<?php
		}
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	
	//--end-of-class
	}

}

/////////////////////////////////////////////////////////////////////////////

/**
* Initiating the plugin...
* @see hefo
*/
new hefo;

?>