<?php
/**
 * Custom template tags for this theme.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package TriFold
 */

/**
 * Adds custom excerpt length
 *
 * @since  1.0.0
 * @param  int $length 
 * @return int
 */
function trifold_excerpt_length( $length ) {
	return 30;
}
add_filter( 'excerpt_length', 'trifold_excerpt_length' );

/**
 * Custom excerpt more text and link
 *
 * @since 1.0.0
 * @param  string $more
 * @return string
 */
function trifold_excerpt_more( $more ){
	return '&hellip;';
}
add_filter( 'excerpt_more', 'trifold_excerpt_more' );

/**
 * Append `Read More %s` link to the end of all excerpt
 *
 * @since  1.0.0
 * @param  string $excerpt
 * @return string
 */
function trifold_the_excerpt( $excerpt ) {
	 /* Translators: The %s is the post title shown to screen readers. */
	$text = sprintf( __( 'Read More %s', 'trifold' ), '<span class="screen-reader-text">' . get_the_title() . '</span>' );
	$more = sprintf( '<a href="%s" class="more-link">%s</a>', get_permalink(), $text );
	return $excerpt . $more;
}
//add_filter( 'the_excerpt', 'trifold_the_excerpt' );

if ( ! function_exists( 'trifold_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
 */
function trifold_posted_on() {
	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
	}

	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	$posted_on = sprintf(
		esc_html_x( 'Posted on %s', 'post date', 'trifold' ),
		'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
	);

	$byline = sprintf(
		esc_html_x( 'by %s', 'post author', 'trifold' ),
		'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
	);

	echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.

}
endif;

if ( ! function_exists( 'trifold_entry_footer' ) ) :
/**
 * Prints HTML with meta information for the categories, tags and comments.
 */
function trifold_entry_footer() {
	// Hide category and tag text for pages.
	if ( 'post' === get_post_type() ) {
		/* translators: used between list items, there is a space after the comma */
		$categories_list = get_the_category_list( esc_html__( '/', 'trifold' ) );
		if ( $categories_list && trifold_categorized_blog() ) {
			printf( '<span class="cat-links">' . esc_html__( 'in %1$s', 'trifold' ) . '</span>', $categories_list ); // WPCS: XSS OK.
		}

	}

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
		comments_popup_link( esc_html__( '0 Comments', 'trifold' ), esc_html__( '1 Comment', 'trifold' ), esc_html__( '% Comments', 'trifold' ) );
		echo '</span>';
	}

}
endif;

/**
 * Returns true if a blog has more than 1 category.
 *
 * @return bool
 */
function trifold_categorized_blog() {
	if ( false === ( $all_the_cool_cats = get_transient( 'trifold_categories' ) ) ) {
		// Create an array of all the categories that are attached to posts.
		$all_the_cool_cats = get_categories( array(
			'fields'     => 'ids',
			'hide_empty' => 1,
			// We only need to know if there is more than one category.
			'number'     => 2,
		) );

		// Count the number of categories that are attached to the posts.
		$all_the_cool_cats = count( $all_the_cool_cats );

		set_transient( 'trifold_categories', $all_the_cool_cats );
	}

	if ( $all_the_cool_cats > 1 ) {
		// This blog has more than 1 category so trifold_categorized_blog should return true.
		return true;
	} else {
		// This blog has only 1 category so trifold_categorized_blog should return false.
		return false;
	}
}

/**
 * Flush out the transients used in trifold_categorized_blog.
 */
function trifold_category_transient_flusher() {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	// Like, beat it. Dig?
	delete_transient( 'trifold_categories' );
}
add_action( 'edit_category', 'trifold_category_transient_flusher' );
add_action( 'save_post',     'trifold_category_transient_flusher' );

/**
 * Get the pagination
 *
 * @since 1.0.0 
 */
function trifold_post_pagination() {
	$type = get_theme_mod( 'pagination-type', trifold_get_default( 'pagination-type' ) );
	switch ( $type ) {
		case 'numeric':
			trifold_numeric_posts_nav();
			break;
		
		default:
			trifold_standard_posts_nav();
			break;
	}
}

if( ! function_exists( 'trifold_standard_posts_nav' ) ) :
/**
 * Add custom standard pagination
 * @return void
 */
function trifold_standard_posts_nav() { 
	global $wp_query;
	if( $wp_query->max_num_pages > 1 ) :
	?>
	<ul class="pager nomargin">
		<li class="previous">
            <?php previous_posts_link( esc_html__( 'Older', 'trifold' ) ); ?>			
		</li>
		<li class="next">
			<?php next_posts_link( esc_html__( 'Newer', 'trifold' ) ); ?>
		</li>
	</ul>
<?php endif;
 }
 endif;

if ( ! function_exists( 'trifold_numeric_posts_nav' ) ) :
/**
 * Add custom numeric pagination
 *
 * @since 1.0.0
 * @return void
 */
function trifold_numeric_posts_nav() {
    global $wp_query, $wp_rewrite;
    $current_page   = ( $wp_query->query_vars['paged'] > 1 ) ? $wp_query->query_vars['paged'] : 1;
    $total_pages 	= $wp_query->max_num_pages;
    $default_link   = add_query_arg( 'paged', '%#%', esc_url( get_pagenum_link( 'page' ) ) );
    $custom_link    = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link(1) ) ) . 'page/%#%/', 'paged' );
    $base 			= ( $wp_rewrite->using_permalinks() ) ? $custom_link : $default_link;
    $next_text_pagination = is_rtl() ? '&laquo;' : '&raquo;';
    $prev_text_pagination = is_rtl() ? '&raquo;' : '&laquo;';

    $args = array(
        'base'      => $base,
        'format'    => '?paged=%#%',
        'total'     => $total_pages,
        'current'   => $current_page,
        'show_all'  => false,
        'end_size'  => 1,
        'mid_size'  => 2,
        'type'      => 'array',
        'add_args'  => ( ! empty( $wp_query->query_vars['s'] ) ) ? array( 's' => get_query_var( 's' ) ) : '',
        'next_text' => $next_text_pagination,
        'prev_text' => $prev_text_pagination
    );      
    $paging = paginate_links( $args ); 
    if( is_array( $paging ) ){
    echo '<ul class="pagination nobottommargin">'; 
    foreach ( $paging as $page ) {
                    echo "<li>$page</li>";
            } 
    echo '</ul>';
	}
}
endif;

/**
 * Custom post navigation for single posts
 *
 * @since  1.0.0 
 * @return string
 */
function trifold_post_navigation(){
	$navigation = '';
	$previous   = get_previous_post_link( '<div class="nav-previous">%link</div>', '<i class="icon-angle-left"></i> %title', true );
	$next       = get_next_post_link( '<div class="nav-next">%link</div>', '%title <i class="icon-angle-right"></i>', true );

	// Only add markup if there's somewhere to navigate to.
	if ( $previous || $next ) {
		$navigation = _navigation_markup( $previous . $next, 'post-navigation' );
	}

	echo $navigation;
}

if ( ! function_exists( 'trifold_the_custom_logo' ) ) :
/**
 * Displays the optional custom logo.
 *
 * Does nothing if the custom logo is not available.
 *
 * @since torte
 */
function trifold_the_custom_logo() {
	if ( function_exists( 'the_custom_logo' ) ) {
		the_custom_logo();
	}
}
endif;

/**
 * Add the Yoast SEO breadcrumb, if the plugin is activated.
 *
 * @since 1.0.0
 *
 * @return void
 */
function trifold_display_breadcrumb() {
	if ( function_exists( 'yoast_breadcrumb' ) ) {
		yoast_breadcrumb( '<p class="yoast-seo-breadcrumb">', '</p>' );
	}
}