<?php
/**
 * Eisai theme template tags.
 */

if ( ! function_exists( 'eisai_body_open' ) ) {
	/**
	 * Shim for wp_body_open, ensuring backwards compatibility with versions of WordPress older than 5.2.
	 */
	function eisai_body_open() {
		/**
		 * Functions hooked into wp_body_open action
		 *
		 * @hooked eisai_skip_link                    - 0
		 */
		do_action( 'wp_body_open' );
	}
}

if ( ! function_exists( 'eisai_unique_id' ) ) {
    /**
     * Get unique ID.
     *
     * This is a PHP implementation of Underscore's uniqueId method. A static variable
     * contains an integer that is incremented with each call. This number is returned
     * with the optional prefix. As such the returned value is not universally unique,
     * but it is unique across the life of the PHP process.
     *
     * @see wp_unique_id() Themes requiring WordPress 5.0.3 and greater should use this instead.
     *
     * @staticvar int $id_counter
     *
     * @param string $prefix Prefix for the returned ID.
     * @return string Unique ID.
     */
    function eisai_unique_id( $prefix = '' ) {
    	static $id_counter = 0;
    	if ( function_exists( 'wp_unique_id' ) ) {
    		return wp_unique_id( $prefix );
    	}
    	return $prefix . (string) ++$id_counter;
    }
}

if ( ! function_exists( 'eisai_archive_categories' ) ) {
    /**
	 * Output categories navigation.
	 *
     * @param string The archive title.
     */
    function eisai_archive_categories( $archive_title ) {

        if ( ! taxonomy_exists( 'category' ) ) {
            return;
        }

		if ( ! is_home() ) {
			echo '<h1 class="screen-reader-text">' . $archive_title . '</h1>';
		}

        ?>
        <ul class="archive-categories uk-list uk-margin-remove uk-padding-remove uk-clearfix">
            <?php
			$blog_page_id 	= get_option( 'page_for_posts', true );
			$blog_link		= ( $blog_page_id ) ? get_page_link( $blog_page_id ) : get_home_url();
            $blog_title 	= ( $blog_page_id ) ? get_the_title( $blog_page_id ) : __( 'All posts', 'eisai' );

            // Output first list item with a blog page title.
            if ( is_home() ) {
                global $wp_query;

                $post_count = $wp_query->found_posts;

                $cat_item = '<li class="cat-item current-cat">';
                $cat_item .= '<span class="uk-h1 uk-margin-remove">';
                $cat_item .= $blog_title . '<span class="post-count">' . $post_count . '</span>';
                $cat_item .= '</span>';
                $cat_item .= '</li>';

                echo wp_kses_post( $cat_item );
            } else {
                $cat_item = '<li class="cat-item">';
                $cat_item .= '<a class="uk-link-text uk-h1 uk-margin-remove" href="' . esc_url( $blog_link ) . '">';
                $cat_item .= $blog_title;
                $cat_item .= '</a>';
                $cat_item .= '</li>';

                echo wp_kses_post( $cat_item );
            }

            /*
             * Allow child themes and plugins to filter the arguments of the get_categories function.
             *
             * @param array   Arguments, passed to get_categories and then to get_terms functions, Default array().
             */
            $args = apply_filters( 'eisai_archive_categories_args', array() );

            $categories = get_categories( $args );

            // Output remaining list items with categories.
            if ( ! empty( $categories ) ) {
                foreach ( $categories as $category ) {
                    $id         = $category->term_id;
                    $name       = $category->name;
                    $post_count = $category->category_count;

                    // For current category item add a .current-cat class and output h1 tag instead of a link.
                    if ( $id === get_queried_object_id() ) {
                        $cat_item  = '<li class="cat-item current-cat">';
                        $cat_item .= '<span class="uk-h1 uk-margin-remove">';
                        $cat_item .= $name . '<span class="post-count">' . $post_count . '</span>';
                        $cat_item .= '</span>';
                        $cat_item .= '</li>';

                        echo wp_kses_post( $cat_item );
                    } else {
                        $cat_item  = '<li class="cat-item">';
                        $cat_item .= '<a class="uk-link-text uk-h1 uk-margin-remove" href="' . esc_url( get_term_link( $id ) ) . '">';
                        $cat_item .= $name;
                        $cat_item .= '</a>';
                        $cat_item .= '</li>';

                        echo wp_kses_post( $cat_item );
                    }
                }
            }
            ?>
        </ul> <!-- .archive-categories -->
        <?php
    }
}

if ( ! function_exists( 'eisai_the_post_meta' ) ) {
    /**
     * Get and Output Post Meta.
     * If it's a single post, output the post meta values specified in the Customizer settings.
     * Found this approach in TwentyTwenty WordPress theme.
     */
    function eisai_the_post_meta( $post_id = null, $location = 'entry-header' ) {

        echo eisai_get_post_meta( $post_id, $location ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in eisai_get_post_meta().

    }
}

if ( ! function_exists( 'eisai_get_post_meta' ) ) {
    /**
     * Get and Output Post Meta.
     * If it's a single post, output the post meta values specified in the Customizer settings.
     */
    function eisai_get_post_meta( $post_id = null, $location = 'entry-header' ) {

        // Require post ID.
    	if ( ! $post_id ) {
    		return;
    	}

        /**
    	 * Filters post types array
    	 *
    	 * This filter can be used to hide post meta information of post, page or custom post type registerd by child themes or plugins.
    	 *
    	 * @param array Array of post types
    	 */
    	$disallowed_post_types = apply_filters( 'eisai_disallowed_post_types_for_meta_output', array( 'page' ) );
    	// Check whether the post type is allowed to output post meta.
    	if ( in_array( get_post_type( $post_id ), $disallowed_post_types, true ) ) {
    		return;
    	}

        $post_meta_wrapper_classes = '';
    	$post_meta_classes         = '';

    	// Get the post meta settings for the location specified.
    	if ( 'entry-header' === $location ) {
    		/**
    		* Filters post meta info visibility
    		*
    		* Use this filter to hide post meta information like Author, Post date, Comments, Is sticky status
    		*
    		* @param array $args {
    		*  @type string 'author'
    		*  @type string 'post-date'
    		*  @type string 'comments'
    		*  @type string 'sticky'
    		* }
    		*/
    		$post_meta = apply_filters(
    			'eisai_post_meta_location_entry_header',
    			array(
    				'author',
    				'post-date',
    				'comments',
    				'sticky',
    			)
    		);
    		$post_meta_wrapper_classes = ' post-meta-single post-meta-entry-header';

    	} elseif ( 'entry-footer' === $location ) {

    		/**
    		* Filters post tags visibility
    		*
    		* Use this filter to hide post tags
    		*
    		* @param array $args {
    		*   @type string 'tags'
    		* }
    		*/
    		$post_meta = apply_filters(
    			'eisai_post_meta_location_entry_footer',
    			array(
    				'tags',
    			)
    		);
    		$post_meta_wrapper_classes = ' post-meta-single post-meta-entry-footer';

    	} elseif ( 'archive' === $location ) {

			/**
    		* Filters post categories visibility
    		*
    		* Use this filter to hide post categories
    		*
    		* @param array $args {
    		*   @type string 'categories'
    		* }
    		*/
    		$post_meta = apply_filters(
    			'eisai_post_meta_location_archive',
    			array(
    				'post-date',
    				'comments',
    			)
    		);
    		$post_meta_wrapper_classes = ' post-meta-archive';

		}

        // If the post meta setting has the value 'empty', it's explicitly empty and the default post meta shouldn't be output.
        if ( $post_meta && ! in_array( 'empty', $post_meta, true ) ) {

            // Make sure we don't output an empty container.
            $has_meta = false;

            global $post;
    		$the_post = get_post( $post_id );
    		setup_postdata( $the_post );

    		ob_start();

    		?>

            <div class="post-meta-wrapper<?php echo esc_attr( $post_meta_wrapper_classes ); ?>">
                <ul class="post-meta uk-clearfix<?php echo esc_attr( $post_meta_classes ); ?>">

                    <?php

                    /**
    				 * Fires after post meta html display.
    				 *
    				 * Allow output of additional post meta info to be added by child themes and plugins.
    				 *
    				 * @param int   $post_ID Post ID.
    				 */
    				do_action( 'eisai_before_post_meta_list', $post_id );

                    // Author.
    				if ( in_array( 'author', $post_meta, true ) ) {

    					$has_meta = true;
    					?>
    					<li class="post-author meta-wrapper">
    						<span class="meta-icon">
    							<span class="screen-reader-text"><?php esc_html_e( 'Post Author', 'eisai' ); ?></span>
                                <span data-uk-icon="icon: eisai-user; ratio: .6"></span>
    						</span>
    						<span class="meta-text">
    							<?php
    							printf(
    								/* translators: %s: Author name */
    								esc_html__( 'by %s', 'eisai' ),
    								'<a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author_meta( 'display_name' ) ) . '</a>'
    							);
    							?>
    						</span>
    					</li>
    					<?php

    				}

    				// Post date.
    				if ( in_array( 'post-date', $post_meta, true ) ) {

    					$has_meta = true;
    					?>
    					<li class="post-date meta-wrapper">
    						<span class="meta-icon">
    							<span class="screen-reader-text"><?php esc_html_e( 'Post date', 'eisai' ); ?></span>
                                <span data-uk-icon="icon: eisai-calendar; ratio: .55"></span>
    						</span>
    						<span class="meta-text">
    							<a href="<?php the_permalink(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>
    						</span>
    					</li>
    					<?php

    				}

    				// Categories.
    				if ( in_array( 'categories', $post_meta, true ) && has_category() ) {

    					$has_meta = true;
    					?>
    					<li class="post-categories meta-wrapper">
    						<span class="meta-icon">
    							<span class="screen-reader-text"><?php esc_html_e( 'Categories', 'eisai' ); ?></span>
                                <span data-uk-icon="icon: eisai-tag; ratio: .55"></span>
    						</span>
    						<span class="meta-text">
    							<?php the_category( ', ' ); ?>
    						</span>
    					</li>
    					<?php

    				}

    				// Comments link.
    				if ( in_array( 'comments', $post_meta, true ) && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {

    					$has_meta = true;
    					?>
    					<li class="post-comment-link meta-wrapper">
    						<span class="meta-icon">
                                <span data-uk-icon="icon: eisai-message-square; ratio: .55"></span>
    						</span>
    						<span class="meta-text">
    							<?php comments_popup_link(); ?>
    						</span>
    					</li>
    					<?php

    				}

    				// Sticky.
    				if ( in_array( 'sticky', $post_meta, true ) && is_sticky() ) {

    					$has_meta = true;
    					?>
    					<li class="post-sticky meta-wrapper">
    						<span class="meta-icon">
                                <span data-uk-icon="icon: eisai-award; ratio: .55"></span>
    						</span>
    						<span class="meta-text">
    							<?php esc_html_e( 'Featured', 'eisai' ); ?>
    						</span>
    					</li>
    					<?php

    				}

    				// Tags.
    				if ( in_array( 'tags', $post_meta, true ) && has_tag() ) {

    					$has_meta = true;
    					?>
    					<li class="post-tags meta-wrapper">
    						<span class="meta-icon">
    							<span class="screen-reader-text"><?php esc_html_e( 'Tags', 'eisai' ); ?></span>
                                <span data-uk-icon="icon: eisai-hash; ratio: .55"></span>
    						</span>
    						<span class="meta-text">
    							<?php the_tags( '', ' ', '' ); ?>
    						</span>
    					</li>
    					<?php

    				}

                    /**
    				 * Fires after post meta html display.
    				 *
    				 * Allow output of additional post meta info to be added by child themes and plugins.
    				 *
    				 * @param int   $post_ID Post ID.
    				 */
    				do_action( 'eisai_after_post_meta_list', $post_id );

                    ?>

                </ul>
            </div>

            <?php

            wp_reset_postdata();

    		$meta_output = ob_get_clean();

    		// If there is meta to output, return it.
    		if ( $has_meta && $meta_output ) {

    			return $meta_output;

    		}

        }

    }
}

if ( ! function_exists( 'eisai_is_comment_by_post_author' ) ) {
    /**
     * Check if the specified comment is written by the author of the post commented on.
     *
     * @param object $comment Comment data.
     *
     * @return bool
     */
    function eisai_is_comment_by_post_author( $comment = null ) {

    	if ( is_object( $comment ) && $comment->user_id > 0 ) {

    		$user = get_userdata( $comment->user_id );
    		$post = get_post( $comment->comment_post_ID );

    		if ( ! empty( $user ) && ! empty( $post ) ) {

    			return $comment->user_id === $post->post_author;

    		}
    	}
    	return false;

    }
}
