<?php

// Global vars
define('ASSETS', get_template_directory_uri() . '/assets/');
define('IMAGES', ASSETS . 'images/');
define('JS',     ASSETS . 'js/');
define('CSS',    ASSETS . 'css/');
define('VIDEOS', ASSETS . 'videos/');
define('FONTS',  ASSETS . 'fonts/');

require_once('functions/functions.utils.php');
require_once('functions/functions.admin.php');
require_once('functions/functions.gutenberg.php');

function t($key) {
    echo nl2br(__($key, 'pradel'));
}

// Theme setup
function theme_setup () {
    load_theme_textdomain('pradel', get_template_directory() . '/languages/themes');
    show_admin_bar(false);  
}
add_action('after_setup_theme', 'theme_setup');


// Add Styles
add_action('wp_enqueue_scripts', 'add_css');
function add_css() {
    wp_enqueue_style('main', CSS . 'main.css?t='.time());
}

add_action('admin_enqueue_scripts', 'add_cssAdmin');
function add_cssAdmin() {
  wp_enqueue_style('add_cssAdmin', CSS . 'admin.css?t='.time());
}

// Add Scripts
add_action('wp_footer', 'add_scripts');
function add_scripts() {
    global $wp_query;

    wp_enqueue_script('app', JS . 'app.js?t='.time());
    wp_enqueue_script('vendors', JS . 'vendors.js?t='.time());
    
    // Expose ajax url in body
    wp_localize_script('app', 'ajaxVars', array(
        'url' => admin_url('admin-ajax.php'),
        'query_vars' => json_encode($wp_query->query)
    ));
}

// // Add Script in header
// add_action('wp_enqueue_scripts', 'add_scriptsheader');
// function add_scriptsheader(){
//     wp_enqueue_script('xxxxx', JS . 'xxxxx.js');
// }


// Init menus
add_action('init', 'register_menus');
function register_menus() {
	register_nav_menus(array(
        'main-menu' => __('Menu principal'),
        'second-menu' => __('Menu secondaire'),
        'footer' => __('Pied de page')
    ));
}

// Remove 2560 limit
add_filter( 'big_image_size_threshold', '__return_false' );

// ALLOW SVG FILES IN BO
function add_file_types_to_uploads($file_types){
    $new_filetypes = array();
    $new_filetypes['svg'] = 'image/svg+xml';
    $file_types = array_merge($file_types, $new_filetypes );
    return $file_types;
}
add_action('upload_mimes', 'add_file_types_to_uploads');



// Popin news AJAX
add_action('wp_ajax_nopriv_news_ajax', 'newsAjax');
add_action('wp_ajax_news_ajax', 'newsAjax');

function newsAjax () {
    $newsId = isset($_GET['newsId']) && !empty($_GET['newsId']) ? $_GET['newsId'] : null;
    if (!$newsId) die();

    $news = get_post($newsId);
    $newsData['title'] = get_the_title($news->ID);
    $newsData['surtitle'] = get_field('surtitle', $news->ID);
    $newsData['publishDate'] = get_field('publishDate', $news->ID);
    $newsData['baseline'] = get_field('baseline', $news->ID);
    $newsData['intro'] = get_field('intro', $news->ID);
    $newsData['videoLink'] = get_field('videoLink', $news->ID);
    $c = wp_get_post_terms($news->ID, 'category');
    $newsData['category'] = count($c) > 0 ? $c[0]->name : null;

    $categories = get_the_category($news->ID);
    foreach($categories as $category) {
        $cat_ID = $category->term_id.',';
    }
    $cat_ID = trim($cat_ID, ',');
    $related = get_posts(array(
        'posts_per_page' => 4,
        'category' => $cat_ID,
        'exclude' => $news->ID
    ));
    foreach($related as $article) {
        // if ($article->ID != $news->ID) {
            $newsData['related'][] = $article;
        // }
    }

    // $articles = getArticles();
    require_once get_stylesheet_directory() . '/layouts/inc/newsPopin.php';
    die();
}

// function getVimeoId($url = '') {
//     $regs = array();
//     $id = '';

//     if (preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $regs)) {
//         $id = $regs[3];
//     }

//     return $id;
// }

// function turn_blogposts_translation_off( $post_types, $is_settings ) {
// unset( $post_types['post'] );

// return $post_types;
// }
// add_filter( 'pll_get_post_types', 'turn_blogposts_translation_off', 10, 2 );

// function turn_categoryblog_translation_off( $taxonomies, $is_settings ) {
//     unset( $taxonomies['category'] );
//     return $taxonomies;
// }
// add_filter( 'pll_get_taxonomies', 'turn_categoryblog_translation_off', 10, 2 );




/**
 * Disable the emoji's
 */
function disable_emojis() {
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' ); 
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); 
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
    add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
    add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'disable_emojis' );
   
/**
* Filter function used to remove the tinymce emoji plugin.
* 
* @param array $plugins 
* @return array Difference betwen the two arrays
*/
function disable_emojis_tinymce( $plugins ) {
    if ( is_array( $plugins ) ) {
        return array_diff( $plugins, array( 'wpemoji' ) );
    } else {
        return array();
    }
}
   
/**
* Remove emoji CDN hostname from DNS prefetching hints.
*
* @param array $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for.
* @return array Difference betwen the two arrays.
*/
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
    if ( 'dns-prefetch' == $relation_type ) {
        /** This filter is documented in wp-includes/formatting.php */
        $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
    
        $urls = array_diff( $urls, array( $emoji_svg_url ) );
    }
   
   return $urls;
}

function favicon4admin() {
    echo '<link rel="Shortcut Icon" type="image/x-icon" href="' . get_bloginfo('wpurl') . '/wp-content/favicon.ico" />';
}
add_action( 'admin_head', 'favicon4admin' );


function getWebinarDatas($obj) {
    $data = array();
    
    $data['ID'] = $obj->ID;
    $data['url'] = get_permalink($obj->ID);
    $data['title'] = get_the_title($obj->ID);
    $data['date_filter'] = get_field('date_filter', $obj->ID);
    $data['date_display'] = get_field('date_display', $obj->ID);
    $data['surtitle'] = get_field('surtitle', $obj->ID);
    $data['maintitle'] = get_field('maintitle', $obj->ID);
    $data['content'] = get_field('content', $obj->ID);
    $data['additional'] = get_field('additional', $obj->ID);
    $data['photos'] = get_field('photos', $obj->ID);
    $data['form_title'] = get_field('form_title', $obj->ID);
    $data['form_url'] = get_field('form_url', $obj->ID);
    $data['form_intro'] = get_field('form_intro', $obj->ID);
    $data['titreform'] = get_field('form_title', $obj->ID);
    $data['urlform'] = get_field('form_url', $obj->ID);
    $data['codeform'] = get_field('form_code', $obj->ID);
    $data['introform'] = get_field('form_intro', $obj->ID);

    if ($data['photos']) {
        $data['cover'] = $data['photos'][0];
    } else {
        $data['cover'] = null;
    }

    $filterDate = DateTime::createFromFormat('d/m/Y', get_field('date_filter', $obj->ID))->format('Y-m-d');
    $data['is_past'] = $filterDate < date('Y-m-d');

    return $data;
}


function replaceWidgetContainer($code, $id) {
    return str_replace('WidgetContainer', 'WidgetContainer-' . $id, $code);
}



// Popin Webinar AJAX
add_action('wp_ajax_nopriv_webinar_ajax', 'webinarAjax');
add_action('wp_ajax_webinar_ajax', 'webinarAjax');

function webinarAjax () {
    $webinarId = isset($_GET['webinarId']) && !empty($_GET['webinarId']) ? $_GET['webinarId'] : null;
    if (!$webinarId) die();
    
    $webinar = get_post($webinarId);
    if (!$webinar) die();
    
    $wData = getWebinarDatas($webinar);

    require_once get_stylesheet_directory() . '/layouts/inc/webinarItem.php';
    die();
}