<?php
/**
 * Custom functions that act independently of the theme templates
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package Excel_Business
 */

if ( ! function_exists( 'excel_business_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
 */
function excel_business_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() )
	);

	echo '<span class="posted-on"><i class="fa fa-calendar-o"></i><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a></span>'; // WPCS: XSS OK.

}
endif;

if( ! function_exists( 'excel_business_byline' ) ) :
/**
 * Posted by
*/
function excel_business_byline(){
    echo '<span class="byline"><i class="fa fa-user"></i> <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></span>'; // WPCS: XSS OK.
}
endif;

if( ! function_exists( 'excel_business_comment_link' ) ) :
/**
 * Comment Links
*/
function excel_business_comment_link(){
    if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments"><i class="fa fa-comments"></i>';
		/* translators: %s: post title */
		comments_popup_link( sprintf( wp_kses( __( 'Leave a Comment<span class="screen-reader-text"> on %s</span>', 'excel-business' ), array( 'span' => array( 'class' => array() ) ) ), get_the_title() ) );
		echo '</span>';
	}
}
endif;

if ( ! function_exists( 'excel_business_cat_tag' ) ) :
/**
 * Prints HTML with meta information for the categories, tags and comments.
 */
function excel_business_cat_tag() {
	// 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__( ', ', 'excel-business' ) );
		if ( $categories_list && excel_business_categorized_blog() ) {
			printf( '<div class="cat-links">%1$s</div>', $categories_list ); // WPCS: XSS OK.
		}

		/* translators: used between list items, there is a space after the comma */
		$tags_list = get_the_tag_list( '', esc_html__( ', ', 'excel-business' ) );
		if ( $tags_list ) {
			printf( '<div class="tags-links">%1$s</div>', $tags_list ); // WPCS: XSS OK.
		}
	}
}
endif;

if( ! function_exists( 'excel_business_categorized_blog' ) ) :
/**
 * Returns true if a blog has more than 1 category.
 *
 * @return bool
 */
function excel_business_categorized_blog() {
	if ( false === ( $all_the_cool_cats = get_transient( 'excel_business_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( 'excel_business_categories', $all_the_cool_cats );
	}

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

if( ! function_exists( 'excel_business_comment_list' ) ) :
/**
 * Callback function for Comment List
 * 
 * @link https://codex.wordpress.org/Function_Reference/wp_list_comments 
 */
function excel_business_comment_list( $comment, $args, $depth ){
    if ( 'div' == $args['style'] ) {
		$tag = 'div';
		$add_below = 'comment';
	} else {
		$tag = 'li';
		$add_below = 'div-comment';
	}
?>
	<<?php echo $tag ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
	
    <?php if ( 'div' != $args['style'] ) : ?>
	<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
	<?php endif; ?>
	   
        <div class="comment-meta">
            <div class="comment-author vcard">
                <?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>        	
        	</div>
    	</div>
        
        <div class="comment-content">
            <?php if ( $comment->comment_approved == '0' ) : ?>
        		<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'excel-business' ); ?></em>
        		<br />
        	<?php endif; ?>
            
            <b class="fn"><?php comment_author(); ?></b>        	
            
            <div class="comment-metadata">            
        		<time datetime="<?php comment_time( 'c' ); ?>">                    
                    <?php 
                        esc_html_e( 'Posted on ', 'excel-business' );    
                        comment_date();
                    ?>
                </time>
            </div>
    
            <?php comment_text(); ?>
    
        	<div class="reply">
        	   <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
        	</div>
        </div>
	
    <?php if ( 'div' != $args['style'] ) : ?>
	</div>
	<?php endif; ?>
<?php
}
endif;

if( ! function_exists( 'excel_business_get_home_sections' ) ) :
/**
 * Returns different home sections
*/
function excel_business_get_home_sections(){
    
    $sections = array();
    
    $ed_banner      = get_theme_mod( 'ed_slider', true );
    $ed_intro       = get_theme_mod( 'ed_intro', true );
    $ed_portfolio   = get_theme_mod( 'ed_portfolio', true );
    $ed_testimonial = get_theme_mod( 'ed_testimonial', true );
    $ed_team        = get_theme_mod( 'ed_team', true );
    $ed_blog        = get_theme_mod( 'ed_blog_section', true );
    $ed_contact     = get_theme_mod( 'ed_contact', true );
    
    if( $ed_banner ) array_push( $sections, 'sections/banner' );
    if( $ed_intro ) array_push( $sections, 'sections/intro' );
    if( $ed_portfolio ) array_push( $sections, 'sections/portfolio' );
    if( $ed_testimonial ) array_push( $sections, 'sections/testimonial' );
    if( $ed_team ) array_push( $sections, 'sections/team' );
    if( $ed_blog ) array_push( $sections, 'sections/blog' );
    if( $ed_contact ) array_push( $sections, 'sections/contact' );
    
    $home_sections = apply_filters( 'excel_business_home_sections', $sections ); 
    return $home_sections;       
}
endif;

if( ! function_exists( 'excel_business_get_categories' ) ) :
/**
 * Function to list post categories in customizer options
*/
function excel_business_get_categories( $select = true, $taxonomy = 'category', $slug = false ){
    
    /* Option list of all categories */
    $categories = array();
    
    if( $select ) $categories[''] = __( 'Choose Category', 'excel-business' );
    
    if( taxonomy_exists( $taxonomy ) ){
        $args = array( 
            'hide_empty' => false,
            'taxonomy'   => $taxonomy 
        );
        
        $catlists = get_terms( $args );
        
        foreach( $catlists as $category ){
            if( $slug ){
                $categories[$category->slug] = $category->name;
            }else{
                $categories[$category->term_id] = $category->name;    
            }        
        }
    }
    return $categories;
}
endif;

if( ! function_exists( 'excel_business_get_posts' ) ) :
/**
 * Fuction to list Custom Post Type
*/
function excel_business_get_posts( $post_type = 'post' ){
    
    $args = array(
    	'posts_per_page'   => -1,
    	'post_type'        => $post_type,
    	'post_status'      => 'publish',
    	'suppress_filters' => true 
    );
    $posts_array = get_posts( $args );
    
    // Initate an empty array
    $post_options = array();
    $post_options[''] = __( ' -- Choose -- ', 'excel-business' );
    if ( ! empty( $posts_array ) ) {
        foreach ( $posts_array as $posts ) {
            $post_options[ $posts->ID ] = $posts->post_title;
        }
    }
    return $post_options;
    wp_reset_postdata();
}
endif;

if( ! function_exists( 'excel_business_get_section_header' ) ) :
/**
 * Returns section header
 * 
 * @param $title Section Title
 * @param $sub_title Section Sub Title
*/
function excel_business_get_section_header( $title, $sub_title ){
    if( $title || $sub_title ){ ?>
        <header class="section-header">
			<?php 
                if( $title ) echo '<h2 class="section-title">' . esc_html( $title ) . '</h2>';
                if( $sub_title ) echo wpautop( wp_kses_post( $sub_title ) );
            ?>
		</header>
		<?php 
    }
}
endif;

/**
 * Check if Excel Addon is active
*/
function excel_business_is_ea_active(){
    return class_exists( 'Excel_Addon' ) ? true : false;
}

/**
 * Query Contact Form 7
 */
function excel_business_is_cf7_activated() {
	return class_exists( 'WPCF7' ) ? true : false;
}