<?php
/**
 * Theme Setup
 * This file is loaded using 'after_setup_theme' hook at priority 10
 *
 * @package    Hoot
 * @subpackage Hoot Ubix
 */

/** Misc **/

// Enable Font Icons
// Disable this (remove this line) if the theme doesnt use font icons,
// or if the font-awesome library is being enqueued by some other plugin using
// a handle other than 'font-awesome' or 'fontawesome' (to avoid loading the
// library twice)
if ( apply_filters( 'hootubix_load_font_awesome', true ) )
	add_theme_support( 'font-awesome' );

// Enable google fonts (fixed fonts, or entire library)
add_theme_support( 'hootubix-google-fonts' );


/** WordPress **/

// Add theme support for WordPress Custom Logo
add_theme_support( 'custom-logo' );

// Adds theme support for WordPress 'featured images'.
add_theme_support( 'post-thumbnails' );

// Automatically add feed links to <head>.
add_theme_support( 'automatic-feed-links' );

// Reintroduce emoji support (removed by hybrid framework)
if ( function_exists( 'print_emoji_styles' ) ) {
	add_action('wp_head', 'print_emoji_detection_script', 7);
	add_action('wp_print_styles', 'print_emoji_styles');
}

/** WordPress Jetpack **/
add_theme_support( 'infinite-scroll', array(
	'type' => apply_filters( 'hootubix_theme_jetpack_infinitescroll_type', 'click' ), // scroll or click
	'container' => apply_filters( 'hootubix_theme_jetpack_infinitescroll_container', 'content' ),
	'footer' => false,
	'wrapper' => true,
	'render' => apply_filters( 'hootubix_theme_jetpack_infinitescroll_render', 'hootubix_jetpack_infinitescroll_render' ),
) );
add_filter( 'jetpack_lazy_images_blacklisted_classes', 'hootubix_theme_jetpack_lazy_load_exclude' );
function hootubix_theme_jetpack_lazy_load_exclude( $classes ) {
	if ( !is_array( $classes ) ) $classes = array();
	$classes[] = 'hootslider-html-slide-img';
	$classes[] = 'hootslider-html-slide-image';
	$classes[] = 'hootslider-image-slide-img';
	$classes[] = 'hootslider-carousel-slide-img';
	return $classes;
}


/** Extensions **/

// Enable custom widgets
add_theme_support( 'hybridextend-widgets' );

// Bug fix for transition on empty ids
// (no need to apply filter 'hybridextend_load_widgets' for adding HYBRIDEXTEND_PREMIUM_INC . 'admin/widget-*.php' to the locations)
foreach ( glob( HYBRIDEXTEND_INC . 'admin/widget-*.php' ) as $filename ) {
	add_filter( 'hootubix_' . str_replace( '-', '_', str_replace( 'widget-', '', basename( $filename, '.php' ) ) ) . '_widget_settings', 'hootubix_filter_wdgid' );
}
function hootubix_filter_wdgid( $id ) {
	if ( isset ( $id['id'] ) && isset( $id['form_options'] ) ) {
		$id['id'] = str_replace( 'hootubix-', 'hoot' . '-', $id['id'] );
		foreach ( $id['form_options'] as $key => $fields ) {
			if ( isset( $fields['id'] ) && $fields['id'] == 'customcss' ) {
				foreach ( $fields['fields'] as $subkey => $field ) {
					if ( isset( $field['id'] ) && $field['id'] == 'widgetid' ) {
						$id['form_options'][$key]['fields'][$subkey]['type'] = str_replace( 'hootubix-', 'hoot' . '-', $field['type'] );
	}	}	}	}	}
	return $id;
}
add_filter( 'hybridextend_custom_image_sizes', 'hootubix_filter_imgid', 99 );
function hootubix_filter_imgid( $cimg ) {
	if ( is_array( $cimg ) ) {
		$cim = array();
		foreach ( $cimg as $key => $value ) {
			$id = str_replace( 'hootubix-', 'hoot' . '-', $key );
			$cim[ $id ] = $value;
		}
		$cimg = $cim;
	}
	return $cimg;
}

// Nicer [gallery] shortcode implementation when Jetpack tiled-gallery is not active
if ( !class_exists( 'Jetpack' ) || !Jetpack::is_module_active( 'tiled-gallery' ) ) 
	add_theme_support( 'cleaner-gallery' );


/** WooCommerce **/

// Woocommerce support and init load theme woo functions
if ( class_exists( 'WooCommerce' ) ) {
	add_theme_support( 'woocommerce' );
	include_once( HYBRID_PARENT . 'woocommerce/functions.php' );
}


/** HootKit **/

/**
 * Register HootKit Features
 *
 * @since 1.7
 * @return void
 */
function hootubix_theme_register_hootkit( $config ) {
	// Array of configuration settings.
	$config = array(
		'nohoot'    => false,
		'theme_css' => true,
	);
	$config['modules'] = array();
	if ( apply_filters( 'hootubix_theme_support_ocdi', true ) ) {
		$config['modules']['importer'] = array( array(
				'import_file_name'           => __( 'Hoot Ubix Demo', 'hoot-ubix' ),
				'import_file_url'            => 'https://demo.wphoot.com/downloads/hoot-ubix-content.xml',
				'import_widget_file_url'     => 'https://demo.wphoot.com/downloads/hoot-ubix-widgets.wie',
				'import_customizer_file_url' => 'https://demo.wphoot.com/downloads/hoot-ubix-customize.dat',
				'import_preview_image_url'   => HYBRID_PARENT_URI . 'screenshot.jpg',
				/* Translators: The %s are placeholders for HTML, so the order can't be changed. */
				'import_notice'              => sprintf( esc_html__( 'You are using the free version of the theme.%1$sSome features (available only in the premium version) will not get imported - You may see %2$s"Could not import"%3$s message for these features in the log once the installation is finished. You can safely ignore these messages.', 'hoot-ubix' ), '<br />', '<em>', '</em>' ),
				'preview_url'                => 'https://demo.wphoot.com/hoot-ubix/',
			), );
	}
	return $config;
}
add_filter( 'hootkit_register', 'hootubix_theme_register_hootkit', 5 );


/* === Tribe The Events Calendar Plugin === */

// Load support if plugin active
if ( class_exists( 'Tribe__Events__Main' ) ) {

	// Hook into 'wp' to use conditional hooks
	add_action( 'wp', 'hootubix_tribeevent', 10 );

	// Add hooks based on view
	// @since 1.7.3
	function hootubix_tribeevent() {
		if ( is_post_type_archive( 'tribe_events' ) || ( function_exists( 'tribe_is_events_home' ) && tribe_is_events_home() ) ) {
			add_filter( 'theme_mod_archive_type', 'hootubix_tribeevent_archivetype', 5 );
			add_filter( 'theme_mod_archive_post_content', 'hootubix_tribeevent_archive', 5 );
			add_filter( 'theme_mod_archive_post_meta', 'hootubix_tribeevent_archive_postmeta', 5 );
			add_action( 'hootubix_display_loop_meta', 'hootubix_tribeevent_loopmeta', 5 );
		}
		if ( is_singular( 'tribe_events' ) ) {
			add_action( 'hootubix_display_loop_meta', 'hootubix_tribeevent_loopmeta_single', 5 );
		}
	}

	// Modify theme options and displays
	// @since 1.7.3
	function hootubix_tribeevent_archivetype( $type ) { return 'big'; }
	function hootubix_tribeevent_archive( $content ) { return 'full-content'; }
	function hootubix_tribeevent_archive_postmeta( $args ) { return ''; }
	function hootubix_tribeevent_loopmeta( $display ) { return false; }
	function hootubix_tribeevent_loopmeta_single( $display ) {
		the_post(); rewind_posts(); // Bug Fix
		return false;
	}

}


/** Conditional Theme Setup */

/* Theme setup on the 'wp' hook. Only used for special scenarios (like enqueueing scripts/styles) based on conditional tags. */
add_action( 'wp', 'hootubix_load_wt_lightslider', 10 );

/**
 * Load lightslider (scripts/styles) on frontpage
 *
 * @since 1.0
 * @access public
 * @return void
 */
function hootubix_load_wt_lightslider() {
	if ( is_front_page() ) {
		add_theme_support( 'hootubix-light-slider' );
	}
}


/** Theme Setup Hooks */

/* Handle content width for embeds and images. Hooked into 'init' so that we can pull custom content width from theme options */
add_action( 'init', 'hootubix_set_content_width', 10 );

/**
 * Handle content width for embeds and images.
 *
 * @since 1.0
 * @access public
 * @return void
 */
function hootubix_set_content_width() {
	$width = intval( hootubix_get_mod( 'site_width' ) );
	$width = !empty( $width ) ? $width : 1260;
	hybrid_set_content_width( $width );
}

/* Modify the '[...]' Read More Text */
add_filter( 'the_content_more_link', 'hootubix_modify_read_more_link' );
if ( apply_filters( 'hootubix_force_excerpt_readmore', true ) ) {
	add_filter( 'excerpt_more', 'hootubix_insert_excerpt_readmore_quicktag', 11 );
	add_filter( 'wp_trim_excerpt', 'hootubix_replace_excerpt_readmore_quicktag', 11, 2 );
} else {
	add_filter( 'excerpt_more', 'hootubix_modify_read_more_link' );
}

/**
 * Modify the '[...]' Read More Text
 *
 * @since 1.0
 * @access public
 * @return string
 */
function hootubix_modify_read_more_link( $more = '[&hellip;]' ) {
	if ( is_admin() )
		return $more;

	$read_more = esc_html( hootubix_get_mod('read_more') );
	$read_more = ( empty( $read_more ) ) ? sprintf( __( 'Read More %s', 'hoot-ubix' ), '&rarr;' ) : $read_more;
	global $post;
	$read_more = '<span class="more-link"><a href="' . esc_url( get_permalink( $post->ID ) ) . '">' . $read_more . '</a></span>';
	return apply_filters( 'hootubix_readmore', $read_more ) ;
}

/**
 * Always display the 'Read More' link in Excerpts.
 * Insert quicktag to be replaced later in 'wp_trim_excerpt()'
 *
 * @since 1.0
 * @access public
 * @return string
 */
function hootubix_insert_excerpt_readmore_quicktag( $more = '' ) {
	if ( is_admin() )
		return $more;
	return '<!--hootubix-read-more-quicktag-->';
}

/**
 * Always display the 'Read More' link in Excerpts.
 * Replace quicktag with read more link
 *
 * @since 1.0
 * @access public
 * @return string
 */
function hootubix_replace_excerpt_readmore_quicktag( $text, $raw_excerpt ) {
	if ( is_admin() )
		return $text;
	$read_more = hootubix_modify_read_more_link();
	$text = str_replace( '<!--hootubix-read-more-quicktag-->', '', $text );
	return $text . $read_more;
}

/* Modify the exceprt length. Make sure to set the priority correctly such as 999, else the default WordPress filter on this function will run last and override settng here.  */
add_filter( 'excerpt_length', 'hootubix_custom_excerpt_length', 999 );

/**
 * Modify the exceprt length.
 *
 * @since 1.0
 * @access public
 * @return void
 */
function hootubix_custom_excerpt_length( $length ) {
	if ( is_admin() )
		return $length;

	$excerpt_length = intval( hootubix_get_mod('excerpt_length') );
	if ( !empty( $excerpt_length ) )
		return $excerpt_length;
	return 105;
}

/**
 * Register recommended plugins via TGMPA
 *
 * @since 1.7
 * @return void
 */
function hootubix_theme_tgmpa_plugins() {
	// Array of plugin arrays. Required keys are name and slug.
	// Since source is from the .org repo, it is not required.
	$plugins = apply_filters( 'hootubix_theme_tgmpa_plugins', array(
		array(
			'name'     => __( '(HootKit) Hoot Ubix 1-Click Content Installation', 'hoot-ubix' ),
			'slug'     => 'hootkit',
			'required' => false,
		),
	) );
	// Array of configuration settings.
	$config = array(
		'is_automatic' => true,
	);
	// Register plugins with TGM_Plugin_Activation class
	tgmpa( $plugins, $config );
}
add_filter( 'tgmpa_register', 'hootubix_theme_tgmpa_plugins' );