<?php
$id        = get_theme_mod('onepress_news_id', 'news');
$disable   = get_theme_mod('onepress_news_disable') == 1 ? true : false;

if (onepress_is_selective_refresh()) {
	$disable = false;
}

if (!$disable) :

	$title     = get_theme_mod('onepress_news_title', esc_html__('Latest News', 'onepress'));
	$subtitle  = get_theme_mod('onepress_news_subtitle', esc_html__('Section subtitle', 'onepress'));
	$number    = absint(get_theme_mod('onepress_news_number', '3'));
	$more_page = get_theme_mod('onepress_news_more_page', '');
	$more_link = get_theme_mod('onepress_news_more_link', '');
	$more_text = get_theme_mod('onepress_news_more_text', esc_html__('Read Our Blog', 'onepress'));
	$desc = get_theme_mod('onepress_news_desc');

	$news_layout   = onepress_sanitize_news_layout( get_theme_mod( 'onepress_news_layout', 'list' ) );
	$grid_columns  = get_theme_mod( 'onepress_news_grid_columns', '2 2 1' );
	$section_class = apply_filters( 'onepress_section_class', 'section-news section-padding onepage-section', 'news' );
	if ( $news_layout === 'grid' ) {
		$section_class .= ' section-news--layout-grid';
	} else {
		$section_class .= ' section-news--layout-list';
	}

?>
	<?php if (!onepress_is_selective_refresh()) { ?>
		<section id="<?php if ($id != '') {
							echo esc_attr($id);
						} ?>" <?php do_action('onepress_section_atts', 'news'); ?> class="<?php echo esc_attr( $section_class ); ?>">
		<?php } ?>
		<?php do_action('onepress_section_before_inner', 'news'); ?>
		<div class="<?php echo esc_attr(apply_filters('onepress_section_container_class', 'container', 'news')); ?>">
			<?php if ($title || $subtitle || $desc) { ?>
				<div class="section-title-area">
					<?php if ($subtitle != '') {
						echo '<h5 class="section-subtitle">' . esc_html($subtitle) . '</h5>';
					} ?>
					<?php if ($title != '') {
						echo '<h2 class="section-title">' . esc_html($title) . '</h2>';
					} ?>
					<?php if ($desc) {
						echo '<div class="section-desc">' . apply_filters('onepress_the_content', wp_kses_post($desc)) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped	
					} ?>
				</div>
			<?php } ?>
			<div class="section-content">
				<div class="row">
					<div class="col-sm-12">
						<div class="blog-entry wow slideInUp <?php echo $news_layout === 'grid' ? ' blog-entry--grid' : ''; ?>">
							<?php

							$cat = absint(get_theme_mod('onepress_news_cat'));
							$orderby = sanitize_text_field(get_theme_mod('onepress_news_orderby'));
							$order = sanitize_text_field(get_theme_mod('onepress_news_order'));
							$hide_meta = sanitize_text_field(get_theme_mod('onepress_news_hide_meta'), false);
							$excerpt_length = absint(get_theme_mod('onepress_news_excerpt_length'));
							$excerpt_type = get_theme_mod('onepress_news_excerpt_type', 'custom');
							if ($hide_meta) {
								onepress_loop_set_prop('show_meta', false);
							}

							onepress_loop_set_prop('excerpt_length', $excerpt_length);
							onepress_loop_set_prop('excerpt_type', $excerpt_type);

							$args = array(
								'posts_per_page' => $number,
								'suppress_filters' => 0,
							);
							if ($cat > 0) {
								$args['category__in'] = array($cat);
							}

							if ($orderby && $orderby != 'default') {
								$args['orderby'] = $orderby;
							}

							if ($order) {
								$args['order'] = $order;
							}

							$query = new WP_Query($args);

							$grid_col_class = '';
							if ( $news_layout === 'grid' ) {
								$spans = onepress_parse_news_grid_columns( $grid_columns );
								$grid_col_class = sprintf(
									'col-lg-%d col-md-%d col-xs-%d section-news-grid__col',
									(int) $spans['lg'],
									(int) $spans['md'],
									(int) $spans['xs']
								);
							}

							?>
							<?php if ($query->have_posts()) : ?>

								<?php
								if ( $news_layout === 'grid' ) {
									onepress_loop_set_prop( 'news_layout', 'grid' );
								}
								?>

								<?php if ( $news_layout === 'grid' ) : ?>
								<div class="row section-news-row">
								<?php endif; ?>

								<?php /* Start the Loop */ ?>
								<?php while ($query->have_posts()) :
									$query->the_post(); ?>
									<?php
									if ( $news_layout === 'grid' ) {
										echo '<div class="' . esc_attr( $grid_col_class ) . '">';
									}
									/*
									 * Include the Post-Format-specific template for the content.
									 * If you want to override this in a child theme, then include a file
									 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
									 */
									get_template_part('template-parts/content', 'list');
									if ( $news_layout === 'grid' ) {
										echo '</div>';
									}
									?>

								<?php endwhile; ?>

								<?php if ( $news_layout === 'grid' ) : ?>
								</div>
								<?php
								onepress_loop_remove_prop( 'news_layout' );
								endif;
								?>
							<?php else : ?>
								<?php get_template_part('template-parts/content', 'none'); ?>
							<?php endif;


							if ($hide_meta) {
								onepress_loop_remove_prop('show_meta');
							}

							onepress_loop_remove_prop('excerpt_length');
							onepress_loop_remove_prop('excerpt_type');

							$link = '';
							$label = '';

							if ($more_page) {
								$page = get_post($more_page);
								if ($page) {
									$link = get_permalink($page);
									if (!$more_text) {
										$label = $page->post_title;
									}
								}
							}

							if (!$link) {
								$link = $more_link;
							}

							if (!$label) {
								$label = $more_text;
							}

							if ($link != '' && $label != '') {
							?>
								<div class="all-news">
									<a class="btn btn-theme-primary-outline" href="<?php echo esc_url($link); ?>"><?php echo esc_html($label); ?></a>
								</div>
							<?php } ?>

						</div>
					</div>
				</div>

			</div>
		</div>
		<?php do_action('onepress_section_after_inner', 'news'); ?>
		<?php if (!onepress_is_selective_refresh()) { ?>
		</section>
	<?php } ?>
<?php endif;
wp_reset_postdata();
