<?php

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

function shrk_sc_video_popup( $atts ) {
	
	extract( shortcode_atts( array(
		'video_popup_id' => null,
		'video'          => '',
		'autoplay'       => '0',
		'linktext'       => '',
		'linktext'       => '',
		'link_style'     => 'textlink',
		'el_class'       => '',
	), $atts ) );

	wp_enqueue_script( 'shrk-sc-video-popup' );

	$video_popup_id = !empty( $atts['video_popup_id'] ) ? $atts['video_popup_id'] : false;

	if ( $link_style == 'theme-button' ) {
		$el_class .= ' button';
	}

	if ( $link_style == 'theme-button-alt' ) {
		$el_class .= ' button alt';
	}

	return '<a href="' . esc_url( $video ) . '" class="shrk-video-popup ' . $el_class . '" data-video-popup-id="' . esc_attr( $video_popup_id ) . '" data-video-popup-url="' . esc_url( $video ) . '" data-video-autoplay="' . esc_attr( $autoplay ) . '">' . esc_html( $linktext ) . '</a>';
} 

add_action( 'init', function () {
	add_shortcode('shrk_video_popup', 'shrk_sc_video_popup');
});

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

	$shrk_vc_shortcodes["shrk_video_popup"] =  array(
		"name" => esc_html__("Video popup", 'dvl-uxshop-shortcodes'), /* */
		"base" => "shrk_video_popup",
		"category" => esc_html__('Theme Specific', 'dvl-uxshop-shortcodes'),
		"icon" => "icon-wpb-shrk",
		"show_settings_on_create" => true,
		"params" => array(
			array(
				"type" => "textfield",
				"class" => "hidden",
				"param_name" => "video_popup_id",
				"value" => uniqid('video-popup-'),
			),
			array(
				"type"        => "textfield",
				"class"       => "",
				"heading"     => esc_html__("Video URL", 'dvl-uxshop-shortcodes'),
				"param_name"  => "video",
				"description" => esc_html__("Enter the video URL. Youtube and Vimeo are supported.", 'dvl-uxshop-shortcodes'),
				),
			array(
				"type"       => 'checkbox',
				"heading"    => esc_html__("Autoplay video", 'dvl-uxshop-shortcodes'),
				"param_name" => "autoplay",
				"value"      => Array(esc_html__("Yes", 'dvl-uxshop-shortcodes') => 'yes'),
			),
			array(
				"type"        => "textfield",
				"class"       => "",
				"heading"     => esc_html__("Link Text", 'dvl-uxshop-shortcodes'),
				"param_name"  => "linktext",
				"save_always" => true, //required to save default value after vc 4.9.1
				"value"       => esc_html__("Play video", 'dvl-uxshop-shortcodes'),
				),
			array(
				"type"       => "dropdown",
				"class"      => "",
				"heading"    => esc_html__("Link style", 'dvl-uxshop-shortcodes'),
				"param_name" => "link_style",
				"value"      => array(
					esc_html__("Text link", 'dvl-uxshop-shortcodes')                => "textlink",
					esc_html__("Button", 'dvl-uxshop-shortcodes')                   => "theme-button",
					esc_html__("Button - Secondary color", 'dvl-uxshop-shortcodes') => "theme-button-alt",
					),
				),
				array(
					"type"        => "textfield",
					"param_name"  => "el_class",
					"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'),
				),
			)
		);

	return $shrk_vc_shortcodes;
},10 ,2 );

function shrk_sc_video_scripts() {
	wp_register_script( 'shrk-sc-video-popup', plugin_dir_url( __FILE__ ) . 'assets/js/video-popup.js', array( 'jquery' ), false, false );
}
add_action( 'wp_enqueue_scripts', 'shrk_sc_video_scripts' );

// Handle AJAX call
function shrk_sc_ajax_get_video_popup() {

	$video_url      = isset( $_POST['video_url'] ) ? esc_url_raw( $_POST['video_url'] ) : null;
	$video_autoplay = isset( $_POST['video_autoplay'] ) && $_POST['video_autoplay'] == 'yes' ? true : false;

	if ( !$video_url ) {
		wp_die( esc_html__("No Video URL!", 'dvl-uxshop-shortcodes') );
	}

	$embed_code = wp_oembed_get( $video_url );

	if ( $video_autoplay ) {
		$embed_code = preg_replace('@embed/([^"&]*)@', 'embed/$1&showinfo=0&autoplay=1', $embed_code);	// Match youtube
		$embed_code = preg_replace('@video/([^"&]*)@', 'video/$1?autoplay=1', $embed_code);				// Match vimeo
	}

	echo $embed_code;

	wp_die();
}
add_action( 'wp_ajax_shrk_get_video_popup', 'shrk_sc_ajax_get_video_popup' );
add_action( 'wp_ajax_nopriv_shrk_get_video_popup', 'shrk_sc_ajax_get_video_popup' );

?>