<?php
/**
 * Speciality section
 *
 * This is the template for the content of speciality section
 *
 * @package Theme Palace
 * @subpackage Photoistic
 * @since Photoistic 1.0.0
 */
if ( ! function_exists( 'photoistic_add_speciality_section' ) ) :
    /**
    * Add speciality section
    *
    *@since Photoistic 1.0.0
    */
    function photoistic_add_speciality_section() {
    	$options = photoistic_get_theme_options();
        // Check if speciality is enabled on frontpage
        $speciality_enable = apply_filters( 'photoistic_section_status', true, 'speciality_section_enable' );

        if ( true !== $speciality_enable ) {
            return false;
        }
        // Get speciality section details
        $section_details = array();
        $section_details = apply_filters( 'photoistic_filter_speciality_section_details', $section_details );

        if ( empty( $section_details ) ) {
            return;
        }

        // Render speciality section now.
        photoistic_render_speciality_section( $section_details );
    }
endif;

if ( ! function_exists( 'photoistic_get_speciality_section_details' ) ) :
    /**
    * speciality section details.
    *
    * @since Photoistic 1.0.0
    * @param array $input speciality section details.
    */
    function photoistic_get_speciality_section_details( $input ) {
        $options = photoistic_get_theme_options();

        // Content type.
        
        $content = array();
       $post_ids = array();

                for ( $i = 1; $i <= 3; $i++ ) {
                    if ( ! empty( $options['speciality_content_post_' . $i] ) )
                        $post_ids[] = $options['speciality_content_post_' . $i];
                }
                $args = array(
                    'post_type'         => 'post',
                    'post__in'          => ( array ) $post_ids,
                    'posts_per_page'    => absint( 3 ),
                    'orderby'           => 'post__in',
                    'ignore_sticky_posts'   => true,
                    );  

            // Run The Loop.
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) : 
                while ( $query->have_posts() ) : $query->the_post();
                    $page_post['id']        = get_the_id();
                    $page_post['title']     = get_the_title();
                    $page_post['excerpt']   = photoistic_trim_content( 20 );
                    $page_post['url']       = get_the_permalink();
                    $page_post['image']  	= has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'full' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-590x650.jpg';

                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();
            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// speciality section content details.
add_filter( 'photoistic_filter_speciality_section_details', 'photoistic_get_speciality_section_details' );


if ( ! function_exists( 'photoistic_render_speciality_section' ) ) :
  /**
   * Start speciality section
   *
   * @return string speciality content
   * @since Photoistic 1.0.0
   *
   */
   function photoistic_render_speciality_section( $content_details = array() ) {
        $options = photoistic_get_theme_options();
        $speciality_subtitle = !empty( $options['speciality_subtitle'] ) ? $options['speciality_subtitle'] : '';
        $speciality_title = !empty( $options['speciality_title'] ) ? $options['speciality_title'] : '';
 
        if ( empty( $content_details ) ) {
            return;
        } ?>

        <div id="photoistic_speciality_section" class="relative page-section same-background">
        
        <div id="speciality-section">
            <div class="wrapper">
                <?php 
                if ( is_customize_preview()):
                    photoistic_section_tooltip( 'speciality-section' );
                endif;
                ?>
                <div class="section-header">
                    <?php if( !empty( $speciality_subtitle ) ): ?>
                        <p class="section-subtitle"><?php echo esc_html( $speciality_subtitle ); ?></p>
                    <?php endif;
                    if( !empty( $speciality_title ) ):
                        ?>
                    <h2 class="section-title"><?php echo esc_html( $speciality_title ); ?></h2>
                <?php endif; ?>
            </div><!-- .section-header -->

            <div class="archive-blog-wrapper col-3 clear">

                <?php foreach( $content_details as $content ): ?>

                    <article>
                        <div class="post-wrapper">
                            <div class="featured-image">
                                <a href="<?php echo esc_url( $content['url'] ); ?>"><img src="<?php echo esc_url( $content['image'] ); ?>" alt="blog"></a>
                            </div><!-- .featured-image -->

                            <div class="entry-container">
                                <header class="entry-header">
                                    <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>"><?php echo esc_html( $content['title'] ); ?></a></h2>
                                </header>

                                <div class="entry-content">
                                    <p><?php echo esc_html( $content['excerpt'] ); ?></p>
                                </div><!-- .entry-content -->
                            </div><!-- .entry-container -->
                        </div><!-- .post-wrapper -->
                    </article>

                <?php endforeach; ?>

            </div><!-- .section-content -->
        </div><!-- .wrapper -->
    </div><!-- #speciality-section -->   

    </div>     

    <?php }
endif;