<?php
/**
 * Template name: Help center
 *
 * @package Multi
 */

get_header();
get_template_part( 'template-parts/content-start' );

$multimagazine_col_class        = 'col3';
$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
if ( empty( $multimagazine_sidebar_position ) ) {
	$multimagazine_sidebar_position = $multimagazine_sidebar_pos_global;
}
if ( 'off' !== $multimagazine_sidebar_position ) {
	$multimagazine_sidebar = get_post_meta( get_the_id(), 'multi_custom_sidebar', true ) ? get_post_meta( get_the_id(), 'multi_custom_sidebar', true ) : 'default';
	if ( 'no' !== $multimagazine_sidebar ) {
		$multimagazine_col_class = 'col2';
	}
}
?>
<div class="main help-center single" role="main">
<?php
if ( have_posts() ) :
	while ( have_posts() ) {
		the_post();
		get_template_part( 'post-list-parts/content' );
		wp_reset_postdata();
	}
endif;
?>
	<div class="in-page-search"><?php get_search_form(); ?></div>
<?php
$multimagazine_options = array(
	'hierarchical' => 0,
	'parent'       => 0,
);
$multimagazine_cats    = get_categories( $multimagazine_options );
foreach ( $multimagazine_cats as $multimagazine_cat ) :
	$multimagazine_catlink = get_category_link( $multimagazine_cat->term_id );
	?>
	<div class="category">
		<h2 class="spec-heading  <?php echo esc_attr( multimagazine_heading_style() ); ?>"><span><a href="<?php echo esc_url( $multimagazine_catlink ); ?>"><?php echo esc_html( $multimagazine_cat->name ); ?></a></span></h2>
		<?php
		$multimagazine_post_options   = array(
			'post_type'           => 'post',
			'post_status'         => 'publish',
			'posts_per_page'      => 7,
			'cat'                 => $multimagazine_cat->term_id,
			'ignore_sticky_posts' => false,
		);
		$multimagazine_main_cat_posts = new WP_Query( $multimagazine_post_options );
		if ( $multimagazine_main_cat_posts->have_posts() ) :
			?>
		<ul class="category-posts">
			<?php
			while ( $multimagazine_main_cat_posts->have_posts() ) :
				$multimagazine_main_cat_posts->the_post();
				if ( multimagazine_post_is_in_descendant_category( $multimagazine_cat->term_id, $post ) ) {
					continue;
				}
				?>
			<li>
				<?php $multimagazine_icon = is_sticky() ? 'm-icon-thumb-tack' : 'm-icon-file'; ?>
				<i class="m-icon <?php echo esc_attr( $multimagazine_icon ); ?>" aria-hidden="true"></i><a href="<?php the_permalink(); ?>"><?php multimagazine_the_title(); ?></a>
			</li>
				<?php
			endwhile;
			?>
		</ul>
			<?php
			$multimagazine_found_posts = $multimagazine_main_cat_posts->found_posts;
			wp_reset_postdata();
		endif;

		if ( $multimagazine_found_posts > 7 ) :
			?>
		<p class="more"><a href="<?php echo esc_url( $multimagazine_catlink ); ?>"><?php esc_html_e( 'See all', 'multimagazine' ); ?> <?php echo esc_html( $multimagazine_found_posts ); ?> <?php esc_html_e( 'articles', 'multimagazine' ); ?></a></p>
		<?php endif; ?>

		<?php
		$multimagazine_sub_options = array(
			'hierarchical' => 0,
			'parent'       => $multimagazine_cat->term_id,
		);
		$multimagazine_subcats     = get_categories( $multimagazine_sub_options );
		if ( count( $multimagazine_subcats ) > 0 ) :
			?>
			<div class="subcats columns">
			<?php
			foreach ( $multimagazine_subcats as $multimagazine_subcat ) :
				$multimagazine_subcatlink = get_category_link( $multimagazine_subcat->term_id );
				$multimagazine_cat_name   = get_cat_name( $multimagazine_subcat->term_id );
				?>
				<div class="col col3 subcat">
					<h3 class="subcat-title"><a href="<?php echo esc_url( $multimagazine_subcatlink ); ?>"><?php echo esc_html( $multimagazine_cat_name ); ?></a></h3>
					<?php
					$multimagazine_subcat_post_options = array(
						'post_type'           => 'post',
						'post_status'         => 'publish',
						'posts_per_page'      => 7,
						'cat'                 => $multimagazine_subcat->term_id,
						'ignore_sticky_posts' => false,
					);
					$multimagazine_subcat_posts        = new WP_Query( $multimagazine_subcat_post_options );

					if ( $multimagazine_subcat_posts->have_posts() ) :
						?>
					<ul class="category-posts">
						<?php
						while ( $multimagazine_subcat_posts->have_posts() ) :
							$multimagazine_subcat_posts->the_post();
							?>
						<li>
							<?php
							$multimagazine_icon = is_sticky() ? 'm-icon-thumb-tack' : 'm-icon-file';
							?>
							<i class="m-icon <?php echo esc_attr( $multimagazine_icon ); ?>" aria-hidden="true"></i><a href="<?php the_permalink(); ?>"><?php multimagazine_the_title(); ?></a>
						</li>
						<?php endwhile; ?>
					</ul>
						<?php
						$multimagazine_found_posts = $multimagazine_subcat_posts->found_posts;
						wp_reset_postdata();
					endif;

					if ( $multimagazine_found_posts > 7 ) :
						?>
					<p class="more"><a href="<?php echo esc_url( $multimagazine_subcatlink ); ?>"><?php esc_html_e( 'See all', 'multimagazine' ); ?> <?php echo esc_html( $multimagazine_found_posts ); ?> <?php esc_html_e( 'articles', 'multimagazine' ); ?></a></p>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>

<?php endforeach; ?>
</div>
<?php
$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
global $multimagazine_sidebar_pos_global;
if ( empty( $multimagazine_sidebar_position ) ) {
	$multimagazine_sidebar_position = $multimagazine_sidebar_pos_global;
}
if ( 'off' !== $multimagazine_sidebar_position ) {
	get_sidebar();
}
?>

<?php get_footer(); ?>
