<?php
/**
 * Pricing section for the homepage.
 *
 * @package Hestia
 * @since Hestia 1.0
 */

if ( ! function_exists( 'hestia_pricing' ) ) :
	/**
	 * Pricing section content.
	 * This function can be called from a shortcode too.
	 * When it's called as shortcode, the title and the subtitle shouldn't appear and it should be visible all the time,
	 * it shouldn't matter if is disable on front page.
	 *
	 * @since Hestia 1.0
	 * @modified 1.1.51
	 */
	function hestia_pricing( $is_shortcode = true ) {

		// When this function is called from selective refresh, $is_shortcode gets the value of WP_Customize_Selective_Refresh object. We don't need that.
		if ( ! is_bool( $is_shortcode ) ) {
			$is_shortcode = false;
		}

		/* Don't show section if Disable section is checked or it doesn't have any content. Show it if it's called as a shortcode */
		$hide_section = get_theme_mod( 'hestia_pricing_hide', true );
		if ( ! $is_shortcode && (bool) $hide_section === true ) {
			if ( is_customize_preview() ) {
				echo '<section class="hestia-pricing pricing section-gray" id="pricing" data-sorder="hestia_pricing" style="display: none;"></section>';
			}
			return;
		}

		$hestia_pricing_title    = get_theme_mod( 'hestia_pricing_title', esc_html__( 'Choose a plan for your next project', 'hestia-pro' ) );
		$hestia_pricing_subtitle = get_theme_mod( 'hestia_pricing_subtitle', esc_html__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'hestia-pro' ) );

		$hestia_pricing_table_one_title = get_theme_mod( 'hestia_pricing_table_one_title', esc_html__( 'Basic Package', 'hestia-pro' ) );
		$hestia_pricing_table_one_price = get_theme_mod( 'hestia_pricing_table_one_price', '<small>$</small>0' );

		$default = sprintf( '<b>%1$s</b> %2$s', esc_html__( '1', 'hestia-pro' ), esc_html__( 'Domain', 'hestia-pro' ) ) .
			sprintf( '\n<b>%1$s</b> %2$s', esc_html__( '1GB', 'hestia-pro' ), esc_html__( 'Storage', 'hestia-pro' ) ) .
			sprintf( '\n<b>%1$s</b> %2$s', esc_html__( '100GB', 'hestia-pro' ), esc_html__( 'Bandwidth', 'hestia-pro' ) ) .
			sprintf( '\n<b>%1$s</b> %2$s', esc_html__( '2', 'hestia-pro' ), esc_html__( 'Databases', 'hestia-pro' ) );

		$hestia_pricing_table_one_features = get_theme_mod( 'hestia_pricing_table_one_features', $default );
		if ( ! is_array( $hestia_pricing_table_one_features ) ) {
			$hestia_pricing_table_one_features = explode( '\n', str_replace( '\r', '', wp_kses_post( force_balance_tags( $hestia_pricing_table_one_features ) ) ) );
		}

		$hestia_pricing_table_one_link  = get_theme_mod( 'hestia_pricing_table_one_link', '#' );
		$hestia_pricing_table_one_text  = get_theme_mod( 'hestia_pricing_table_one_text', esc_html__( 'Free Download', 'hestia-pro' ) );
		$hestia_pricing_table_two_title = get_theme_mod( 'hestia_pricing_table_two_title', esc_html__( 'Premium Package', 'hestia-pro' ) );
		$hestia_pricing_table_two_price = get_theme_mod( 'hestia_pricing_table_two_price', '<small>$</small>49' );

		$default = sprintf( '<b>%1$s</b> %2$s', esc_html__( '5', 'hestia-pro' ), esc_html__( 'Domain', 'hestia-pro' ) ) .
			sprintf( ' \n<b>%1$s</b> %2$s', esc_html__( 'Unlimited', 'hestia-pro' ), esc_html__( 'Storage', 'hestia-pro' ) ) .
			sprintf( ' \n<b>%1$s</b> %2$s', esc_html__( 'Unlimited', 'hestia-pro' ), esc_html__( 'Bandwidth', 'hestia-pro' ) ) .
			sprintf( ' \n<b>%1$s</b> %2$s', esc_html__( 'Unlimited', 'hestia-pro' ), esc_html__( 'Databases', 'hestia-pro' ) );

		$hestia_pricing_table_two_features = get_theme_mod( 'hestia_pricing_table_two_features', $default );
		if ( ! is_array( $hestia_pricing_table_two_features ) ) {
			$hestia_pricing_table_two_features = explode( '\n', str_replace( '\r', '', wp_kses_post( force_balance_tags( $hestia_pricing_table_two_features ) ) ) );
		}
		$hestia_pricing_table_two_link = get_theme_mod( 'hestia_pricing_table_two_link', '#' );
		$hestia_pricing_table_two_text = get_theme_mod( 'hestia_pricing_table_two_text', esc_html__( 'Order Now', 'hestia-pro' ) );

		$wrapper_class   = $is_shortcode === true ? 'is-shortcode' : '';
		$container_class = $is_shortcode === true ? '' : 'container';

		hestia_before_pricing_section_trigger();
		?>
		<section class="hestia-pricing pricing section-gray <?php echo esc_attr( $wrapper_class ); ?>" id="pricing" data-sorder="hestia_pricing">
			<?php hestia_before_pricing_section_content_trigger(); ?>
			<div class="<?php echo esc_attr( $container_class ); ?>">
				<?php hestia_top_pricing_section_content_trigger(); ?>
				<div class="row">
					<div class="col-md-4 col-lg-4">
						<?php if ( ! empty( $hestia_pricing_title ) || is_customize_preview() ) : ?>
							<h2 class="hestia-title"><?php echo wp_kses_post( $hestia_pricing_title ); ?></h2>
						<?php endif; ?>
						<?php if ( ! empty( $hestia_pricing_subtitle ) || is_customize_preview() ) : ?>
							<p class="text-gray"><?php echo wp_kses_post( $hestia_pricing_subtitle ); ?></p>
						<?php endif; ?>
					</div>
					<div class="col-md-8 col-lg-7 col-lg-offset-1">
						<div class="row">
							<div class="col-ms-6 col-sm-6 hestia-table-one" <?php echo hestia_add_animationation( 'fade-up' ); ?>>
								<div class="card card-pricing card-raised">
									<div class="content">
										<?php if ( ! empty( $hestia_pricing_table_one_title ) || is_customize_preview() ) : ?>
											<h6 class="category"><?php echo esc_html( $hestia_pricing_table_one_title ); ?></h6>
										<?php endif; ?>
										<?php if ( ! empty( $hestia_pricing_table_one_price ) || is_customize_preview() ) : ?>
											<h3 class="card-title"><?php echo wp_kses_post( $hestia_pricing_table_one_price ); ?></h3>
										<?php endif; ?>

										<?php if ( ! empty( $hestia_pricing_table_one_features ) ) : ?>
											<ul>
												<?php foreach ( $hestia_pricing_table_one_features as $feature ) : ?>
													<li><?php echo wp_kses_post( $feature ); ?></li>
												<?php endforeach; ?>
											</ul>
										<?php endif; ?>

										<?php
										if ( ( ! empty( $hestia_pricing_table_one_link ) && ! empty( $hestia_pricing_table_one_text ) ) || is_customize_preview() ) {
											$link_html = '<a href="' . esc_url( $hestia_pricing_table_one_link ) . '"';
											if ( function_exists( 'hestia_is_external_url' ) ) {
												$link_html .= hestia_is_external_url( $hestia_pricing_table_one_link );
											}
											$link_html .= ' class="btn btn-primary">';
											$link_html .= esc_html( $hestia_pricing_table_one_text );
											$link_html .= '</a>';
											echo wp_kses_post( $link_html );
										}
										?>
									</div>
								</div>
							</div>
							<div class="col-ms-6 col-sm-6 hestia-table-two" <?php echo hestia_add_animationation( 'fade-left' ); ?>>
								<div class="card card-pricing card-plain">
									<div class="content">
										<?php if ( ! empty( $hestia_pricing_table_two_title ) || is_customize_preview() ) : ?>
											<h6 class="category"><?php echo esc_html( $hestia_pricing_table_two_title ); ?></h6>
										<?php endif; ?>
										<?php if ( ! empty( $hestia_pricing_table_two_price ) || is_customize_preview() ) : ?>
											<h3 class="card-title"><?php echo wp_kses_post( $hestia_pricing_table_two_price ); ?></h3>
										<?php endif; ?>
										<?php if ( ! empty( $hestia_pricing_table_two_features ) ) : ?>
											<ul>
												<?php foreach ( $hestia_pricing_table_two_features as $feature ) : ?>
													<li><?php echo wp_kses_post( $feature ); ?></li>
												<?php endforeach; ?>
											</ul>
										<?php endif; ?>
										<?php
										if ( ( ! empty( $hestia_pricing_table_two_link ) && ! empty( $hestia_pricing_table_two_text ) ) || is_customize_preview() ) {
											echo '<a href="' . esc_url( $hestia_pricing_table_two_link ) . '"';
											if ( function_exists( 'hestia_is_external_url' ) ) {
												echo hestia_is_external_url( $hestia_pricing_table_two_link );
											}
											echo ' class="btn btn-primary">' . esc_html( $hestia_pricing_table_two_text ) . '</a>';
										}
										?>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<?php hestia_bottom_pricing_section_content_trigger(); ?>
			</div>
			<?php hestia_after_pricing_section_content_trigger(); ?>
		</section>
		<?php
		hestia_after_pricing_section_trigger();
	}
endif;

if ( function_exists( 'hestia_pricing' ) ) {
	$section_priority = apply_filters( 'hestia_section_priority', 35, 'hestia_pricing' );
	add_action( 'hestia_sections', 'hestia_pricing', absint( $section_priority ) );
}
