<?php
/**
 * Template part for displaying posts
 *
 * @package BlogBull
 */
/**
 * Update post meta and check if it was updated.
 *
 * @param int $post_id The post ID.
 * @param string $meta_key The meta key.
 * @param mixed $meta_value The meta value.
 * @return bool  True if updated, false otherwise.
 */
function blogbull_update_post_meta_and_check($post_id, $meta_key, $meta_value)
{
    $result = update_post_meta($post_id, $meta_key, $meta_value);
    return $result === true || is_numeric($result); // Meta was updated or added.
}
// Get options and post meta
$options = [
    'enable_single_author_meta' => blogbull_get_option('enable_single_author_meta'),
    'select_single_author_meta' => blogbull_get_option('select_single_author_meta'),
    'single_author_meta_title' => blogbull_get_option('single_author_meta_title'),
    'enable_single_date_meta' => blogbull_get_option('enable_single_date_meta'),
    'select_single_date' => blogbull_get_option('select_single_date'),
    'single_date_meta_title' => blogbull_get_option('single_date_meta_title'),
    'select_single_date_format' => blogbull_get_option('select_single_date_format'),
    'enable_single_category_meta' => blogbull_get_option('enable_single_category_meta'),
    'single_category_label' => blogbull_get_option('single_category_label'),
    'select_single_category_color' => blogbull_get_option('select_single_category_color'),
    'enable_single_read_time_meta' => blogbull_get_option('enable_single_read_time_meta'),
    'enable_single_tag_meta' => blogbull_get_option('enable_single_tag_meta'),
    'show_sticky_article_navigation' => blogbull_get_option('show_sticky_article_navigation'),
    'show_related_posts' => blogbull_get_option('show_related_posts'),
    'show_author_posts' => blogbull_get_option('show_author_posts'),
    'show_author_info' => blogbull_get_option('show_author_info'),
];
$meta = [
    'navigation' => get_post_meta($post->ID, 'blogbull_single_post_navigation', true),
    'category_meta' => get_post_meta($post->ID, 'blogbull_single_category_meta', true),
    'date_meta' => get_post_meta($post->ID, 'blogbull_single_date_meta', true),
    'author_meta' => get_post_meta($post->ID, 'blogbull_single_author_meta', true),
    'related_post' => get_post_meta($post->ID, 'blogbull_single_related_post', true),
    'author_post' => get_post_meta($post->ID, 'blogbull_single_author_post', true),
    'post_author' => get_post_meta($post->ID, 'blogbull_single_post_author', true),
];
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-default'); ?>>
    <?php blogbull_get_breadcrumb(); ?>
    <?php blogbull_post_thumbnail(); ?>
    <div class="entry-details">
        <header class="entry-header">
            <div class="entry-meta-wrapper">
                <?php
                if ($options['enable_single_read_time_meta']) {
                    blogbull_get_readtime();
                    echo '<div class="entry-meta-separator"></div>';
                }
                
                if ($meta['category_meta'] === '1' || ($meta['category_meta'] === '' && $options['enable_single_category_meta'])) {
                    blogbull_post_category($options['select_single_category_color'], $options['single_category_label']);
                }
                
                ?>
            </div>
            <?php the_title('<h1 class="entry-title entry-title-large entry-title-prime">', '</h1>'); ?>
            <?php if ('post' === get_post_type()) : ?>
                <div class="entry-meta-wrapper">
                    <?php
                    if ($meta['date_meta'] === '1' || ($meta['date_meta'] === '' && $options['enable_single_date_meta'])) {
                        blogbull_posted_on($options['select_single_date_format'], $options['single_date_meta_title'], $options['select_single_date']);
                    }
                    if ($meta['author_meta'] === '1' || ($meta['author_meta'] === '' && $options['enable_single_author_meta'])) {
                        echo '<div class="entry-meta-separator"></div>';
                        blogbull_posted_by($options['select_single_author_meta'], $options['single_author_meta_title']);
                    }
                    ?>
                </div>
            <?php endif; ?>
        </header>
        <div class="entry-content">
            <?php
            the_content(sprintf(
                wp_kses(
                    __('Continue reading<span class="screen-reader-text"> "%s"</span>', 'blogbull'),
                    ['span' => ['class' => []]]
                ),
                wp_kses_post(get_the_title())
            ));
            wp_link_pages([
                'before' => '<div class="page-links">' . esc_html__('Pages:', 'blogbull'),
                'after' => '</div>',
            ]);
            ?>
        </div>
        <footer class="entry-footer">
            <?php blogbull_entry_footer($options['enable_single_tag_meta']); ?>
        </footer>
    </div>
</article>
<?php if ('post' === get_post_type()) : ?>
    <?php
    // Author info
    if ($meta['post_author'] === '1' || ($meta['post_author'] === '' && $options['show_author_info'])) {
        get_template_part('template-parts/single/single-author-info');
    }
    // Related posts
    if (!$meta['related_post'] && $options['show_related_posts'] || $meta['related_post'] === '1') {
        get_template_part('template-parts/single/single-related-posts');
    }
    // Author posts
    if (!$meta['author_post'] && $options['show_author_posts'] || $meta['author_post'] === '1') {
        get_template_part('template-parts/single/single-author-posts');
    }
    ?>
<?php endif; ?>
<?php
// Sticky article navigation
if ($meta['navigation'] === '1' || ($meta['navigation'] === '' && $options['show_sticky_article_navigation'])) {
    get_template_part('template-parts/single/sticky-article-nav');
}
?>