<?php
/**
 * Display related functions
 *
 * @package Meridian One
 * @since 1.0
 */

/**
 * Table Of Contents
 *
 * meridian_one_the_custom_logo ( Displays custom logo )
 * meridian_one_content_class ( Classes for #content )
 * meridian_one_pingback_header ( Add a pingback url auto-discovery header for singularly identifiable articles )
 * meridian_one_excerpt_more ( Continue reading for excerpt )
 * meridian_one_post_content_first_url ( Get the first URL in the post content )
 * meridian_one_home_section_blog_display_posts ( Outputs blog posts listing on homepage )
 */

if ( ! function_exists( 'meridian_one_the_custom_logo' ) ) :

	/**
	 * Displays custom logo
	 *
	 * @since 1.0
	 */
	function meridian_one_the_custom_logo() {
		if ( function_exists( 'the_custom_logo' ) ) {
			the_custom_logo();
		}
	}

endif; // End if meridian_one_the_custom_logo exists

if ( ! function_exists( 'meridian_one_content_class' ) ) :

	/**
	 * Classes for #content
	 *
	 * @since 1.0
	 */
	function meridian_one_content_class( $extra_classes = '' ) {

		// to store classes
		$classes = array();

		// if has sidebar make it a column
		if ( is_active_sidebar( 'sidebar-1' ) && ! meridian_one_woocommerce_is_checkout() && ! meridian_one_woocommerce_is_account() ) {
			$classes[] = 'col';
			$classes[] = 'col-8';
		}

		// if there are classes generate and output
		if ( count( $classes ) ) {

			// generate the classes string
			$classes_string = '';
			foreach( $classes as $class ) {
				$classes_string .= $class . ' ';
			}

			// append the extra classes
			$classes_string .= $extra_classes;

			// output
			echo 'class="' . esc_attr( $classes_string ) .'"';

		// if no classes return
		} else {
			return;
		}

	}

endif; // end if meridian_one_content_class exists

if ( ! function_exists( 'meridian_one_pingback_header' ) ) :

	/**
	 * Add a pingback url auto-discovery header for singularly identifiable articles.
	 *
	 * @since 1.0
	 */
	function meridian_one_pingback_header() {
		if ( is_singular() && pings_open() ) {
			echo '<link rel="pingback" href="' . esc_url( get_bloginfo( 'pingback_url' ) ) . '">';
		}
	}

endif; // end if meridian_one_pingback_header exists
add_action( 'wp_head', 'meridian_one_pingback_header' );

if ( ! function_exists( 'meridian_one_excerpt_more' ) ) :

	/**
	 * Continue reading for excerpt
	 *
	 * @since 1.0
	 */
	function meridian_one_excerpt_more( $more ) {
		global $post;
		return '<div class="blog-post-read-more"><a href="' . esc_url( get_permalink($post->ID) ) . '">' . esc_html__( 'Continue reading', 'meridian-one' ) . '</a></div>';
	} add_filter('excerpt_more', 'meridian_one_excerpt_more');

endif; // end if meridian_one_excerpt_more exists

if ( ! function_exists( 'meridian_one_post_content_first_url' ) ) :

	/**
	 * Get the first URL in the post content
	 *
	 * @since 1.0
	 */
	function meridian_one_post_content_first_url() {

		// get post contet
		$post_content = get_the_content();

		// check for URL
		$the_url = get_url_in_content( $post_content );

		// pass it back
		return ( $the_url ) ? $the_url : apply_filters( 'the_permalink', get_permalink() );

	}

endif; // end if meridian_one_post_content_first_url exists

if ( ! function_exists( 'meridian_one_home_section_blog_display_posts' ) ) :

	/**
	 * Outputs blog posts listing on homepage
	 *
	 * @since 1.0
	 */
	function meridian_one_home_section_blog_display_posts( $category = false ) {

		// query args
		$args = array(
			'posts_per_page' => 3,
			'ignore_sticky_posts' => true,
			'no_found_rows' => true,
		);

		if ( is_numeric( $category ) ) {
			$category = intval( $category );
			$args['tax_query'] = array(
				array(
					'taxonomy' => 'category',
					'terms' => array( $category ),
					'field' => 'term_id'
				),
			);
		}

		// query posts
		$meridian_one_query = new WP_Query( $args );

		// if there are posts
		if ( $meridian_one_query->have_posts() ) :

			// count
			$count = 0;

			?><div class="section-blog-items col col-9 col-last clearfix"><?php					

					// loop posts
					while ( $meridian_one_query->have_posts() ) : $meridian_one_query->the_post(); $count++; ?>

						<div class="section-blog-item col col-4 <?php if ( $count == 3 ) echo 'col-last'; ?>">
							<?php if ( has_post_thumbnail() ) : ?>
								<div class="section-blog-item-thumb">
									<?php the_post_thumbnail( 'meridian-one-fourth' ); ?>
									<div class="section-blog-item-thumb-cats">
										<?php the_category( ' ' ); ?>
									</div><!-- .section-blog-item-thumb-cats -->
								</div><!-- .section-blog-item-thumb -->
							<?php endif; ?>
							<h2 class="section-blog-item-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
							<span class="section-blog-item-extra"><?php the_time( get_option( 'date_format' ) ); ?></span>
						</div><!-- .section-blog-item -->

					<?php endwhile;

			?></div><!-- .section-blog-items --><?php

		// finish if posts
		endif; 

		// reset query
		wp_reset_postdata();

	}

endif; 