<?php
/**
 * Call To Action section
 *
 * This is the template for the content of Call To Action section
 *
 * @package Theme Palace
 * @subpackage Pushpa
 * @since Pushpa 1.0.0
 */
if ( ! function_exists( 'pushpa_add_cta_section' ) ) :
    /**
    * Add Call To Action section
    *
    *@since Pushpa 1.0.0
    */
    function pushpa_add_cta_section() {
        $options = pushpa_get_theme_options();
        
        // Check if Call To Action is enabled on frontpage
        $cta_enable = apply_filters( 'pushpa_section_status', true, 'cta_section_enable' );

        if ( true !== $cta_enable ) {
            return false;
        }

        // Get Call To Action section details
        $section_details = array();
        $section_details = apply_filters( 'pushpa_filter_cta_section_details', $section_details );
        if ( empty( $section_details ) ) {
            return;
        }

        // Render Call To Action section now.
        pushpa_render_cta_section( $section_details[0] );
    }
endif;

if ( ! function_exists( 'pushpa_get_cta_section_details' ) ) :
    /**
    * Call To Action section details.
    *
    * @since Pushpa 1.0.0
    * @param array $input Call To Action section details.
    */
    function pushpa_get_cta_section_details( $input ) {
        $options = pushpa_get_theme_options();
        $content = array();

                $page_id = ! empty( $options['cta_content_page'] ) ? $options['cta_content_page'] : '';
                $args = array(
                        'post_type'             => 'page',
                        'posts_per_page'        => 1,
                        'p'                     => $page_id,
                        '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['url']       = get_the_permalink();
                $page_post['excerpt']   = pushpa_trim_content( 25 );
                $page_post['image']  	= has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'large' ) : get_template_directory_uri().'/assets/uploads/no-featured-image-600x450.jpg';

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

        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;

// Call To Action section content details.
add_filter( 'pushpa_filter_cta_section_details', 'pushpa_get_cta_section_details' );


if ( ! function_exists( 'pushpa_render_cta_section' ) ) :
  /**
   * Start Call To Action section
   *
   * @return string Call To Action content
   * @since Pushpa 1.0.0
   *
   */
   function pushpa_render_cta_section( $content_details = array() ) {
        $options    = pushpa_get_theme_options();
        $btn_label  = $options['cta_read_more'];

        if ( empty( $content_details ) ) {
            return;
        } ?>
        <div id="pushpa_cta" class="relative">
            <div class="wrapper">
                <?php if ( is_customize_preview()):
                    pushpa_section_tooltip( 'pushpa_cta' );
                endif; ?>
                <article style="background-image: url('<?php echo esc_url( $content_details['image'] ); ?>');">
                    <div class="entry-container">
                        <div class="entry-meta">
                            <span class="posted-on">
                                <?php pushpa_posted_on($content_details['id']); ?>
                            </span><!-- .posted-on -->  

                            <span class="cat-links">
                               <?php the_category( '', '', $content_details['id'] ); ?>
                           </span>
                       </div>

                        <div class="section-header">
                            <h2 class="section-title"><?php echo esc_html( $content_details['title'] ); ?>.</h2>
                        </div><!-- .section-header -->
                        <?php if ( !empty( $options['cta_read_more'] ) ) : ?>
                            <div class="read-more">
                                <a href="<?php echo esc_url( $content_details['url'] ); ?>" class="btn" tabindex="0"><?php echo esc_html( $options['cta_read_more'] ); ?></a>
                            </div>
                        <?php endif; ?>
                    </div>
                </article>
            </div>
        </div><!-- #pushpa_featured_posts -->
    <?php }
endif;