<?php
/**
 * Help Us Section
 *
 * This is the template for the content of subscription section
 *
 * @package Theme Palace
 * @subpackage Charity Give
 * @since Charity Give 1.0.0
 */
if ( ! function_exists( 'charity_give_add_donation_section' ) ) :
    /**
    * Add subscription section
    *
    *@since Charity Give 1.0.0
    */
    function charity_give_add_donation_section() {
        $options = charity_give_get_theme_options();

        // Check if subscription is enabled on frontpage
        $donation_enable = apply_filters( 'charity_give_section_status', true, 'donation_section_enable' );

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

        // Render subscription section now.
        charity_give_render_donation_section();
    }
endif;

if ( ! function_exists( 'charity_give_render_donation_section' ) ) :
  /**
   * Start subscription section
   *
   * @return string subscription content
   * @since Charity Give 1.0.0
   *
   */
   function charity_give_render_donation_section() {
    $options = charity_give_get_theme_options();
    if ( ! class_exists( 'Give' ) ) {
      return;
    }

    $donation_button_label = ! empty( $options['donation_btn_title'] ) ? $options['donation_btn_title'] : esc_html__( 'Donate', 'charity-give' );
    $content_count         = 3;

    $form_ids = array();

    for ( $i = 1; $i <= $content_count; $i++ ) {
        if ( ! empty( $options['dontation_form_' . $i] ) )
            $form_ids[] = $options['dontation_form_' . $i];
    }

    $args = array(
      'post_type'           => 'give_forms',
      'post__in'            => ( array ) $form_ids,
      'post_status'         => 'publish',
      'posts_per_page'      => absint( $content_count ),
      'orderby'             => 'post__in',
      'ignore_sticky_posts' => true,
    );

    $query = new WP_Query( $args );
    if ( $query->have_posts() ) :
      ?>
        <div id="charity_give_donation_section" class="relative page-section">
            <div class="wrapper">
                <?php if ( is_customize_preview()):
                      charity_give_section_tooltip( 'charity_give_donation_section' );
                    endif; ?>
                <?php if ( !empty( $options['donation_sub_title'] ) || !empty( $options['donation_title'] ) ) : ?>
                    <div class="section-header">
                        <?php if ( !empty( $options['donation_title'] ) ): ?>
                            <h2 class="section-title"><?php echo esc_html( $options['donation_title'] ); ?></h2>
                        <?php endif ?>                    
                        <?php if ( !empty( $options['donation_sub_title'] ) ): ?>
                            <p class="section-subtitle"><?php echo esc_html( $options['donation_sub_title'] ); ?></p>
                        <?php endif ?>
                    </div><!-- .section-header -->
                <?php endif; ?>
                <div class="section-content col-3 clear">
                    <?php
                    while ( $query->have_posts() ) :
                        $query->the_post();

                        //post image
                        $image = has_post_thumbnail( get_the_ID() ) ? get_the_post_thumbnail_url( get_the_ID(), 'post-thumbnail' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-600x450.jpg';
                    ?>
                        <article class="hentry">
                            <div class="help-wrapper">
                                <div class="featured-image" style="background-image: url('<?php echo esc_url( $image ); ?>');">
                                    <a href="<?php the_permalink(); ?>" class="post-thumbnail-link"></a>
                                    <div class="overlay"></div>
                                    <a href="<?php the_permalink(); ?>" class="btn readmore"><?php esc_html_e('Read More', 'charity-give'); ?></a>
                                </div><!-- .featured-image -->
                                <?php
                                $form_id         = get_the_ID();
                                $goal_stats      = give_goal_progress_stats( $form_id );
                                $goal_option     = get_post_meta( $form_id, '_give_goal_option', true );
                                $goal_format     = get_post_meta( $form_id, '_give_goal_format', true );
                                $currency_symbol = give_currency_symbol();
                                ?>

                                <?php if ( $goal_option == 'enabled' ) : ?>
                                    <div class="entry-container">

                                        <header class="entry-header">
                                            <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                        </header>

                                        <?php if ( !empty( get_the_excerpt() ) ) : ?>
                                            <div class="entry-content">
                                                <p><?php the_excerpt(); ?></p>
                                            </div>
                                        <?php endif; ?>

                                        <?php if ( $goal_stats['raw_actual'] || $goal_stats['raw_goal'] ) : ?>
                                            <div class="price">
                                                <span class="trip-price">
                                                    <span class="current-price"><?php echo esc_html( $currency_symbol . $goal_stats['raw_actual'] ); ?><?php esc_html_e(' raised of ', 'charity-give'); ?><?php echo esc_html( $currency_symbol . $goal_stats['raw_goal'] ); ?></span>
                                                </span>
                                                <?php 
                                                $prgoress_bar_shortcode = '[give_goal id="'. get_the_id() . '" show_text="false"]';
                                                echo do_shortcode( wp_kses_post( $prgoress_bar_shortcode ) );
                                                ?>
                                            </div>
                                        <?php endif; ?>

                                        <?php if ( !empty ( $donation_button_label) ) : ?>
                                            <div class="read-more">
                                                <a href="<?php the_permalink(); ?>" class="btn"><?php echo esc_html( $donation_button_label ); ?></a>
                                            </div>
                                        <?php endif; ?>
                                    </div><!-- .entry-container -->
                                <?php endif; ?>
                             </div>
                        </article>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </div>
            </div>
        </div> <!-- .cause-panel -->
      <?php
    endif;
    }
endif;


