<?php

class ReedwanMetaboxes {
	public function __construct()
	{
		add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
		add_action('save_post', array($this, 'save_meta_boxes'));
	}
	public function add_meta_boxes()
	{
		$this->add_meta_box('post_featured', 'Featured Image on Single Post Options','post');
		$this->add_meta_box('post_featured', 'Featured Image on Single Post Options','reviews');
		$this->add_meta_box('post_reviews', 'Post Reviews', 'reviews');
		
	}
	public function add_meta_box($id, $label, $post_type)
	{
	    add_meta_box( 
	        'reedwan_' . $id,
	        __($label, 'Backstreet'),
	        array($this, $id),
	        $post_type
	    );
	}
	public function save_meta_boxes($post_id)
	{
		if(defined( 'DOING_AUTOSAVE') && DOING_AUTOSAVE) {
			return;
		}
		
		foreach($_POST as $key => $value) {
			update_post_meta($post_id, $key, $value);
		}
	}
	public function post_featured()
	{		
		include 'featured-metaboxes.php';
	}
	public function post_show_reviews()
	{		
		include 'show-review-metaboxes.php';
	}
	public function post_reviews()
	{		
		include 'review-metaboxes.php';
	}
	public function text($id, $label, $desc = '')
	{
		global $post;
		
		$html .= '<div class="reedwan_metabox_field">';
			$html .= '<label for="reedwan_' . $id . '">';
			$html .= $label;
			$html .= '</label>';
			$html .= '<div class="field">';
				$html .= '<input type="text" id="reedwan_' . $id . '" name="reedwan_' . $id . '" value="' . get_post_meta($post->ID, 'reedwan_' . $id, true) . '" />';
				if($desc) {
					$html .= '<p>' . $desc . '</p>';
				}
			$html .= '</div>';
		$html .= '</div>';
		
		echo $html;
	}
	public function select($id, $label, $options, $desc = '')
	{
		global $post;
		
		$html .= '<div class="reedwan_metabox_field">';
			$html .= '<label for="reedwan_' . $id . '">';
			$html .= $label;
			$html .= '</label>';
			$html .= '<div class="field">';
				$html .= '<select id="reedwan_' . $id . '" name="reedwan_' . $id . '">';
				foreach($options as $key => $option) {
					if(get_post_meta($post->ID, 'reedwan_' . $id, true) == $key) {
						$selected = 'selected="selected"';
					} else {
						$selected = '';
					}
					
					$html .= '<option ' . $selected . 'value="' . $key . '">' . $option . '</option>';
				}
				$html .= '</select>';
				if($desc) {
					$html .= '<p>' . $desc . '</p>';
				}
			$html .= '</div>';
		$html .= '</div>';
		
		echo $html;
	}
	
}

$metaboxes = new ReedwanMetaboxes;