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

/**
 * Determine whether this is an AMP response.
 *
 * Note that this must only be called after the parse_query action.
 *
 * @link https://github.com/Automattic/amp-wp
 * @return bool Is AMP endpoint (and AMP plugin is active).
 */
function pixigo_is_amp() {
	return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
}

/**
 * Determine whether amp-live-list should be used for the comment list.
 *
 * @return bool Whether to use amp-live-list.
 */
function pixigo_using_amp_live_list_comments() {
	if ( ! pixigo_is_amp() ) {
		return false;
	}
	$amp_theme_support = get_theme_support( 'amp' );
	return ! empty( $amp_theme_support[0]['comments_live_list'] );
}

/**
 * Add pagination reference point attribute for amp-live-list when theme supports AMP.
 *
 * This is used by the navigation_markup_template filter in the comments template.
 *
 * @link https://www.ampproject.org/docs/reference/components/amp-live-list#pagination
 *
 * @param string $markup Navigation markup.
 * @return string Markup.
 */
function pixigo_add_amp_live_list_pagination_attribute( $markup ) {
	return preg_replace( '/(\s*<[a-z0-9_-]+)/i', '$1 pagination ', $markup, 1 );
}

/**
 * Prints the header of the current displayed page based on its contents.
 */
function pixigo_index_header() {
	if ( is_home() && ! is_front_page() ) {
		?>
		<header>
			<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
		</header>
		<?php
	} elseif ( is_search() ) {
		?>
		<header class="page-header">
			<h1 class="page-title">
			<?php
				/* translators: %s: search query. */
				printf( esc_html__( 'Search Results for: %s', 'pixigo' ), '<span>' . get_search_query() . '</span>' );
			?>
			</h1>
		</header><!-- .page-header -->
		<?php
	} elseif ( is_archive() ) {
		?>
		<header class="page-header">
			<?php
				the_archive_title( '<h1 class="page-title">', '</h1>' );
				the_archive_description( '<div class="archive-description">', '</div>' );
			?>
		</header><!-- .page-header -->
		<?php
	}
}

/**
 * Prints HTML with meta information for the current post-date/time.
 */
function pixigo_posted_on() {

	$date_type = esc_html( get_theme_mod( 'pixigo_post_date_type', 'published' ) );
	$is_post_modified = get_the_time( 'U' ) !== get_the_modified_time( 'U' );

	if ( $date_type == 'published' ) {

		$time_string = '<time itemprop="datePublished" class="entry-date published" content="%1$s">%2$s</time>';

		if ( $is_post_modified ) {
			$time_string .= '<meta itemprop="dateModified" content="%3$s">';
		}
	} else {

		$time_string  = '<meta itemprop="datePublished" content="%1$s">';
		$time_string .= '<time itemprop="dateModified" class="entry-date updated" content="%3$s">%4$s</time>';

	}

	$time_string = sprintf(
		$time_string,
		esc_attr( get_the_date( DATE_W3C ) ),
		esc_html( get_the_date( 'jS M \'y' ) ),
		esc_attr( get_the_modified_date( DATE_W3C ) ),
		esc_html( get_theme_mod( 'pixigo_last_modified_text', '' ) ) . ' ' . esc_html( get_the_modified_date( 'jS M \'y' ) )
	);

	echo '<span class="posted-on">' . $time_string . ' </span>'; // WPCS: XSS ok.

}

/**
 * Prints HTML with meta information for the current author.
 * Todo: remove if not to be used.
 */
function pixigo_posted_by() {
	$author_name   = get_the_author();
	$author_letter = $author_name[0];
	?>
	<span class="author-name byline">
		<a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>'">
			<span class="author-char">
				<?php echo $author_letter; // WPCS: XSS OK. ?>
			</span>
			<?php echo $author_name; // WPCS: XSS OK. ?>
		</a>
	</span>
	<?php
}

/**
 * Prints a link list of the current categories for the post.
 *
 * If additional post types should display categories, add them to the conditional statement at the top.
 */
function pixigo_post_categories() {
	// Only show categories on post types that have categories.
	if ( 'post' === get_post_type() ) {

		$categories = get_the_category();
		if ( ! $categories || is_wp_error( $categories ) ) {
			return;
		}
		?>

		<div class="cat-link">
		<?php foreach ( $categories as $category ) : ?>
			<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
				<span><?php echo esc_html( $category->name ); ?></span>
			</a>
		<?php endforeach; ?>
		</div>

		<?php

	}
}

/**
 * Prints a link list of the current tags for the post.
 *
 * If additional post types should display tags, add them to the conditional statement at the top.
 */
function pixigo_post_tags() {
	// Only show tags on post types that have categories.
	if ( 'post' === get_post_type() ) {
		/* translators: used between list items, there is a space after the comma */
		$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'pixigo' ) );
		if ( $tags_list ) {
			/* translators: 1: list of tags. */
			printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', 'pixigo' ) . ' </span>', $tags_list ); // WPCS: XSS OK.
		}
	}
}

/**
 * Outputs the comment count for use in post meta
 *
 * Checks whether disqus comment system is installed
 * and creates an appropriate hyperlink to the comment section
 */
function pixigo_comment_count() {

	if ( get_comments_number() <= 0 ) {
		return;
	}

	?>

	<span class="comment-count">
		<?php if ( pixigo_is_disqus_active() ) : ?>
			<a href="<?php echo the_permalink(); ?>#disqus_thread" itemprop="interactionCount">Comments</a>
		<?php else : ?>
			<a href="<?php comments_link(); ?>" itemprop="interactionCount">
				<?php echo comments_number( 'Add a comment', '1 Comment', '% Comments' ); ?>
			</a>
		<?php endif; ?>
	</span>

	<?php
}

/**
 * Prints comments link when comments are enabled.
 */
function pixigo_comments_link() {
	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
		comments_popup_link(
			sprintf(
				wp_kses(
					/* translators: %s: post title */
					__( 'Leave a Comment<span class="screen-reader-text"> on %s</span>', 'pixigo' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				get_the_title()
			)
		);
		echo ' </span>';
	}
}

/**
 * Prints edit post/page link when a user with sufficient priveleges is logged in.
 */
function pixigo_edit_post_link() {
	edit_post_link(
		sprintf(
			wp_kses(
				/* translators: %s: Name of current post. Only visible to screen readers */
				__( 'Edit <span class="screen-reader-text">%s</span>', 'pixigo' ),
				array(
					'span' => array(
						'class' => array(),
					),
				)
			),
			get_the_title()
		),
		'<span class="edit-link">',
		' </span>'
	);
}

/**
 * Displays an optional post thumbnail.
 *
 * Wraps the post thumbnail in an anchor element on index views, or a div
 * element when on single views.
 */
function pixigo_post_thumbnail() {
	if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
		return;
	}

	global $post;

	if ( is_singular() ) :

		if ( is_single() ) {

			// For all single posts; from customizer.
			if ( get_theme_mod( 'post_hide_featured_image', 0 ) ) {
				return;
			}

			// Per individual post.
			$individual_val = get_post_meta( $post->ID, '_pixigo_indi_post_options', true );
			if ( isset( $individual_val['featured_image'] ) && $individual_val['featured_image'] ) {
				return;
			}
		} elseif ( is_page() ) {

			if ( get_theme_mod( 'page_hide_featured_image', 1 ) ) {
				return;
			}
		} // End is_single().
		?>

		<div class="post-thumbnail">
			<?php
			the_post_thumbnail(
				'full',
				array(
					'class'    => 'skip-lazy',
					'itemprop' => 'image',
				)
			);
			?>
		</div><!-- .post-thumbnail -->

		<?php
	else :

		if ( get_theme_mod( 'archive_hide_featured_image', 0 ) ) {
			return;
		}

		?>

		<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
			<?php
			global $wp_query;
			if ( 0 === $wp_query->current_post ) {
				the_post_thumbnail(
					'full',
					array(
						'class'    => 'skip-lazy',
						'itemprop' => 'image',
						'alt'      => the_title_attribute(
							array(
								'echo' => false,
							)
						),
					)
				);
			} else {
				the_post_thumbnail(
					'post-thumbnail', array(
						'itemprop' => 'image',
						'alt'      => the_title_attribute(
							array(
								'echo' => false,
							)
						),
					)
				);
			}
			?>
		</a>

		<?php
	endif; // End is_singular().
}

/**
 * Prints HTML with title and link to original post where attachment was added.
 *
 * @param object $post object.
 */
function pixigo_attachment_in( $post ) {
	if ( ! empty( $post->post_parent ) ) :
		$postlink = sprintf(
			/* translators: %s: original post where attachment was added. */
			esc_html_x( 'in %s', 'original post', 'pixigo' ),
			'<a href="' . esc_url( get_permalink( $post->post_parent ) ) . '">' . esc_html( get_the_title( $post->post_parent ) ) . '</a>'
		);

		echo '<span class="attachment-in"> ' . $postlink . ' </span>'; // WPCS: XSS OK.

	endif;

}

/**
 * Prints HTML with for navigation to previous and next attachment if available.
 */
function pixigo_the_attachment_navigation() {
	?>
	<nav class="navigation post-navigation" role="navigation">
		<h2 class="screen-reader-text"><?php echo esc_html__( 'Post navigation', 'pixigo' ); ?></h2>
		<div class="nav-links">
			<div class="nav-previous">
				<div class="post-navigation-sub">
					<?php echo esc_html__( 'Previous attachment:', 'pixigo' ); ?>
				</div>
				<?php previous_image_link( false ); ?>
			</div><!-- .nav-previous -->
			<div class="nav-next">
				<div class="post-navigation-sub">
					<?php echo esc_html__( 'Next attachment:', 'pixigo' ); ?>
				</div>
				<?php next_image_link( false ); ?>
			</div><!-- .nav-next -->
		</div><!-- .nav-links -->
	</nav><!-- .navigation .attachment-navigation -->
	<?php
}
