<?php

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

function shrk_sc_styled_map($atts) {

	$output = '';
	$data_atts = array();

	extract(shortcode_atts(array(
		'title' => '',
		'coord' => '47.376848,8.535347',
		'height' => 450,
		'zoom' => 16,
		'type' => 'ROADMAP',
		'mapstyle' => 'theme',
		'scrollwheel' => 'no',
		'marker' => 'no',
		'marker_icon' => '',
		'infowindowtitle' => '',
		'infowindowhtml' => '',
		'el_class' => ''
	), $atts));

	 //Enqueue google maps api script
	wp_enqueue_script( 'google-maps-api' );

	if ( $marker_icon && trim( $marker_icon ) != '') {
		$marker_icon = wp_get_attachment_image_src( $marker_icon, 'full' );
		$marker_icon = $marker_icon[0];
	}

	if ( !empty( $coord ) ) { $data_atts[] = 'data-map-coord="' . esc_attr( $coord ) . '"'; }
	if ( !empty( $zoom ) ) { $data_atts[] = 'data-map-zoom="' . esc_attr( $zoom ) . '"'; }
	if ( !empty( $type ) ) { $data_atts[] = 'data-map-type="' . esc_attr( $type ) . '"'; }
	if ( !empty( $mapstyle ) ) { $data_atts[] = 'data-map-mapstyle="' . esc_attr( $mapstyle ) . '"'; }
	if ( !empty( $scrollwheel ) ) { $data_atts[] = 'data-map-scrollwheel="' . esc_attr( $scrollwheel ) . '"'; }
	if ( !empty( $marker ) ) { $data_atts[] = 'data-map-marker="' . esc_attr( $marker ) . '"'; }
	if ( !empty( $infowindowtitle ) ) { $data_atts[] = 'data-map-infowindowtitle="' . esc_attr( $infowindowtitle ) . '"'; }
	if ( !empty( $marker_icon ) ) { $data_atts[] = 'data-map-marker-icon="' . esc_url( $marker_icon ) . '"'; }

	$output .= '<div class="shrk-styled-map-wrapper ' . esc_attr( $el_class ) . '">';
	if ( '' != trim( $title ) ) {
		$output .= '<h1>' . esc_html( $title ) . '</h1>';
	}
	$output .= '<div class="shrk-styled-map" style="height: ' . intval(trim($height)) . 'px; width: 100%;" ' . implode(' ', $data_atts) . '></div>';
	if ( '' != trim($infowindowhtml) && '' != trim(base64_decode($infowindowhtml)) ) {
		$output .= '<div class="infowindow-content" style="display: none;">' . urldecode(base64_decode($infowindowhtml)) . '</div>';
	}
	$output .= '</div>';
	return $output;
}

add_action( 'init', function () {
	add_shortcode('shrk_styled_map', 'shrk_sc_styled_map');
});

add_filter( 'shrk_filter_vc_mappings', function ( $shrk_vc_shortcodes ) {
	global $allowedtags;

	$shrk_vc_shortcodes["shrk_styled_map"] =  array(
		"name" => esc_html__("Styled Map", 'dvl-uxshop-shortcodes'), 
		"base" => "shrk_styled_map",
		"category" => esc_html__('Theme Specific', 'dvl-uxshop-shortcodes'),
		"icon" => "icon-wpb-shrk",
		"show_settings_on_create" => true,
		"params" => array(
			array(
				"type" => "textfield",
				"heading" => esc_html__("Title", 'dvl-uxshop-shortcodes'),
				"param_name" => "title",
				"description" => esc_html__("What text to use as a map title. Leave blank if no title is needed.", 'dvl-uxshop-shortcodes')
				),
			array(
				"type" => "textfield",
				"heading" => esc_html__("Map Height (px)", 'dvl-uxshop-shortcodes'),
				"param_name" => "height",
				"description" => esc_html__("The height of the map (in pixels)", 'dvl-uxshop-shortcodes'),
				"value" => "450",
				"save_always" => true
				),                    
			array(
				"type" => "textfield",
				"heading" => esc_html__("Map Center", 'dvl-uxshop-shortcodes'),
				"param_name" => "coord",
				"description" => wp_kses( __("Coordinates in the form of latitude and longitude comma separated pair (for example: 51.509811, -0.114380 ). <a href='https://support.google.com/maps/answer/18539' target='_blank'>Get coordinates of an address</a>", 'dvl-uxshop-shortcodes'), $allowedtags )
				),
			array(
				"type" => "dropdown",
				"heading" => esc_html__("Map Zoom level", 'dvl-uxshop-shortcodes'),
				"param_name" => "zoom",
				"description" => esc_html__("The map zoom level. The bigger the number the more zoom'ed in the map will be.", 'dvl-uxshop-shortcodes'),
				"value" => array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" )
				),
			array(
				"type" => "dropdown",
				"heading" => esc_html__("Map type", 'dvl-uxshop-shortcodes'),
				"param_name" => "type",
				"value" => array( 
					esc_html__("Roadmap", 'dvl-uxshop-shortcodes') => "ROADMAP",
					esc_html__("Satellite", 'dvl-uxshop-shortcodes') => "SATELLITE", 
					esc_html__("Hybrid", 'dvl-uxshop-shortcodes') => "HYBRID",
					esc_html__("Terrain", 'dvl-uxshop-shortcodes') => "TERRAIN"

					),
				"description" => esc_html__("Initial map type", 'dvl-uxshop-shortcodes')
				),
			array(
				"type" => 'dropdown',
				"heading" => esc_html__("Show marker", 'dvl-uxshop-shortcodes'),
				"param_name" => "marker",
				"description" => esc_html__("Show a marker at the center of the map.", 'dvl-uxshop-shortcodes'),
				"value" => Array(esc_html__("Yes, show a marker", 'dvl-uxshop-shortcodes') => 'yes', esc_html__("No don't show a marker", 'dvl-uxshop-shortcodes') => 'no'),
				"save_always" => true
				),
			array(
				"type" => "attach_image",
				"class" => "",
				"heading" => esc_html__("Marker icon", 'dvl-uxshop-shortcodes'),
				"description" => esc_html__("Select a custom icon for the marker", 'dvl-uxshop-shortcodes'),
				"param_name" => "marker_icon",
				"value" => '',
				),
			array(
				"type" => "textfield",
				"heading" => esc_html__("Marker title", 'dvl-uxshop-shortcodes'),
				"param_name" => "infowindowtitle",
				"description" => esc_html__("The marker title. Leave empty if you don't need one.", 'dvl-uxshop-shortcodes'),
				"dependency" => Array('element' => "marker", 'value' => array('yes'))
				),                    
			array(
				"type" => "textarea_raw_html",
				"heading" => esc_html__("Info Window Content", 'dvl-uxshop-shortcodes'),
				"param_name" => "infowindowhtml",
				"description" => esc_html__("The content of the info window that pops up when you click at the marker. You can use html. Leave empty for no info window.", 'dvl-uxshop-shortcodes'),
				"dependency" => Array('element' => "marker", 'value' => array('yes'))
				),
			array(
				"type" => 'checkbox',
				"heading" => esc_html__("Scrollwheel zoom", 'dvl-uxshop-shortcodes'),
				"param_name" => "scrollwheel",
				"description" => esc_html__("Use mouse scrollwheel as zoom control, leave unchecked so that page scrolling doesn't interfere with the map.", 'dvl-uxshop-shortcodes'),
				"value" => Array(esc_html__("Yes", 'dvl-uxshop-shortcodes') => 'yes'),
				"save_always" => true,
				),
			array(
				"type" => 'dropdown',
				"heading" => esc_html__("Applied Map style", 'dvl-uxshop-shortcodes'),
				"param_name" => "mapstyle",
				"description" => esc_html__("You can select between the default map style of the theme, the default (official) google maps style, or you can set up your own under 'theme options'\\'Advanced'.", 'dvl-uxshop-shortcodes'),
				"value" => Array(
					esc_html__("Theme map style", 'dvl-uxshop-shortcodes') => 'theme',
					esc_html__("Default map style (official)", 'dvl-uxshop-shortcodes') => 'default',
					esc_html__("Custom map style", 'dvl-uxshop-shortcodes') => 'custom'
					)
				),
			array(
				"type" => "textfield",
				"heading" => esc_html__("Extra class name", 'dvl-uxshop-shortcodes'),
				"param_name" => "el_class",
				"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')
				)
			)
);

	return $shrk_vc_shortcodes;
} );

?>