<?php
/**
 * Inner sections hooks and functions
 * 
 * @package One Newspaper
 * @since 1.0.0
 */
use OneNewspaper\CustomizerDefault as ONP;
if( ! function_exists( 'one_newspaper_single_related_posts' ) ) :
    /**
     * Single related posts
     * 
     * @package One Newspaper
     */
    function one_newspaper_single_related_posts() {
        if( get_post_type() != 'post' ) return;
        $single_post_related_posts_option = ONP\one_newspaper_get_customizer_option( 'single_post_related_posts_option' );
        if( ! $single_post_related_posts_option ) return;
        $related_posts_title = ONP\one_newspaper_get_customizer_option( 'single_post_related_posts_title' );

        $related_posts_args = array(
            'posts_per_page'   => 4,
            'post__not_in'  => array( get_the_ID() ),
            'ignore_sticky_posts'    => true
        );
        $current_post_categories = get_the_category(get_the_ID());
        if( $current_post_categories ) :
            foreach( $current_post_categories as $current_post_cat ) :
                $query_cats[] =  $current_post_cat->term_id;
            endforeach;
            $related_posts_args['category__in'] = $query_cats;
        endif;
        $related_posts = new WP_Query( $related_posts_args );
        if( ! $related_posts->have_posts() ) return;
            $relatedPostsClass = 'single-related-posts-section-wrap layout--list';
  ?>
            <div class="<?php echo esc_attr( $relatedPostsClass ); ?>">
                <div class="single-related-posts-section">
                    <a href="javascript:void(0);" class="related_post_close">
                        <i class="fas fa-times-circle"></i>
                    </a>
                    <?php
                        if( $related_posts_title ) echo '<h2 class="one-newspaper-block-title"><span>' .esc_html( $related_posts_title ). '</span></h2>';
                        echo '<div class="single-related-posts-wrap">';
                            while( $related_posts->have_posts() ) : $related_posts->the_post();
                                ?>
                                    <article post-id="post-<?php the_ID(); ?>" <?php post_class('one-newspaper-card'); ?>>
                                        <?php if( has_post_thumbnail() ) : ?>
                                            <figure class="post-thumb-wrap <?php if(!has_post_thumbnail()){ echo esc_attr('no-feat-img');} ?>">
                                                <?php one_newspaper_post_thumbnail(); ?>
                                            </figure>
                                        <?php endif; ?>
                                        <div class="post-element">
                                            <h2 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                            <div class="post-meta">
                                                <?php
                                                    one_newspaper_posted_on();
                                                ?>
                                            </div>
                                        </div>
                                    </article>
                                <?php
                            endwhile;
                        echo '</div>';
                    ?>
                </div>
            </div>
    <?php
    }
endif;
add_action( 'one_newspaper_single_post_append_hook', 'one_newspaper_single_related_posts' );

if( ! function_exists( 'one_newspaper_before_content_advertisement_part' ) ) :
    /**
     * One Newspaper before content advertisement part for single
     * 
     * @since 1.0.0
     */
    function one_newspaper_before_content_advertisement_part() {
        $advertisement_repeater = ONP\one_newspaper_get_customizer_option( 'advertisement_repeater' );
        $advertisement_repeater_decoded = json_decode( $advertisement_repeater );
        $before_content_advertisement = array_values(array_filter( $advertisement_repeater_decoded, function( $element ) {
            if( property_exists( $element, 'item_checkbox_before_post_content' ) ) return ( $element->item_checkbox_before_post_content == true && $element->item_option == 'show' ) ? $element : ''; 
        }));
        if( empty( $before_content_advertisement ) ) return;
        $image_option = array_column( $before_content_advertisement, 'item_image_option' );
        $alignment = array_column( $before_content_advertisement, 'item_alignment' );
        $elementClass = 'alignment--' . $alignment[0];
        $elementClass .= ' image-option--' . ( ( $image_option[0] == 'full_width' ) ? 'full-width' : 'original' );
        ?>
            <section class="one-newspaper-advertisement-section-before-content one-newspaper-advertisement <?php echo esc_html( $elementClass ); ?>">
                <div class="one-newspaper-container">
                    <div class="row">
                        <div class="advertisement-wrap">
                            <?php
                                if( ! empty( $advertisement_repeater_decoded ) ) :
                                    foreach( $before_content_advertisement as $field ) :
                                        ?>
                                        <div class="advertisement">
                                            <a href="<?php echo esc_url( $field->item_url ); ?>" target="<?php echo esc_attr( $field->item_target ); ?>" rel="<?php echo esc_attr( $field->item_rel_attribute ); ?>">
                                                <img src="<?php echo esc_url( wp_get_attachment_image_url( $field->item_image, 'full' ) ); ?>" loading="lazy">
                                            </a>
                                        </div>
                                        <?php
                                    endforeach;
                                endif;
                            ?>
                        </div>
                    </div>
                </div>
            </section>
        <?php
    }
    add_action( 'one_newspaper_before_single_content_hook', 'one_newspaper_before_content_advertisement_part' );
 endif;

 if( ! function_exists( 'one_newspaper_after_content_advertisement_part' ) ) :
    /**
     * One Newspaper after content advertisement part for single
     * 
     * @since 1.0.0
     */
    function one_newspaper_after_content_advertisement_part() {
        $advertisement_repeater = ONP\one_newspaper_get_customizer_option( 'advertisement_repeater' );
        $advertisement_repeater_decoded = json_decode( $advertisement_repeater );
        $after_content_advertisement = array_values(array_filter( $advertisement_repeater_decoded, function( $element ) {
            if( property_exists( $element, 'item_checkbox_after_post_content' ) ) return ( $element->item_checkbox_after_post_content == true && $element->item_option == 'show' ) ? $element : ''; 
        }));
        if( empty( $after_content_advertisement ) ) return;
        $image_option = array_column( $after_content_advertisement, 'item_image_option' );
        $alignment = array_column( $after_content_advertisement, 'item_alignment' );
        $elementClass = 'alignment--' . $alignment[0];
        $elementClass .= ' image-option--' . ( ( $image_option[0] == 'full_width' ) ? 'full-width' : 'original' );
        ?>
            <section class="one-newspaper-advertisement-section-after-content one-newspaper-advertisement <?php echo esc_html( $elementClass ); ?>">
                <div class="one-newspaper-container">
                    <div class="row">
                        <div class="advertisement-wrap">
                            <?php
                                if( ! empty( $advertisement_repeater_decoded ) ) :
                                    foreach( $after_content_advertisement as $field ) :
                                        ?>
                                        <div class="advertisement">
                                            <a href="<?php echo esc_url( $field->item_url ); ?>" target="<?php echo esc_attr( $field->item_target ); ?>" rel="<?php echo esc_attr( $field->item_rel_attribute ); ?>">
                                                <img src="<?php echo esc_url( wp_get_attachment_image_url( $field->item_image, 'full' ) ); ?>" loading="lazy">
                                            </a>
                                        </div>
                                        <?php
                                    endforeach;
                                endif;
                            ?>
                        </div>
                    </div>
                </div>
            </section>
        <?php
    }
    add_action( 'one_newspaper_after_single_content_hook', 'one_newspaper_after_content_advertisement_part' );
 endif;