<?php

/**
@ Thiết lập các chức năng sẽ được theme hỗ trợ
**/
if ( ! function_exists( 'ththeme_setup' ) ) {
    /*
    * Nếu chưa có hàm thachpham_theme_setup() thì sẽ tạo mới hàm đó
    */
    function ththeme_setup() {      
        /*
        * Thêm chức năng post thumbnail
        */
        add_theme_support( 'post-thumbnails' );
        //Add image style crop
        // add_image_size (  'thumbnail_375x180' , 375, 180,  true);
        // add_image_size (  'thumbnail_500x240' , 500, 240,  true);
        // add_image_size (  'thumbnail_200x200' , 200, 200,  true);
        /*
        * Thêm chức năng title-tag để tự thêm <title>
        */
        add_theme_support( 'title-tag' );
        /*
        * Thêm chức năng post format
        */
        add_theme_support( 'post-formats',
            array(
                'default',
                'lesson',
            )
        );
        /*
        * Thêm chức năng custom background
        */
        $default_background = array(
           'default-color' => '#e8e8e8',
        );
        add_theme_support( 'custom-background', $default_background );

        /*
        * Tạo menu cho theme
        */
        register_nav_menu ( 'main-menu', __('Main Menu', 'ththeme') );
        register_nav_menu ( 'footer-menu', __('Footer Menu', 'ththeme') );
        register_nav_menu ( 'useful-links-menu', __('Useful Links', 'ththeme') );
    }
    add_action ( 'init', 'ththeme_setup' );
}


/**
@ Thiết lập hàm hiển thị menu
**/
if ( ! function_exists( 'ththeme_main_menu' ) ) {
  function ththeme_main_menu($container_class, $container_id, $menu_class, $menu_id) {
        // $girltheme_walker = new Megamenu_Nav_Walker;
        wp_nav_menu( array(
            'theme_location' => 'main-menu',
            'menu_class' => $menu_class,
            'menu_id' => $menu_id,
            'container' => 'div',
            'container_id' => $container_id,
            'container_class' => $container_class,
        ) );
  }
}
if ( ! function_exists( 'ththeme_footer_menu' ) ) {
  function ththeme_footer_menu () {
        // $girltheme_walker = new Megamenu_Nav_Walker;
        wp_nav_menu( array(
            'theme_location' => 'footer-menu',
            'menu_class' => 'footer-menu',
            'menu_id' => '',
            'container' => '',
            'container_id' => '',
            'container_class' => '',
        ) );
  }
}
if ( ! function_exists( 'ththeme_useful_links' ) ) {
  function ththeme_useful_links () {
        // $girltheme_walker = new Megamenu_Nav_Walker;
        wp_nav_menu( array(
            'theme_location' => 'useful-links-menu',
            'menu_class' => 'gdlr-core-custom-menu-widget gdlr-core-menu-style-half',
            'menu_id' => 'menu-useful-links',
            'container' => 'div',
            'container_id' => '',
            'container_class' => 'menu-useful-links-container',
        ) );
  }
}


add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class ($classes, $item) {
    if (in_array('current-menu-item', $classes) ){
        $classes[] = 'active ';
    }
    if($item->menu_item_parent == 0)
        $classes[] = 'logisco-normal-menu';
    return $classes;
}


//Set count view posts
function wpb_get_post_views($postID){
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return 0;
    }else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
    return $count;
}

function custom_excerpt_length( $length ) {
    return 90;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

function wpd_lesson_category_template( $template ) {
    $cat = get_queried_object();
    if( 9 == $cat->category_parent )
        $template = locate_template( 'category-9.php' );
    return $template;
}
add_filter( 'category_template', 'wpd_lesson_category_template' );

if ( ! function_exists( 'ththeme_posts_navigation' ) ) :
    /**
     * Documentation for function.
     */
    function ththeme_posts_navigation() {
        the_posts_pagination(
            array(
                'mid_size'  => 2,
                'prev_text' => sprintf(
                    '%s <span class="nav-prev-text">%s</span>',
                    '❮',
                    __( '', 'ththeme' )
                ),
                'next_text' => sprintf(
                    '<span class="nav-next-text">%s</span> %s',
                    __( '❯', 'ththeme' ),
                    ''
                ),
            )
        );
    }
endif;

// function wp_dropdown_cats_multiple( $output, $r ) {

//     if( isset( $r['multiple'] ) && $r['multiple'] ) {

//         $output = preg_replace( '/^<select/i', '<select multiple', $output );

//         $output = str_replace( "name='{$r['name']}'", "name='{$r['name']}[]'", $output );

//         $selected = is_array($r['selected']) ? $r['selected'] : explode( ",", $r['selected'] );
//         foreach ( array_map( 'trim', $selected ) as $value )
//             $output = str_replace( "value=\"{$value}\"", "value=\"{$value}\" selected", $output );

//     }

//     return $output;
// }
// add_filter( 'wp_dropdown_cats', 'wp_dropdown_cats_multiple', 10, 2 );