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

        if ( true !== $promotion_enable ) {
            return false;
        }
        // Get about section details
        $section_details = array();
        $section_details = apply_filters( 'photoistic_filter_promotion_section_details', $section_details );

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

        // Render about section now.
        photoistic_render_promotion_section( $section_details );
    }
endif;

if ( ! function_exists( 'photoistic_get_promotion_section_details' ) ) :
    /**
    * about section details.
    *
    * @since Photoistic 1.0.0
    * @param array $input about section details.
    */
    function photoistic_get_promotion_section_details( $input ) {
        $options = photoistic_get_theme_options();
        
        $content = array();
       $page_id = ! empty( $options['promotion_content_page'] ) ? $options['promotion_content_page'] : '';
                $args = array(
                    'post_type'         => 'page',
                    'page_id'           => $page_id,
                    'posts_per_page'    => 1,
                    );

            // 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( 30 );
                    $page_post['url']       = get_the_permalink();

                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();

        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// about section content details.
add_filter( 'photoistic_filter_promotion_section_details', 'photoistic_get_promotion_section_details' );


if ( ! function_exists( 'photoistic_render_promotion_section' ) ) :
  /**
   * Start about section
   *
   * @return string about content
   * @since Photoistic 1.0.0
   *
   */
   function photoistic_render_promotion_section( $content_details = array() ) {
        $options = photoistic_get_theme_options();
        $promotion_btn_title = ! empty( $options['promotion_btn_title'] ) ? $options['promotion_btn_title'] : '';

        if ( empty( $content_details ) ) {
            return;
        } 
        ?>

        <div id="photoistic_promotion_section">

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

       <div id="promotion-section" class="relative page-section" style="background-image: url('<?php echo esc_url( $options['promotion_image'] ); ?>');">
            <div class="overlay"></div>
            <div class="wrapper">
            <?php if ( is_customize_preview()):
                photoistic_section_tooltip( 'promotion-section' );
                endif; ?>
                <?php if( !empty( $content['title'] ) ): ?>
                    <header class="entry-header">
                        <h2 class="entry-title"><?php echo esc_html( $content['title'] ); ?></h2>
                    </header>

                <?php endif;

                if( !empty( $content['excerpt'] ) ):

                    ?>

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

            <?php endif; ?>

            <?php if( !empty( $content['url'] ) && !empty( $promotion_btn_title ) ): ?>

                <div class="read-more">
                    <a href="<?php echo esc_url( $content['url'] ); ?>" class="btn"><?php echo esc_html( $promotion_btn_title ); ?></a>
                </div><!-- .read-more -->
            <?php endif; ?>

        </div><!-- .wrapper -->
    </div><!-- #promotion-section -->        

    <?php 
    endforeach;
    ?>
    </div>
    <?php
}
endif;