<?php
/**
 * Call to action section
 *
 * This is the template for the content of cta section
 *
 * @package Theme Palace
 * @subpackage enlightme Pro
 * @since enlightme Pro 1.0.0
 */
if ( ! function_exists( 'enlightme_pro_add_cta_section' ) ) :
    /**
    * Add cta section
    *
    *@since enlightme Pro 1.0.0
    */
    function enlightme_pro_add_cta_section() {
        $options = enlightme_pro_get_theme_options();
        // Check if cta is enabled on frontpage
        $cta_enable = apply_filters( 'enlightme_pro_section_status', true, 'cta_section_enable' );

        if ( true !== $cta_enable ) {
            return false;
        }
        // Get cta section details
        $section_details = array();
        $section_details = apply_filters( 'enlightme_pro_filter_cta_section_details', $section_details );

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

        // Render cta section now.
        enlightme_pro_render_cta_section( $section_details );
    }
endif;

if ( ! function_exists( 'enlightme_pro_get_cta_section_details' ) ) :
    /**
    * cta section details.
    *
    * @since enlightme Pro 1.0.0
    * @param array $input cta section details.
    */
    function enlightme_pro_get_cta_section_details( $input ) {
        $options = enlightme_pro_get_theme_options();

        // Content type.
        $cta_content_type  = $options['cta_content_type'];
        
        $content = array();
        switch ( $cta_content_type ) {
            
            case 'custom':
                $custom['title']        = ! empty( $options['cta_title'] ) ? $options['cta_title'] : '';
                $custom['url']          = ! empty( $options['cta_btn_link'] ) ? $options['cta_btn_link'] : '';

                // Push to the main array.
                array_push( $content, $custom );
            break;

            case 'page':
                $page_id = ! empty( $options['cta_content_page'] ) ? $options['cta_content_page'] : '';
                $args = array(
                    'post_type'         => 'page',
                    'page_id'           => $page_id,
                    'posts_per_page'    => 1,
                    );                    
            break;

            case 'post':
                $post_id = ! empty( $options['cta_content_post'] ) ? $options['cta_content_post'] : '';
                $args = array(
                    'post_type'         => 'post',
                    'p'                 => $post_id,
                    'posts_per_page'    => 1,
                    'ignore_sticky_posts' => true,
                    );
            break;

            default:
            break;
        }

        if ( 'custom' !== $cta_content_type ) :
            // 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();
                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();
        endif;
            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// cta section content details.
add_filter( 'enlightme_pro_filter_cta_section_details', 'enlightme_pro_get_cta_section_details' );


if ( ! function_exists( 'enlightme_pro_render_cta_section' ) ) :
  /**
   * Start cta section
   *
   * @return string cta content
   * @since enlightme Pro 1.0.0
   *
   */
   function enlightme_pro_render_cta_section( $content_details = array() ) {
        $options = enlightme_pro_get_theme_options();
        $image   = ! empty( $options['cta_image'] ) ? $options['cta_image'] : '' ;
        $cta_subtitle = ! empty( $options['cta_sub_title'] ) ? $options['cta_sub_title'] : '';
        $cta_btn_title = ! empty( $options['cta_btn_title'] ) ? $options['cta_btn_title'] : '';
        if ( empty( $content_details ) ) {
            return;
        } ?>

        <div id="enlightme_pro_cta_section">
        
        <?php foreach ( $content_details as $content ): ?>

            <div id="business-cta-section" class="relative page-section" style="background-image: url('<?php echo esc_url( $image ) ;?>');">
                <div class="overlay"></div>
                <div class="wrapper">
                <?php if ( is_customize_preview()):
                enlightme_pro_section_tooltip( 'business-cta-section' );
                endif; ?>
                    <div class="section-header">
                        <?php if( !empty( $cta_subtitle ) ): ?>
                            <h3><?php echo esc_html( $cta_subtitle ); ?></h3>
                        <?php endif;

                        if( !empty( $content['title'] ) ): ?>
                        <h2 class="section-title"><?php echo esc_html( $content['title'] ) ; ?></h2>
                    <?php endif; ?>
                </div><!-- .section-header -->

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

        <?php endforeach; ?>

        </div>
        
    <?php }
endif;