<?php

define('KOPA_DOMAIN', 'newmoment');

/**
 * Kopa Framework by Kopatheme
 * this include calls a file that automatically includes all
 * the files within the folder framework and therefore makes
 * all functions and classes available for later use
 */
require_once( get_template_directory() . '/framework/kopa-framework.php' );
require_once( get_template_directory() . '/themeoption/theme_options.php' );
require_once( get_template_directory() . '/themeoption/layout_manager.php' );
require_once( get_template_directory() . '/themeoption/sidebar_manager.php' );

/**
 * Change admin theme option page slug
 */
add_filter('kopa_menu_settings', 'kopa_change_admin_page_slug');
function kopa_change_admin_page_slug( $menu ) {
	$menu['menu_slug'] = 'news-moment';
	return $menu;
}

/**
 * Change suffix in export file name
 */
add_filter( 'kopa_backup_export_type_labels', 'kopa_add_theme_options_backup_label' );
function kopa_add_theme_options_backup_label( $labels ) {
	$labels['theme-options'] = 'Theme News Moment';
	return $labels; 
}

/**
 * Register Widgets
 */
add_action( 'widgets_init', 'kopa_register_widgets' );

function kopa_register_widgets() {
    include( 'widgets/class-kopa-widget-article-list.php' );
    include( 'widgets/class-kopa-widget-video.php' );
    include( 'widgets/class-kopa-widget-single-post.php' );
    include( 'widgets/class-kopa-widget-contact-info.php' );
    include( 'widgets/class-kopa-widget-tag.php' );
    include( 'widgets/class-kopa-widget-tab.php' );
    include( 'widgets/class-kopa-widget-category.php' );
}

/*======================================================
*============== STARR OTHER FUCTIONS ===================
*======================================================*/

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'));
    add_theme_support('post-thumbnails');
    add_theme_support('loop-pagination');
    add_theme_support('automatic-feed-links');

    global $content_width;
    if ( ! isset( $content_width ) ) {
        $content_width = 900;
    }

    register_nav_menus(array(
        'top-nav'      => __( 'Top Menu (All Items Flat)', 'newmoment' ),
        'main-nav'     => __( 'Main Menu', 'newmoment' )
    ));

    if (!is_admin()) {
        add_filter('wp_title', 'kopa_wp_title', 10, 2);
        add_action('wp_enqueue_scripts', 'kopa_front_enqueue_scripts');
        add_action('wp_enqueue_scripts', 'newmoment_customize_css', 40);
        add_filter('body_class', 'kopa_body_class');
        add_filter('excerpt_more', 'kopa_custom_excerpt_more');
        add_filter('excerpt_length', 'kopa_custom_excerpt_length', 55);
        add_action('wp_head', 'kopa_head');
    }

    kopa_add_image_sizes();
}
function kopa_head() {

    if (kopa_get_option('kopa_favicon')) { 
        echo '<link rel="shortcut icon" type="image/x-icon"  href="'.kopa_get_option('kopa_favicon').'">';
    }

    if (kopa_get_option('kopa_favicon_57')) {
        echo '<link rel="apple-touch-icon" sizes="57x57" href="'.kopa_get_option('kopa_favicon_57').'">';
    }

    if (kopa_get_option('kopa_favicon_72')) { 
        echo '<link rel="apple-touch-icon" sizes="72x72" href="'.kopa_get_option('kopa_favicon_72').'">';
    }

    if (kopa_get_option('kopa_favicon_114')) {
        echo '<link rel="apple-touch-icon" sizes="114x114" href="'.kopa_get_option('kopa_favicon_114').'">';
    } 

    if (kopa_get_option('kopa_favicon_144')) {
        echo '<link rel="apple-touch-icon" sizes="144x144" href="'.kopa_get_option('kopa_favicon_144').'">';
    }

    $dir = get_template_directory_uri();
    echo    '<!--[if lt IE 9]>
                    <script>
                      document.createElement(\'header\');
                      document.createElement(\'nav\');
                      document.createElement(\'section\');
                      document.createElement(\'article\');
                      document.createElement(\'aside\');
                      document.createElement(\'footer\');
                      document.createElement(\'hgroup\');
                    </script>
                    <link rel="stylesheet" href="'.$dir .'/css/ie.css" type="text/css" media="all" />
                <![endif]-->

                <!--[if IE 9]>
                    <link rel="stylesheet" href="'.$dir .'/css/ie9.css" type="text/css" media="all" />
                <![endif]-->';
}

function kopa_wp_title( $title, $sep ) {
    global $paged, $page;

    if ( is_feed() ) {
        return $title;
    }

    // Add the site name.
    $title .= get_bloginfo( 'name' );

    // Add the site description for the home/front page.
    $site_description = 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', 'newmoment' ), max( $paged, $page ) );
    }

    return $title;
}

function kopa_front_enqueue_scripts() {
    if (!is_admin()) {
        global $wp_styles, $is_IE, $wp_version;

        $dir = get_template_directory_uri();

        /* STYLESHEETs */ 
        
        wp_enqueue_style('kopa-bootstrap', $dir . '/css/bootstrap.css');
        wp_enqueue_style('kopa_font_awesome');
        wp_enqueue_style('kopa-superfish', $dir . '/css/superfish.css');
        wp_enqueue_style('kopa-owl.carousel', $dir . '/css/owl.carousel.css');
        wp_enqueue_style('kopa-owl.theme', $dir . '/css/owl.theme.css');
        wp_enqueue_style('kopa-navgoco', $dir . '/css/jquery.navgoco.css');
        wp_enqueue_style('kopa-bootstrap-slider', $dir . '/css/boostrap-slider.css');
        wp_enqueue_style('kopa-colorbox', $dir . '/css/colorbox.css');
        wp_enqueue_style('kopa-style', get_stylesheet_uri());
        wp_enqueue_style('kopa-extra', $dir . '/css/extra.css');
        wp_enqueue_style('kopa-responsive', $dir . '/css/responsive.css');

        /* JAVASCRIPTs */

        wp_localize_script('jquery', 'kopa_front_variable', kopa_front_localize_script());
        wp_enqueue_script('modernizr.custom', $dir . '/js/modernizr.custom.js', array('jquery'), null, true);
        wp_enqueue_script('jquery-masonry');
        wp_enqueue_script('kopa-bootstrap', $dir . '/js/bootstrap.min.js', array('jquery'), null, true);
        wp_enqueue_script('kopa-bootstrap-slider', $dir . '/js/bootstrap-slider.js', array('jquery'), null, true);
        wp_enqueue_script('kopa-custom-js', $dir . '/js/custom.js', array('jquery'), null, true);
        wp_enqueue_script('kopa-html5-placeholder', $dir . '/js/placeholder.js', array('jquery'), null, true);
        

        
        // send localization to frontend
        wp_localize_script('kopa-custom-js', 'kopa_custom_front_localization', kopa_custom_front_localization());

        if (is_single() || is_page()) {
            wp_enqueue_script('comment-reply');
        }
    }
}

function kopa_front_localize_script() {
    $kopa_variable = array(
        'ajax' => array(
            'url' => admin_url('admin-ajax.php')
        ),
        'template' => array(
            'post_id' => (is_singular()) ? get_queried_object_id() : 0
        ),
    );
    return $kopa_variable;
}

function kopa_custom_front_localization() {
    $front_localization = array(
        'url' => array(
            'template_directory_uri' => get_template_directory_uri(),
        ),
        'validate' => array(
            'form' => array(
                'submit'  => __('SEND', 'newmoment'),
                'sending' => __('SENDING...', 'newmoment')
            ),
            'name' => array(
                'required'  => __('Please enter your name.', 'newmoment'),
                'minlength' => __('At least {0} characters required.', 'newmoment'),
            ),
            'email' => array(
                'required' => __('Please enter your email.', 'newmoment'),
                'email'    => __('Please enter a valid email.', 'newmoment')
            ),
            'message' => array(
                'required'  => __('Please enter a message.', 'newmoment'),
                'minlength' => __('At least {0} characters required.', 'newmoment'),
            )
        ),
        'breaking' => array(
            'speed' => (int)kopa_get_option('kopa_headline_speed',8000),
        )
    );

    return $front_localization;
}

function newmoment_customize_css(){
    $custom_css = kopa_get_option('kopa_custom_css');
    if( !empty($custom_css) ){
        wp_add_inline_style('newmoment-style', $custom_css);
    }
}

function kopa_custom_excerpt_more( $more ) {
    return '';
}


/**
 * Get The Excerpt ( the_excerpt ) With Lenght In Theme Option Via Hook the_excerpt
 */
function kopa_custom_excerpt_length( $length ) {
    if ( is_home() || is_archive() || is_search() || is_category() ) {
        $length = (int)kopa_get_option('kopa_blog_excerpt_lenght', $length );
    }else {
        $length = (int)kopa_get_option('kopa_frontpage_excerpt_lenght', $length );
    }
    return $length;    
}

/*
 * Template tag: Show headline
 */
function kopa_the_headline(){
    $limit = (int) kopa_get_option('kopa_headline_limit', 5);
    if ($limit) {
        $prefix = kopa_get_option('kopa_headline_prefix', __('Breaking news','newmoment'));
        $cats = (array)kopa_get_option('kopa_headline_cats');
        $cats = implode(',', $cats);

        if( !empty($cats) ){
            $posts = new WP_Query('cat='.$cats.'&posts_per_page='.$limit);
        }else{
            $posts = new WP_Query( 'posts_per_page='.$limit);
        }
        ?>
        <section class="breaking-news-box">

            <div class="wrapper">
                
                <div class="kp-headline-wrapper clearfix">
                    <span class="kp-headline-title"><?php echo $prefix; ?></span>
                    <div class="kp-headline clearfix">                        
                        <dl class="ticker-1 clearfix">
                            <?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
                                <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
                            <?php endwhile; ?>
                        </dl>
                        <!--ticker-1-->
                    </div>
                    <!--kp-headline-->
                </div>
                <!-- kp-headline-wrapper -->

                <span class="left-corner"></span>

                <span class="right-corner"></span>
                
            </div>
            <!-- wrapper -->
        </section>
        <?php
        wp_reset_postdata();
    }
}

/*
* Template tag: Show top new
*/
function kopa_the_topnew(){
    if(kopa_get_option('kopa_top_new_status') == 'hide') {
        return;
    }
    $limit = (int) kopa_get_option('kopa_top_new_limit', 5);
    $cats = (array)kopa_get_option('kopa_top_new_cats');
    $status = kopa_get_option('kopa_status_default_thumbnail');
    if( $status == 'enable' ){
        $status = true;
    }else {
        $status = false;
    }
    $class = array('first-item','second-item','third-item','fourth-item','last-item');
    if ($limit) {
        $cats = implode(',', $cats);
        $args = array(
            'posts_per_page'      => $limit,
            'ignore_sticky_posts' => true
        );
        $tax_query = array();
        if ( $cats ) {
            $tax_query[] = array(
                'taxonomy' => 'category',
                'field'    => 'id',
                'terms'    => $cats
            );
        }
        if ( $tax_query ) {
            $args['tax_query'] = $tax_query;
        }
        $posts = new WP_Query( $args );
        ?>
            <div class="widget-area-1">

                <div class="widget kopa-carousel-list-1-widget">
                    
                    <div class="owl-carousel kopa-carousel-1">

                        <?php $index = 0 ; while ( $posts->have_posts() ) : $posts->the_post(); ?>
                        
                            <div class="item">
                                <article class="entry-item <?php echo $class[$index]; ?>">
                                    <?php if(has_post_thumbnail() || $status) : ?>
                                        <div class="entry-thumb">
                                            <a href="<?php the_permalink(); ?>">
                                                <?php the_post_thumbnail('kopa-size-4'); ?>
                                            </a>
                                            <div class="mask"></div>
                                        </div>
                                    <?php endif; ?>
                                    <div class="entry-content text-center">
                                        <?php kopa_get_rate(get_the_id()); ?>
                                        <h6 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                                    </div>    
                                </article>    

                            </div>

                        <?php $index++; if($index == 5){ $index = 0; } endwhile; ?>

                    </div>
                    <!-- kopa-carousel-1 -->

                </div>
                <!-- kopa-carousel-list-1-widget -->
                
            </div>
            <!-- widget-area-1 -->
    <?php
    }
    wp_reset_postdata();
}

/*
 * Template tag: Show breadcrumb
 */
function kopa_get_breadcrumb() {
        if ('hide' == kopa_get_option('kopa_status_breadcrumb'))
            return;

    if (is_main_query()) {
        global $post, $wp_query;

        $prefix = '&nbsp;&raquo;&nbsp;';
        $current_class = 'current-page';
        $description = '';
        $breadcrumb_before = '<div class="breadcrumb clearfix">';
        $breadcrumb_after = '</div>';
        $breadcrumb_home = '<span class="kopa-brf">'.__('You are here: ', 'newmoment') .'</span> <span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="' . home_url('/') . '"><span itemprop="title">' . __('Home', 'newmoment') . '</span></a></span>';
        $breadcrumb = '';
        ?>

        <?php
        if (is_home()) {
            $breadcrumb.= $breadcrumb_home;
            if ( get_option( 'page_for_posts' ) ) {
                $breadcrumb.= $prefix . sprintf('<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb" class="%1$s">%2$s</span>', $current_class, get_the_title(get_option('page_for_posts')));
            } else {
                $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, __('Blog', 'newmoment'));
            }
        } else if (is_post_type_archive('product') && get_option('woocommerce_shop_page_id')) {
            $breadcrumb.= $breadcrumb_home;
            $breadcrumb.= $prefix . sprintf('<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb" class="%1$s">%2$s</span>', $current_class, get_the_title(get_option('woocommerce_shop_page_id')));
        } else if (is_tag()) {
            $breadcrumb.= $breadcrumb_home;

            $term = get_term(get_queried_object_id(), 'post_tag');
            $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $term->name);
        } else if (is_category()) {
            $breadcrumb.= $breadcrumb_home;

            $category_id = get_queried_object_id();
            $terms_link = explode(',', substr(get_category_parents(get_queried_object_id(), TRUE, ','), 0, (strlen(',') * -1)));
            $n = count($terms_link);
            if ($n > 1) {
                for ($i = 0; $i < ($n - 1); $i++) {
                    $breadcrumb.= $prefix . $terms_link[$i];
                }
            }
            $breadcrumb.= $prefix . sprintf('<span class="%1$s" itemprop="title">%2$s</span>', $current_class, get_the_category_by_ID(get_queried_object_id()));

        } else if ( is_tax('product_cat') ) {
            $breadcrumb.= $breadcrumb_home;
            $breadcrumb.= $prefix . '<span itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'.get_page_link( get_option('woocommerce_shop_page_id') ).'"><span itemprop="title">'.get_the_title( get_option('woocommerce_shop_page_id') ).'</span></a></span>';
            $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

            $parents = array();
            $parent = $term->parent;
            while ($parent):
                $parents[] = $parent;
                $new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
                $parent = $new_parent->parent;
            endwhile;
            if( ! empty( $parents ) ):
                $parents = array_reverse($parents);
                foreach ($parents as $parent):
                    $item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
                    $breadcrumb .= $prefix . '<span itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="' . get_term_link( $item->slug, 'product_cat' ) . '"><span itemprop="title">' . $item->name . '</span></a></span>';
                endforeach;
            endif;

            $queried_object = get_queried_object();
            $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $queried_object->name);
        } else if ( is_tax( 'product_tag' ) ) {
            $breadcrumb.= $breadcrumb_home;
            $breadcrumb.= $prefix . '<a href="'.get_page_link( get_option('woocommerce_shop_page_id') ).'">'.get_the_title( get_option('woocommerce_shop_page_id') ).'</a>';
            $queried_object = get_queried_object();
            $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $queried_object->name);
        } else if (is_single()) {
            global $post;
            $breadcrumb.= $breadcrumb_home;
            if ( is_singular('product')) :
                $breadcrumb .= $prefix . '<span itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_page_link( get_option('woocommerce_shop_page_id') ).'" itemprop="url"><span itemprop="title">'.get_the_title( get_option('woocommerce_shop_page_id') ).'</span></a></span>';

                if ($terms = get_the_terms( $post->ID, 'product_cat' )) :
                    $term = apply_filters( 'jigoshop_product_cat_breadcrumb_terms', current($terms), $terms);
                    $parents = array();
                    $parent = $term->parent;
                    while ($parent):
                        $parents[] = $parent;
                        $new_parent = get_term_by( 'id', $parent, 'product_cat');
                        $parent = $new_parent->parent;
                    endwhile;
                    if(!empty($parents)):
                        $parents = array_reverse($parents);
                        foreach ($parents as $parent):
                            $item = get_term_by( 'id', $parent, 'product_cat');
                            $breadcrumb .= $prefix . '<span itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="' . get_term_link( $item->slug, 'product_cat' ) . '" itemprop="url"><span itemprop="title">' . $item->name . '</span></a></span>';
                        endforeach;
                    endif;
                    $breadcrumb .= $prefix . '<span itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="' . get_term_link( $term->slug, 'product_cat' ) . '" itemprop="url"><span itemprop="title">' . $term->name . '</span></a></span>';
                endif;

                $breadcrumb.= $prefix . sprintf('<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a class="%1$s" itemprop="url" href="%2$s"><span itemprop="title">%3$s</span></a></span>', $current_class, get_permalink(get_queried_object_id()), get_the_title(get_queried_object_id()));

            else : 
                $categories = get_the_category(get_queried_object_id());
                if ($categories) {
                    foreach ($categories as $category) {
                        $breadcrumb.= $prefix . sprintf('<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="%1$s" itemprop="url"><span itemprop="title">%2$s</span></a></span>', get_category_link($category->term_id), $category->name);
                    }
                }

                $post_id = get_queried_object_id();
                $breadcrumb.= $prefix . sprintf('<span itemprop="title">%1$s</span>',get_the_title(get_queried_object_id()));
            endif;

        } else if (is_page()) {
            if (!is_front_page()) {
                $post_id = get_queried_object_id();
                $breadcrumb.= $breadcrumb_home;
                $post_ancestors = get_post_ancestors($post);
                if ($post_ancestors) {
                    $post_ancestors = array_reverse($post_ancestors);
                    foreach ($post_ancestors as $crumb)
                        $breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_permalink($crumb), get_the_title($crumb));
                }
                $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_title(get_queried_object_id()));
            }
        } else if (is_year() || is_month() || is_day()) {
            $breadcrumb.= $breadcrumb_home;

            $date = array('y' => NULL, 'm' => NULL, 'd' => NULL);

            $date['y'] = get_the_time('Y');
            $date['m'] = get_the_time('m');
            $date['d'] = get_the_time('j');

            if (is_year()) {
                $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $date['y']);
            }

            if (is_month()) {
                $breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_year_link($date['y']), $date['y']);
                $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, date_i18n('F', $date['m']));
            }

            if (is_day()) {
                $breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_year_link($date['y']), $date['y']);
                $breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_month_link($date['y'], $date['m']), date_i18n('F', $date['m']));
                $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $date['d']);
            }

        } else if (is_search()) {
            $breadcrumb.= $breadcrumb_home;

            $s = get_search_query();
            $c = $wp_query->found_posts;

            $description = sprintf(__('<span class="%1$s">Your search for "%2$s"', 'newmoment'), $current_class, $s);
            $breadcrumb .= $prefix . $description;
        } else if (is_author()) {
            $breadcrumb.= $breadcrumb_home;
            $author_id = get_queried_object_id();
            $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</a>', $current_class, sprintf(__('Posts created by %1$s', 'newmoment'), get_the_author_meta('display_name', $author_id)));
        } else if (is_404()) {
            $breadcrumb.= $breadcrumb_home;
            $breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, __('Error 404', 'newmoment'));
        }

        if ($breadcrumb)
            echo apply_filters('kopa_breadcrumb', $breadcrumb_before . $breadcrumb . $breadcrumb_after);
    }
}

/*
 * Template tag: Show author post link
 */
function kopa_author_posts_link($deprecated = '') {
        if ( !empty( $deprecated ) )
                _deprecated_argument( __FUNCTION__, '2.1' );

        global $authordata;
        $link = sprintf(
                '<a href="%1$s" class="entry-author pull-left" title="%2$s"><i class="fa fa-user"></i><span>%3$s</span></a>',
                get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
                esc_attr( sprintf( __( 'Posts by %s', 'newmoment' ), get_the_author() ) ),
                get_the_author()
        );
        echo  $link;
}

/**
 * Get gallery string ids after getting matched gallery array
 * @return array of attachment ids in gallery
 * @return empty if no gallery were found
 */
function kopa_content_get_gallery_attachment_ids( $content ) {
    $gallery = kopa_content_get_gallery( $content );

    if (isset( $gallery[0] )) {
        $gallery = $gallery[0];
    } else {
        return '';
    } 

    if ( isset($gallery['shortcode']) ) {
        $shortcode = $gallery['shortcode'];
    } else {
        return '';
    } 

    // get gallery string ids
    preg_match_all('/ids=\"(?:\d+,*)+\"/', $shortcode, $gallery_string_ids);
    if ( isset( $gallery_string_ids[0][0] ) ) {
        $gallery_string_ids = $gallery_string_ids[0][0];
    } else {
        return '';
    } 

    // get array of image id
    preg_match_all('/\d+/', $gallery_string_ids, $gallery_ids);
    if ( isset( $gallery_ids[0] ) ) {
        $gallery_ids = $gallery_ids[0];
    } else {
        return '';
    } 

    return $gallery_ids;
}

/**
 * Get Gallery 
 * Return Shortcode [gallery] in post
 */
function kopa_content_get_gallery($content, $enable_multi = false) {
    return kopa_content_get_media($content, $enable_multi, array('gallery'));
}

/**
 * Get Video
 * Return Shortcode [video], [youtube], [vimeo] in post
 */
function kopa_content_get_video($content, $enable_multi = false) {
    return kopa_content_get_media($content, $enable_multi, array('vimeo', 'youtube', 'video'));
}

/**
 * Get Audio 
 * Return Shortcode [audio], [soundcloud] In Post
 */
function kopa_content_get_audio($content, $enable_multi = false) {
    return kopa_content_get_media($content, $enable_multi, array('audio', 'soundcloud'));
}

/**
 * Get Shortcode
 * Return Shortcode Array
 */
function kopa_content_get_media($content, $enable_multi = false, $media_types = array()) {
    $media = array();
    $regex_matches = '';
    $regex_pattern = get_shortcode_regex();
    preg_match_all('/' . $regex_pattern . '/s', $content, $regex_matches);
    foreach ($regex_matches[0] as $shortcode) {
        $regex_matches_new = '';
        preg_match('/' . $regex_pattern . '/s', $shortcode, $regex_matches_new);

        if (in_array($regex_matches_new[2], $media_types)) :
            $media[] = array(
                'shortcode' => $regex_matches_new[0],
                'type' => $regex_matches_new[2],
                'url' => $regex_matches_new[5]
            );
            if (false == $enable_multi) {
                break;
            }
        endif;
    }    return $media;
}

/**
 * Add class <body> Via Hook body_class
 */
function kopa_body_class($classes) {
    $template_setting = kopa_get_template_setting();
    switch ( $template_setting['layout_id'] ){
        case 'page-full-width':
        case 'post-full-width':
        case 'error-404':
            $classes[] = 'kopa-full-width';
            break;
        case 'single-right-sidebar':
        case 'page-right-sidebar':
            $classes[] = 'kopa-single-page';
            break;
        case 'single-full-width':
            $classes[] = 'kopa-single-page no-sidebar';
            break;
        case '404':
            $classes[] = 'kopa-error-page';
            break;
        case 'blog-masonry':
            $classes[] = 'kopa-cat-page cat-mansory';
            break;
        case 'blog-classic':
            $classes[] = 'kopa-cat-page cat-1';
            break;
        case 'frontpage-1':
            $classes[] = 'kopa-home-1';
            break;
        case 'frontpage-2':
            $classes[] = 'kopa-home-2';
            break;
        case 'frontpage-3':
            $classes[] = 'kopa-home-3';
            break;
        default:
            break;
            $classes[] = '';
    }
    if( kopa_get_option('kopa_top_new_status') == 'hide') {
        $classes[] = 'hide-top-new';
    }
    return $classes;
}


/**
 * Get client IP
 */
function kopa_get_client_IP() {
    $IP = NULL;

    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        //check if IP is from shared Internet
        $IP = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        //check if IP is passed from proxy
        $ip_array = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
        $IP = trim($ip_array[count($ip_array) - 1]);
    } elseif (!empty($_SERVER['REMOTE_ADDR'])) {
        //standard IP check
        $IP = $_SERVER['REMOTE_ADDR'];
    }
    return $IP;
}

/*
 * Kopa get rating (editor rate & user rate)
 */
function kopa_get_rate($post_id) {
    $kopa_editor_rate_total = (float) get_post_meta( $post_id, 'kopa_editor_user_total_all_rating_' . 'newmoment', true );
    $kopa_user_total_rate = get_post_meta(get_the_ID(), 'kopa_user_total_rate_' . 'newmoment', true);
    $kopa_user_post_rate = get_post_meta(get_the_ID(), 'kopa_user_post_rate_' . 'newmoment', true);
    
    $total_vote = sizeof($kopa_user_post_rate);
    $kopa_user_rate_total = (float)($kopa_user_total_rate / $total_vote);


    if ( $kopa_editor_rate_total > 0 && $kopa_user_rate_total > 0) {
        $total = ($kopa_editor_rate_total+$kopa_user_rate_total)/2;
    }elseif($kopa_editor_rate_total <= 0 ){
        $total = $kopa_user_rate_total;
    }elseif($kopa_user_rate_total <= 0 ){
       $total = $kopa_editor_rate_total;
    }

    $count = floor($total);
    if ($total > 0 ){
        $ret = '<ul class="kopa-rating clearfix">';

        for( $i=0; $i<$count; $i++ ){
            $ret .= '<li><a class="fa fa-star"></a></li>';
        }

        if ( ($total - $count) >= 0.5){
            $ret .= '<li><a class="fa fa-star-half-o"></a></li>';
            $count++;
        }

        for ( $j=0; $j < 5 - $count; $j++ ){
            $ret .= '<li><a class="fa fa-star-o"></a></li>';
        }
        $ret .= '</ul>';
    }else{
        $ret = '';
    }



    echo $ret;
}

/**
 * Get Post Meta
 */
function kopa_get_post_meta($pid, $key = '', $single = false, $type = 'String', $default = '') {
    $data = get_post_meta($pid, $key, $single);
    switch ($type) {
        case 'Int':
            $data = (int) $data;
            return ($data >= 0) ? $data : $default;
            break;
        default:
            return ($data) ? $data : $default;
            break;
    }
}

/**
 * Set View Count Of Post
 */
function kopa_set_view_count($post_id) {
    $new_view_count = 0;
    $meta_key = 'kopa_' . 'newmoment' . '_total_view';

    $current_views = (int) get_post_meta($post_id, $meta_key, true);

    if ($current_views) {
        $new_view_count = $current_views + 1;
        update_post_meta($post_id, $meta_key, $new_view_count);
    } else {
        $new_view_count = 1;
        add_post_meta($post_id, $meta_key, $new_view_count);
    }
    return $new_view_count;
}

/**
 * Get View Count Of Post
 */
function kopa_get_view_count($post_id) {
    $key = 'kopa_' . 'newmoment' . '_total_view';
    return kopa_get_post_meta($post_id, $key, true, 'Int');
}

/**
 * Get Cat Slug by id
 */
function get_cat_slug($cat_id) {
    $cat_id = (int) $cat_id;
    $category = get_category($cat_id);
    return $category->slug;
}

/**
 * Get The Excerpt ( the_excerpt ) forWidget
 */
function kopa_get_the_excerpt_for_widget($excerpt, $content, $length = 0) {
    if ( $length != 0){
        $kopa_length = $length;
    }elseif( is_home() || is_archive() || is_search() || is_category() ) {
        $kopa_length = (int) kopa_get_option('kopa_blog_excerpt_lenght');
    }else{
        $kopa_length = (int) kopa_get_option('kopa_frontpage_excerpt_lenght');
    }
    $temp_excerp = $excerpt;
    if ( empty($temp_excerp) ) {
        $temp_excerp =  strip_tags($content);
        $temp_excerp =  strip_shortcodes($temp_excerp);
    }

    $kopa_excerpt = wp_trim_words($temp_excerp, $kopa_length, $more = null);
    return $kopa_excerpt;
}

/**
 * Get The Excerpt ( the_excerpt ) forWidget
 */
function kopa_get_the_excerpt_for_blog_1($excerpt, $content) {

    $kopa_length = (int) kopa_get_option('kopa_blog_excerpt_lenght');
    $temp_excerp = $excerpt;
    if ( empty($temp_excerp) ) {
        $temp_excerp =  strip_tags($content);
        $temp_excerp =  strip_shortcodes($temp_excerp);
    }

    $kopa_excerpt = wp_trim_words($temp_excerp, $kopa_length, $more = null);
    return $kopa_excerpt;
}

function kopa_get_image_sizes() {
    $sizes = array(
        'kopa-size-1'    => array(333, 269, true, __('Image size 1: 333 x 269 ', 'newmoment')),
        'kopa-size-2'    => array(154, 110, true, __('Image size 2: 154 x 110', 'newmoment')),
        'kopa-size-3'    => array(788, 393, true, __('Image size 3: 788 x 393', 'newmoment')),
        'kopa-size-4'    => array(236, 393, true, __('Image size 4: 236 x 150', 'newmoment')),
        'kopa-size-5'    => array(800, 405, true, __('Image size 5: 800 x 405', 'newmoment')),
        'kopa-size-6'    => array(227, 268, true, __('Image size 6: 227 x 268', 'newmoment')),
        'kopa-size-7'    => array(397, 177, true, __('Image size 7: 397 x 177', 'newmoment')),
        'kopa-size-8'    => array(256, 172, true, __('Image size 8: 256 x 172', 'newmoment')),
        'kopa-size-9'    => array(580, 727, true, __('Image size 9: 580 x 727', 'newmoment')),
        'kopa-size-10'    => array(75, 57, true, __('Image size 10: 75 x 57', 'newmoment')),
        'kopa-size-11'    => array(550, 357, true, __('Image size 11: 550 x 357', 'newmoment')),
        'kopa-size-12'    => array(580, 428, true, __('Image size 11: 580 x 428', 'newmoment')),
    );
    return apply_filters('kopa_get_image_sizes', $sizes);
}

function kopa_add_image_sizes() {
    $sizes = kopa_get_image_sizes();
    foreach ($sizes as $slug => $details) {
        add_image_size($slug, $details[0], $details[1], $details[2]);
    }
}