<?php
/**
 * Breaking News
 *
 * This template can be overridden by copying it to your-child-theme/templates/breaking-news.php.
 *
 * HOWEVER, on occasion TieLabs will need to update template files and you
 * will need to copy the new files to your child theme to maintain compatibility.
 *
 * @author   TieLabs
 * @version  2.1.0
 */

defined( 'ABSPATH' ) || exit; // Exit if accessed directly


// Enqueue the Sliders Js file
wp_enqueue_script( 'tie-js-sliders' );

// Check if the breaking news is hidden on mobiles
if( $type == 'header' && TIELABS_HELPER::is_mobile_and_hidden( 'breaking_news' )) return;

// Cache field key
$cache_key = apply_filters( 'TieLabs/cache_key', '' );

// Classes and attr.
$breaking_attr  = array();
$breaking_class = array( 'breaking' );


// Effect type.
if( ! empty( $breaking_effect ) ){
	$breaking_attr[] = 'data-type="'. $breaking_effect .'"';

	if( $breaking_effect == 'slideUp' || $breaking_effect == 'slideDown' ){
		$breaking_class[] = 'up-down-controls';
	}
}

// Breaking News arrows.
if( ! empty( $breaking_arrows ) ){
	$breaking_attr[]  = 'data-arrows="true"';
	$breaking_class[] = 'controls-is-active';
}

$breaking_class = join( ' ', array_filter( apply_filters( 'TieLabs/Breaking_news/class', $breaking_class ) ) );
$breaking_attr = join( ' ', array_filter( apply_filters( 'TieLabs/Breaking_news/attr', $breaking_attr ) ) );

?>

<div class="<?php echo esc_attr( $breaking_class ) ?>">

	<span class="breaking-title">
		<span class="fa fa-bolt" aria-hidden="true"></span>
		<span class="breaking-title-text"><?php echo ! empty( $breaking_title ) ? $breaking_title : esc_html__( 'Trending', TIELABS_TEXTDOMAIN ); ?></span>
	</span>

	<ul id="breaking-news-<?php echo esc_attr( $breaking_id ) ?>" class="breaking-news" <?php echo ( $breaking_attr ); ?>>

		<?php

			if( $breaking_type != 'custom' ){

				// Get the Cached data
				if ( $type == 'header' && tie_get_option( 'cache' ) && ( false !== ( $cached_data = get_transient( $cache_key )) ) && ! ( defined( 'WP_CACHE' ) && WP_CACHE ) ){
					if( isset( $cached_data['breaking-news'] )){
						$cached_breaking_news = $cached_data['breaking-news'];
					}
				}

				// It wasn't there, so render the Breaking news and save it as a transient
				if( empty( $cached_breaking_news )){

					ob_start();

					# Category or Tags
					if( $type == 'header' ){

						$args = array(
							'number' => ( ! empty( $breaking_number ) ? $breaking_number : 10 ),
							'update_post_meta_cache' => false,
							'update_post_term_cache' => false,
						);

						if( $breaking_type == 'tag' ){
							$args['tags'] = $breaking_tag;
						}
						else{
							$args['id'] = $breaking_cat;
						}
					}
					else{
						$args = $breaking_block;
					}

					$breaking_query = tie_query( $args );

					if( $breaking_query->have_posts() ){
						while( $breaking_query->have_posts() ){ $breaking_query->the_post(); ?>

							<li class="news-item">
								<a href="<?php the_permalink()?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
							</li>

							<?php
						}
					}

					wp_reset_postdata();

					$cached_breaking_news = ob_get_clean();

					if( $type == 'header' ){
						$GLOBALS[ $cache_key ]['breaking-news'] = $cached_breaking_news;
					}
				}

				echo ( $cached_breaking_news );
			}

			else{

				if( ! empty( $breaking_custom ) && is_array( $breaking_custom ) ){
					$count = 0;
					foreach ($breaking_custom as $custom_text){
						$count++;

						$text = $link = '';

						# WPML
						if( ! empty( $custom_text['text'] ) ){
							$text = ( $type == 'block' ) ? $custom_text['text'] : apply_filters( 'wpml_translate_single_string', $custom_text['text'], TIELABS_THEME_SLUG, 'Breaking News Custom Text #'.$count );
						}

						if( ! empty( $custom_text['link'] ) ){
							$link = ( $type == 'block' ) ? $custom_text['link'] : apply_filters( 'wpml_translate_single_string', $custom_text['link'], TIELABS_THEME_SLUG, 'Breaking News Custom Link #'.$count );
						}
						?>

						<li class="news-item">
							<a href="<?php echo esc_url( $link ) ?>" title="<?php echo esc_attr( $text ) ?>"><?php echo esc_html( $text ); ?></a>
						</li>

						<?php
					}
				}

			}
		?>

	</ul>
</div><!-- #breaking /-->
