<?php
/**
 * Template part for displaying posts
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Prime Spa Center
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<div id="featured-posts" class="post-item ">
        <?php
			// Get the post ID
			$post_id = get_the_ID();

			// Check if there are audio embedded in the post content
			$post = get_post($post_id);
			$content = do_shortcode(apply_filters('the_content', $post->post_content));
			$prime_spa_center_embeds = get_media_embedded_in_content($content);

			if (!empty($prime_spa_center_embeds)) {
			    // Loop through embedded media and display only audio
			    foreach ($prime_spa_center_embeds as $embed) {
			        // Check if the embed code contains an audio tag or specific audio providers like SoundCloud
			        if (strpos($embed, 'audio') !== false || strpos($embed, 'soundcloud') !== false) {
			            ?>
			            <div class="custom-embedded-audio">
			                <div class="media-container">
			                    <?php echo $embed; ?>
			                </div>
			                <div class="media-comments">
			                    <?php
			                    // Add your comments section here
			                    comments_template(); // This will include the default WordPress comments template
			                    ?>
			                </div>
			            </div>
			            <?php
			        }
			    }
			}
		?>

		<div class="entry-container">
			<header class="entry-header">
				<?php  
				$prime_spa_center_title_setting  = prime_spa_center_get_option( 'prime_spa_center_post_title_setting' );
				if ( $prime_spa_center_title_setting ){?>
					<h2 class="entry-title">
						<a href="<?php the_permalink();?>"><?php the_title();?></a>
				   </h2>
				<?php } ?>
			</header>
			<?php  
				$prime_spa_center_meta_setting  = prime_spa_center_get_option( 'prime_spa_center_post_meta_setting' );
				if ( $prime_spa_center_meta_setting ){?>
				<ul class="auther-comment">
					<li> <svg width="23" height="23" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 19a2 2 0 01-2-2V7a2 2 0 012-2h4l2 2h4a2 2 0 012 2v1M5 19h14a2 2 0 002-2v-5a2 2 0 00-2-2H9a2 2 0 00-2 2v5a2 2 0 01-2 2z"></path></svg>
					<a href="<?php echo esc_url(get_author_posts_url( get_the_author_meta( 'ID' ) ));?>" class=""> <?php echo esc_html_e('By','prime-spa-center'); ?> <?php esc_html(the_author()); ?></a></li>
					<li><a href="javascript:void(0);" class=""><?php echo esc_html(get_comments_number($post->ID)); ?> comment</a></li>
				</ul>
			<?php } ?>
			<?php  
				$prime_spa_center_meta_setting  = prime_spa_center_get_option( 'prime_spa_center_post_content_setting' );
				if ( $prime_spa_center_meta_setting ){?>
				<div class="entry-content">
					<?php
						$prime_spa_center_excerpt_length = prime_spa_center_get_option('excerpt_length');
						    $content = get_the_content();
						    echo esc_html(wp_trim_words($content, $prime_spa_center_excerpt_length));
					?>
				</div><!-- .entry-content -->
			<?php } ?>
			<?php  
			 $prime_spa_center_button_setting  = prime_spa_center_get_option( 'prime_spa_center_post_button_setting' );
			 if ( $prime_spa_center_button_setting ){?>
				<?php $prime_spa_center_readmore_text = prime_spa_center_get_option( 'readmore_text' );?>
				<?php if (!empty($prime_spa_center_readmore_text) ) :?>
					<div class="read-more">
						<a href="<?php the_permalink();?>"><?php echo esc_html($prime_spa_center_readmore_text);?> <i class="fas fa-arrow-right"></i> </a>
					</div><!-- .read-more -->
				<?php endif; ?>
		    <?php } ?>
		</div><!-- .entry-container -->
	</div><!-- .post-item -->
</article><!-- #post-## -->
