<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function shrk_sc_banner( $atts ) {

	if ( !function_exists( 'get_woocommerce_term_meta' ) || !function_exists( 'wp_get_attachment_image_srcset' ) || !function_exists( 'shrk_get_image_aspect' ) ) {
		return;
	}
	
	$target_title = $heading = $img_src = $srcset = $img_ar = '';
	$style_atts = array();
	extract( shortcode_atts( array(
		'banner_source'    => 'custom',
		'product_cat'      => '', // product_cat overrides product_cat_slug
		'product_cat_slug' => '',
		'product_id'       => '',
		'banner_image'     => '',
		'banner_height'    => '300px',
		'heading_text'     => '',
		'font_size'        => 'medium',
		'target_url'       => '',
		'el_class'         => '',
		'banner_style'     => 'style-1',
		'css'              => ''
	), $atts ) );

	switch ( $banner_source ) {
		case 'product_category':
			$product_cat = absint( trim( $product_cat ) );
			$term        = !empty( $product_cat ) ? get_term_by( 'id', $product_cat, 'product_cat' ) : get_term_by( 'slug', $product_cat_slug, 'product_cat' );
			if ( $term ) {
				$img_id      = get_woocommerce_term_meta( absint($term->term_id), 'thumbnail_id', true );
				$img_src     = function_exists( 'woo_shrk_get_category_image' ) ? woo_shrk_get_category_image( absint($term->term_id), 'shrk_section_bg' ) : '';
				$target_url  = get_term_link( $term );
				$heading     = $term->name; 
				$srcset      = wp_get_attachment_image_srcset( $img_id, 'shrk_section_bg' );
				$img_ar      = shrk_get_image_aspect( $img_id, 'shrk_section_bg' );
			}

			break;
		case 'product':
			$product_id = absint( trim( $product_id ) );
			$heading    = get_the_title( $product_id );
			$target_url = get_permalink( $product_id );

			if ( function_exists( 'shrk_get_meta_option' ) && shrk_get_meta_option( 'product_cover_image', $product_id ) ) {
				$cover_image_id = shrk_get_meta_option( 'product_cover_image', $product_id );
				$cover          = wp_get_attachment_image_src( $cover_image_id, 'shrk_section_bg' );
				$srcset         = wp_get_attachment_image_srcset( $cover_image_id, 'shrk_section_bg' );
				$img_src        = $cover[0];
				$img_ar         = shrk_get_image_aspect( $cover_image_id, 'shrk_section_bg' );
			}

			break;
		case 'custom':
			$heading      = $heading_text;
			if ( function_exists( 'vc_build_link' ) ) {
				$target       = function_exists( 'vc_build_link' ) ? vc_build_link( $target_url ) : '';
				$target_url   = $target['url'];
			}
			$target_title = isset( $target['title'] ) ? $target['title'] : '';
			$img          = wp_get_attachment_image_src( preg_replace( '/[^\d]/', '', $banner_image ), 'shrk_section_bg' );
			$srcset       = wp_get_attachment_image_srcset( preg_replace( '/[^\d]/', '', $banner_image ), 'shrk_section_bg' );
			$img_src      = $img[0];
			$img_ar       = shrk_get_image_aspect( preg_replace( '/[^\d]/', '', $banner_image ), 'shrk_section_bg' );


			break;
		default:
			$heading = 'No source set';
			$img_src = '';
			$img_ar  = '';

			break;
	}

	$el_class  = $el_class . ' ' . $banner_style;

	if ( function_exists( 'vc_shortcode_custom_css_class') ) {
		$el_class .= ' ' . apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, vc_shortcode_custom_css_class( $css, ' ' ), 'shrk_banner', $atts );
	}

	if ( !empty( $banner_height ) ) {
		$style_atts[] = 'height: ' . esc_attr( $banner_height ) . ';';
	}
	
	$output = '<div class="shrk-banner lazyload-bg lazyload-on-visible' . esc_attr( $el_class ) .'" data-image-bg="' . esc_url( $img_src ) . '" style="' . esc_attr( implode( ' ', $style_atts ) ) . '" data-srcset="' . esc_attr( $srcset ) . '" data-image-ar="' . esc_attr( $img_ar ) . '">';
	
	if ( !empty( $target_url ) ) {
		$output .= '<a class="banner-target" title="' . esc_attr( $target_title ) . '" href="' . esc_url( $target_url ) . '">';
	}
	$output .= '<h2 class="banner-heading responsive-heading ' . esc_attr( $font_size ) . '">' . esc_html( $heading ) . '</h2>';

	if ( !empty( $target_url ) ) {
		$output .= '</a>';
	}

	$output .= '</div>';
	return $output;

} 

add_action( 'init', function () {
	add_shortcode('shrk_banner', 'shrk_sc_banner');
});

add_filter( 'shrk_filter_vc_mappings', function ( $shrk_vc_shortcodes, $d ) {

	$shrk_vc_shortcodes["shrk_banner"] =  array(
		"name"                    => esc_html__("Banner", 'dvl-uxshop-shortcodes'), /* */
		"category"                => esc_html__('Theme Specific', 'dvl-uxshop-shortcodes'),
		"base"                    => "shrk_banner",
		"icon"                    => "icon-wpb-shrk",
		"show_settings_on_create" => true,
		"params"                  => array(
			array(
				"type"       => "dropdown",
				"heading"    => esc_html__("Data source", 'dvl-uxshop-shortcodes'),
				"param_name" => "banner_source",
				"value"      => array(
					esc_html__("Product category", 'dvl-uxshop-shortcodes') => "product_category",
					esc_html__("Product", 'dvl-uxshop-shortcodes')          => "product",
					esc_html__("Custom", 'dvl-uxshop-shortcodes')           => "custom",
				),
				"save_always" => true, //required to save default value after vc 4.9.1
			),
			array(
				"type"        => "woo_categories",
				"heading"     => esc_html__("Product category", 'dvl-uxshop-shortcodes'),
				"description" => esc_html__("Select the product category to get the title and image from.", 'dvl-uxshop-shortcodes'),
				"param_name"  => "product_cat",
				"multiple"    => false,
				"output"      => "id",
				"value"       => "",
				"dependency"  => Array( 'element' => "banner_source", 'value' => array( 'product_category' ) ),
			),
			array(
				"type"        => "woo_products",
				"heading"     => esc_html__("Product", 'dvl-uxshop-shortcodes'),
				"description" => esc_html__("Select the product from which you want to get the title and image cover.", 'dvl-uxshop-shortcodes'),
				"param_name"  => "product_id",
				"multiple"    => false,
				"value"       => "",
				"dependency"  => Array( 'element' => "banner_source", 'value' => array( 'product' ) ),
				),
			array(
				"type"       => "attach_image",
				"heading"    => esc_html__("Banner Image", 'dvl-uxshop-shortcodes'),
				"param_name" => "banner_image",
				"dependency" => Array( 'element' => "banner_source", 'value' => array( 'custom' ) ),
				),
			array(
				"type"       => "textfield",
				"heading"    => esc_html__("Heading", 'dvl-uxshop-shortcodes'),
				"param_name" => "heading_text",
				"value"      => '',
				"dependency" => Array( 'element' => "banner_source", 'value' => array( 'custom' ) ),
				),
			array(
				"type"       => "vc_link",
				"heading"    => esc_html__("Target link", 'dvl-uxshop-shortcodes'),
				"param_name" => "target_url",
				"value"      => '',
				"dependency" => Array( 'element' => "banner_source", 'value' => array( 'custom' ) ),
				),
			array(
				"type"        => "dropdown",
				"heading"     => esc_html__("Font size", 'dvl-uxshop-shortcodes'),
				"description" => esc_html__("Override default font size if you wish.", 'dvl-uxshop-shortcodes'),
				"param_name"  => "font_size",
				"value"       => array(
					esc_html__("Large", 'dvl-uxshop-shortcodes')  => "large",
					esc_html__("Medium", 'dvl-uxshop-shortcodes') => "medium",
					esc_html__("Small", 'dvl-uxshop-shortcodes')  => "small",
				),
				"save_always" => true, //required to save default value after vc 4.9.1
				),
			array(
				"type"       => "textfield",
				"heading"    => esc_html__("Banner height", 'dvl-uxshop-shortcodes'),
				"param_name" => "banner_height",
				"value"      => '300px',
				),
			array(
				"type"       => "dropdown",
				"heading"    => esc_html__("Banner style", 'dvl-uxshop-shortcodes'),
				"param_name" => "banner_style",
				"value"      => array(
					esc_html__("Style A", 'dvl-uxshop-shortcodes') => "style-1",
					esc_html__("Style B", 'dvl-uxshop-shortcodes') => "style-2",
				),
				"save_always" => true, //required to save default value after vc 4.9.1
			),
			array(
				"type"        => "textfield",
				"heading"     => esc_html__("Extra class name", 'dvl-uxshop-shortcodes'),
				"description" => esc_html__("If you wish to style particular content element differently, then use this field to add a class name and then refer to it in your css file.", 'dvl-uxshop-shortcodes'),
				"param_name"  => "el_class",
				),
			array(
				'type'       => 'css_editor',
				'heading'    => __( 'Css', 'dvl-uxshop-shortcodes' ),
				'param_name' => 'css',
				'group'      => __( 'Design options', 'dvl-uxshop-shortcodes' ),
				),
			)
		);

	return $shrk_vc_shortcodes;
},10 ,2 );

?>