<?php
if (!function_exists('united_blog_suggested_blog')):
    /**
     * Suggested Blog
     *
     * @since United Blog 1.0.0
     *
     */
    function united_blog_suggested_blog()
    {
        if (1 != united_blog_get_option('enable_suggested_blog')) {
            return null;
        }
        $show_suggested_blog_category = united_blog_get_option('show_suggested_blog_category', true);
        $suggested_blog_category_color_display = united_blog_get_option('suggested_blog_category_color_display', 'display_as_bg');
        $suggested_blog_category_limit = united_blog_get_option('suggested_blog_category_limit', 1);
        $suggested_blog_post_meta = united_blog_get_option('suggested_blog_post_meta');
        $suggested_blog_post_meta_settings = array(
            'date_format' => united_blog_get_option('suggested_blog_posts_date_format', 'format_2'),
            'show_icons' => united_blog_get_option('show_suggested_blog_post_meta_icon', true),
        );
        $united_blog_suggested_blog_args = array(
            'post_type' => 'post',
            'posts_per_page' => 6,
            'cat' => absint(united_blog_get_option('select_category_for_suggested_blog')),
        ); ?>
        <section class="united-block united-suggested-block">
            <header class="section-header">
                <h2 class="section-title">
                    <span class="section-title-line title-line-start"></span>
                    <span class="section-title-text"><?php echo esc_html(united_blog_get_option('suggested_blog_title')); ?></span>
                    <span class="section-title-line title-line-end"></span>
                </h2>
            </header>

            <div class="wrapper">
                <div class="section-content">
                    <?php
                    $united_blog_suggested_blog_query = new WP_Query($united_blog_suggested_blog_args);
                    if ($united_blog_suggested_blog_query->have_posts()) :
                        while ($united_blog_suggested_blog_query->have_posts()) : $united_blog_suggested_blog_query->the_post();
                            global $post;
                            ?>
                            <article id="recommendation-post-<?php the_ID(); ?>" <?php post_class('united-article united-article-default  has-format-icon  '); ?>>

                                <?php if (has_post_thumbnail()) : ?>
                                    <div class="entry-image entry-image-medium">
                                        <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true"
                                           tabindex="-1">
                                            <?php
                                            the_post_thumbnail(
                                                'medium_large',
                                                array(
                                                    'alt' => the_title_attribute(
                                                        array(
                                                            'echo' => false,
                                                        )
                                                    ),
                                                )
                                            );
                                            ?>
                                        </a>
                                    </div>
                                <?php endif; ?>

                                <div class="entry-details">
                                    <header class="entry-header">
                                        <?php
                                        if ($show_suggested_blog_category) :
                                            united_blog_post_categories($suggested_blog_category_color_display, $suggested_blog_category_limit);
                                        endif;                                        
                                        
                                        ?>

                                        <h2 class="entry-title entry-title-medium">
                                            <a href="<?php the_permalink(); ?>"
                                               rel="bookmark"><?php the_title(); ?></a>
                                        </h2>

                                        <?php united_blog_post_meta_info($suggested_blog_post_meta, $suggested_blog_post_meta_settings); ?>
                                    </header>
                                </div>
                            </article>
                        <?php
                        endwhile;
                        wp_reset_postdata();
                    endif;
                    ?>
                </div>
            </div>
        </section>
        <?php
    }
endif;
add_action('united_blog_action_featured_page', 'united_blog_suggested_blog', 20);
/**
 * Metabox.
 *
 * @package United Blog
 */
if (!function_exists('united_blog_add_meta_box')) :
    /**
     * Add the Meta Box
     *
     * @since 1.0.0
     */
    function united_blog_add_meta_box()
    {
        $meta_box_on = array('post', 'page');
        foreach ($meta_box_on as $meta_box_as) {
            add_meta_box(
                'united-blog-theme-settings',
                esc_html__('Layout Options', 'united-blog'),
                'united_blog_render_layout_option_metabox',
                $meta_box_as,
                'side',
                'low'
            );
        }
    }
endif;
add_action('add_meta_boxes', 'united_blog_add_meta_box');
if (!function_exists('united_blog_render_layout_option_metabox')) :
    /**
     * Render theme settings meta box.
     *
     * @since 1.0.0
     */
    function united_blog_render_layout_option_metabox($post, $metabox)
    {
        $post_id = $post->ID;
        $united_blog_post_meta_value = get_post_meta($post_id);
        // Meta box nonce for verification.
        wp_nonce_field(basename(__FILE__), 'united_blog_meta_box_nonce');
        ?>
        <div id="pb_metabox-container" class="pb-metabox-container">
            <div id="pb-metabox-layout">
                <div class="row-content">
                    <p>
                    <div class="pb-row-content">
                        <label for="united-blog-meta-checkbox">
                            <input type="checkbox" name="united-blog-meta-checkbox" id="united-blog-meta-checkbox"
                                   value="yes" <?php if (isset ($united_blog_post_meta_value['united-blog-meta-checkbox'])) checked($united_blog_post_meta_value['united-blog-meta-checkbox'][0], 'yes'); ?> />
                            <?php echo esc_html__('Disable Featured Image on single page', 'united-blog') ?>
                        </label>
                    </div>
                    </p>
                </div>
            </div>
        </div>
        <?php
    }
endif;
if (!function_exists('united_blog_save_settings_meta')) :
    /**
     * Save meta box value.
     *
     * @param int $post_id Post ID.
     * @param WP_Post $post Post object.
     * @since 1.0.0
     *
     */
    function united_blog_save_settings_meta($post_id, $post)
    {
        // Verify nonce.
        if (!isset($_POST['united_blog_meta_box_nonce']) || !wp_verify_nonce($_POST['united_blog_meta_box_nonce'], basename(__FILE__))) {
            return;
        }
        // Bail if auto save or revision.
        if (defined('DOING_AUTOSAVE') || is_int(wp_is_post_revision($post)) || is_int(wp_is_post_autosave($post))) {
            return;
        }
        // Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
        if (empty($_POST['post_ID']) || $_POST['post_ID'] != $post_id) {
            return;
        }
        // Check permission.
        if ('page' === $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id)) {
                return;
            }
        } else if (!current_user_can('edit_post', $post_id)) {
            return;
        }
        $united_blog_meta_checkbox = isset($_POST['united-blog-meta-checkbox']) ? esc_attr($_POST['united-blog-meta-checkbox']) : '';
        update_post_meta($post_id, 'united-blog-meta-checkbox', sanitize_text_field($united_blog_meta_checkbox));
    }
endif;
add_action('save_post', 'united_blog_save_settings_meta', 10, 2);


if (!function_exists('united_blog_single_related_post')):
    /**
     * Related Post
     *
     * @since United Blog 1.0.0
     *
     */
    function united_blog_single_related_post()
    {
        $show_single_category = united_blog_get_option('show_single_category', true);
        $single_category_color_display = united_blog_get_option('single_category_color_display', 'display_as_bg');
        $single_category_limit = united_blog_get_option('single_category_limit', 1);
        $single_post_meta = united_blog_get_option('single_post_meta');
        $single_post_meta_settings = array(
            'date_format' => united_blog_get_option('single_posts_date_format', 'format_2'),
            'show_icons' => united_blog_get_option('show_single_post_meta_icon', true),
        );
        if (1 == united_blog_get_option('related_post')) { ?>
            <!-- related post -->
            <section class="united-block united-related-block">
                <?php if (!empty(united_blog_get_option('related_post_title'))) { ?>
                    <header class="section-header">
                        <h2 class="section-title">
                            <span class="section-title-line title-line-start"></span>
                            <span class="section-title-text"> <?php echo esc_html(united_blog_get_option('related_post_title')); ?></span>
                            <span class="section-title-line title-line-end"></span>
                        </h2>
                    </header>
                <?php } ?>
                <div class="wrapper">
                    <div class="section-content">
                        <?php global $post;
                        $post_id = $post->ID;
                        $category_ids = array();
                        $categories = get_the_category(get_the_ID());
                        if (!empty($categories)) {
                            foreach ($categories as $category) {
                                $category_ids[] = $category->term_id;
                            }
                        }
                        if (!empty($category_ids)) {
                            $args = array(
                                'posts_per_page' => 3,
                                'category__in' => $category_ids,
                                'post__not_in' => array(get_the_ID()),
                                'order' => 'ASC',
                                'orderby' => 'rand'
                            );
                            $related_posts = new WP_Query($args);
                            if ($related_posts->have_posts()):
                                while ($related_posts->have_posts()) : $related_posts->the_post(); ?>
                                    <article id="related-post-<?php the_ID(); ?>" <?php post_class('united-article united-article-default'); ?>>
                                        <?php if (has_post_thumbnail()) : ?>
                                            <div class="entry-image entry-image-medium">
                                                <a class="post-thumbnail" href="<?php the_permalink(); ?>"
                                                   aria-hidden="true"
                                                   tabindex="-1">
                                                    <?php
                                                    the_post_thumbnail(
                                                        'medium',
                                                        array(
                                                            'alt' => the_title_attribute(
                                                                array(
                                                                    'echo' => false,
                                                                )
                                                            ),
                                                        )
                                                    );
                                                    ?>
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                        <?php
                                        if ($show_single_category) :
                                            united_blog_post_categories($single_category_color_display, $single_category_limit);
                                        endif;
                                        ?>

                                        <div class="entry-details">
                                            <header class="entry-header">
                                                <?php
                                                the_title('<h3 class="entry-title entry-title-medium"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h3>');
                                                ?>
                                                <div class="entry-meta">
                                                    <?php
                                                    united_blog_post_meta_info($single_post_meta, $single_post_meta_settings);
                                                    ?>
                                                </div><!-- .entry-meta -->
                                            </header><!-- .entry-header -->
                                        </div>
                                    </article><!-- #post-<?php the_ID(); ?> -->
                                    <?php
                                    wp_reset_postdata();
                                endwhile; ?>
                            <?php endif;
                        }
                        ?>
                    </div>
                </div>
            </section> <!-- end of related post -->
            <?php
        }

    }
endif;