<?php
/**
 * Jetpack Compatibility File
 * See: http://jetpack.me/
 *
 * @package Organic Startup
 * @since Organic Startup 1.0
 */

/**
 * Add support for Jetpack's Featured Content and Infinite Scroll
 */

if ( ! function_exists( 'organic_startup_jetpack_setup' ) ) :

	function organic_startup_jetpack_setup() {

		// See: http://jetpack.me/support/infinite-scroll/ for more.
		add_theme_support(
			'infinite-scroll', array(
				'container'      => 'infinite-container',
				'wrapper'        => false,
				'posts_per_page' => '12',
				'render'         => 'organic_startup_render_infinite',
				'footer_widgets' => array( 'footer' ),
				'footer'         => 'wrap',
			)
		);

		// Add theme support for CPT.
		add_theme_support( 'jetpack-portfolio' );
		add_theme_support( 'jetpack-testimonial' );

	}
endif;
add_action( 'after_setup_theme', 'organic_startup_jetpack_setup' );

/**
 * Infinite Scroll: function for rendering posts
 */

if ( ! function_exists( 'organic_startup_render_infinite' ) ) :

	function organic_startup_render_infinite() {

		if ( class_exists( 'WooCommerce' ) && ( is_shop() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
			organic_startup_woocommerce_product_columns_wrapper();
			woocommerce_product_loop_start();
		} elseif ( is_archive() || is_search() ) {
			echo '<div class="infinite-rendered">';
		}

		if ( class_exists( 'WooCommerce' ) && ( is_shop() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
			while ( have_posts() ) :
				the_post();
				wc_get_template_part( 'content', 'product' );
			endwhile; // end of the loop.
		} else {
			get_template_part( 'content/loop', 'archive' );
		}

		if ( class_exists( 'WooCommerce' ) && ( is_shop() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
			woocommerce_product_loop_end();
			organic_startup_woocommerce_product_columns_wrapper_close();
		} elseif ( is_archive() || is_search() ) {
			echo '</div>';
		}

	}
endif;

/**
 * Enables Jetpack's Infinite Scroll in archives, but not on home blog.
 *
 * @return bool
 */

if ( ! function_exists( 'organic_startup_jetpack_infinite_scroll_supported' ) ) :
	function organic_startup_jetpack_infinite_scroll_supported() {
		return current_theme_supports( 'infinite-scroll' ) && ( is_archive() || is_search() ) && ! is_home();
	}
endif;
add_filter( 'infinite_scroll_archive_supported', 'organic_startup_jetpack_infinite_scroll_supported' );
