<?php 

function svg($id, $cssClass = '', $additionnal = '') {
    echo '<svg role="img" class="icon icon--' . $id . ' ' . $cssClass . '" ' . $additionnal . '><use xlink:href="' . IMAGES . 'sprite/sprite.svg#' . $id . '"></use></svg>';
}

function getImageUrl($object, $size = 'large') {
    
    if (!$object) return null;

    $size = wp_is_mobile() ? 'medium' : $size;

    return $object['sizes'][$size];
}
function getImageSrcSetSizes($object, $sizes = 'auto') {
    
    if (!isset($object['ID']) || intval($object['ID']) == 0) return '';

    $srcset = wp_get_attachment_image_srcset(intval($object['ID']));

    return ' srcset="'.$srcset.'" sizes="'.$sizes.'" ';
}

function loadFonts($fonts, $types) {
    foreach ($fonts as $font) {
        foreach ($types as $type) {
            echo '<link rel="preload" href="' . FONTS . $font . '.' . $type .'" as="font" type="font/' . $type . '" crossorigin="anonymous">
    ';
        }
    }
}

function dump ($var, $exit = false) {
    var_dump($var);
    if ($exit) exit();
}

function randomPicId () {
    $data = array(0, 1, 10, 100, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1008, 1009, 101, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1018, 1019, 102, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 103, 1031, 1032, 1033, 1035, 1036, 1037, 1038, 1039, 104, 1040, 1041, 1042, 1043, 1044, 1045, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 106, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 107, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 108, 1080, 1081, 1082, 1083, 1084, 109, 11, 110, 111, 112, 113, 114, 115, 116);
    return $data[array_rand($data)];
}

function getFirstLetter ($text) {
    return substr(strip_tags($text), 0, 1);
}

function removeFirstLetter ($text) {
    return substr(strip_tags($text), 1);
}

function getActualitesPermalink ($category) {
    $url = get_page_link(pll_get_post(14));

    if ($category) {
        $url .= '?c=' . $category->slug;
    }

    return $url;
}

function getExpertisePermalink ($tag) {
    $url = get_page_link(pll_get_post(17));

    if ($tag) {
        $url .= $tag;
    }

    return $url;
}

function getCabinetPermalink ($tag) {
    $url = get_page_link(pll_get_post(10));

    if ($tag) {
        $url .= $tag;
    }

    return $url;
}

function getYearFromDate ($date) {
    return DateTime::createFromFormat('d/m/Y', $date)->format('Y');
}


function getVideoProvider($url) {
    if (getYoutubeId($url)) {
        return "youtube";
    }
    
    if (getVimeoId($url)) {
        return "vimeo";
    }

    return null;
}

function getVideoId($url) {
    if (getYoutubeId($url)) {
        return getYoutubeId($url);
    }

    if (getVimeoId($url)) {
        return getVimeoId($url);
    }

    return null;
}

function getYoutubeId($url) {
    preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
    return (!empty($match[1])) ? $match[1] : null;
}

function getVimeoId($url) {
    preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/", $url, $match);
    return (!empty($match[5])) ? $match[5] : null;
}

function pradel_format_attr($text) {
    $text = strip_tags($text);
    $text = str_replace('"', "'", $text);
    return $text;
}

function isMobileDevice() { 
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo 
|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i" 
, $_SERVER["HTTP_USER_AGENT"]); 
} 

function addZero($number, $length = 2) {
    return str_pad($number, $length, '0', STR_PAD_LEFT);
}