<?php
/**
 * Jetpack Compatibility File.
 *
 * @link https://jetpack.me/
 *
 * @package Trade_Line
 */

/**
 * Add theme support for Infinite Scroll.
 *
 * @since 1.0.0
 *
 * See: https://jetpack.me/support/infinite-scroll/
 * See: https://jetpack.me/support/responsive-videos/
 */
function trade_line_jetpack_setup() {
	$pagination_type = trade_line_get_option( 'pagination_type' );
	if ( in_array( $pagination_type, array( 'infinite-scroll-click', 'infinite-scroll' ) ) ) {
		$type = ( 'infinite-scroll-click' === $pagination_type ) ? 'click' : 'scroll' ;
		add_theme_support( 'infinite-scroll', array(
			'type'           => $type,
			'container'      => 'main',
			'footer'         => 'page',
			'wrapper'        => false,
			'render'         => 'trade_line_infinite_scroll_render',
			'footer_widgets' => array( 'footer-1', 'footer-2', 'footer-3', 'footer-4' )
		) );
	}
	// Add theme support for Responsive Videos.
	add_theme_support( 'jetpack-responsive-videos' );
	// Add theme support for Jetpack Portfolios.
	add_theme_support( 'jetpack-portfolio' );
	// Add theme support for Jetpack Testimonials.
	add_theme_support( 'jetpack-testimonial' );
}
add_action( 'after_setup_theme', 'trade_line_jetpack_setup' );

/**
 * Custom render function for Infinite Scroll.
 *
 * @since 1.0.0
 */
function trade_line_infinite_scroll_render() {
	while ( have_posts() ) {
		the_post();
		if ( is_search() ) :
		    get_template_part( 'template-parts/content', 'search' );
		else :
		    get_template_part( 'template-parts/content', get_post_format() );
		endif;
	}
}


/**
 * Load Jetpack scripts.
 *
 * @since 1.0.0
 */
function trade_line_jetpack_scripts() {

	if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'custom-content-types' ) && post_type_exists( 'jetpack-portfolio' ) ) {

		$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

		wp_enqueue_script( 'jquery-isotope', get_template_directory_uri() . '/third-party/isotope/js/isotope.pkgd' . $min . '.js', array( 'jquery' ), '2.2.2', true );

		wp_enqueue_script( 'trade-line-jetpack', get_template_directory_uri() . '/js/jetpack' . $min . '.js', array( 'jquery' ), '2.0.0', true );
	}
}
add_action( 'wp_enqueue_scripts', 'trade_line_jetpack_scripts' );
