<?php
/**
 * Create meta options for the portfolio 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_portfolio_settings = array(
	'id' => 'wpex-portfolio-meta-box-slider',
	'title' =>  __('Post Options', 'wpex'),
	'page' => 'portfolio',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		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' => __('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' => 'Full Width'
        ),
		array(
            'name' => __('Post Image Slider', 'wpex'),
            'id' => $prefix . 'portfolio_post_slider',
			'desc' => __('Should this portfolio post have an image slider created automatically from your media attachments?', 'wpex'),
            'type' => 'select',
            'options' => array( 'no', 'yes' ),
			'std' => 'no'
        ),
		array(
            'name' => __('Entry Format', 'wpex'),
            'id' => $prefix . 'portfolio_post_style',
			'desc' => __('Select the format for this post\'s entry.', 'wpex'),
            'type' => 'select',
            'options' => array( 'Default', 'Redirect', 'Lightbox' ),
			'std' => 'Default'
        ),
		array(
            'name' => __('oEmbed URL (video)','wpex'),
            'desc' =>  __('Enter the video URL that is compatible with WP\'s built-in oEmbed feature.', 'wpex') .' <a href="http://codex.wordpress.org/Embeds" target="_blank">'. __('Learn More', 'wpex') .' &rarr;</a>',
            'id' => $prefix . 'portfolio_post_video',
            'type' => 'text',
            'std' => ''
        ),
		array(
            'name' => __('Redirect URL','wpex'),
            'desc' =>  __('If this is a "redirect style portfolio the post will re-direct to this URL.','wpex'),
            'id' => $prefix . 'portfolio_post_redirect_url',
            'type' => 'text',
            'std' => ''
        ),
		array(
            'name' => __('Post Media Alternative', 'wpex'),
            'id' => $prefix . 'portfolio_post_media_alternative',
			'desc' => __('If you rather not show a video, featured image or slider on the top of the single post, you can enter your alternative content here.', 'wpex'),
            'type' => 'textarea',
			'std' => ''
        ),
		array(
            'name' => __('Post Details', 'wpex'),
            'id' => $prefix . 'portfolio_post_details',
			'desc' => __('Add extra details to your portfolio post.', 'wpex'),
            'type' => 'editor',
			'std' => ''
        ),
	),
);

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

add_action('admin_menu', 'wpex_add_box_portfolio_settings');

function wpex_add_box_portfolio_settings() {
	global $wpex_meta_box_portfolio_settings;
	
	add_meta_box($wpex_meta_box_portfolio_settings['id'], $wpex_meta_box_portfolio_settings['title'], 'wpex_show_box_portfolio_settings', $wpex_meta_box_portfolio_settings['page'], $wpex_meta_box_portfolio_settings['context'], $wpex_meta_box_portfolio_settings['priority']);

}

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

function wpex_show_box_portfolio_settings() {
	global $wpex_meta_box_portfolio_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_portfolio_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>',
				'<tr style="border-top:1px solid #eeeeee;">',
				'<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>';
			
			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;" />';
			
			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:75%; margin-right: 20px; float:left;">', $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' => '10',
							'textarea_name' => $field['id'],
							'media_buttons' => false
						);
						
						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_portfolio');

/*-----------------------------------------------------------------------------------*/
//	Save data when portfolio is edited
/*-----------------------------------------------------------------------------------*/
 
function wpex_save_data_portfolio($post_id) {
	global $wpex_meta_box_portfolio_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_portfolio_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);
		}
	}

}
?>