<?php
/*
Plugin Name: Add X-Hacker Header
Plugin URI: http://officel.biz
Description: HTTPレスポンスヘッダに X-hacker ヘッダを追加する
Version: 1.1.0
Author: Y.Nishimura
Author URI: http://officel.biz/
*/

// １．管理画面（のメニューが表示される時）で呼び出される関数を指定する
add_action('admin_menu', 'addxheader_menu');

// ２．メニューに追加する
function addxheader_menu(){
	add_submenu_page('plugins.php', 'Add X-Hacker Header', 'Add X-Hacker', 'manage_options', 'addxhacker', 'addxhacker_conf');
}

// ３．呼び出される設定画面
function addxhacker_conf(){
	// ３．１．フォームデータにsubmitがある（更新ボタンが押された）場合
	if ( isset($_POST['submit']) ) {
		// ３．１．１．ログイン中のユーザが権限を持っているかのチェック
		if ( function_exists('current_user_can') && !current_user_can('manage_options') )
			die(__('Cheatin&#8217; uh?'));
		// ３．１．２．渡された文字列
		$words = $_POST['words'];

		// ３．１．３．文字列が空だったらデータを削除
		if ( empty($words) ) {
			delete_option('x_hacker_header_words');
		}
		// ３．１．４．オプションを更新
		update_option('x_hacker_header_words', $words);
	}
	// ３．２．更新した旨を表示、その後フォームを出力
	if ( !empty($_POST['submit'] ) ) : ?>
<div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>
<?php endif; ?>
<div class="wrap">
<h2>Add X-Hacker Header Settings</h2>
<div class="narrow">
<form action="" method="post" id="addxhacker-conf">
<fieldset>
<legend>X-hacker:</legend>
<label for="words">Messages</label>
<input id="words" name="words" type="text" size="40" maxlength="100" value="<?php echo get_option('x_hacker_header_words'); ?>" style="" />
<input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" />
</fieldset>
</form>
</div>
</div>
<?php
}
// ４．フックで呼び出される関数を作る
function addxhacker($content) {
	// ４．０．フィルタフックには引数がある（受け渡すデータ）
	// ４．１．オプションを呼び出す
	$headers = array( 'X-hacker' => get_option('x_hacker_header_words') );
	// ４．２．フックにより異なるが、同じ形でデータを戻すことでチェインする
	return array_merge($headers,$content);
}
// ５．フックに登録する。
add_filter('wp_headers', 'addxhacker');


?>
