<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;

$home_spotlight_title = isset($campoal_options['conikal_home_spotlight_title_field']) ? $campoal_options['conikal_home_spotlight_title_field']: '';
$home_spotlight_text = isset($campoal_options['conikal_home_spotlight_text_field']) ? $campoal_options['conikal_home_spotlight_text_field'] : '';
$topics_number = isset($campoal_options['conikal_home_topics_number_field']) ? $campoal_options['conikal_home_topics_number_field'] : '';
$trending_topics = isset($campoal_options['conikal_trending_topics_field']) ? $campoal_options['conikal_trending_topics_field'] : '';
$trending_topics = !empty($trending_topics) ? explode(',', $trending_topics) : '';
?>

	<div class="ui very raised padded segment">
		<div class="ui large center aligned header">
	    	<div class="content">
	    		<?php echo esc_html($home_spotlight_title); ?>
	    		<div class="sub header"><?php echo esc_html($home_spotlight_text); ?></div>
	    	</div>
	    </div>
	    <div class="ui basic center aligned segment">
	    <?php
	    if (empty($trending_topics)) {
			$terms = get_terms( array(
			    'taxonomy' => 'petition_topics',
			    'orderby' => 'count',
			    'order' => 'DESC',
			    'hide_empty' => true,
			    'number' => $topics_number,
			) );
			if ($terms) {
			  	foreach ($terms as $term ) { ?>
			  	<a href="<?php echo esc_url( get_term_link($term->term_id ) ) ?>" data-bjax>
			    	<span class="ui basic circular small button"><?php echo esc_html($term->name) ?></span>
			    </a>
		<?php }
			}
		} else {
			foreach ($trending_topics as $slug_id) {
				$slug_id = str_replace(' ', '', $slug_id); 
				$topic = get_term_by('slug', $slug_id, 'petition_topics');
				if ($topic) { ?>
				<a href="<?php echo esc_url( get_term_link($topic->term_id ) ) ?>" data-bjax>
			    	<span class="ui basic circular small button"><?php echo esc_html($topic->name) ?></span>
			    </a>
				<?php }
			}
		} ?>
		</div>
	</div>
