<?php

    if ( ! defined( 'ABSPATH' ) ) exit; //It was a ajax file
    
    if (file_exists("./../../../../wp-load.php")) {
        define('WP_USE_THEMES', false); // Don't load theme support functionality
        require('./../../../../wp-load.php');
    } else {
        require('./../../../../config/config.inc.php');
        require('./../../../../init.php');
        require_once"./../../digitalsignage4.php";
        DigitalSignage4_Translate::setLang(substr(get_bloginfo('language'), 0, 2));
    }


    require_once './DigitalSignage4_DisplayedVideoProduct.php';

    DigitalSignage4_FrontShortcodeVideoDisplayerAjaxHandler::ajax();

    class DigitalSignage4_FrontShortcodeVideoDisplayerAjaxHandler
    {
        public static function ajax()
        {
            if (isset($_GET['idPlaylist']))
            {
                $idPlaylist = sanitize_text_field(wp_unslash($_GET['idPlaylist']));
            }
            $playlist = DigitalSignage4_PlaylistManager::set(DigitalSignage4_caller::getInstance())->get_playlist($idPlaylist);
            $videos = $playlist->get_videos();
                    
                    $videos = array_slice($videos, 0, $playlist->get_limit());

                    $videosMp4 = array_map(function ($id) {
						$sql="SELECT * FROM ".DigitalSignage4_caller::get_video_table_name()." WHERE `id_demande` = %d";
						global $wpdb;
						$sql=$wpdb->prepare($sql,$id);
			
                        $localDemandes = DigitalSignage4_DemandeManager::set(DigitalSignage4_caller::getInstance())->getLocalDemandes($sql);
                        $localDemande = $localDemandes[0];

                        return $localDemande->getVideoUrl();
                    }, $videos);

                    echo wp_json_encode($videosMp4);
        }
        
    }
