<?php
/**
 * Blog functions.
 *
 * @package Nevo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Initiate columns.
 *
 * @since 1.0
 */
function nevo_blog_columns() {
	$columns = false;
	if ( is_home() || is_category() || is_tag() || is_date() || is_author() ) {
		$columns = true;
	}
	return apply_filters( 'nevo_blog_columns', $columns );
}

if ( ! function_exists( 'nevo_post_image' ) ) {
	/**
	 * Prints the Post Image to post excerpts
	 */
	function nevo_post_image($size = 'medium', $div_classes = '', $img_classes = '', $style = '', $category = true) {
		// Don't run the function unless we have a post thumbnail.
		if ( ! has_post_thumbnail() || ! $size ) {
			return;
		}
		$div_classes = 'post-image size-full position-relative ' . $div_classes;
		$attrs = apply_filters('nevo_post_image_default_attribute', array(
			//'alt' => the_title_attribute('echo=0'),
			'itemprop' => 'image',
			'class' => $img_classes,
			'style' => $style
		));
		$is_video = get_post_format() === 'video';
		?>
		<div class="<?php echo esc_attr($div_classes); ?>">
			<?php if ($category) { $setting_values = array( 'categories' => $category);
				nevo_display_entry_meta_items($setting_values); }
			?>
			<a href="<?php the_permalink(); ?>" title="<?php echo the_title_attribute('echo=0') ?>" rel="bookmark">
				<?php echo get_the_post_thumbnail(get_the_ID(),apply_filters('nevo_post_image_size', $size), $attrs); ?>
				<?php if ($is_video): ?>
					<!-- Icon play SVG -->
					<span class="video-play-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="nevo-play-circle" viewBox="0 0 16 16">
						  <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/>
						  <path d="M6.271 5.055a.5.5 0 0 1 .52.038l3.5 2.5a.5.5 0 0 1 0 .814l-3.5 2.5A.5.5 0 0 1 6 10.5v-5a.5.5 0 0 1 .271-.445"/>
						</svg>
					</span>
				<?php endif; ?>
			</a>
		</div>
		<?php
	}
}

if ( ! function_exists( 'nevo_featured_image' ) ) {
	/**
	 * Build the page header.
	 *
	 * @since 1.0.0
	 *
	 * @param string $class The featured image container class.
	 */
	function nevo_featured_image( $class ) {
		// Don't run the function unless we have a post thumbnail.
		if ( ! has_post_thumbnail() ) {
			return;
		}
		
		// Don't run the function unless we're on a page it applies to.
		if ( is_singular() ) {
			$image_ratio = Nevo()->get_setting( 'post_single_images_ratio' );
			$attrs = apply_filters( 'nevo_featured_image_default_attribute', array(
				//'alt' => the_title_attribute( 'echo=0' ),
				'itemprop' => 'image',
				'class' => 'size-full cv-col-12',
				'style' => $image_ratio
			) );
			?>
			<div class="featured-image cv-col-12 <?php echo esc_attr( $class ); ?>">
				<?php
					the_post_thumbnail(
						apply_filters( 'nevo_featured_image_default_size', 'full' ),
						$attrs
					);
				?>
			</div>
			<?php
		}
	}
}

if ( ! function_exists( 'nevo_featured_inside_single' ) ) {
	add_action( 'nevo_after_entry_header', 'nevo_featured_inside_single', 10 );
	/**
	 * Only add to single post.
	 *
	 * @since 1.0.0
	 */
	function nevo_featured_inside_single() {
		if ( is_single() ) {
			nevo_featured_image( 'post-image' );
		}
	}
}

if ( ! function_exists( 'nevo_display_post_single_elements' ) ) {
	
	add_action( 'nevo_after_entry', 'nevo_display_post_single_elements', 10 );
	/**
	 * Build the single post elements.
	 *
	 * @since 1.0.0
	 */
	function nevo_display_post_single_elements() {
		if ( ! is_single() || ! Nevo()->get_setting( 'post_single_elements', 'elements' ) ) {
			return;
		}
		$single_elements = Nevo()->get_setting( 'post_single_elements' );
		echo '<div class="single-after-entry">';
		
		do_action( 'nevo_before_content_single_elements' );
		
		foreach ( $single_elements as $element ) {
			$element = wp_parse_args(
				$element,
				array(
					'title'       => '',
					'elements'    => '',
					'_visibility' => '',
				)
			);
			if ( 'hidden' !== $element['_visibility'] ) {
				nevo_get_template_part( 'blog/' . esc_html($element['elements']) );
			}
		}
		
		do_action( 'nevo_after_content_single_elements' );
		
		echo '</div>';
	}
}

if ( ! function_exists( 'nevo_custom_image_default_size' ) ) {
	add_filter( 'nevo_featured_image_default_size', 'nevo_custom_image_default_size' );
	/**
	 * Add size in post image size
	 *
	 * @since 1.0
	 */
	function nevo_custom_image_default_size() {
		$imagesize = Nevo()->get_setting( 'post_single_thumbnail_size' );
		
		if ( is_single() && $imagesize != 'default' ) {
			return $imagesize;
		}
		if ( is_page() ) {
			return 'full';
		}
	}
}

function nevo_post_single_page_header_elements( $elements ){
	$layout = Nevo()->get_setting( 'post_single_layout' );
	
	if ( 'single-post-1' === $layout ) {
		$elements = array(
			0 => array( 'elements' => 'breadcrumb' ),
		);
	}
	return apply_filters( 'nevo_post_single_page_header_elements', $elements );
}

/**
 * Collects all available image sizes which we use in the Customizer.
 *
 * @since 1.0
 *
 * @return array
 */
function nevo_blog_get_image_sizes() {
	$sizes = get_intermediate_image_sizes();

	$new_sizes = array(
		//'default'	=> 'Default',
		'full' 		=> 'full',
	);

	foreach ( $sizes as $key => $name ) {
		$new_sizes[ $name ] = $name;
	}

	return $new_sizes;
}

if ( ! function_exists( 'nevo_modify_query_posts' ) ) {
	add_action( 'pre_get_posts', 'nevo_modify_query_posts' );
	function nevo_modify_query_posts( $query ) {
		if ( is_admin() || ! $query->is_main_query() ) {
			return;
		}

		if ( nevo_blog_columns() || is_search() ) {
			$posts_per_page = nevo_get_post_setting('posts_per_page');

			if ( $posts_per_page ) {
				$posts_to_show = $posts_per_page;
			} else {
				$posts_to_show = get_option( 'posts_per_page' );
			}

			$query->set( 'posts_per_page', $posts_to_show );
		}
	}
}

if ( ! function_exists( 'nevo_category_color' ) ) :

	/**
	 * Getting Category Color.
	 *
	 * @param int $wp_category_id Category id.
	 *
	 * @return string The category color.
	 */
	function nevo_category_color( $wp_category_id ) {

		$args     = array(
			'orderby'    => 'id',
			'hide_empty' => 0,
		);
		$category = get_categories( $args );

		foreach ( $category as $category_list ) {
			$color = Nevo()->get_setting( 'nevo_category_color_' . $wp_category_id );

			return $color;
		}

	}

endif;