
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

/* -----------------------------------------------------------------------------
 * Setup Custom Gallery
 * -------------------------------------------------------------------------- */

add_action( 'wp', 'presso_setup_custom_gallery' );
function presso_setup_custom_gallery() {
	$enable_custom_gallery = presso_get_theme_option( 'enable_custom_gallery', '1' );

	if ( $enable_custom_gallery ) {
		add_filter( 'post_gallery', 'presso_post_gallery_shortcode', 10, 3 );
	}
}





/* -----------------------------------------------------------------------------
 * Render custom post gallery
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_post_gallery_shortcode' ) ) {
	function presso_post_gallery_shortcode( $output = '', $atts, $instance ) {
		extract( shortcode_atts( array(
			'ids'    => '',
			'layout'    => 'grid-6',
		), $atts ) );

		if ( $layout == 'original' ) {
			return $output;
		}

		$ids = explode( ',', $ids );

		ob_start();
		presso_the_gallery( $ids, $layout );

		return ob_get_clean();
	}
}

if ( ! function_exists( 'presso_the_gallery' ) ) {
	function presso_the_gallery( $ids, $layout = 'grid-5', $class = '' ) {
		if ( empty( $ids ) ) return;

		if ( ! is_array( $ids ) ) {
			$ids = array( $ids );
		}

		$additional_class = array( $class );
		$additional_class[] = 'vw-gallery--'.$layout;

		set_query_var( 'gallery_photos', $ids );
		set_query_var( 'gallery_layout', $layout );
		set_query_var( 'gallery_class', $additional_class );

		if ( 0 === strpos( $layout, 'grid') ) {
			get_template_part( 'templates/gallery/grid', str_replace( 'grid-', '', $layout ) );
		} else {
			get_template_part( 'templates/gallery/'.$layout );
		}
	}
}





/* -----------------------------------------------------------------------------
 * Photo Proofing
 * -------------------------------------------------------------------------- */

$presso_meta_key = 'vw_approved_photos';
$presso_is_proofing_enabled = false;

add_action( 'wp', 'presso_init_photo_proofing' );
if ( ! function_exists( 'presso_init_photo_proofing' ) ) {
	function presso_init_photo_proofing() {
		global $presso_is_proofing_enabled, $post;

		if ( is_singular( 'presso_gallery' ) ) {
			$presso_is_proofing_enabled = (bool) get_post_meta( $post->ID, 'vw_enable_photo_proofing', true );
			
			if ( $presso_is_proofing_enabled ) {
				add_action( 'wp_footer', 'presso_setup_photo_proofing' );
			}
		}
	}
}

if ( ! function_exists( 'presso_setup_photo_proofing' ) ) {
	function presso_setup_photo_proofing() {
		wp_nonce_field( 'vw_photo_proofing_nonce', 'vw_photo_proofing_nonce' );
	}
}

if ( ! function_exists( 'presso_is_proofing_enabled' ) ) {
	function presso_is_proofing_enabled() {
		global $presso_is_proofing_enabled;

		return $presso_is_proofing_enabled;
	}
}





/* -----------------------------------------------------------------------------
 * Approve/Unapprove photo
 * -------------------------------------------------------------------------- */

add_action( 'wp_ajax_presso_photo_proofing', 'presso_approve_photo' );
add_action( 'wp_ajax_nopriv_presso_photo_proofing', 'presso_approve_photo' );
if ( ! function_exists( 'presso_approve_photo' ) ) {
	function presso_approve_photo() {
		global $presso_meta_key;

		$nonce = sanitize_text_field( $_GET[ '_wpnonce' ] );
		$method = sanitize_text_field( $_GET[ 'method' ] );
		$galleryid = intval( $_GET[ 'galleryid' ] );
		$photoid = intval( $_GET[ 'photoid' ] );

		if ( wp_verify_nonce( $nonce, 'vw_photo_proofing_nonce' ) ) {

			if ( 'approve' == $method ) {
				$approved_list = get_post_meta( $galleryid, $presso_meta_key );

				if ( ! in_array( $photoid, $approved_list ) ) {
					$approved_list[] = $photoid;
					add_post_meta( $galleryid, $presso_meta_key, $photoid );
				}

				echo 'approved';
				
			} else {
				delete_post_meta( $galleryid, $presso_meta_key, $photoid );

				echo 'rejected';
				
			}
			
		} else {
			echo 'fail';

		}
	}
}