<?php
/**
 * The template for displaying featured content
 *
 * @package Personal_Trainer
 */
?>

<?php
$enable = get_theme_mod( 'personaltrainer_professionals_option', 'disabled' );

global $wp_query;

// Get Page ID outside Loop
$page_id = absint( $wp_query->get_queried_object_id() );

// Front page displays in Reading Settings
$page_for_posts = absint( get_option( 'page_for_posts' ) );

if ( ! ( 'entire-site' == $enable  || ( ( is_front_page() || ( is_home() && $page_for_posts !== $page_id ) ) && ( 'homepage' === $enable || 'homepage-about' === $enable ) ) ) ) {
	return;
}

$type        = get_theme_mod( 'personaltrainer_professionals_type', 'demo' );
$title       = get_theme_mod( 'personaltrainer_professionals_title', esc_html__( 'Meet Professionals', 'personal-trainer' ) );
$description = get_theme_mod( 'personaltrainer_professionals_description' );
?>

<div class="professionals-content-wrapper section<?php echo get_theme_mod( 'personaltrainer_professionals_disable_image' ) ? ' image-disabled' : ''; ?>">
	<div class="wrapper">
		<?php if ( '' !== $title || $description ) : ?>
		<div class="section-heading-wrapper featured-section-headline">
			<?php if ( '' !== $title ) : ?>
				<div class="section-heading-wrap">
					<h2 class="section-title"><?php echo wp_kses_post( $title ); ?></h2>
				</div><!-- .heading-wrap -->
			<?php endif; ?>

			<?php if ( $description ) : ?>
				<div class="section-description">
					<?php echo wp_kses_post( $description ); ?>
				</div><!-- .section-description -->
			<?php endif; ?>
		</div><!-- .section-heading-wrapper -->
		<?php endif; ?>

		<div class="section-content-wrap">
			<div class="cycle-slideshow"
			    data-cycle-log="false"
			    data-cycle-pause-on-hover="true"
			    data-cycle-swipe="true"
			    data-cycle-auto-height=container
			    data-cycle-fx="fade"
				data-cycle-speed="1000"
				data-cycle-timeout="4000"
				data-cycle-loader=false
				data-cycle-slides="> .meet-slide"
				>

				<div class="cycle-prev fa fa-angle-left" aria-label="<?php esc_attr_e( 'Previous Slide', 'personal-trainer' ); ?>" aria-hidden="true"><span class="screen-reader-text"><?php esc_html_e( 'Previous Slide', 'personal-trainer' ); ?></span></div>
				<div class="cycle-next fa fa-angle-right" aria-label="<?php esc_attr_e( 'Next Slide', 'personal-trainer' ); ?>" aria-hidden="true"><span class="screen-reader-text"><?php esc_html_e( 'Next Slide', 'personal-trainer' ); ?></span></div>

				<div class="cycle-pager"></div>

				<?php
				$number = get_theme_mod( 'personaltrainer_professionals_number', 3 );

				if ( ! $number ) {
					// If number is 0, then this section is disabled
					return;
				}

				$args = array(
					'orderby'             => 'post__in',
					'ignore_sticky_posts' => 1 // ignore sticky posts
				);

				$post_list  = array();// list of valid post/page ids
				$no_of_post = 0; // for number of posts

				$args['post_type'] = $type;

				for ( $i = 1; $i <= $number; $i++ ) {
					$post_id = get_theme_mod( 'personaltrainer_professionals_page_' . $i );

					if ( $post_id ) {
						// Polylang Support.
						if ( class_exists( 'Polylang' ) ) {
							$post_id = pll_get_post( $post_id, pll_current_language() );
						}

						$post_list = array_merge( $post_list, array( $post_id ) );

						$no_of_post++;
					}
				}

				$args['post__in'] = $post_list;

				if ( $no_of_post ) {
					$args['posts_per_page'] = $no_of_post;

					$loop = new WP_Query( $args );

					if ( $loop -> have_posts() ) {
						while ( $loop -> have_posts() ) {
							$loop -> the_post();

							get_template_part( 'template-parts/professionals/content', 'post-types' );
						} // endwhile().

						wp_reset_postdata();
					}
				}
				?>
			</div> <!-- cycle-slideshow -->

			<?php
			$more_btn_link   = get_theme_mod( 'personaltrainer_professionals_more_btn_link', '#' );
			$more_btn_text   = get_theme_mod( 'personaltrainer_professionals_more_btn_text', esc_html__( 'View Full Team', 'personal-trainer' ) );
			$more_btn_target = get_theme_mod( 'personaltrainer_professionals_more_btn__target' );

			if ( $more_btn_link || $more_btn_text ) : ?>
				<p class="view-more"><a target="<?php echo $more_btn_target ? '_blank' : '_self' ?>" href="<?php echo esc_url( $more_btn_link ); ?>"><?php echo esc_html( $more_btn_text ); ?></a></p>
			<?php endif; ?>
		</div><!-- .section-content-wrap -->
	</div><!-- .wrapper -->
</div> <!-- .professionals-content-wrapper -->
