<?php
/**
 * The post accordion template.
 *
 * @package easy_accordion_free
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

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

if ( $acc_section_title ) { ?>
	<h2 class="eap_section_title eap_section_title_<?php echo esc_attr( $post_id ); ?>"><?php echo wp_kses_post( $main_section_title ); ?></h2>
<?php } ?>
<div id="<?php echo esc_attr( $eap_accordion_uniq_id ); ?>">
<div id="sp-ea-<?php echo esc_attr( $post_id ); ?>" class="<?php echo esc_attr( $accordion_wrapper_class ); ?>" data-ex-icon="<?php echo esc_attr( $eap_expand_icon ); ?>" data-col-icon="<?php echo esc_attr( $eap_collapse_icon ); ?>" data-ea-active="<?php echo esc_attr( $eap_active_event ); ?>" data-ea-mode="<?php echo esc_attr( $accordion_layout ); ?>" data-preloader="<?php echo esc_attr( $eap_preloader ); ?>" data-scroll-active-item="<?php echo esc_attr( $eap_scroll_to_active_item ); ?>" data-offset-to-scroll="<?php echo esc_attr( $eap_offset_to_scroll ); ?>">
<?php if ( $eap_preloader ) { ?>
	<div id="eap-preloader-<?php echo esc_attr( $post_id ); ?>" class="accordion-preloader">
		<img src="<?php echo esc_url( SP_EA_URL . 'public/assets/ea_loader.svg' ); ?>" alt="<?php esc_attr_e( 'Loader image', 'easy-accordion-free' ); ?>"/>
	</div>
	<?php
}
global $wp_embed;
$ea_key                         = 1;
$eapro_allowed_description_tags = eapro_allowed_description_tags();

foreach ( $content_sources as $key => $content_source ) {
	$content_title = $content_source['accordion_content_title'];
	$content       = apply_filters( 'sp_easy_accordion_content', $content_source['accordion_content_description'] );
	$content       = str_replace( ']]>', ']]&gt;', $content );
	if ( 'ea-first-open' === $eap_accordion_mode ) {
		$a_open_first      = ( 1 === $ea_key ) ? 'collapsed show' : '';
		$expand_icon_first = ( 1 === $ea_key ) ? $eap_expand_icon : $eap_collapse_icon;
		$expand_class      = ( 1 === $ea_key ) ? 'ea-expand' : '';
		$aria_expanded     = ( 1 === $ea_key ) ? 'true' : 'false';
	} elseif ( 'ea-multi-open' === $eap_accordion_mode ) {
		$a_open_first      = 'collapsed show';
		$expand_icon_first = $eap_expand_icon;
		$expand_class      = 'ea-expand';
		$aria_expanded     = 'true';
	} elseif ( 'ea-all-close' === $eap_accordion_mode ) {
		$a_open_first      = 'spcollapse';
		$expand_icon_first = $eap_collapse_icon;
		$expand_class      = '';
		$aria_expanded     = 'false';
	}
	$eap_exp_icon_markup = ( $eap_icon ) ? '<i class="ea-expand-icon ea-icon-expand-' . $expand_icon_first . '"></i> ' : '';
	$data_sptarget       = '#collapse' . $post_id . $key;
	$eap_icon_markup     = $eap_exp_icon_markup;
	$eap_single_collapse = ! $eap_mutliple_collapse ? 'data-parent="#sp-ea-' . esc_attr( $post_id ) . '"' : '';
	?>
	<!-- Start accordion card div. -->
	<div class="ea-card <?php echo esc_attr( $expand_class . ' ' . $accordion_item_class ); ?>">
		<!-- Start accordion header. -->
		<h3 class="ea-header">
			<!-- Add anchor tag for header. -->
			<a class="collapsed" id="ea-header-<?php echo esc_attr( $post_id . $key ); ?>" data-sptoggle="spcollapse" data-sptarget="<?php echo esc_attr( $data_sptarget ); ?>" aria-controls="collapse<?php echo esc_attr( $post_id . $key ); ?>" href="#" <?php echo esc_attr( $nofollow_link_text ); ?> aria-expanded="<?php echo esc_attr( $aria_expanded ); ?>" tabindex="0">
			<?php
			// Add icon and title.
			echo wp_kses_post( $eap_icon_markup . $content_title );
			?>
			</a> <!-- Close anchor tag for header. -->
		</h3>	<!-- Close header tag. -->
		<!-- Start collapsible content div. -->
		<div class="sp-collapse spcollapse <?php echo esc_attr( $a_open_first ); ?>" id="collapse<?php echo esc_attr( $post_id . $key ); ?>" <?php echo wp_kses_post( $eap_single_collapse ); ?> role="region" aria-labelledby="ea-header-<?php echo esc_attr( $post_id . $key ); ?>">  <!-- Content div. -->
			<div class="ea-body">
			<?php
			if ( ! empty( $content ) ) {
				// Add escaping filter to the accordion content before autoembed and do_shortcode unless the shortcode scripts or few tags generated by shortcodes are removed by the filter.
				$content_to_embed = wp_kses( $content, $eapro_allowed_description_tags );
				$embedded_content = $wp_embed->autoembed( $content_to_embed );

				if ( $eap_autop ) {
					$embedded_content = wpautop( $embedded_content );
					// Remove empty p tags before and after shortcodes and then do_shortcode.
					echo do_shortcode( shortcode_unautop( $embedded_content ) );
				} else {
					echo do_shortcode( $embedded_content );
				}
			} else {
				esc_html_e( 'No Content', 'easy-accordion-free' );
			}
			?>
			</div> <!-- Close content div. -->
		</div> <!-- Close collapse div. -->
	</div> <!-- Close card div. -->
	<?php
	++$ea_key;
}
if ( $eap_schema_markup ) {
	echo '<script type="application/ld+json">
	{
	  "@context": "https://schema.org",
	  "@type": "FAQPage",
	  "mainEntity": [';
	foreach ( $content_sources as $keys => $content_source ) {
		$content_title       = $content_source['accordion_content_title'];
		$content_description = $content_source['accordion_content_description'];
		echo '{
			"@type": "Question",
			"name": "' . esc_attr( wp_strip_all_tags( $content_title ) ) . '",
			"acceptedAnswer": {
			  "@type": "Answer",
			  "text": "' . esc_html( wp_strip_all_tags( strip_shortcodes( $content_description ) ) ) . '"
			}
		  }';
		if ( $keys !== $key ) {
			echo ',';
		}
	}
	echo ']
	}
	</script>';
}
?>
</div>
</div>
