<?php

add_action( 'after_setup_theme', 'kopa_front_after_setup_theme' );

function kopa_front_after_setup_theme() {
    add_theme_support( 'post-formats', array( 'gallery', 'audio', 'video', 'quote' ) );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'loop-pagination' );
    add_theme_support( 'title-tag' );

    $args = array(
        'flex-width'    => true,
        'flex-height'    => true,
        'default-image' => '',
    );
    add_theme_support( 'custom-header', $args );

    register_nav_menus(array(
        'top-nav'      => __( 'Top Menu', 'garajez-lite' ),
        'main-nav'     => __( 'Main Menu', 'garajez-lite' )
    ));

    global $content_width;
    if ( ! isset( $content_width ) ) {
        $content_width = 911;
    }

    add_action( 'widgets_init', 'kopa_register_widgets' );

    if ( ! is_admin() ){        
        add_action( 'wp_enqueue_scripts', 'kopa_enqueue_script' );        
    }
}


function kopa_i18n() {
    load_theme_textdomain('garajez-lite', get_template_directory() . '/languages');
}

/**
 * @package Kopa
 * @subpackage Garajez
 * @author: kopatheme
 * @description: Load stylesheet, javascript file and localization variables
 */
function kopa_enqueue_script() {
    if ( ! is_admin() ) {
        global $wp_styles, $is_IE, $wp_version;
        $dir = get_template_directory_uri();

        // custom body class
        add_filter('body_class', 'kopa_body_class');
        
        if ( 'off' !== _x( 'on', 'Google font: on or off', 'upside' ) ) {
            $google_fonts_url = add_query_arg( 'family', urlencode( 'Open Sans:400,300,600,700,800|Roboto Condensed:300italic,400italic,700italic,400,300,700|Roboto Slab:400,700,300,100|Oswald:400,300,700' ), "//fonts.googleapis.com/css" );
            wp_enqueue_style( KOPA_PREFIX . 'font-google', $google_fonts_url, array(), false, 'all');
        }

        wp_enqueue_style(KOPA_PREFIX . 'bootstrap', $dir . '/css/bootstrap.css', array(), false, 'all');

        if ( ! class_exists('Kopa_Framework') ) {
            wp_enqueue_style(KOPA_PREFIX . 'font-awesome', $dir . '/css/font-awesome.css', array(), false, 'all');
        }

        wp_enqueue_style('kopa_font_awesome');
        wp_enqueue_style(KOPA_PREFIX . 'superfish', $dir . '/css/superfish.css', array(), false, 'all');
        wp_enqueue_style(KOPA_PREFIX . 'megafish', $dir . '/css/megafish.css');
        wp_enqueue_style(KOPA_PREFIX . 'navgoco', $dir . '/css/jquery.navgoco.css');
        wp_enqueue_style(KOPA_PREFIX . 'owl-carousel', $dir . '/css/owl.carousel.css', array(), false, 'all');
        wp_enqueue_style(KOPA_PREFIX . 'owl-theme', $dir . '/css/owl.theme.css', array(), false, 'all');
        wp_enqueue_style(KOPA_PREFIX . 'magnific-popup', $dir . '/css/magnific-popup.css', array(), false, 'all');
        wp_enqueue_style(KOPA_PREFIX . 'animate', $dir . '/css/animate.css', array(), false, 'all');
        wp_enqueue_style(KOPA_PREFIX . 'style', get_stylesheet_uri());
        wp_enqueue_style(KOPA_PREFIX . 'extra', $dir . '/css/extra.css');


        // load css file for ie
        if ( $is_IE ) {
            wp_register_style(KOPA_PREFIX . 'ie', $dir . '/css/ie.css');
            wp_register_style(KOPA_PREFIX . 'ie9', $dir . '/css/ie9.css');

            $wp_styles->add_data(KOPA_PREFIX . 'ie', 'conditional', 'lt IE 9');
            wp_enqueue_style(KOPA_PREFIX . 'ie');

            $wp_styles->add_data(KOPA_PREFIX . 'ie9', 'conditional', 'IE 9');
            wp_enqueue_style(KOPA_PREFIX . 'ie9');
        }

        // load js files
        wp_enqueue_script(KOPA_PREFIX . 'modernizr.custom', $dir . '/js/modernizr.custom.js', array('jquery'), null, TRUE);
        wp_enqueue_script('jquery-masonry');
        wp_enqueue_script(KOPA_PREFIX . 'bootstrap', $dir . '/js/bootstrap.js', array('jquery'), null, TRUE);
        wp_enqueue_script('jquery-form');
        wp_enqueue_script(KOPA_PREFIX . 'custom', $dir . '/js/custom.js', array(), null, TRUE);

        // send localization to frontend
        wp_localize_script(KOPA_PREFIX . 'custom', 'kopa_custom_front_localization', kopa_custom_front_localization());

        if (is_single() || is_page()) {
            wp_enqueue_script('comment-reply');
        }
    }
}

/**
 * @package Kopa
 * @subpackage Garajez
 * @author: kopatheme
 * @description: Pass localization variables to js
 */
function kopa_custom_front_localization() {
    $front_localization = array(
        'url' => array(
            'template_directory_uri' => get_template_directory_uri().'/',
            'ajax' => admin_url('admin-ajax.php'),
            'post_id' => (is_singular()) ? get_queried_object_id() : 0
        )
    );

    return $front_localization;
}

/**
 * @package Kopa
 * @subpackage Garajez
 * @author: kopatheme
 * @description: get custom body class
 */
function kopa_body_class($classes) {
    $kopa_setting = garajez_get_template_setting();
    $sidebar = array();
    if ( $kopa_setting ) {
        $sidebar = $kopa_setting['sidebars'];
    }
    if ( isset($sidebar['position_1']) && !is_active_sidebar($sidebar['position_1']) ) {
        $classes[] = 'kopa-fullwidth';
    }

    if ( is_category() || is_home() && !is_front_page()) {
        $classes[] = 'kopa-blog-page';
    }

    if ( is_single()) {
        $classes[] = 'kopa-single-page';
    }

    return $classes;
}

/**
 * Create a nicely formatted and more specific title element text for output
 * in head of document, based on current view.
 *
 * @package Kopa
 *
 * @param string $title Default title text for current view.
 * @param string $sep Optional separator.
 * @return string The filtered title.
 */
function kopa_wp_title( $title, $sep ) {
    global $paged, $page;

    if ( is_feed() ) {
        return $title;
    }

    // Add the site name.
    $title .= esc_attr(get_bloginfo( 'name' ), 'display');

    // Add the site description for the home/front page.
    $site_description = esc_attr(get_bloginfo( 'description', 'display' ));
    if ( $site_description && ( is_home() || is_front_page() ) ) {
        $title = "$title $sep $site_description";
    }

    // Add a page number if necessary.
    if ( $paged >= 2 || $page >= 2 ) {
        $title = "$title $sep " . sprintf( __( 'Page %s', 'garajez-lite' ), max( $paged, $page ) );
    }

    return $title;
}

/**
 * @package Kopa
 * @subpackage Garajez
 * @author: kopatheme
 * @description: get img url of post
 */
function kopa_get_image_src($post_id = 0, $size = 'full', $use_sample_thumbnail) {
    $thumb = get_the_post_thumbnail($post_id, $size);
    if (!empty($thumb)) {
        $_thumb = array();
        $regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
        preg_match($regex, $thumb, $_thumb);
        $thumb = $_thumb[2];
    } elseif($use_sample_thumbnail) {
        $thumb = get_template_directory_uri() . "/images/thumbnails/no-thumbnail.gif";
    } else {
        $thumb = '';
    }
    return $thumb;
}

function kopa_get_image_by_id($img_id, $size='full'){
    $thumb = wp_get_attachment_image($img_id,$size);
    if (!empty($thumb)) {
        $_thumb = array();
        $regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
        preg_match($regex, $thumb, $_thumb);
        $thumb = $_thumb[2];
    }
    return $thumb;
}

/**
 * @package Kopa
 * @subpackage Garajez
 * @author: kopatheme
 * @description: get img of post using bfithumb
 */
function kopa_get_image($post_id = 0, $alt = '', $width = 0, $height = 0, $crop = true, $use_sample_thumbnail = false, $get_by_img_id = false,$return_src=false){
    $key_name = 'thumbnail_'.$width.'x'.$height;
    $custom_thumbnail = get_post_meta( $post_id, $key_name, true );

    $img_src = kopa_get_image_src($post_id, 'full', $use_sample_thumbnail);
    if ( true == $get_by_img_id) {
        $img_src = kopa_get_image_by_id($post_id);
    }
    if(!empty($custom_thumbnail)){
        return '<img src="' . esc_url($custom_thumbnail) . '" alt="' . esc_attr($alt) . '" />';
    }else{
        if ( empty($img_src) ) {
            return '';
        }
        $params = array( 'width' => $width, 'height' => $height, 'crop' => $crop );
        if(!$return_src){
        return '<img src="' . esc_url(bfi_thumb($img_src, $params)) . '" alt="' . esc_attr($alt) . '" />';
        } else {
            return bfi_thumb($img_src, $params);
        }
    }
}

function kopa_post_bfi_thumb($post_id = 0, $width = 0, $height = 0, $crop = true) {
    $src = NULL;
    $img_src = kopa_get_image_src($post_id, 'full');
    $params = array( 'width' => $width, 'height' => $height, 'crop' => $crop );
    return bfi_thumb($img_src, $params);
}

/**
 * @package Kopa
 * @subpackage Garajez
 * @author: kopatheme
 * @description: get custom excerpt by word
 */
function kopa_get_the_excerpt_for_widget($excerpt='', $content = '', $length = 0) {
    if ( $length != 0 ){
        $kopa_length = $length;
    }elseif(is_category() || is_tag() || is_home()) {
        $kopa_length = (int) get_theme_mod('blog_excerpt', 55);
    }else{
        $kopa_length = 55;
    }

    if ( empty($excerpt) ) {
        $more_arr = get_extended($content);
        if ( count($more_arr) > 0 ) {
          $temp_excerp = $more_arr['main'];
        } else {
            $temp_excerp =  $content;
        }
        $temp_excerp =  strip_tags($temp_excerp);
        $temp_excerp =  strip_shortcodes($temp_excerp);
        $kopa_excerpt = wp_trim_words($temp_excerp, $kopa_length, $more = null);
    } else {
        $kopa_excerpt = wp_trim_words($excerpt, $kopa_length, $more = null);
    }

    //return apply_filters('kopa_filter_excerpt', $kopa_excerpt);
    return apply_filters( 'kopa_filter_excerpt', $kopa_excerpt );
}

/*
 * Remove dimension of post thumbnail
 */
add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 );
function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

function mitta_right_sidebar_params($params){
    $params[0]['before_title'] = '<h3 class="widget-title style3"><span>';
    $params[0]['after_title'] = '</span></h3>';

    return $params;
}

function kopa_register_widgets() {
    if ( 'free' == KOPA_THEME_TYPE ){// add slider for blog
        register_sidebar( array(
            'name' => __( 'Blog slider', 'garajez-lite'),
            'id' => 'sidebar-blog-slider',
            'description' => __( 'Show slider for blog', 'garajez-lite' ),
            'before_title' => '<h1>',
            'after_title' => '</h1>',
        ) );

        if ( class_exists('Kopa_Framework') ) {
            register_widget('Kopa_Widget_Blog_Slider');
        }
    }
}

add_filter('wp_video_shortcode', 'kopa_video_shortcode');
function kopa_video_shortcode($html) {
    if (!empty($html)) {
        $out = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $html);
        $out = preg_replace('/(width|height)="\d*"\s/', "", $out);
    }

    return $out;
}

add_filter('the_category', 'garajez_remove_category_rel');
function garajez_remove_category_rel($output) {
    $output = str_replace(' rel="category tag', '', $output);
    $output = str_replace(' rel="category"', '', $output);
    return $output;
}

add_action( 'init', 'garajez_add_editor_styles' );
function garajez_add_editor_styles() {
    add_editor_style( 'custom-editor-style.css' );
}


function garajez_get_template_setting ( $defaults = array() ) {
    if ( function_exists('kopa_get_template_setting') ) {
        return kopa_get_template_setting();
    }
    return $defaults;
}