<?php
/*
* Functions just for this particular theme
* This file can be overridden by child theme, paste it into breek-child/functions/theme-functions.php
*/

if ( ! function_exists( 'wp_body_open' ) ) {
    function wp_body_open() {
        do_action( 'wp_body_open' );
    }
}

if( !function_exists('breek_lite_get_option') ){
    function breek_lite_get_option( $option = '', $default = '' ) {
        $prefix = BREEK_LITE_THEMEPREFIX . '_';
        return get_theme_mod( $prefix . $option, $default );
    }
}

/* Skip to content link for accessibility */
function breek_lite_render_skip_to_content_link() {
    $content_id = '#single';
    if( is_home( ) || is_front_page() ){
        $content_id = '#home';
    }
    if( is_archive() || is_search() ){
        $content_id = '#archives';
    }
    echo '<a href="'.esc_attr($content_id).'" class="skip-link screen-reader-text" tabindex="0">'. esc_html__( 'Skip to content', 'breek-lite' ) .'</a>';
}

/* Custom Pagination */

function breek_lite_pagination($query = NULL){
	global $wp_query, $paged;
    if($query) $wp_query = $query;
    if( !empty($wp_query->query['paged']) ){
        $paged = $wp_query->query['paged'];
    }
    
?>
    <div class="separator last hide-on-tablet hide-on-mobile"></div>
    <div class="clear"></div>
    <!-- start: .epcl-pagination -->
    <div class="epcl-pagination section">
        <div class="nav">
            <?php echo get_previous_posts_link( esc_html__('Previous', 'breek-lite') ); ?>
            <span class="page-number">
                <?php esc_html_e('Page', 'breek-lite'); ?> <?php echo max(1, get_query_var('paged') ); ?>
                <?php esc_html_e('of', 'breek-lite'); ?> <?php echo intval($wp_query->max_num_pages); ?>
            </span>
            <?php echo get_next_posts_link( esc_html__('Next', 'breek-lite') ); ?>
        </div>
    </div>
    <!-- end: .epcl-pagination -->
<?php
}

add_filter('wp_list_categories', 'breek_lite_at_count_span');
add_filter('get_archives_link', 'breek_lite_archives_count');

function breek_lite_at_count_span($links) {
    $links = str_replace('</a> (', '</a> <span>', $links);
    $links = str_replace(')', '</span>', $links);
    return $links;
}

function breek_lite_archives_count($links){
    $links = str_replace('</a>&nbsp;(', '</a> <span>', $links);
    $links = str_replace(')</li>', '</span></li>', $links);
    return $links;
}

// Add search button to the end of the main menu
function breek_lite_search_nav_item($items, $args) {
    if ($args->theme_location == 'breek_lite_header' && breek_lite_get_option('enable_search_header', 1) == '1' ){
        return $items .= '<li class="search-menu-item hide-on-mobile hide-on-tablet"><a href="#search-lightbox" class="lightbox mfp-inline"><i class="remixicon remixicon-search-line"></i></a></li>';
    }
    return $items;
}
add_filter('wp_nav_menu_items','breek_lite_search_nav_item', 10, 2);

// Add subscribe button to the end of the main menu
function breek_lite_subscribe_nav_item($items, $args) {
    $subscribe_title = esc_html__("Subscribe", 'breek-lite');
    if( function_exists('breek_lite_get_option') && breek_lite_get_option('title_subscribe_button') ){
        $subscribe_title = breek_lite_get_option('title_subscribe_button');
    }
    if ($args->theme_location == 'breek_lite_header' && breek_lite_get_option('header_enable_subscribe') == '1' ){
        return $items .= '<li class="subscribe-menu-item hide-on-mobile hide-on-tablet"><a href="'.esc_url(breek_lite_get_option('subscribe_url')).'" target="_blank" class="subscribe epcl-button red hide-on-tablet hide-on-mobile" rel="nofollow noopener">'.esc_html($subscribe_title).' <i class="fa fa-paper-plane"></i></a></li>';
    }
    return $items;
}
add_filter('wp_nav_menu_items','breek_lite_subscribe_nav_item', 10, 2);

// Render categories with colors

function breek_lite_render_categories( $class = 'absolute' ){
    $categories = get_the_category();

    if( empty($categories) ) return;

    if( function_exists('breek_lite_get_option') && breek_lite_get_option('enable_single_category') === '0' && is_single() ){
        return;
    }

    $html = '<div class="tags '.$class.'">';
    $i = 0;
    $limit = 2;
    if( breek_lite_get_option('category_limit') ){
        $limit = absint( breek_lite_get_option('category_limit') );
    }
    foreach($categories as $c){
        if( $i == $limit ) break;
        $html .= '<a href="'.esc_url( get_category_link($c) ).'" class="tag-link-'.$c->term_id.'">'.esc_html($c->name).'</a>';
        $i++;
    }
    $html .= '</div>';

    return $html;
}

// SVG sanitization
function breek_lite_svg_kses_ruleset() {
    $kses_defaults = wp_kses_allowed_html( 'post' );

    $svg_args = array(
        'svg'   => array(
            'xlink'           => true,
            'class'           => true,
            'aria-hidden'     => true,
            'aria-labelledby' => true,
            'role'            => true,
            'xmlns'           => true,
            'width'           => true,
            'height'          => true,
            'viewbox'         => true, // <= Must be lower case!
        ),
        'use' => array(
            'xlink:href' => true,
        ),
        'g'     => array( 'fill' => true ),
        'title' => array( 'title' => true ),
        'path'  => array(
            'd'    => true,
            'fill' => true,
        ),
    );
    return array_merge( $kses_defaults, $svg_args );
}