<?php 


#-----------------------------------------------------------------#
# //skew slider
#-----------------------------------------------------------------# 


#-----------------------------------------------------------------#
# Create admin slider section
#-----------------------------------------------------------------# 
function slant_skew_slider_register() {  
    
	$labels = array(
	 	'name' => __( 'Slides', 'taxonomy general name', KP_THEME_NAME),
		'singular_name' => __( 'Slide', KP_THEME_NAME),
		'search_items' =>  __( 'Search Slides', KP_THEME_NAME),
		'all_items' => __( 'All Slides', KP_THEME_NAME),
		'parent_item' => __( 'Parent Slide', KP_THEME_NAME),
		'edit_item' => __( 'Edit Slide', KP_THEME_NAME),
		'update_item' => __( 'Update Slide', KP_THEME_NAME),
		'add_new_item' => __( 'Add New Slide', KP_THEME_NAME),
	    'menu_name' => __( 'Skew Slider', KP_THEME_NAME)
	 );
	 
	 $args = array(
			'labels' => $labels,
			'singular_label' => __('Skew Slider', KP_THEME_NAME),
			'public' => false,
			'show_ui' => true,
			'hierarchical' => false,
			'menu_position' => 6,
			'exclude_from_search' => true,
			'supports' => array('title'),
       );  
   
    register_post_type( 'skew_slider' , $args );  
}  

add_action('init', 'slant_skew_slider_register');



#-----------------------------------------------------------------#
# Custom slider columns
#-----------------------------------------------------------------# 

add_filter('manage_edit-skew_slider_columns', 'edit_columns_skew_slider');  

function edit_columns_skew_slider($columns){

	$columns = array(
    "cb" => "<input type=\"checkbox\" />",
	"title" => "Title",
	"thumbnail" => "Thumbnail",
	"caption" => "Caption",
    "date" => "Date",
  );  
	
	return $columns;
}  
  
  
add_action('manage_posts_custom_column',  'skew_slider_custom_columns', 10, 2);  

function skew_slider_custom_columns($skewslider_columns, $post_id){  

	switch ($skewslider_columns) {
	    case 'thumbnail':
	        $thumbnail = rwmb_meta( 'slant_skew_slide_image', 'type=image&size=thumbnail' );


	        if( !empty($thumbnail) ) {
			foreach ( $thumbnail as $image )
			{
			
			  $slide_img = $image['url'];
			  break;
			}
	            echo '<a href="'.get_admin_url() . 'post.php?post=' . $post_id.'&action=edit"><img class="slider-thumb" src="' . $slide_img . '" /></a>';
	        } else {
	            echo '<a href="'.get_admin_url() . 'post.php?post=' . $post_id.'&action=edit"><img class="slider-thumb" src="' . KP_FRAMEWORK_DIRECTORY . 'assets/img/slider-default-thumb.jpg" /></a>' .
	                 '<strong><a class="row-title" href="'.get_admin_url() . 'post.php?post=' . $post_id.'&action=edit">';
	        }
	    break; 
		
		
		case 'caption':
			$caption = rwmb_meta( 'slant_skew_slide_descrption' );
	        echo $caption;
	    break;  
		
				
		   
		default:
			break;
	}  
}  


add_action( 'admin_menu', 'slant_skew_slider_ordering' );

function slant_skew_slider_ordering() {
	add_submenu_page(
		'edit.php?post_type=skew_slider',
		'Order Slides',
		'Order',
		'edit_pages', 'slide-order',
		'slant_skew_slider_order_page'
	);
}

function slant_skew_slider_order_page(){ ?>
	
	<div class="wrap">
		<h2>Sort Slides</h2>
		<p>Simply drag the slide up or down and they will be saved in that order.</p>
	<?php $slides = new WP_Query( array( 'post_type' => 'skew_slider', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'menu_order' ) ); ?>
	<?php if( $slides->have_posts() ) : ?>
		
		<?php wp_nonce_field( basename(__FILE__), 'slant_meta_box_nonce' ); ?>
		
		<table class="wp-list-table widefat fixed posts" id="sortable-table">
			<thead>
				<tr>
					<th class="column-order">Order</th>
					<th class="manage-column column-thumbnail">Image</th>
					<th class="manage-column column-caption">Caption</th>
				</tr>
			</thead>
			<tbody data-post-type="skew_slider">
			<?php while( $slides->have_posts() ) : $slides->the_post(); ?>
				<tr id="post-<?php the_ID(); ?>">
				
					<td class="column-order"><img src="<?php echo KP_FRAMEWORK_DIRECTORY . 'assets/img/sortable.png'; ?>" title="" alt="Move Icon" width="25" height="25" class="" /></td>
					<td class="thumbnail column-thumbnail">
						<?php 
						global $post;

					 $thumbnail = rwmb_meta( 'slant_skew_slide_image', 'type=image&size=thumbnail' );
				            
					if( !empty($thumbnail) ) {
					foreach ( $thumbnail as $image )
					{
					
					  $slide_img = $image['url'];
					  break;
					}
						echo '<img class="slider-thumb" src="' . $slide_img . '" />';
					} else {
						echo '<img class="slider-thumb" src="' . KP_FRAMEWORK_DIRECTORY . 'assets/img/slider-default-thumb.jpg" />';
					} ?>
						
					</td>
					<td class="caption column-caption">
						<?php 
						$caption = rwmb_meta( 'slant_skew_slide_descrption' );
	        			echo $caption; ?>
					</td>
				</tr>
			<?php endwhile; ?>
			</tbody>
			<tfoot>
				<tr>
					<th class="column-order">Order</th>
					<th class="manage-column column-thumbnail">Image</th>
					<th class="manage-column column-caption">Caption</th>
				</tr>
			</tfoot>

		</table>

	<?php else: ?>

		<p>No slides found, why not <a href="post-new.php?post_type=skew_slider">create one?</a></p>

	<?php endif; ?>
	<?php wp_reset_postdata(); ?>

	</div><!-- .wrap -->
	
<?php }


add_action( 'admin_enqueue_scripts', 'slant_slider_enqueue_scripts' );

function slant_slider_enqueue_scripts() {
	wp_enqueue_script( 'jquery-ui-sortable' );
	wp_enqueue_script( 'slant-reorder', KP_FRAMEWORK_DIRECTORY . '/assets/js/reorder.js' );
}


add_action( 'wp_ajax_slant_update_slide_order', 'slant_update_slide_order' );

//slide order ajax callback 
function slant_update_slide_order() {
	
	    global $wpdb;
	 
	    $post_type     = $_POST['postType'];
	    $order        = $_POST['order'];
		
		if (  !isset($_POST['slant_meta_box_nonce']) || !wp_verify_nonce( $_POST['slant_meta_box_nonce'], basename( __FILE__ ) ) )
			return;
		
	    foreach( $order as $menu_order => $post_id ) {
	        $post_id         = intval( str_ireplace( 'post-', '', $post_id ) );
	        $menu_order     = intval($menu_order);
			
	        wp_update_post( array( 'ID' => stripslashes(htmlspecialchars($post_id)), 'menu_order' => stripslashes(htmlspecialchars($menu_order)) ) );
    	}
 
	    die( '1' );
}


//order the default skew slider page correctly 
function set_skew_slider_admin_order($wp_query) {  
  if (is_admin()) {  
  
    $post_type = $wp_query->query['post_type'];  
  
    if ( $post_type == 'skew_slider') {  
   
      $wp_query->set('orderby', 'menu_order');  
      $wp_query->set('order', 'ASC');  
    }  
  }  
}  

add_filter('pre_get_posts', 'set_skew_slider_admin_order'); 



?>