<?php
add_action('init', 'nusantara_portfolio');  
  
function nusantara_portfolio() {  
    $args = array('labels' => array(
			'name' => __('Portfolio', 'nusantara'), 
			'singular_name' => __('Portfolio', 'nusantara'), 
			'all_items' => __('All Portfolio', 'nusantara'), 
			'add_new' => __('Add New', 'nusantara'), 
			'add_new_item' => __('New Portfolio', 'nusantara'), 
			'edit' => __( 'Edit', 'nusantara' ), 
			'edit_item' => __('Edit Portfolio', 'nusantara'), 
			'new_item' => __('New Portfolio Type', 'nusantara'), 
			'view_item' => __('View Portfolio', 'nusantara'), 
			'search_items' => __('Search Portfolio', 'nusantara'), 
			'not_found' =>  __('Nothing found fortfolio.', 'nusantara'), 
			'not_found_in_trash' => __('Nothing found portfolio in Trash', 'nusantara'), 
			'parent_item_colon' => ''
			), 

                        'description' => __( 'This is the portfolio custom post type', 'nusantara' ), 
			'public' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'show_ui' => true,
			'query_var' => true,
			'menu_position' => 8, 
			'menu_icon' => get_stylesheet_directory_uri() . '/inc/assets/images/icons/cpt.png', 
			'rewrite'	=> array( 'slug' => 'portfolio', 'with_front' => false ), 
			'has_archive' => 'portfolio', 
			'capability_type' => 'post',
			'hierarchical' => false,			
			'supports' => array( 
                                      'title',
                                      'editor',
                                      'trackbacks',
                                      'custom-fields',
                                      'comments',                                      
                                      'thumbnail',
                                      'author'                                      
                                      )
	 	
	); 

    register_post_type( 'portfolio' , $args );
  
}  


    
// now let's add custom tags (these act like categories)
register_taxonomy( 'custom_tag', 
    	array('portfolio'), 
    	array('hierarchical' => false,                   
    		'labels' => array(
    			'name' => __( 'Portfolio Tags', 'nusantara' ), 
    			'singular_name' => __( 'Portfolio Tag', 'nusantara' ), 
    			'search_items' =>  __( 'Search Custom Tags', 'nusantara' ), 
    			'all_items' => __( 'All portfolio Tags', 'nusantara' ), 
    			'parent_item' => __( 'Parent Custom Tag', 'nusantara' ), 
    			'parent_item_colon' => __( 'Parent Custom Tag:', 'nusantara' ), 
    			'edit_item' => __( 'Edit Custom Tag', 'nusantara' ), 
    			'update_item' => __( 'Update Custom Tag', 'nusantara' ), 
    			'add_new_item' => __( 'Add New Custom portfolio Tag', 'nusantara' ), 
    			'new_item_name' => __( 'New Custom Tag Name', 'nusantara' ) 
    		),
    		'show_ui' => true,
    		'query_var' => true,
    	)
    ); 




$nusantara_meta_box_portfolio = array(
	'id' => 'portfolio-meta-box-slider',
	'title' =>  __('Portfolio Settings', 'nusantara'),
	'page' => 'portfolio',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
               array( 			
				'name' => __('Action button one ', 'nusantara'),
				'desc' => __('Enter text for button one ', 'nusantara'),
				'id' => 'nusantara_actionb_one',
				'type' => 'text',
				'std' => '',
                          
				
			),
                array( 			
				'name' => __('Button one URL Destination', 'nusantara'),
				'desc' => __('Enter URL destination for button one', 'nusantara'),
				'id' => 'nusantara_actionlink_one',
				'type' => 'text',
				'std' => '',
                          
				
			),

               array( 			
				'name' => __('Action button two', 'nusantara'),
				'desc' => __('Enter text for button two', 'nusantara'),
				'id' => 'nusantara_actionb_two',
				'type' => 'text',
				'std' => '',
                          
				
			),
                array( 			
				'name' => __('Button two URL Destination', 'nusantara'),
				'desc' => __('Enter URL destination for button two', 'nusantara'),
				'id' => 'nusantara_actionlink_two',
				'type' => 'text',
				'std' => '',
                          
				
			),
		
		array(
				'name' => __('Description', 'nusantara'),
				'desc' => __('Enter your descriptions', 'nusantara'),
				'id' => 'nusantara_desc_portfolio',
				'type' => 'textarea',
				'std' => ''
			),
						
				
	),
);

function nusantara_box_portfolio() {
	global $nusantara_meta_box_portfolio;	
	add_meta_box($nusantara_meta_box_portfolio['id'], 
        $nusantara_meta_box_portfolio['title'], 'nusantara_show_box_portfolio', 
        $nusantara_meta_box_portfolio['page'], 
        $nusantara_meta_box_portfolio['context'], 
        $nusantara_meta_box_portfolio['priority']);	
}


function nusantara_show_box_portfolio() {
	global $nusantara_meta_box_portfolio, $post;
	
	// Use nonce for verification
	echo '<input type="hidden" name="nusantara_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; 
	echo '<table class="form-table">'; 
	foreach ($nusantara_meta_box_portfolio['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 textarea		
			case 'textarea':
                        echo '<tr>',
				'<th><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 id='".$field['id']."' name='", $field['id'], "' rows='8' cols='60' tabindex='6'>" . esc_attr($meta) . "</textarea>";
			
		        break;
			
			//If Text		
			case 'text':
			
			echo '<tr style="border-bottom: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;
			

		}

	}
 
	echo '</table>';
}

function nusantara_save_data_portfolio($post_id) {
	global $nusantara_meta_box_portfolio, $meta_box_featured_portfolio;	
	if(!isset($_POST['nusantara_meta_box_nonce'])) $_POST['nusantara_meta_box_nonce'] = "undefine";
 
	// verify nonce
	if (!wp_verify_nonce($_POST['nusantara_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 ($nusantara_meta_box_portfolio['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);
		}
	}
	
}
add_action('save_post', 'nusantara_save_data_portfolio');
add_action('admin_menu', 'nusantara_box_portfolio');