<?php
/**
 * Jetpack Compatibility File
 * See: http://jetpack.com/
 *
 * @package Gateway
 */

function gateway_jetpack_setup() {

	/**
	 * Add theme support for Infinite Scroll.
	 * See: http://jetpack.com/support/infinite-scroll/
	 */
	add_theme_support( 'infinite-scroll', array(
		'container'      => 'main',
		'footer'         => 'page',
		'footer_widgets' => array( 'footer-1', 'footer-2', 'footer-3' ),
		'render'         => 'gateway_infinite_scroll_render',
	) );

	/*
	 * Enable support for Responsive Videos.
	 * http://jetpack.com/support/responsive-videos/
	 */
	add_theme_support( 'jetpack-responsive-videos' );

	/*
	 * Enable support for Site Logo.
	 * http://jetpack.com/support/site-logo/
	 */
	add_image_size( 'gateway-site-logo', '300', '300' );
	add_theme_support( 'site-logo', array( 'size' => 'gateway-site-logo' ) );

	/*
	 * Enable support for Featured Content.
	 * http://jetpack.com/support/featured-content/
	 */
	add_theme_support( 'featured-content', array(
		'filter'     => 'gateway_get_featured_posts',
		'max_posts'  => 3,
		'post_types' => array( 'post', 'page' ),
	) );

	/*
	 * Enable support for Excerpts on Pages since Featured Content is enabled for Pages.
	 * See http://codex.wordpress.org/Excerpt
	 */
	add_post_type_support( 'page', 'excerpt' );

	/*
	 * Enable support for Content Options.
	 * http://jetpack.com/support/content-options/
	 */
	add_theme_support( 'jetpack-content-options', array(
		'blog-display'    => 'excerpt',
		'post-details'    => array(
			'stylesheet' => 'gateway-parent-styles',
			'date'       => '.entry-date, .posted-on',
			'categories' => '.cat-links',
			'tags'       => '.tags-links',
			'author'     => '.byline',
		),
		'featured-images' => array(
			'archive'          => true,
			'page'             => true,
			'fallback'         => true,
			'fallback-default' => false,
		),
	) );
}
add_action( 'after_setup_theme', 'gateway_jetpack_setup' );

function gateway_infinite_scroll_render() {
	while( have_posts() ) {
		the_post();
		get_template_part( 'template-parts/content', get_post_format() );
	}
}

function gateway_get_featured_posts() {
    return apply_filters( 'gateway_get_featured_posts', array() );
}

function gateway_has_featured_posts( $minimum = 1 ) {
    if ( is_paged() )
        return false;

    $minimum = absint( $minimum );
    $featured_posts = apply_filters( 'gateway_get_featured_posts', array() );

    if ( ! is_array( $featured_posts ) )
        return false;

    if ( $minimum > count( $featured_posts ) )
        return false;

    return true;
}

/**
 * Custom function to check for a post thumbnail;
 * If Jetpack is not available, fall back to has_post_thumbnail()
 */
function gateway_has_post_thumbnail( $post = null ) {
	if ( function_exists( 'jetpack_has_featured_image' ) ) {
		return jetpack_has_featured_image( $post );
	} else {
		return has_post_thumbnail( $post );
	}
}
