<?php

function debug($arr) {
    echo '<pre>';
    print_r( $arr );
    echo '</pre>';
}


function cc_mime_types($mimes) {
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');


/*-----------------------------------------------------------------------------------*/
/* Enable more buttons in admin
/*-----------------------------------------------------------------------------------*/
function enable_more_buttons($buttons) {
    $buttons[] = 'hr';
    // $buttons[] = 'sub';
    // $buttons[] = 'sup';
    // $buttons[] = 'fontselect';
    // $buttons[] = 'fontsizeselect';
    // $buttons[] = 'cleanup';
    // $buttons[] = 'styleselect';
    return $buttons;
}
add_filter("mce_buttons_2", "enable_more_buttons");

// function biuro_theme_setup() {
//     add_theme_support( 'post-formats', array(
//         'aside',
//         // 'image',
//         // 'video',
//         // 'quote',
//         // 'link',
//         // 'gallery',
//         // 'audio',
//     ) );


//     // Make sure featured images are enabled
//     add_theme_support( 'post-thumbnails' );

//     update_option( 'thumbnail_crop', 1 );

//     add_image_size( 'placeholder', 26, 14, true );

//     update_option( 'thumbnail_size_w', 320 );
//     update_option( 'thumbnail_size_h', 175 );
//     update_option( 'thumbnail_crop', 1 );

//     add_image_size( 'thumbnail@2x', 640, 350, true );

//     update_option( 'medium_size_w', 480 );
//     update_option( 'medium_size_h', 262 );
//     update_option( 'medium_crop', 1 );

//     add_image_size( 'medium@2x', 960, 524, true );

//     update_option( 'large_size_w', 715 );
//     update_option( 'large_size_h', 390 );
//     update_option( 'large_crop', 1 );

//     add_image_size( 'large@2x', 1430, 780, true );

//     // removing medium_large thumbnail
//     add_filter( 'intermediate_image_sizes', function( $sizes ) {
//         return array_filter( $sizes, function( $val ) {
//             return 'medium_large' !== $val; // Filter out 'medium_large'
//         });
//     });



//     /*
//      * Let WordPress manage the document title.
//      * By adding theme support, we declare that this theme does not use a
//      * hard-coded <title> tag in the document head, and expect WordPress to
//      * provide it for us.
//      */
    add_theme_support( 'title-tag' );
// }

// add_action( 'after_setup_theme', 'biuro_theme_setup' );


function filter_site_upload_size_limit( $size ) {
    // Set the upload size limit to 60 MB for users lacking the 'manage_options' capability.
    // if ( ! current_user_can( 'manage_options' ) ) {
        // 60 MB.
        $size = 10 * 1024 * 1024;
    // }
    return $size;
}
add_filter( 'upload_size_limit', 'filter_site_upload_size_limit', 20 );

function my_deregister_scripts(){
    wp_dequeue_script( 'wp-embed' );
}
add_action( 'wp_footer', 'my_deregister_scripts' );



/**
 * Register our sidebars and widgetized areas.
 */
function biuro_widger_areas() {

    register_sidebar( array(
        'name' => 'Footer area',
        'id' => 'footer_main',
        'before_widget' => '',
        'after_widget'  => '',
        'before_title'  => '',
        'after_title'   => '',
    ) );

    register_sidebar( array(
        'name' => 'Content append area',
        'id' => 'append_area',
        'before_widget' => '',
        'after_widget'  => '',
        'before_title'  => '',
        'after_title'   => '',
    ) );
}
add_action( 'widgets_init', 'biuro_widger_areas' );






/**
 * Disable the emoji's
 */

add_action( 'init', 'disable_emojis' );

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_filter( 'emoji_svg_url', '__return_false' );
}

/**
 * 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 init_biuro_theme() {

    // Add categories for pages
    // register_taxonomy_for_object_type( 'category', 'page' );

    // Remove Custom fields from posts
    // remove_meta_box('postcustom','post','normal');
    // remove_meta_box( $id, $page, $context );

    // Remove tags support from posts
    // unregister_taxonomy_for_object_type('post_tag', 'post');

    /*
     * Let WordPress manage the document title.
     * By adding theme support, we declare that this theme does not use a
     * hard-coded <title> tag in the document head, and expect WordPress to
     * provide it for us.
     */
    add_theme_support( 'title-tag' );

    // Register menus
    register_nav_menus(
        array(
            'main-menu' => 'Main menu',
            'region-menu' => 'Region menu'
        )
    );

    pll_register_string('biuro-language', 'Language', 'Biuro');
    pll_register_string('biuro-job-search', 'Job search', 'Biuro');
}
add_action('init', 'init_biuro_theme');


$l = get_nav_menu_locations();
$m = $l ? wp_get_nav_menu_object( $l[ 'main-menu' ] ) : null;
$items = $m ? wp_get_nav_menu_items( $m->term_id, array( 'order' => 'DESC' ) ) : array();

$searchPageURL = $items[0] ? $items[0]->url : '/';
