<?php
/**
 * Entry meta header
 *
 * @package Fell
 * @since   Fell 2.0
 */

/**
 * Entry meta header
 */
function fell_entry_meta_header() {
  $parent_id   = wp_get_post_parent_id( get_the_ID() );
  $post_type   = get_post_type();
  $post_format = get_post_format();

  echo '<div class="entry-meta entry-meta-header">';

  if ( ! is_single() && ( 'aside' === $post_format || 'status' === $post_format ) ) {
    $author = get_the_author();
    $date   = get_the_date();

    printf(
      /* translators: %1$s: Author, %2$s: Publish date */
      '<span class="entry-meta__author-and-date">' . esc_html__( '%1$s @ %2$s', 'fell' ) . '</span>',
      sprintf(
        '<a href="%1$s" aria-label="%2$s">%3$s</a>',
        esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
        /* translators: %s post author */
        sprintf( esc_attr__( 'Author: %s', 'fell' ), $author ), // phpcs:ignore WordPress.Security.EscapeOutput
        $author // phpcs:ignore WordPress.Security.EscapeOutput
      ),
      sprintf(
        '<a href="%1$s" aria-label="%2$s">%3$s</a>',
        esc_url( get_permalink() ),
        /* translators: %s post date */
        sprintf( esc_attr__( 'Posted: %s', 'fell' ), $date ), // phpcs:ignore WordPress.Security.EscapeOutput
        $date // phpcs:ignore WordPress.Security.EscapeOutput
      )
    );

  } else {

    /* Posted date */
    $date = get_the_date();
    printf(
      '<span class="entry-meta__date"><a href="%1$s" aria-label="%3$s">%2$s</a></span>',
      esc_url( get_permalink() ),
      $date, // phpcs:ignore WordPress.Security.EscapeOutput
      sprintf(
        /* translators: %s post date */
        esc_attr__( 'Posted: %s', 'fell' ),
        $date // phpcs:ignore WordPress.Security.EscapeOutput
      )
    );

    /* Comment count */
    $comment_count = get_comments_number();
    if ( $comment_count && comments_open() ) {
      printf(
        '<span class="entry-meta__comments"><a href="%2$s#comments">%1$s</a></span>',
        sprintf(
          /* translators: %s comment count */
          esc_attr( _n( '%s comment', '%s comments', $comment_count, 'fell' ) ),
          esc_attr( number_format_i18n( $comment_count ) )
        ),
        esc_url( get_permalink() ),
      );
    }

    /* Post author */
    $author = get_the_author();
    printf(
      '<span class="entry-meta__author"><a href="%1$s" aria-label="%3$s">%2$s</a></span>',
      esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
      sprintf(
        /* translators: %s post author */
        esc_html__( 'By %s', 'fell' ),
        $author // phpcs:ignore WordPress.Security.EscapeOutput
      ),
      sprintf(
        /* translators: %s post author */
        esc_attr__( 'Author: %s', 'fell' ),
        $author // phpcs:ignore WordPress.Security.EscapeOutput
      )
    );

    /* Attachment resolution */
    if ( wp_attachment_is_image() ) {
      $metadata = wp_get_attachment_metadata();

      printf(
        '<span class="entry-meta__image-size"><a href="%1$s"><span class="screen-reader-text">%2$s</span>%3$sx%4$s</a></span>',
        esc_url( wp_get_attachment_url() ),
        esc_html__( 'Attachment resolution', 'fell' ),
        absint( $metadata['width'] ),
        absint( $metadata['height'] )
      );
    }
  }

  echo '</div><!-- .entry-meta-header -->';

}
