<?php
/**
* Plugin Name:       Product Gallery Slider For WooCommerce
* Plugin URI:        
* Description:       Beautiful image and video gallery slider for WooCommerce products.
* Version:           1.9
* Requires at least: 5.0
* Requires PHP:      7.4
* Author:            Themesvila
* Author URI:        https://themesvila.com/
* License:           GPL v2 or later
* License URI:       https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain:       product-gallery-slider
*/
 
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

define( 'PGSFW_INC', plugin_dir_path( __FILE__ ) . 'inc/' );

class BHWC_Product_Gallery{
	public function __construct(){		
		add_action('wp_enqueue_scripts' , array($this , 'pgsfw_enqueue_scripts'));
		add_filter( 'wc_get_template', array($this, 'wcspg_display_custom_gallery' ), 10, 5 );
		add_action('admin_enqueue_scripts' , array($this , 'bhpgs_admin_scripts'));
		add_action('wp_head' , array($this , 'pgsfw_gallery_option_css'));
		add_action('after_setup_theme' , array($this , 'themesetup'));
		add_action( 'init', array($this, 'bhgallery_load_textdomain') );
		add_action('init' , array($this , 'include_files'));		
		add_filter('attachment_fields_to_edit', array($this , 'pgsw_ytvideo_url_add_field'), 10, 2);
		add_filter('attachment_fields_to_save', array($this , 'pgsw_ytvideo_url_save_field'), 10, 2);
		add_filter( 'plugin_action_links_'.plugin_basename(__FILE__), array( $this, 'bhgallery_slider_settings_link' ) );		
	}

	// Add youtube field in media edit screen
	public function pgsw_ytvideo_url_add_field($form_fields, $post) {
		$form_fields['pgsw_youtube_url_field'] = [
			'label' => 'Youtube ID',
			'input' => 'text',
			'value' => get_post_meta($post->ID, '_pgsw_youtube_url_field', true),
			'helps' => 'Enter Youtube Video ID'
		];
		return $form_fields;
	}

	// Save custom field
	public function pgsw_ytvideo_url_save_field($post, $attachment) {
		if (isset($attachment['pgsw_youtube_url_field'])) {
			update_post_meta($post['ID'], '_pgsw_youtube_url_field', sanitize_text_field($attachment['pgsw_youtube_url_field']));
		}
		return $post;
	}	
	
	function bhgallery_load_textdomain() {
		load_plugin_textdomain( 'product-gallery-slider', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 
	}
	
	public function bhgallery_slider_settings_link($links) {
	    $newlink = sprintf("<a href='%s'>%s</a>",'admin.php?page=gallerysettings',__('Settings','product-gallery-slider'));
	    $links[] = $newlink;
	    return $links;
	}
	

/**
 * Include JS Files 
 */
public function pgsfw_enqueue_scripts() {
	
	
	if (!is_admin()) {	
		if ( class_exists( 'WooCommerce' ) && is_product() ) {
			wp_enqueue_style('pgsfw-fancybox', plugins_url('css/fancybox.css', __FILE__),'1.1.0', true);
			wp_enqueue_style('pgsfw-slick', plugins_url('css/slick.css', __FILE__),'1.0.0', true);
			wp_enqueue_style('pgsfw-slick-theme', plugins_url('css/slick-theme.css', __FILE__),'1.0.0', true);
			wp_enqueue_style('pgsfw-style', plugins_url('css/style.css', __FILE__),'1.0.0', true);
			wp_enqueue_style('pgsfw-fontawesome-css', '//use.fontawesome.com/releases/v5.11.2/css/all.css','4.7.0', true);
			wp_enqueue_script('pgsfw-fancybox', plugins_url('js/fancybox.js', __FILE__),array('jquery'),'1.1.0', true);
			wp_enqueue_script('pgsfw-slick', plugins_url('js/slick.js', __FILE__),array('jquery'),'1.1.0', true);
			wp_register_script('pgsfw-script', plugins_url('js/pgsfw-script.js', __FILE__),array('jquery'),'1.0', true);
			
			
			$marrow_disable = get_option('pgsfw_msadisable');
			$garrow_disable = get_option('pgsfw_tadisable');
			$gallery_layout = get_option('pgsfw_layout');
			$gallery_img = get_option('pgsfw_dgimg');
			$video_height = get_option('pgsfw_dgvgh');

			$translation_array = array(
				'pgsfw_marrow_disable'   => $marrow_disable,
				'pgsfw_garrow_disable'   => $garrow_disable,
				'pgsfw_gallery_vertical'  => $gallery_layout,
				'pgsfw_gallery_img'   => $gallery_img,
				'video_height'   => $video_height,
			);
			
			wp_localize_script( 'pgsfw-script', 'object_name', $translation_array );
			
			// Enqueued script with localized data.
			wp_enqueue_script( 'pgsfw-script' );
		}
	}
}

	/*
	* Admin Scripts
	*/
	public function bhpgs_admin_scripts(){

		// Define	
		define('BHPGFILADMINPATH' , plugins_url('inc/admin/' , __FILE__) );	
		
		wp_enqueue_style( 'bhpgs-admin-style', BHPGFILADMINPATH . 'css/admin-style.css', false, "1.0.0");
		  

		// Color Picker JS
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'active_color_picker', BHPGFILADMINPATH . 'js/active_color_picker.js' , array( 'jquery', 'wp-color-picker' ), '', true  );

	}	

		/**
		 * @param $located
		 * @param $template_name
		 * @param $args
		 * @param $template_path
		 * @param $default_path
		 */
		public function wcspg_display_custom_gallery( $located, $template_name, $args, $template_path, $default_path ) {
			if ( 'single-product/product-image.php' == $template_name ) {
				$located = PGSFW_INC . 'product-image.php';
			}		
			
			return $located;
		}
	

	public function include_files(){
		// Add Admin Page
		include_once(PGSFW_INC. '/admin/admin.php');
	}

	public function themesetup() {
		add_theme_support('wc-product-gallery-zoom');
	}
	
	// Adding Preloader options css
	public function pgsfw_gallery_option_css(){
		$arrowleft = get_option('pgsfw_salp');
		$arrowright = get_option('pgsfw_sarp');
		$abg_color = get_option('arbgcolor');
		$aricolor = get_option('aricolor');
		$harbgcolor = get_option('harbgcolor');
		$haricolor = get_option('haricolor');
		$mslidersize = get_option('mslidersize');
		$tarrsize = get_option('tarrsize');
		
		?>

		<style type="text/css">		
			
			.pgsfw-slider-for .slick-prev.slick-arrow{
				<?php 
				if(isset($arrowleft)){
					echo 'left:' . esc_html($arrowleft) . ';';
				}?>
			}
			.pgsfw-slider-for .slick-next.slick-arrow{
				<?php 
				if(isset($arrowright)){
					echo 'right:' . esc_html($arrowright) . ';';
				}?>
			}		
			
			.pgsfw-slider-for .slick-arrow{
				<?php 
					if(isset($abg_color)){
						echo 'background:' . esc_html($abg_color) . ';';
					} 
		
					if(isset($abg_color)){
						echo 'border-color:' . esc_html($abg_color) . ';';
					} 
		
					if(isset($mslidersize)){
						echo 'width:' . esc_html($mslidersize) . ';';
					} 
		
					if(isset($mslidersize)){
						echo 'height:' . esc_html($mslidersize) . ';';
					} 
		
					if(isset($mslidersize)){
						echo 'line-height:' . esc_html($mslidersize) . ';';
					} 
				
				?>
				}
			
		#pgsfw-gallery .slick-arrow {
				<?php 
					if(isset($abg_color)){
						echo 'background:' . esc_html($abg_color) . ';';
					} 
		
					if(isset($abg_color)){
						echo 'border-color:' . esc_html($abg_color) . ';';
					} 
		
					if(isset($tarrsize)){
						echo 'width:' . esc_html($tarrsize) . ';';
					} 
		
					if(isset($mslidersize)){
						echo 'height:' . esc_html($tarrsize) . ';';
					} 
		
					if(isset($tarrsize)){
						echo 'line-height:' . esc_html($tarrsize) . ';';
					} 
				
				?>
				}			
			
			.pgsfw-slider-for .slick-arrow:hover,
			.pgsfw-slider-for .slick-arrow:focus,
			#pgsfw-gallery .slick-arrow:hover,
			#pgsfw-gallery .slick-arrow:focus{
				<?php 
					if(isset($harbgcolor)){
						echo 'background:' . esc_html($harbgcolor) . ';';
					} 
		
					if(isset($harbgcolor)){
						echo 'border-color:' . esc_html($harbgcolor) . ';';
					} 
				
				?>
			}
			.pgsfw-slider-for .slick-prev::before,
			.pgsfw-slider-for .slick-next::before,
			#pgsfw-gallery .slick-prev::before, 
			#pgsfw-gallery .slick-next::before{
				<?php 
					if(isset($aricolor)){
						echo 'color:' . esc_html($aricolor) . ';';
					} 
		
				?>				
			}
			
			.pgsfw-slider-for .slick-prev:hover:before,
			.pgsfw-slider-for .slick-next:hover:before,
			#pgsfw-gallery .slick-prev:hover:before, 
			#pgsfw-gallery .slick-next:hover:before{
				<?php 
					if(isset($haricolor)){
						echo 'color:' . esc_html($haricolor) . ';';
					} 
		
				?>				
			}
		
		</style>
	<?php	
	}

}


new BHWC_Product_Gallery();