<?php
/**
 * Main file for the plugin
 *
 * @package Easy_Justified_gallery
 * @version 1.1.1
 */

/**
 * Plugin Name: Easy Justified Gallery
 * Plugin URI: http://imyuvii.com
 * Description: Easiest way to create justified image gallery from your media library. All you have to do is add [easyGallery] ShortCode
 * Version: 1.1.1
 * Author: Yuvraj Jhala
 * Author URI: http://imyuvii.com
 * License: GPL2
 */
require 'inc/functions.php';
use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_shortcode( 'easyGallery', 'webtangl_ejg_short_code' );
register_activation_hook( __FILE__, 'webtangl_ejg_activate' );

add_action( 'carbon_fields_register_fields', 'webtangl_ejg_attach_theme_options' );
/**
 * Loads the plugin options
 *
 * @return void
 */
function webtangl_ejg_attach_theme_options() {
	Container::make( 'theme_options', __( 'EJ Gallery' ) )
		->add_fields(
			array(
				Field::make( 'html', 'webtangl_ejg_info' )
					->set_html( '<p>Select images and configure options in below form. Add <b>[easyGallery]</b> ShortCode to your page or post.</p>' ),
				Field::make( 'text', 'webtangl_ejg_row_height', 'Image Height' )
					->set_attribute( 'type', 'number' )
					->set_default_value( 250 ),
				Field::make( 'text', 'webtangl_ejg_margin', 'Space between images (PX)' )->set_attribute( 'type', 'number' )->set_default_value( 10 ),
				Field::make( 'checkbox', 'webtangl_ejg_caption', 'Display Image Caption?' ),
				Field::make( 'select', 'webtangl_ejg_target', 'Open image in' )->set_options(
					array(
						'Popup'       => 'Popup',
						'New Tab'     => 'New Tab',
						'Same Window' => 'Same Window',
					)
				),
				Field::make( 'media_gallery', 'webtangl_ejg_images', __( 'Gallery Images' ) )->set_type( array( 'image' ) ),
			)
		);
}

add_action( 'after_setup_theme', 'webtangl_ejg_load' );
/**
 * Loads the composer dependencies
 *
 * @return void
 */
function webtangl_ejg_load() {
	require_once 'carbon-fields/vendor/autoload.php';
	\Carbon_Fields\Carbon_Fields::boot();
}

/**
 * This will display additional action links on plugins page
 *
 * @param Link $links array of links.
 * @return mixed
 */
function webtangl_ejg_action_links( $links ) {
	$links[] = '<a href="' . esc_url( get_admin_url( null, 'options-general.php?page=easy-justified-gallery' ) ) . '">Settings</a>';
	return $links;
}

add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'webtangl_ejg_action_links' );

/**
 * Enqueue scripts & CSS
 *
 * @param Hook $hook Hook name.
 * @return void
 */
function webtangl_ejg_scripts( $hook ) {
	if ( ! wp_script_is( 'jquery', 'enqueued' ) ) {
		wp_enqueue_script( 'jquery' );
	}
	wp_enqueue_style( 'egj-style-lightbox', plugins_url( '/assets/css/lightbox.css', __FILE__ ), array(), '1.0.4', 'all' );
	wp_enqueue_style( 'egj-style', plugins_url( '/assets/css/style.css', __FILE__ ), array(), '1.0.4', 'all' );
	wp_enqueue_script( 'egj-script-main', plugins_url( '/assets/js/main.js', __FILE__ ), false, '1.0.4', 'all' );
	wp_enqueue_script( 'egj-script-lightbox', plugins_url( '/assets/js/lightbox.js', __FILE__ ), false, '1.0.4', 'all' );
}
add_action( 'wp_enqueue_scripts', 'webtangl_ejg_scripts' );

/**
 * Front layout
 *
 * @param Atts    $atts Attributes.
 * @param Content $content Content.
 * @return false|string
 */
function webtangl_ejg_short_code( $atts, $content = null ) {
	ob_start();
	$images = carbon_get_theme_option( 'webtangl_ejg_images' );

	if ( carbon_get_theme_option( 'webtangl_ejg_row_height' ) !== '' && intval( carbon_get_theme_option( 'webtangl_ejg_row_height' ) ) > 0 ) {
		$row_height = intval( carbon_get_theme_option( 'webtangl_ejg_row_height' ) );
	} else {
		$row_height = 200;
	}

	/**
	 * Setting up the href target
	 */
	$target = carbon_get_theme_option( 'webtangl_ejg_target' );
	if ( 'New Tab' === $target ) {
		$target = 'target="_BLANK"';
	} elseif ( 'Same Window' === $target ) {
		$target = '';
	} elseif ( strtolower( $target ) === 'popup' ) {
		$target = 'data-popup';
	}

	/**
	 * Getting all the gallery images and storing it in a variable
	 */
	if ( isset( $images[0] ) && '' !== $images[0] ) {
		echo '<div id="webtangl_ejg">';
		foreach ( $images as $image_id ) {
			$image_full_obj  = wp_get_attachment_image_src( $image_id, 'full' );
			$image_thumb_obj = wp_get_attachment_image_src( $image_id, 'medium' );
			$image_full_url  = isset( $image_full_obj[0] ) ? $image_full_obj[0] : null;
			$image_thumb     = isset( $image_thumb_obj[0] ) ? $image_thumb_obj[0] : null;
			$caption         = ( get_bloginfo( 'version' ) >= floatval( '4.6.0' ) ) ? wp_get_attachment_caption( $image_id ) : '&nbsp;';
			echo '<a ' . esc_attr( $target ) . ' rel="gallery1" href="' . esc_url( $image_full_url ) . '">';
			echo '<img alt="' . esc_attr( $caption ) . '" src="' . esc_url( $image_thumb ) . '"/>';
			echo '</a>';
		}
		echo '</div>';
	} else {
		echo '<div><strong>' . esc_html_e( 'Easy Justified Gallery' ) . '</strong><br/> <small>' . esc_html_e( 'Please upload images. To upload images go to ' ) . '<strong><i>Settings >> Justified Gallery >> Browse Images</i></strong></small></div>';
	}
	?>
	<script type="text/javascript">
		jQuery(document).ready(function( $ ) {
			$("#webtangl_ejg").justifiedGallery({
				rowHeight :<?php echo esc_attr( $row_height ); ?>,
				maxRowHeight:<?php echo esc_attr( $row_height ); ?>,
				captions :<?php echo carbon_get_theme_option( 'webtangl_ejg_caption' ) ? 'true' : 'false'; ?>,
				waitThumbnailsLoad :true,
				margins :<?php echo intval( carbon_get_theme_option( 'webtangl_ejg_margin' ) ); ?>
			}).on('jg.complete', function () {
				<?php if ( carbon_get_theme_option( 'webtangl_ejg_target' ) === 'Popup' ) { ?>
				$(this).find('a').colorbox({
					maxWidth : '80%',
					maxHeight : '80%',
					opacity : 0.8,
					transition : 'elastic',
					current : ''
				});
				<?php } ?>
			});
		});
	</script>
	<?php return ob_get_clean();
}
?>
