<?php
/**
 * Essentials Files and theme supports for theme
 * @since 1.0
 * @package RoyalNews
 */

if( !function_exists( 'royalnews_schema_html_tag' ) ){
	/**
	 * Schema for html tag
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_schema_html_tag( $type ) {
		$schema_body = '';
		if( 'body' == $type ){
			# Check conditions.
			$is_blog = ( is_home() || is_archive() || is_attachment() || is_tax() || is_single() ) ? true : false;

			# Set up default itemtype.
			$itemtype = 'WebPage';

			# Get itemtype for the blog.
			$itemtype = ( $is_blog ) ? 'Blog' : $itemtype;

			# Get itemtype for search results.
			$itemtype = ( is_search() ) ? 'SearchResultsPage' : $itemtype;
			$schema_body = "itemtype='https://schema.org/{$itemtype}' itemscope='itemscope' ";
		}elseif( 'header' == $type ){
			$schema_body = "itemtype='https://schema.org/WPHeader' itemscope='itemscope' role='banner' ";
		}elseif( 'footer' == $type ){
			$schema_body = "itemtype='https://schema.org/WPFooter' itemscope='itemscope' role='contentinfo'";
		}elseif( 'article' == $type ){
			$schema_body = "itemtype='https://schema.org/CreativeWork' itemscope='itemscope'";
		}
		
		echo $schema_body;
	}
}

if( !function_exists( 'royalnews_the_category' ) ){	
	/**
	 * Prints the category of the posts
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_the_category( $post_id = false ){
		$cat = get_the_category( $post_id );
		if( !empty( $cat ) ){ 
			$numItems = count( $cat );
			$i = 0; ?>
			<ul class="royalnews-post-categories">
				<?php foreach ( $cat as $c ) { ?>
					<li>
						<a href="<?php echo esc_url( get_category_link( $c ) ); ?>">
							<?php echo esc_html( $c->name ); ?>
						</a>
					</li>
					<?php  if( ++$i != $numItems ) {
					    echo "&nbsp;";
					}
				} ?>
			</ul>					
		<?php }
	}
}

if( !function_exists( 'royalnews_the_date' ) ){	
	/**
	 * Prints HTML with meta information for the current post-date/time.
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_the_date( $post_id = null ) {		
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';

		$time_tag = sprintf(
			$time_string,
			esc_attr( get_the_date( DATE_W3C, $post_id ) ),
			esc_html( get_the_date( get_option('date_format'), $post_id ) ),
			esc_attr( get_the_modified_date( DATE_W3C, $post_id ) ),
			esc_html( get_the_modified_date( DATE_W3C, $post_id ) )
		);

		printf(
			'<span class="royalnews-posted-on">
				<a href="%1$s" rel="bookmark">
					%2$s
				</a>
			</span>',
			esc_url( get_day_link( get_the_time('Y'), get_the_time('m'), get_the_time('d') ) ),
			$time_tag
		);
	}
}

if( !function_exists( 'royalnews_posted_by' ) ){
	/**
	 * Meta information about theme author.
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_posted_by( $post_id = false ){
		if( $post_id ){
			$author_id = get_post_field( 'post_author', $post_id );
			$author_name = get_the_author_meta( 'display_name', $author_id );
		}
		printf(
			/* translators:1-author link, 2-author image link, 
			 * 3- author text, 4- author name.
			 */
			'<div class="royalnews-post-author">
				<span class="royalnews-author-text">
					%2$s
				</span>
				<a class="royalnews-author-link" href="%1$s">
					<span class="author">
						%3$s
					</span>
				</a>
			</div>',
			esc_url( get_author_posts_url( $post_id ? get_the_author_meta( 'ID', $author_id ) : get_the_author_meta( 'ID' ) ) ),
			esc_html__( 'By', 'royalnews' ),
			esc_html( $post_id ? $author_name : get_the_author_meta( 'name' ) )
		);
	}
}

if( !function_exists( 'royalnews_get_comment_number' ) ){
	/**
	 * Returns Number of comment 
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_get_comment_number(){
		if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
			echo '<span class="royalnews-comments">';
			comments_popup_link(
				'<i class="far fa-comment"></i> '.esc_html__( 'Post a comment', 'royalnews' ),		
				'<i class="fas fa-comment"></i> '.esc_html__( '1 commment', 'royalnews' ),
				'<i class="fas fa-comments"></i> % '. esc_html__( 'comments' , 'royalnews' )
			);
			echo '</span>';
		}
	}
}

if( !function_exists( 'royalnews_posts_by_type' ) ){
	/**
	 * Returns post by type
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_posts_by_type( $type, $cat_id, $post_to_display = false ){
		$posts = array();
		if( 'post' == $type ){
			$recent_posts = wp_get_recent_posts(array(
			    'numberposts' => $post_to_display ? $post_to_display : -1,
			    'post_status' => 'publish'
			));

			foreach ( $recent_posts as $post) {
				$posts[] = $post[ 'ID' ]; 
			}
		}elseif( 'category' == $type ){			
			$args = array(
				'post_type' => 'post',
				'posts_per_page' => $post_to_display ? $post_to_display : -1,
				'orderby' => 'date',
				'order' => 'DESC',
				'ignore_sticky_posts' => 1,
			);
			if( $cat_id ){
				$args[ 'cat' ] = $cat_id; 
			}

			$query = new WP_Query( $args );
			if ( $query->have_posts() ) {
			    while ( $query->have_posts() ) {
			        $query->the_post();
			        $posts[] = get_the_ID();
			    }
			}
			wp_reset_postdata();					
		}
		if( empty( $posts ) ){
			return false;
		}else{
			return $posts;
		}
	}
}

if( !function_exists( 'royalnews_posts_navigator' ) ){	
	/**
	* The post Navigation
	*
	* @since 1.0
	* @package RoyalNews
	*/
	function royalnews_posts_navigator( $echo = true ) {			
		the_posts_pagination(
			array(
				'mid_size'  => 2,
				'prev_text' => '<i class="fas fa-chevron-left"></i>',
				'next_text' => '<i class="fas fa-chevron-right"></i>',
			)
		);
	}
}

if( !function_exists( 'royalnews_single_post_navigator' ) ){	
	/**
	* Pagination for single post in single page
	*
	* @since 1.0
	* @package RoyalNews
	*/
	function royalnews_single_post_navigator(){
		the_post_navigation( array(
			'prev_text' => '<span class="screen-reader-text">' . esc_html__( 'Previous Post', 'royalnews' ) . '</span><span class="nav-title"><i class="fas fa-long-arrow-alt-left"></i> %title</span>',
			'next_text' => '<span class="screen-reader-text">' . esc_html__( 'Next Post', 'royalnews' ) . '</span><span class="nav-title">%title <i class="fas fa-long-arrow-alt-right"></i></span>',
		));
	}
}

if( !function_exists( 'royalnews_post_content_navigation' ) ){	
	/**
	 * Pagination for the content seperated by page break.
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_post_content_navigation(){
		wp_link_pages( array(
			'before'      => '<div class="royalnews-page-links">' . esc_html__( 'Pages:', 'royalnews' ),
	        'after'       => '</div>',
	        'link_before' => '<span class="royalnews-page-number">',
	        'link_after'  => '</span>'
		) );
	}
}

if( !function_exists( 'royalnews_display_tag_list' ) ){
	/**
	 * Displayes the tag
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_display_tag_list(){
		$tags_= get_the_tag_list( '', ', ' );
		if ( $tags ) {
			printf(
				/* translators: 1: posted in label, only visible to screen readers. 2: list of tags. */
				'<div class="tags-links"><div class="screen-reader-text">%2$s </div>%3$s</div>',
				esc_html__( 'Tags:', 'royalnews' ),
				esc_html( $tags )
			); // WPCS: XSS OK.
		}
	}
}

if( !function_exists( 'royalnews_banner_title' ) ){
	/**
	 * Displayes title for banner
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_banner_title(){

		if( is_page() || is_singular() ){
			the_title( '<h2 class="royalnews-page-title">', '</h2>' );
		}elseif(  is_archive() ){
			the_archive_title( '<h2 class="royalnews-page-title">', '</h2>' );
			the_archive_description( '<div class="taxonomy-description">', '</div>' );
		}elseif( is_front_page() ){
			$blog_title = get_the_title( get_option( 'page_for_posts' ) ); ?>
			<h2 class="royalnews-page-title"><?php echo esc_html( $blog_title ) ?></h2>
		<?php }elseif( is_home() ){
			# when home page is latest posts the custom title.
			$blog_title = apply_filters( 'royalnews_blog_title', true );?>

			<h2 class="royalnews-page-title"><?php echo esc_html( $blog_title ) ?></h2>

		<?php }elseif(  is_search() ){
			/* translators: %s: search page result */ 
			?>
			<h2 class="royalnews-page-title">
				<?php 
					printf( 
						esc_html__( 'Search Results for: %s', 'royalnews' ), 
						'<span>' . get_search_query() . '</span>' 
					);
				?>
			</h2>
		<?php }
	}
}

if( !function_exists( 'royalnews_get_meta' ) ){	
	/**
	 * get post meta by Post ID
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_get_meta( $meta_key = false, $post_id = null,  $single = true  ){
		if( $meta_key ){
			$meta_key =  'royalnews-' . $meta_key;
			if( is_front_page() ){
				$post_id = get_option( 'page_for_posts' );
				}elseif( class_exists( 'Woocommerce' ) && is_shop() ){
					$post_id = get_option( 'woocommerce_shop_page_id' );
				}else{
				$post_id = $post_id ? $post_id : get_the_ID();
			}
			return get_post_meta( $post_id, $meta_key, $single );
		}
	}
}