<?php
/**
 * Create meta options for the page post type
 * @package Ultra WordPress Theme
 * @since 1.0
 * @author AJ Clarke : http://wpexplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://wpexplorer.com
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

$prefix = 'wpex_';

$wpex_meta_box_page_settings = array(
	'id' => 'wpex-page-meta-box-slider',
	'title' =>  __('Page Settings', 'wpex'),
	'page' => 'page',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array(
            'name' => __('Post Layout', 'wpex'),
            'id' => $prefix . 'post_layout',
			'desc' => __('Should this portfolio post have a sidebar?', 'wpex'),
            'type' => 'select',
            'options' => array('Right Sidebar','Left Sidebar','Full Width'),
			'std' => 'Right Sidebar'
        ),
		array(
            'name' => __('Disable Heading?','wpex'),
            'desc' =>  __('Check to disable the main heading where the title and breadcrumbs is.', 'wpex'),
            'id' => $prefix . 'page_disable_heading',
			'type' => 'select',
			'std' => 'no',
			'options' => array('no', 'yes'),
        ),
		array(
            'name' => __('Above Page Content','wpex'),
            'desc' =>  __('Enter the content you want displayed above this page. This can be used for adding things such as your page sliders or a Google Map. You could even add an image with HTMl or a video embed!', 'wpex'),
            'id' => $prefix . 'page_slider_shortcode',
            'type' => 'textarea',
            'std' => ''
        ),
		array(
            'name' => __('Featured Image Overlay Content','wpex'),
            'desc' =>  __('Featured image overlayed content.', 'wpex'),
            'id' => $prefix . 'page_featured_img_overlay',
            'type' => 'editor',
            'std' => ''
        )
	),
);

/*-----------------------------------------------------------------------------------*/
// Display meta box in edit page page
/*-----------------------------------------------------------------------------------*/

add_action('admin_menu', 'wpex_add_box_page_settings');

function wpex_add_box_page_settings() {
	global $wpex_meta_box_page_settings;
	
	add_meta_box($wpex_meta_box_page_settings['id'], $wpex_meta_box_page_settings['title'], 'wpex_show_box_page_settings', $wpex_meta_box_page_settings['page'], $wpex_meta_box_page_settings['context'], $wpex_meta_box_page_settings['priority']);

}

/*-----------------------------------------------------------------------------------*/
//	Callback function to show fields in meta box
/*-----------------------------------------------------------------------------------*/

function wpex_show_box_page_settings() {
	global $wpex_meta_box_page_settings, $post;
	
	// Use nonce for verification
	echo '<input type="hidden" name="wpex_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
 
	echo '<table class="form-table">';
 
	foreach ($wpex_meta_box_page_settings['fields'] as $field) {
		
		// get current post meta data & set default value if empty
		$meta = get_post_meta($post->ID, $field['id'], true);
		
		if (empty ($meta)) {
			$meta = $field['std']; 
		}
		
		switch ($field['type']) {
 
			
			//If Select	
			case 'select':
			
				echo '<tr>',
				'<th style="width:50%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#777; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
				'<td>';
					echo'<select name="'.$field['id'].'">';
				
					foreach ($field['options'] as $option) {
						
						echo'<option';
						if ($meta == $option ) { 
							echo ' selected="selected"'; 
						}
						echo'>'. $option .'</option>';
					
					} 
					
					echo'</select>';

				echo '</td>';
			
			break;
			
			
			//If Text		
			case 'text':
			
			echo '<tr style="border-top:1px solid #eeeeee;">',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#777; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
				'<td>';
			echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" size="30" style="width:75%; margin-right: 20px; float:left;" />';
			
			echo '</td>';
			
			break;
			
			
			
			//If Textarea
			case 'textarea':
			
			echo '<tr style="border-top:1px solid #eeeeee;">',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#777; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
				'<td>';
			echo '<textarea type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" size="30" style="width:100%; float:left;" rows="3">', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '</textarea>';
			
			break;			
			
			//if Editor
			case 'editor':
				
				echo '<tr style="border-top:1px solid #eeeeee;">',
					'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="display:block; color:#777; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
					'<td>';
					
					echo '<div style="width:520px;background:#fff;">';
										
					$settings = array(
						'textarea_rows' => '5',
						'textarea_name' => $field['id'],
						'media_buttons' => true
					);						
					wp_editor( $meta ? htmlspecialchars_decode($meta) : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)) , $field['id'], $settings );
					
					echo '</div>';
				
				echo '</td>';
		}

	}
 
	echo '</table>';
}
 
add_action('save_post', 'wpex_save_data_page');

/*-----------------------------------------------------------------------------------*/
//	Save data when page is edited
/*-----------------------------------------------------------------------------------*/

function wpex_save_data_page($post_id) {
	global $wpex_meta_box_page_settings;
	
	if(!isset($_POST['wpex_meta_box_nonce'])) $_POST['wpex_meta_box_nonce'] = "undefine";
 
	// verify nonce
	if (!wp_verify_nonce($_POST['wpex_meta_box_nonce'], basename(__FILE__))) {
		return $post_id;
	}
 
	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}
 
	// check permissions
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post_id;
		}
	} elseif (!current_user_can('edit_post', $post_id)) {
		return $post_id;
	}
 
	//Save fields
	foreach ($wpex_meta_box_page_settings['fields'] as $field) {
		$old = get_post_meta($post_id, $field['id'], true);
		$new = $_POST[$field['id']];
 
		if ($new && $new != $old) {
			update_post_meta($post_id, $field['id'], stripslashes(htmlspecialchars($new)));
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $field['id'], $old);
		}
	}

}