<?php 

// Custom fields for WP write panel
// This code is protected under Creative Commons License: http://creativecommons.org/licenses/by-nc-nd/3.0/

$new_metaboxes = array(
		"thumb" => array (
			"name"		=> "Thumbnail",
			"default" 	=> "",
			"label" 	=> "Main Photo",
			"type" 		=> "text",
			"desc"      => "Enter the URL for the General Product Photo"
		),
		"thumb1" => array (
			"name"		=> "Thumbnail1",
			"default" 	=> "",
			"label" 	=> "First Photo",
			"type" 		=> "text",
			"desc"      => "Enter the URL for the First Product Photo"
		),
		"thumb2" => array (
			"name"		=> "Thumbnail2",
			"default" 	=> "",
			"label" 	=> "Second Photo",
			"type" 		=> "text",
			"desc"      => "Enter the URL for the Second Product Photo"
		),
		"thumb3" => array (
			"name"		=> "Thumbnail3",
			"default" 	=> "",
			"label" 	=> "Third Photo",
			"type" 		=> "text",
			"desc"      => "Enter the URL for the Third Product Photo"
		),
		"thumb4" => array (
			"name"		=> "Thumbnail4",
			"default" 	=> "",
			"label" 	=> "Fourth Photo",
			"type" 		=> "text",
			"desc"      => "Enter the URL for the Fourth Product Photo"
		),
		"thumb5" => array (
			"name"		=> "Thumbnail5",
			"default" 	=> "",
			"label" 	=> "Fifth Photo",
			"type" 		=> "text",
			"desc"      => "Enter the URL for the Fifth Product Photo"
		),
		"optiontitle" => array (
			"name"		=> "optiontitle",
			"default" 	=> "",
			"label" 	=> "Option Title",
			"type" 		=> "text",
			"desc"      => "Enter the Title for Your Option Dropdown (e.g Color, Size)"
		),
		"option1" => array (
			"name"		=> "option1",
			"default" 	=> "",
			"label" 	=> "Option 1",
			"type" 		=> "text",
			"desc"      => "Enter the First Option for your Dropdown (e.g Black, Small)"
		),
		"option2" => array (
			"name"		=> "option2",
			"default" 	=> "",
			"label" 	=> "Option 2",
			"type" 		=> "text",
			"desc"      => "Enter the Second Option for your Dropdown"
		),
		"option3" => array (
			"name"		=> "option3",
			"default" 	=> "",
			"label" 	=> "Option 3",
			"type" 		=> "text",
			"desc"      => "Enter the Third Option for your Dropdown"
		),
		"option4" => array (
			"name"		=> "option4",
			"default" 	=> "",
			"label" 	=> "Option 4",
			"type" 		=> "text",
			"desc"      => "Enter the Fourth Option for your Dropdown"
		),
		"option5" => array (
			"name"		=> "option5",
			"default" 	=> "",
			"label" 	=> "Option 5",
			"type" 		=> "text",
			"desc"      => "Enter the Fifth Option for your Dropdown"
		),
		"price" => array (
			"name"		=> "price",
			"default" 	=> "",
			"label" 	=> "Price",
			"type" 		=> "text",
			"desc"      => "Enter the Price for the Product including the currency symbol ($1.00)"
		),
		"shipping" => array (
			"name"		=> "shipping",
			"default" 	=> "",
			"label" 	=> "Shipping",
			"type" 		=> "text",
			"desc"      => "Enter the Price for the Shipping excluding the currency symbol (1.00)"
		),		
	);
	
function envirashop_meta_box_content() {
	global $post, $new_metaboxes;
	echo '<table>'."\n";
	foreach ($new_metaboxes as $new_metabox) {
		$new_metaboxvalue = get_post_meta($post->ID,$new_metabox["name"],true);
		if ($new_metaboxvalue == "" || !isset($new_metaboxvalue)) {
			$new_metaboxvalue = $new_metabox['default'];
		}
		echo "\t".'<tr>';
		echo "\t\t".'<th style="text-align: right;"><label for="'.$new_metabox.'">'.$new_metabox['label'].':</label></th>'."\n";
		echo "\t\t".'<td><input size="70" type="'.$new_metabox['type'].'" value="'.$new_metaboxvalue.'" name="envirashop_'.$new_metabox["name"].'" id="'.$new_metabox.'"/></td>'."\n";
		echo "\t".'</tr>'."\n";
		echo "\t\t".'<tr><td></td><td><span style="font-size:11px">'.$new_metabox['desc'].'</span></td></tr>'."\n";				
	}
	echo '</table>'."\n\n";
}

function envirashop_metabox_insert($pID) {
	global $new_metaboxes;
	foreach ($new_metaboxes as $new_metabox) {
		$var = "envirashop_".$new_metabox["name"];
		if (isset($_POST[$var])) {			
			if( get_post_meta( $pID, $new_metabox["name"] ) == "" )
				add_post_meta($pID, $new_metabox["name"], $_POST[$var], true );
			elseif($_POST[$var] != get_post_meta($pID, $new_metabox["name"], true))
				update_post_meta($pID, $new_metabox["name"], $_POST[$var]);
			elseif($_POST[$var] == "")
				delete_post_meta($pID, $new_metabox["name"], get_post_meta($pID, $new_metabox["name"], true));
		}
	}
}

function envirashop_meta_box() {
	if ( function_exists('add_meta_box') ) {
		add_meta_box('envirashop-settings',$GLOBALS['themename'].' Envirashop Product Settings','envirashop_meta_box_content','post','normal');
		add_meta_box('envirashop-settings',$GLOBALS['themename'].' Envirashop Product Settings','envirashop_meta_box_content','page','normal');
	}
}

add_action('admin_menu', 'envirashop_meta_box');
add_action('wp_insert_post', 'envirashop_metabox_insert');
?>