<?php
/**
	Custom template tags for this theme
	@package WordPress
	@subpackage Uikit Starter
	@since 1.0.1
*/

/**
	Special thanks to Twenty Twenty-One WordPress Theme.
	@link https://wordpress.org/themes/twentytwentyone/
	@author WordPress team
	@license http://www.gnu.org/licenses/gpl-2.0.html GPL v2 or later
*/


/* Prepare
______________________________
*/

	// If this file is called directly,abort.
	if(!defined('WPINC')){die;}


/* Exec
______________________________
*/

	/**
		@since 1.0.1
			Prints HTML with meta information for the current post-date/time and author.
		@return (void)
	*/
	if(!function_exists('__template_the_posted_on')) :
	function __template_the_posted_on()
	{
		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';

		$time_string = sprintf(
			$time_string,
			esc_attr(get_the_date(DATE_W3C)),
			esc_html(get_the_date())
		);
		echo '<span class="posted-on">';
		printf(
			/* translators: %s: publish date. */
			esc_html__('Published %s','uikit-starter'),
			// phpcs:ignore WordPress.Security.EscapeOutput
			$time_string
		);
		echo '</span>';

	}// Method
	endif;


	/**
		@since 1.0.1
			Prints HTML with meta information for the categories, tags and comments.
	*/
	if(!function_exists('__template_the_entry_footer')) :
	function __template_the_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__(', ','uikit-starter'));
			if($categories_list && __template_the_categorized_blog()) :
				// WPCS: XSS OK.
				printf('<span class="cat-links uk-display-block">' . '<span uk-icon="file-text"></span>' . ' ' . esc_html__('Posted in: %1$s','uikit-starter') . '</span>' . ' ',$categories_list);
			endif;

			/* translators: used between list items, there is a space after the comma */
			$tags_list = get_the_tag_list('',esc_html__(', ','uikit-starter'));
			if($tags_list) :
				// WPCS: XSS OK.
				printf('<span class="tags-links uk-display-block">' . '<span uk-icon="tag"></span>' . ' ' . esc_html__('Tagged: %1$s','uikit-starter') . '</span>' . ' ',$tags_list);
			endif;
		}// endif

		if(!is_single() && !post_password_required() && (comments_open() || get_comments_number())){
			echo '<span class="comments-link">' . '<span uk-icon="comments"></span>' . ' ';
			comments_popup_link(esc_html__('Leave a comment','uikit-starter'),esc_html__('1 Comment','uikit-starter'),esc_html__('% Comments','uikit-starter'));
			echo '</span>';
		}// endif

		edit_post_link(
			sprintf(
				/* translators: %s: Name of current post */
				esc_html__('Edit %s','uikit-starter'),
				the_title('<span class="screen-reader-text">"','"</span>',false)
			),
			'<span class="edit-link uk-display-block">' . '<span uk-icon="file-edit"></span>' . ' ',
			'</span>'
		);

	}// Method
	endif;


	/**
		@since 1.0.1
			Returns true if a blog has more than 1 category.
		@return (bool)
	*/
	if(!function_exists('__template_the_categorized_blog')) :
	function __template_the_categorized_blog()
	{
		if(FALSE === ($all_the_cool_cats = get_transient('ultra_framework_categories'))){
			/**
				@description
					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,
			));

			/**
				@description
					Count the number of categories that are attached to the posts.
			*/
			$all_the_cool_cats = count($all_the_cool_cats);
			set_transient('ultra_framework_categories',$all_the_cool_cats);
		}// endif

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

	}// Method
	endif;


	/**
		@since 1.0.1
			Displays the optional custom logo.
		@return (void)
	*/
	if(!function_exists('__template_the_custom_logo')) :
	function __template_the_custom_logo()
	{
		if(function_exists('the_custom_logo')){
			the_custom_logo();
		}// endif

	}// Method
	endif;


	/**
		@since 1.0.1
			Display navigation to next/previous set of posts when applicable.
	*/
	if(!function_exists('__template_the_paging_nav')) :
	function __template_the_paging_nav()
	{
		global $wp_query,$wp_rewrite;

		/**
			@description
				Don't print empty markup if there's only one page.
		*/
		if($wp_query->max_num_pages < 2){return;}

		$paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
		$pagenum_link = html_entity_decode(get_pagenum_link());
		$query_args = array();
		$url_parts = explode('?',$pagenum_link);

		if(isset($url_parts[1])){
			wp_parse_str($url_parts[1],$query_args);
		}// endif

		$pagenum_link = remove_query_arg(array_keys($query_args),$pagenum_link);
		$pagenum_link = trailingslashit($pagenum_link) . '%_%';

		$format = $wp_rewrite->using_index_permalinks() && !strpos($pagenum_link,'index.php') ? 'index.php/' : '';
		$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit($wp_rewrite->pagination_base . '/%#%','paged') : '?paged=%#%';

		/**
			@description
				Set up paginated links.
		*/
		$links = paginate_links(array(
			'base' => $pagenum_link,
			'format' => $format,
			'total' => $wp_query->max_num_pages,
			'current' => $paged,
			'mid_size' => 1,
			'add_args' => array_map('urlencode',$query_args),
			'prev_text' => __('&larr; Previous','uikit-starter'),
			'next_text' => __('Next &rarr;','uikit-starter'),
			'type' => 'list',
		));

		if($links){
			?><nav class = "navigation paging-navigation" role = "navigation">
				<h1 class = "screen-reader-text"><?php _e('Posts navigation','uikit-starter'); ?></h1>
				<?php echo $links; ?>
			</nav><!-- .navigation --><?php
		}// endif

	}// Method
	endif;


	/**
		@since 1.0.1
			Displays an optional post thumbnail.
	*/
	if(!function_exists('__template_the_post_thumbnail')) :
	function __template_the_post_thumbnail()
	{
		if(post_password_required() || is_attachment() || !has_post_thumbnail()){return;}

		if(is_singular()){
			?><div class = "post-thumbnail">
				<?php the_post_thumbnail(); ?>
			</div><!-- .post-thumbnail -->

		<?php }else{ ?>
			<a class = "post-thumbnail" href = "<?php the_permalink(); ?>" aria-hidden = "true">
				<?php the_post_thumbnail('post-thumbnail',array('alt' => the_title_attribute('echo=0'))); ?>
			</a><?php
		
		}// endif

	}// Method
	endif;
