<?php
/**
 * Gallery Custom Post
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */
if ( !defined( 'ABSPATH' ) ) exit;

// Register gallery custom post
function vr_register_post_gallerys() {

	register_post_type( 'vr_gallerys',
	array(
		'menu_icon' 	=> 'dashicons-format-gallery',
		'supports' 		=> array( 'title', 'editor', 'thumbnail', 'author' ),
		'public' 		=> true,
		'show_ui' 		=> true,
		'labels'        => array(
			'name'                	=> __( 'Gallerys', PLUGIN_NAME ),
			'singular_name'       	=> __( 'Gallery', PLUGIN_NAME ),
			'add_new'             	=> __( 'Add New Gallery', PLUGIN_NAME ),
			'add_new_item'        	=> __( 'Add New Gallery', PLUGIN_NAME ),
			'menu_name'           	=> __( 'Gallerys', PLUGIN_NAME ),
			'parent_item_colon'   	=> __( 'Parent Gallery', PLUGIN_NAME ),
			'all_items'           	=> __( 'All Gallerys', PLUGIN_NAME ),
			'view_item'           	=> __( 'View Gallery', PLUGIN_NAME ),
			'edit_item'           	=> __( 'Edit Gallery', PLUGIN_NAME ),
			'update_item'         	=> __( 'Update Gallery', PLUGIN_NAME ),
			'search_items'        	=> __( 'Search Gallery', PLUGIN_NAME ),
			'not_found' 			=> __(  'No Gallery found', PLUGIN_NAME ),
			'not_found_in_trash'	=> __(  'No Gallery found in Trash', PLUGIN_NAME ),
			'parent' 				=> __(  'Parent Gallery', PLUGIN_NAME ),
		)
	));

	register_taxonomy(
		'vr_gallerys_categories',
		'vr_gallerys',
		array(
			'labels' => array(
				'name' 				=> __( 'Gallerys Categories', PLUGIN_NAME ),
				'singular_name' 	=> __( 'Category', PLUGIN_NAME ),
				'search_items'  	=> __( 'Search Categories', PLUGIN_NAME ),
				'all_items' 		=> __( 'All Categories', PLUGIN_NAME ),
				'parent_item' 		=> __( 'Parent Category', PLUGIN_NAME ),
				'parent_item_colon' => __( 'Parent Category:', PLUGIN_NAME ),
				'edit_item' 		=> __( 'Edit Category', PLUGIN_NAME ),
				'update_item' 		=> __( 'Update Category', PLUGIN_NAME ),
				'add_new_item' 		=> __( 'Add New Category', PLUGIN_NAME ),
				'new_item_name'		=> __( 'New Category Name', PLUGIN_NAME ),
				'menu_name' 		=> __( 'Categories', PLUGIN_NAME ),
			),
			'hierarchical' => true,
			'public' => true,
			'rewrite' => true
		)
	);

	register_taxonomy(
		'vr_gallerys_tags',
		'vr_gallerys',
		array(
			'labels' => array(
				'name' 				=> __( 'Sliders Tags', PLUGIN_NAME ),
				'singular_name' 	=> __( 'Tag', PLUGIN_NAME ),
				'search_items'  	=> __( 'Search Tags', PLUGIN_NAME ),
				'all_items' 		=> __( 'All Tags', PLUGIN_NAME ),
				'parent_item' 		=> __( 'Parent Tag', PLUGIN_NAME ),
				'parent_item_colon'	=> __( 'Parent Tag:', PLUGIN_NAME ),
				'edit_item' 		=> __( 'Edit Tag', PLUGIN_NAME ),
				'update_item' 		=> __( 'Update Tag', PLUGIN_NAME ),
				'add_new_item' 		=> __( 'Add New Tag', PLUGIN_NAME ),
				'new_item_name' 	=> __( 'New Tag Name', PLUGIN_NAME ),
				'menu_name' 		=> __( 'Tags', PLUGIN_NAME ),
			 ),
			'hierarchical' => false,
			'public' => true,
			'rewrite' => true
		)
	);

}
add_action( 'init', 'vr_register_post_gallerys' );
$vr_gallerys_metabox = new VP_Metabox( plugin_dir_path( __FILE__ ) . 'gallerys_metabox.php' );

// Gallerys Shortcode
function vr_shortcode_gallerys( $atts ) {

	extract( shortcode_atts( array(
		'column'			=> '',
		'pagination'		=> false,
		'module'			=> '',
		'category'			=> '',
		'posts_per_page'	=> '',
		'ani'				=> '',
		'animation'			=> ''
	), $atts ) );

	if ( $animation != '' ) { $animation = 'data-animation="' . $animation . '"'; $ani = 'fx-start'; }
	if ( $module == 'uneven' || $posts_per_page == 'uneven9' ) { $posts_per_page = 9; }
	if ( $module == 'uneven5' ) { $posts_per_page = 5; }
	if ( $module == 'uneven12' ) { $posts_per_page = 12; }
	
	global $wp_query;
	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$wp_query = new WP_Query( array(
		'post_type'			        => 'vr_gallerys',
		'posts_per_page'	        => esc_attr( $posts_per_page ),
		'post_status' 		        => 'publish',
		'vr_gallerys_categories'    => esc_attr( $category ),
		'orderby'			        => 'date',
		'order'				        => 'DESC',
		'paged'				        => $paged
	) );

	ob_start(); ?>
	
	<?php if ( $module == 'sortable' ) : ?>
		
		<div class="gallery-sortable clearfix <?php echo sanitize_html_class( $ani ); ?>" <?php echo $animation; ?>>
		
			<div class="gallery-filter">
				<ul>
					<li class="active"><a href="#" data-type="all"><?php _e( 'All', PLUGIN_NAME ); ?></a></li>
					<?php $categories = get_categories( array( 'taxonomy'=> 'vr_gallerys_categories', 'title_li'=>'' ) );
					foreach ( $categories as $category ) {
						if ( $category->name != '' ) {
					?>
							<li><a href="#" data-type="<?php echo $category->slug; ?>"><?php echo $category->name; ?></a></li>
					<?php 
						}
					}
					?>
				</ul>
			</div>

			<?php $i = 1; ?>
			<div class="gallery-list col-md-12">
				<div class="row gallery-row">
				
					<?php 
					while ( $wp_query->have_posts() ) : $wp_query->the_post();

						$terms = get_the_terms( get_the_ID(), 'vr_gallerys_categories' );
						$vr_term_slug;
						$vr_term_name;
						if ( $terms != null  ) {
							foreach ( $terms as $term ) {
								$vr_term_slug =  $term->slug;
								$vr_term_name =  $term->name;
								unset( $term );
							}
						}
					?>

						<article class="gallery-full col-sm-6 <?php echo wp_strip_all_tags( $column ); ?>" data-id="id-<?php echo $i; ?>" data-type="<?php echo wp_strip_all_tags( $vr_term_slug ); ?>">
							<div class="thumbnail fx-hover">
								<div class="fx-hover-back"></div>
								<div class="fx-hover-content">
									<h3><a href="<?php echo get_the_permalink(); ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
									<p><?php echo wp_strip_all_tags( $vr_term_name ); ?></p>
									<div class="line"></div>
								</div>
								<div class="fx-hover-icon">
									<a class="icon-medium icon-main" href="<?php echo aio_src( get_the_ID() ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
									<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
								</div>
								<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 400, 400, true, true, true ) ); ?>" width="400" height="400" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
							</div>
						</article>
					<?php 
						$i++;
					endwhile;
					?>
				</div>
			</div>

		</div>
		
		<!-- begin .pagination -->
		<nav class="pagination">
			<?php
			echo paginate_links( array(
				'base'               => str_replace( PHP_INT_MAX, '%#%', esc_url( get_pagenum_link( PHP_INT_MAX ) ) ),
				'format'             => '?paged=%#%',
				'current'            => max( 1, get_query_var( 'paged' ) ),
				'total'              => $wp_query->max_num_pages,
				'type'               => 'list',
				'prev_text'	=> __( 'Prev', PLUGIN_NAME ),
				'next_text'	=> __( 'Next', PLUGIN_NAME ),
			));
			?>
		</nav>
		<!-- end .pagination -->
		
	<?php elseif ( $module == 'mansonry' ) : ?>
		
		<div class="row">
		<div class="gallery-mansonry clearfix">
			<?php
			while ( $wp_query->have_posts() ) : $wp_query->the_post();

				$terms = get_the_terms( get_the_ID(), 'vr_gallerys_categories' );
				$vr_term_slug;
				$vr_term_name;
				if ( $terms != null  ) {
					foreach ( $terms as $term ) {
						$vr_term_slug =  $term->slug;
						$vr_term_name =  $term->name;
						unset( $term );
					}
				}
			?>

				<article class="gallery-box <?php echo wp_strip_all_tags( $column ); ?>">
					<div class="inner inner-hover clearfix">
						<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
							<div class="thumbnail fx-hover">
								<div class="fx-hover-back"></div>
								<div class="fx-hover-icon">
									<a class="icon-medium icon-main" href="<?php echo aio_src( get_the_ID() ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
									<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
								</div>
								<img src="<?php echo aio_resize( aio_src( get_the_ID() ), 400, 320, true, true, true ); ?>" width="400" height="320" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
							</div>
						<?php endif; ?>
						<h3><a href="<?php echo get_the_permalink(); ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
						<p><?php echo wp_strip_all_tags( $vr_term_name ); ?></p>
					</div>
				</article>

			<?php endwhile; ?>
		</div>
		</div>
		
		<!-- begin .pagination -->
		<nav class="pagination">
			<?php
			echo paginate_links( array(
				'base'               => str_replace( PHP_INT_MAX, '%#%', esc_url( get_pagenum_link( PHP_INT_MAX ) ) ),
				'format'             => '?paged=%#%',
				'current'            => max( 1, get_query_var( 'paged' ) ),
				'total'              => $wp_query->max_num_pages,
				'type'               => 'list',
				'prev_text'	=> __( 'Prev', PLUGIN_NAME ),
				'next_text'	=> __( 'Next', PLUGIN_NAME ),
			));
			?>
		</nav>
		<!-- end .pagination -->

		
	<?php elseif ( $module == 'uneven5' ) : ?>
	
		<?php $num = 1; ?>
		<div class="gallery-unevens clearfix  ">
			<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();

			$terms = get_the_terms( get_the_ID() , 'vr_gallerys_categories' );
			$vr_term_slug;
			$vr_term_name;
			if ( $terms != null  ) {
				foreach ( $terms as $term ) {
					$vr_term_slug = $term->slug;
					$vr_term_name = $term->name;
					unset( $term );
				}
			}
			?>

				<?php 
				echo aio_uneven_5( $num, aio_src( get_the_ID() ), $animation ); 
				$num = $num + 1;
				?>
				<div class="thumbnail fx-hover">
					<div class="fx-hover-back"></div>
					<div class="fx-hover-content">
						<a href="<?php echo get_the_permalink(); ?>"><h3><?php echo wp_strip_all_tags( get_the_title() ); ?></h3></a>
						<p><?php echo wp_strip_all_tags( $vr_term_name ); ?></p>
						<div class="line"></div>
					</div>
					<div class="fx-hover-icon">
						<a class="icon-medium icon-main" href="<?php echo aio_src( get_the_ID() ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
						<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
					</div>
				</div>
			</article>
			
		<?php endwhile; ?>
		</div>
		
	<?php elseif ( $module == 'uneven9' ) : ?>
	
		<?php $num = 1; ?>
		<div class="gallery-unevens clearfix  ">
			<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();

			$terms = get_the_terms( get_the_ID() , 'vr_gallerys_categories' );
			$vr_term_slug;
			$vr_term_name;
			if ( $terms != null  ) {
				foreach ( $terms as $term ) {
					$vr_term_slug = $term->slug;
					$vr_term_name = $term->name;
					unset( $term );
				}
			}
			?>

				<?php 
				echo aio_uneven_9( $num, aio_src( get_the_ID() ), $animation ); 
				$num = $num + 1;
				?>
				<div class="thumbnail fx-hover">
					<div class="fx-hover-back"></div>
					<div class="fx-hover-content">
						<a href="<?php echo get_the_permalink(); ?>"><h3><?php echo wp_strip_all_tags( get_the_title() ); ?></h3></a>
						<p><?php echo wp_strip_all_tags( $vr_term_name ); ?></p><div class="line"></div>
					</div>
					<div class="fx-hover-icon">
						<a class="icon-medium icon-main" href="<?php echo aio_src( get_the_ID() ); ?> " data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
						<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
					</div>
				</div>
			</article>
			
		<?php endwhile; ?>
		</div>
		
	<?php elseif ( $module == 'widget4' ) :  ?>

		<div class="gallery-widget clearfix">
			<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
				<article class="gallery-widget-4">
					<div class="thumbnail fx-hover">
						<div class="fx-hover-back"></div>
						<div class="fx-hover-icon">
							<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery-widget" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
						</div>
						<img src="<?php echo esc_url( aio_resize(aio_src( get_the_ID()), 400, 400, true, true, true) ); ?>" width="400" height="400" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
					</div>
				</article>
			<?php endwhile; ?>
		</div>

	<?php elseif ( $module == 'widget6' ) :  ?>

		<div class="gallery-widget clearfix">
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			<article class="gallery-widget-6">
				<div class="thumbnail fx-hover">
					<div class="fx-hover-back"></div>
					<div class="fx-hover-icon">
						<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery-widget" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
					</div>
					<img src="<?php echo esc_url( aio_resize(aio_src( get_the_ID()), 400, 400, true, true, true) ); ?>" width="400" height="400" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
				</div>
			</article>
		<?php endwhile; ?>
		</div>

	<?php else : ?>
		
		<div class="row row-equal row-full fx">
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
		
			<?php
			$terms = get_the_terms( get_the_ID() , 'vr_gallerys_categories' );
			$vr_term_slug;
			$vr_term_name;
			if ( $terms != null  ) {
				foreach ( $terms as $term ) {
					$vr_term_slug =  $term->slug;
					$vr_term_name =  $term->name;
					unset( $term );
				}
			}
			?>

			<article class="gallery-full column-equal <?php echo wp_strip_all_tags( $column ); ?> <?php echo sanitize_html_class( $ani ); ?>" <?php echo $animation; ?>>
				<div class="thumbnail fx-hover">
					<div class="fx-hover-back"></div>
					<div class="fx-hover-content">
						<a href="<?php echo get_the_permalink(); ?>"><h3><?php echo wp_strip_all_tags( get_the_title() ); ?></h3></a>
						<p><?php echo wp_strip_all_tags( $vr_term_name ); ?></p>
						<div class="line"></div>
					</div>
					<div class="fx-hover-icon">
						<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
						<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
					</div>
					<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 400, 400, true, true, true ) ); ?>" width="400" height="400" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
				</div>
			</article>
		
		<?php endwhile; ?>
		</div>

	<?php endif; ?>
		
	<?php
	wp_reset_query();
	$html = ob_get_clean();
	return $html;
}
add_shortcode( 'shortcode_gallerys', 'vr_shortcode_gallerys' );
