<?php

add_theme_support( 'menus' );
add_theme_support( 'widgets' );
add_theme_support( 'post-thumbnails');

if( function_exists('acf_add_options_page') ) {
    acf_add_options_page();
    acf_set_options_page_menu("Theme Option");
}

add_action('admin_bar_menu', 'add_toolbar_items', 100);
function add_toolbar_items($admin_bar){
    $admin_bar->add_menu( array(
        'id'    => 'my-item',
        'title' => 'Theme Option',
        'href'  => site_url().'/wp-admin/admin.php?page=acf-options',
        'meta'  => array(
            'title' => __('Theme Option'),
        ),
    ));
} 
function add_theme_scripts() {
    //    Style
    wp_enqueue_style( 'style-bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css');
    wp_enqueue_style( 'style-font-awesome', get_template_directory_uri() . '/css/font-awesome.css');
    wp_enqueue_style( 'style-owl-carousel', get_template_directory_uri() . '/css/owl.carousel.css');
    wp_enqueue_style( 'style-animate', get_template_directory_uri() . '/css/animate.css');
    wp_enqueue_style( 'style-bootstrapcdn', 'https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' );
    wp_enqueue_style( 'style-main', get_template_directory_uri() . '/css/style.css');
    wp_enqueue_style( 'style', get_stylesheet_uri() );

    // Script
    wp_enqueue_script( 'script-jquery', get_template_directory_uri() . '/js/jquery.min.js', array(), null, true);
    wp_enqueue_script( 'script-bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array(), null, true);
    wp_enqueue_script( 'script-carousel', get_template_directory_uri() . '/js/owl.carousel.js', array(), null, true);
    wp_enqueue_script( 'script-wow', get_template_directory_uri() . '/js/wow.js', array(), null, true);
    wp_enqueue_script( 'script-custom-script', get_template_directory_uri() . '/js/custom_script.js', array(), null, true);
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

function register_my_menus() {
    register_nav_menus(
        array(
            'header_menu' => __( 'Header Menu' ),
            'footer_menu' => __( 'Footer Menu' ),
        )
    );
}
add_action( 'init', 'register_my_menus' );

function rrregister_sidebars() {

    /* Register the primary sidebar. */
    register_sidebar(
        array(
            'id' => 'sidebar-1',
            'name' => __( 'Footer Column One', 'theme' ),
            'description' => __( 'A short description of the sidebar.', 'theme' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s footer_item">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>'
        )
    );
    register_sidebar(
        array(
            'id' => 'sidebar-2',
            'name' => __( 'Footer Column Two', 'theme' ),
            'description' => __( 'A short description of the sidebar.', 'theme' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s footer_item">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>'
        )
    );
    register_sidebar(
        array(
            'id' => 'sidebar-3',
            'name' => __( 'Footer Column Three', 'theme' ),
            'description' => __( 'A short description of the sidebar.', 'theme' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s footer_item">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>'
        )
    );
    register_sidebar(
        array(
            'id' => 'sidebar-4',
            'name' => __( 'Sidebar', 'theme' ),
            'description' => __( 'Page sidebar', 'theme' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s footer_item">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>'
        )
    );
}
add_action( 'widgets_init', 'rrregister_sidebars' );

add_filter('nav_menu_css_class','arg_menu_classes',110,3);

function arg_menu_classes($classes, $item, $args) {
    if($args->theme_location == 'header_menu') {
        $classes[] = 'nav-item';
    }
    return $classes;
}
function example_cats_related_post() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
        foreach ($categories as $category):
            array_push($cat_ids, $category->term_id);
        endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array(

        'category__in'   => $cat_ids,
        'post_type'      => $current_post_type,
        'post_not_in'    => array($post_id),
        'posts_per_page'  => '3'


    );

    $related_cats_post = new WP_Query( $query_args );

    if($related_cats_post->have_posts()): ?>
            <div class="row gray_bg_plain">
                <div class="container">
                    <div class="row m_t_80 m_b_80">
                        <div class="title_text col-md-12 text-center">
                            <h3>YOU MIGHT ALSO LIKE...</h3>
                        </div>
                        <div class="col-md-12">
                            <div class="row">
                                <?php while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
                                <div class="col-md-4">
                                    <div class="blog_body_box">
                                        <?php echo get_the_post_thumbnail(); ?>
                                        <div class="blog_content">
                                            <p><span><?php echo custom_taxonomies_terms_links(); ?><?php $categories = get_the_category();
                                                    if ( ! empty( $categories ) ) {
                                                        echo esc_html( $categories[0]->name );
                                                    } ?></span>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;<?php echo esc_html ( get_the_date() ); ?></p>
                                            <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                                        </div>
                                    </div>
                                </div>
                                <?php endwhile; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php
        // Restore original Post Data
        wp_reset_postdata();
    endif;

}
// get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // get post by post id
    $post = &get_post($post->ID);
    // get post type by post
    $post_type = $post->post_type;
    // get post type taxonomies
    $taxonomies = get_object_taxonomies($post_type);
    $out = "";
    foreach ($taxonomies as $taxonomy) {
        $out .= "";
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );
        if ( !empty( $terms ) ) {
            foreach ( $terms as $term )
               // $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
                $out .= '<a>'.$term->name.'</a> ';
        }
        $out .= "";
    }
    $out .= "";
    return $out;
} ?>
