<?php
/**
 * Category filters template for archive pages
 *
 * @package Multi
 */

?>

<?php
if ( is_category() ) :
	$multimagazine_current_category = get_category( get_query_var( 'cat' ) );
	$multimagazine_options          = array(
		'hierarchical' => false,
		'parent'       => $multimagazine_current_category->term_id,
	);
	$multimagazine_categories       = get_terms( 'category', $multimagazine_options );
	$multimagazine_show_all         = false;

	if ( ! count( $multimagazine_categories ) ) {
		$multimagazine_options    = array(
			'hierarchical' => false,
			'parent'       => $multimagazine_current_category->parent,
		);
		$multimagazine_categories = get_terms( 'category', $multimagazine_options );
		if ( 0 === absint( $multimagazine_current_category->parent ) ) {
			$multimagazine_show_all = true;
		}
	}
	?>
	<div class="filters">
		<ul class="filter-list">
			<?php
			if ( $multimagazine_show_all ) :
				$multimagazine_base_link = get_permalink( get_option( 'page_for_posts' ) );
				if ( empty( $multimagazine_base_link ) ) {
					$multimagazine_base_link = home_url( '/' );
				}
				?>
				<li><a href="<?php echo esc_url( $multimagazine_base_link ); ?>"><?php esc_html_e( 'All', 'multimagazine' ); ?></a></li>
			<?php endif; ?>
			<?php foreach ( $multimagazine_categories as $multimagazine_category ) : ?>
			<li><a href="<?php echo esc_url( get_category_link( $multimagazine_category->term_id ) ); ?>"
				<?php if ( $multimagazine_category->term_id === $multimagazine_current_category->term_id ) : ?>
				class="selected"
			<?php endif; ?>><?php echo esc_html( $multimagazine_category->name ); ?></a></li>
			<?php endforeach; ?>
		</ul>
	</div>
	<?php
endif;
