<?php
/**
 * Custom functions that act independently of the theme templates
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package Signify
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @since 1.0.0
 *
 * @param array $classes Classes for the body element.
 * @return array (Maybe) filtered body classes.
 */
function signify_body_classes( $classes ) {
	// Adds a class of custom-background-image to sites with a custom background image.
	if ( get_background_image() ) {
		$classes[] = 'custom-background-image';
	}

	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Always add a front-page class to the front page.
	if ( is_front_page() && ! is_home() ) {
		$classes[] = 'page-template-front-page';
	}

	// Adds a class of (full-width|box) to blogs.
	if ( 'boxed' === get_theme_mod( 'signify_layout_type' ) ) {
		$classes[] = 'boxed-layout';
	} else {
		$classes[] = 'fluid-layout';
	}
	$menu = get_theme_mod( 'signify_menu_type' );
	// Adds a class of navigation-(default|classic) to blogs.
	if ( 'classic' === get_theme_mod( 'signify_menu_type', 'classic' ) ) {
		$classes[] = 'navigation-classic';
	} else {
		$classes[] = 'navigation-default';
	}

	//Adds a class when sticky playlist is enabled
	$sticky_playlist = get_theme_mod( 'signify_sticky_playlist_visibility' , 'disabled' );

	if( 'disabled' !== $sticky_playlist ) {
		$classes[] = 'sticky-playlist-enabled';
	}

	// Adds a class with respect to layout selected.
	$layout  = signify_get_theme_layout();
	$sidebar = signify_get_sidebar_id();

	$layout_class = "no-sidebar content-width-layout";

	if ( 'no-sidebar-full-width' === $layout ) {
		$layout_class = 'no-sidebar full-width-layout';
	} elseif ( 'left-sidebar' === $layout ) {
		if ( '' !== $sidebar ) {
			$layout_class = 'two-columns-layout content-right';
		}
	} elseif ( 'right-sidebar' === $layout ) {
		if ( '' !== $sidebar ) {
			$layout_class = 'two-columns-layout content-left';
		}
	}

	$classes[] = $layout_class;

	$classes[] = esc_attr( get_theme_mod( 'signify_content_layout', 'excerpt' ) );

	$classes[] = 'header-media-fluid';

	$enable_slider = signify_check_section( get_theme_mod( 'signify_slider_option', 'disabled' ) );

	$header_image = signify_featured_overall_image();

	if ( 'disable' !== $header_image ) {
		$classes[] = 'has-header-media';
	}

	if ( $enable_slider || 'disable' !== $header_image ) {
		$classes[] = 'absolute-header';
	}

	if ( ! signify_has_header_media_text() ) {
		$classes[] = 'header-media-text-disabled';
	}

	// Add a class if there is a custom header.
	if ( has_header_image() ) {
		$classes[] = 'has-header-image';
	}

	$color_scheme = get_theme_mod( 'color_scheme', 'default' ) ;

	if( 'tune' === $color_scheme || 'music-dark' === $color_scheme ) {
		$classes[] = 'color-scheme-music';
	}

	// Added color scheme to body class.
	$classes['color-scheme'] = 'color-scheme-' . esc_attr( $color_scheme );

	if ( 'wedding' === $color_scheme ||  'photography' === $color_scheme || 'corporate' === $color_scheme || 'tune' === $color_scheme || 'music-dark' === $color_scheme ) {
		$classes['absolute-header'] = 'transparent-header-color-scheme';
	}

	$classes['header-layout'] = esc_attr( get_theme_mod( 'signify_header_layout', 'header-style-one' ) );

	// Add Menu Type.
	$classes[] = 'menu-type-' . esc_attr( get_theme_mod( 'signify_menu_type', 'classic' ) );

	if ( get_theme_mod( 'signify_display_header_top' ) ) {
		$classes['header-top'] = 'has-header-top';
	}

	return $classes;
}
add_filter( 'body_class', 'signify_body_classes' );

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

if ( ! function_exists( 'signify_comments' ) ) :
	/**
	 * Enable/Disable Comments
	 *
	 * @uses comment_form_default_fields filter
	 * @since 1.0.0
	 */
	function signify_comments( $open, $post_id ) {
		$comment_select = get_theme_mod( 'signify_comment_option', 'use-wordpress-setting' );

		if ( 'disable-completely' === $comment_select ) {
			return false;
		} elseif ( 'disable-in-pages' === $comment_select && is_page() ) {
			return false;
		}

		return $open;
	}
endif; // signify_comments.
add_filter( 'comments_open', 'signify_comments', 10, 2 );

if ( ! function_exists( 'signify_comment_form_fields' ) ) :
	/**
	 * Modify Comment Form Fields
	 *
	 * @uses comment_form_default_fields filter
	 * @since 1.0.0
	 */
	function signify_comment_form_fields( $fields ) {
		$disable_website = get_theme_mod( 'signify_website_field', 1  );

		if ( isset( $fields['url'] ) && ! $disable_website ) {
			unset( $fields['url'] );
		}

		return $fields;
	}
endif; // signify_comment_form_fields.
add_filter( 'comment_form_default_fields', 'signify_comment_form_fields' );

/**
 * Adds font family custom CSS
 */
function signify_get_font_family_css() {
	$font_family_options = signify_font_family_options();

	$fonts = signify_avaliable_fonts();

	$css = array();

	foreach ( $font_family_options as $key => $value ) {
		$option = get_theme_mod( $key, $value['default'] );

		if ( $option !== $value['default'] ) {
			$css[] = $value['selector'] . ' { font-family: ' . $fonts [ $option ]['label'] . '; }';
		}
	}

	$css = implode( PHP_EOL, $css );

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_get_font_family_css', 11 );

/**
 * Adds events background CSS
 */
function signify_events_bg_css() {
	$enable   = get_theme_mod( 'signify_events_option', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_events_bg_image' );

	if ( $background ) {
		$css = '#events-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_events_bg_css', 11 );

/**
 * Adds testimonial background CSS
 */
function signify_testimonial_bg_css() {
	$enable   = get_theme_mod( 'signify_testimonial_option', 'disabled' );

	if ( ! signify_check_section( $enable ) || ( 'tune' != get_theme_mod( 'color_scheme' ) ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_testimonial_bg_image' );

	if ( $background ) {
		$css = '#testimonial-content-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_testimonial_bg_css', 11 );

/**
 * Adds custom overlay for Promotion Headline Background Image
 */
function signify_promo_head_bg_image_overlay_css() {
	$enable   = get_theme_mod( 'signify_promo_head_visibility', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$overlay = get_theme_mod( 'signify_promo_head_background_image_opacity', 0 );

	$css = '';

	$overlay_bg = $overlay / 100;

	if ( $overlay ) {
		$css = '.promotion-section .post-thumbnail-background:before {
					background-color: rgba(0, 0, 0, ' . esc_attr( $overlay_bg ) . ' );
				}'; // Dividing by 100 as the option is shown as % for user
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_promo_head_bg_image_overlay_css', 11 );

/**
 * Adds custom overlay for Contact Background Image
 */
function signify_contact_bg_css() {
	$enable   = get_theme_mod( 'signify_contact_option', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$background = '';

	$type  = get_theme_mod( 'signify_contact_type', 'page' );

	if ( 'post' === $type && 'publish' === get_post_status( get_theme_mod( 'signify_contact_post' ) ) ) {
		$background = get_the_post_thumbnail_url( get_theme_mod( 'signify_contact_post' ) );
	} elseif ( 'page' === $type  && 'publish' === get_post_status( get_theme_mod( 'signify_contact_page' ) ) ) {
		$background = get_the_post_thumbnail_url( get_theme_mod( 'signify_contact_page' ) );
	} elseif ( 'custom' === $type ) {
		$background = get_theme_mod( 'signify_contact_bg_image' );
	}

	$css = '';

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_contact_bg_css', 11 );

/**
 * Adds custom overlay for Header Media
 */
function signify_header_media_image_overlay_css() {
	$overlay = get_theme_mod( 'signify_header_media_image_opacity' );

	$css = '';

	$overlay_bg = $overlay / 100;

	if ( $overlay ) {
	$css = '.custom-header-overlay {
		background-color: rgba(255, 255, 255, ' . esc_attr( $overlay_bg ) . ' );
	} '; // Dividing by 100 as the option is shown as % for user
}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_header_media_image_overlay_css', 11 );

/**
 * Adds featured video background CSS
 */
function signify_featured_video_bg_css() {
	$enable   = get_theme_mod( 'signify_featured_video_option', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_featured_video_bg_image' );

	if ( $background ) {
		$css = '#featured-video-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_featured_video_bg_css', 11 );

/**
 * Adds portfolio background CSS
 */
function signify_portfolio_bg_css() {
	$enable   = get_theme_mod( 'signify_portfolio_option', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_portfolio_bg_image' );

	if ( $background ) {
		$css = '#portfolio-content-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_portfolio_bg_css', 11 );

/**
 * Adds Woo Products Showcase background CSS
 */
function signify_woo_product_showcase_bg_css() {
	$enable   = get_theme_mod( 'signify_woo_products_showcase_option', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_woo_products_showcase_bg_image' );

	if ( $background ) {
		$css = '#product-content-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_woo_product_showcase_bg_css', 11 );

/**
 * Adds team background CSS
 */
function signify_team_bg_css() {
	$enable   = get_theme_mod( 'signify_team_option', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_team_bg_image' );

	if ( $background ) {
		$css = '#team-content-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_team_bg_css', 11 );

/**
 * Remove first post from blog as it is already show via recent post template
 */
function signify_alter_home( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
		$cats = get_theme_mod( 'signify_front_page_category' );

		if ( is_array( $cats ) && ! in_array( '0', $cats ) ) {
			$query->query_vars['category__in'] = $cats;
		}

		if ( get_theme_mod( 'signify_exclude_slider_post' ) ) {
			$quantity = get_theme_mod( 'signify_slider_number', 2 );

			$post_list	= array();	// list of valid post ids

			for( $i = 1; $i <= $quantity; $i++ ){
				if ( get_theme_mod( 'signify_slider_post_' . $i ) && get_theme_mod( 'signify_slider_post_' . $i ) > 0 ) {
					$post_list = array_merge( $post_list, array( get_theme_mod( 'signify_slider_post_' . $i ) ) );
				}
			}

			if ( ! empty( $post_list ) ) {
				$query->query_vars['post__not_in'] = $post_list;
			}
		}
	}
}
add_action( 'pre_get_posts', 'signify_alter_home' );

/**
 * Function to add Scroll Up icon
 */
function signify_scrollup() {
	$disable_scrollup =  ! get_theme_mod( 'signify_disable_scrollup', 1 );

	if ( $disable_scrollup ) {
		return;
	}

	echo '<a href="#masthead" id="scrollup" class="backtotop">' . '<span class="screen-reader-text">' . esc_html__( 'Scroll Up', 'signify-pro' ) . '</span></a>' ;

}
add_action( 'wp_footer', 'signify_scrollup', 1 );

if ( ! function_exists( 'signify_content_nav' ) ) :
	/**
	 * Display navigation/pagination when applicable
	 *
	 * @since 1.0.0
	 */
	function signify_content_nav() {
		global $wp_query;

		// Don't print empty markup in archives if there's only one page.
		if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) ) {
			return;
		}

		$pagination_type = get_theme_mod( 'signify_pagination_type', 'default' );

		if ( ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'infinite-scroll' ) ) || class_exists( 'Catch_Infinite_Scroll' ) ) {
			// Support infinite scroll plugins.
			the_posts_navigation();
		} elseif ( 'numeric' === $pagination_type && function_exists( 'the_posts_pagination' ) ) {
			the_posts_pagination( array(
				'prev_text'          => '<span>' . esc_html__( 'Prev', 'signify-pro' ) . '</span>',
				'next_text'          => '<span>' . esc_html__( 'Next', 'signify-pro' ) . '</span>',
				'screen_reader_text' => '<span class="nav-subtitle screen-reader-text">' . esc_html__( 'Page', 'signify-pro' ) . ' </span>',
			) );
		} else {
			the_posts_navigation();
		}
	}
endif; // signify_content_nav

/**
 * Check if a section is enabled or not based on the $value parameter
 * @param  string $value Value of the section that is to be checked
 * @return boolean return true if section is enabled otherwise false
 */
function signify_check_section( $value ) {
	return ( 'entire-site' == $value  || ( is_front_page() && 'homepage' === $value ) );
}

/**
 * Return the first image in a post. Works inside a loop.
 * @param [integer] $post_id [Post or page id]
 * @param [string/array] $size Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32).
 * @param [string/array] $attr Query string or array of attributes.
 * @return [string] image html
 *
 * @since 1.0.0
 */
function signify_get_first_image( $postID, $size, $attr, $src = false ) {
	ob_start();

	ob_end_clean();

	$image 	= '';

	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post_field('post_content', $postID ) , $matches);

	if ( isset( $matches[1][0] ) ) {
		// Get first image.
		$first_img = $matches[1][0];

		if ( $src ) {
			//Return url of src is true
			return $first_img;
		}

		return '<img class="wp-post-image" src="'. esc_url( $first_img ) .'">';
	}

	return false;
}

function signify_get_theme_layout() {
	$layout = '';

	if ( is_page_template( 'templates/no-sidebar.php' ) ) {
		$layout = 'no-sidebar';
	} elseif ( is_page_template( 'templates/full-width-page.php' ) ) {
		$layout = 'no-sidebar-full-width';
	} elseif ( is_page_template( 'templates/left-sidebar.php' ) ) {
		$layout = 'left-sidebar';
	} elseif ( is_page_template( 'templates/right-sidebar.php' ) ) {
		$layout = 'right-sidebar';
	} else {
		$layout = get_theme_mod( 'signify_default_layout', 'right-sidebar' );

		if ( is_home() || is_archive() ) {
			$layout = get_theme_mod( 'signify_homepage_archive_layout', 'right-sidebar' );
		}

		if ( class_exists( 'WooCommerce' ) && ( is_shop() || is_woocommerce() || is_cart() || is_checkout() ) ) {
			$layout = get_theme_mod( 'signify_woocommerce_layout', 'right-sidebar' );
		}
	}

	return $layout;
}

function signify_get_sidebar_id() {
	$sidebar = '';

	$layout = signify_get_theme_layout();

	$sidebaroptions = '';

	if ( 'no-sidebar-full-width' === $layout || 'no-sidebar' === $layout ) {
		return $sidebar;
	}

	// WooCommerce Shop Page excluding Cart and checkout.
	if ( class_exists( 'WooCommerce' ) && is_woocommerce() ) {
		$shop_id        = get_option( 'woocommerce_shop_page_id' );
		$sidebaroptions = get_post_meta( $shop_id, 'signify-sidebar-option', true );
	} else {
		global $post, $wp_query;

		// Front page displays in Reading Settings.
		$page_on_front  = get_option( 'page_on_front' );
		$page_for_posts = get_option( 'page_for_posts' );

		// Get Page ID outside Loop.
		$page_id = $wp_query->get_queried_object_id();
		// Blog Page or Front Page setting in Reading Settings.
		if ( $page_id == $page_for_posts || $page_id == $page_on_front ) {
	        $sidebaroptions = get_post_meta( $page_id, 'signify-sidebar-option', true );
	    } elseif ( is_singular() ) {
	    	if ( is_attachment() ) {
				$parent 		= $post->post_parent;
				$sidebaroptions = get_post_meta( $parent, 'signify-sidebar-option', true );

			} else {
				$sidebaroptions = get_post_meta( $post->ID, 'signify-sidebar-option', true );
			}
		}
	}

	if ( is_active_sidebar( 'sidebar-woo' ) && class_exists( 'WooCommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() ) ) {
		$sidebar = 'sidebar-woo'; // WooCommerce Sidebar.
	} elseif ( is_active_sidebar( 'sidebar-optional-one' ) && 'optional-sidebar-one' === $sidebaroptions ) {
		$sidebar = 'sidebar-optional-one';
	} elseif ( is_active_sidebar( 'sidebar-optional-two' ) && 'optional-sidebar-two' === $sidebaroptions ) {
		$sidebar = 'sidebar-optional-two';
	} elseif ( is_active_sidebar( 'sidebar-optional-three' ) && 'optional-sidebar-three' === $sidebaroptions ) {
		$sidebar = 'sidebar-optional-three';
	} elseif ( is_active_sidebar( 'sidebar-optional-homepage' ) && ( is_front_page() || ( is_home() && $page_id != $page_for_posts ) ) ) {
		$sidebar = 'sidebar-optional-homepage';
	} elseif ( is_active_sidebar( 'sidebar-optional-archive' ) && ( is_archive() || ( is_home() && $page_id != $page_for_posts ) ) ) {
		$sidebar = 'sidebar-optional-archive';
	} elseif ( is_page() && is_active_sidebar( 'sidebar-optional-page' ) ) {
		$sidebar = 'sidebar-optional-page';
	} elseif ( is_single() && is_active_sidebar( 'sidebar-optional-post' ) ) {
		$sidebar = 'sidebar-optional-post';
	} elseif ( is_active_sidebar( 'sidebar-1' ) ) {
		$sidebar = 'sidebar-1'; // Primary Sidebar.
	}

	return $sidebar;
}

if ( ! function_exists( 'signify_truncate_phrase' ) ) :
	/**
	 * Return a phrase shortened in length to a maximum number of characters.
	 *
	 * Result will be truncated at the last white space in the original string. In this function the word separator is a
	 * single space. Other white space characters (like newlines and tabs) are ignored.
	 *
	 * If the first `$max_characters` of the string does not contain a space character, an empty string will be returned.
	 *
	 * @since 1.0.0
	 *
	 * @param string $text            A string to be shortened.
	 * @param integer $max_characters The maximum number of characters to return.
	 *
	 * @return string Truncated string
	 */
	function signify_truncate_phrase( $text, $max_characters ) {

		$text = trim( $text );

		if ( mb_strlen( $text ) > $max_characters ) {
			//* Truncate $text to $max_characters + 1
			$text = mb_substr( $text, 0, $max_characters + 1 );

			//* Truncate to the last space in the truncated string
			$text = trim( mb_substr( $text, 0, mb_strrpos( $text, ' ' ) ) );
		}

		return $text;
	}
endif; //signify_truncate_phrase

if ( ! function_exists( 'signify_get_the_content_limit' ) ) :
	/**
	 * Return content stripped down and limited content.
	 *
	 * Strips out tags and shortcodes, limits the output to `$max_char` characters, and appends an ellipsis and more link to the end.
	 *
	 * @since 1.0.0
	 *
	 * @param integer $max_characters The maximum number of characters to return.
	 * @param string  $more_link_text Optional. Text of the more link. Default is "(more...)".
	 * @param bool    $stripteaser    Optional. Strip teaser content before the more text. Default is false.
	 *
	 * @return string Limited content.
	 */
	function signify_get_the_content_limit( $max_characters, $more_link_text = '(more...)', $stripteaser = false ) {

		$content = get_the_content( '', $stripteaser );

		// Strip tags and shortcodes so the content truncation count is done correctly.
		$content = strip_tags( strip_shortcodes( $content ), apply_filters( 'get_the_content_limit_allowedtags', '<script>,<style>' ) );

		// Remove inline styles / .
		$content = trim( preg_replace( '#<(s(cript|tyle)).*?</\1>#si', '', $content ) );

		// Truncate $content to $max_char
		$content = signify_truncate_phrase( $content, $max_characters );

		// More link?
		if ( $more_link_text ) {
			$link   = apply_filters( 'get_the_content_more_link', sprintf( '<a href="%s" class="more-link">%s</a>', esc_url( get_permalink() ), $more_link_text ), $more_link_text );
			$output = sprintf( '<p>%s %s</p>', $content, $link );
		} else {
			$output = sprintf( '<p>%s</p>', $content );
			$link = '';
		}

		return apply_filters( 'signify_get_the_content_limit', $output, $content, $link, $max_characters );

	}
endif; //signify_get_the_content_limit

if ( ! function_exists( 'signify_content_image' ) ) :
	/**
	 * Template for Featured Image in Archive Content
	 *
	 * To override this in a child theme
	 * simply fabulous-fluid your own signify_content_image(), and that function will be used instead.
	 *
	 * @since 1.0.0
	 */
	function signify_content_image() {
		if ( has_post_thumbnail() && signify_jetpack_featured_image_display() && is_singular() ) {
			global $post, $wp_query;

			// Get Page ID outside Loop.
			$page_id = $wp_query->get_queried_object_id();

			if ( $post ) {
				if ( is_attachment() ) {
					$parent = $post->post_parent;

					$individual_featured_image = get_post_meta( $parent, 'signify-featured-image', true );
				} else {
					$individual_featured_image = get_post_meta( $page_id, 'signify-featured-image', true );
				}
			}

			if ( empty( $individual_featured_image ) ) {
				$individual_featured_image = 'default';
			}

			if ( 'disable' === $individual_featured_image ) {
				echo '<!-- Page/Post Single Image Disabled or No Image set in Post Thumbnail -->';
				return false;
			} else {
				$class = array();

				$image_size = 'post-thumbnail';

				if ( 'default' !== $individual_featured_image ) {
					$image_size = $individual_featured_image;
					$class[]    = 'from-metabox';
				} else {
					$layout = signify_get_theme_layout();

					if ( 'no-sidebar-full-width' === $layout ) {
						$image_size = 'signify-slider';
					}
				}

				$class[] = $individual_featured_image;
				?>
				<div class="post-thumbnail <?php echo esc_attr( implode( ' ', $class ) ); ?>">
					<a href="<?php the_permalink(); ?>">
					<?php the_post_thumbnail( $image_size ); ?>
					</a>
				</div>
			<?php
			}
		} // End if ().
	}
endif; // signify_content_image.

if ( ! function_exists( 'signify_enable_homepage_posts' ) ) :
	/**
	 * Determine Homepage Content disabled or not
	 * @return boolean
	 */
	function signify_enable_homepage_posts() {
	   if ( ! ( ! get_theme_mod( 'signify_display_homepage_posts', 1 ) && is_front_page() ) ) {
			return true;
		}
		return false;
	}
endif; // signify_enable_homepage_posts.

if ( ! function_exists( 'signify_sections' ) ) :
	/**
	 * Display Sections on header and footer with respect to the section option set in signify_sections_sort
	 */
	function signify_sections( $selector = 'header' ) {
		$sections = get_theme_mod( 'signify_sections_sort', signify_get_default_sections_value() );

		$separated_sections = explode( 'main-content,', $sections );

		$sections = $separated_sections[0];

		if ( 'footer' === $selector ) {
			$sections = $separated_sections[1];
		}

		$sections =  ! empty( $sections ) ? explode( ',', $sections ) : array();

		foreach ( $sections as $section ) {
			$all_sections = signify_get_sortable_sections();

			if ( isset( $all_sections[ $section ]['template-part'] ) ) {
				get_template_part( $all_sections[ $section ]['template-part'] );
			}

			if ( 'header-media' === $section && get_theme_mod( 'signify_primary_below_header_media' ) && 'header-style-one' === get_theme_mod( 'signify_header_layout', 'header-style-one' ) ) {
				get_template_part( 'template-parts/navigation/navigation-primary' );
			}
		}
	}
endif;

if ( ! function_exists( 'signify_get_sortable_sections' ) ) :
	/**
	 * Returns list of sortable sections
	 */
	function signify_get_sortable_sections() {
		$sortable_sections = array(
				'header-media'          => array(
					'label'         => esc_html__( 'Header Media', 'signify-pro' ),
					'section'       => 'header_image',
					'template-part' => 'template-parts/header/header-media',
				),
				'slider'                => array(
					'label'         => esc_html__( 'Slider', 'signify-pro' ),
					'section'       => 'signify_featured_slider',
					'template-part' => 'template-parts/slider/display-slider',
				),
				'service'              => array(
					'label'         => esc_html__( 'Services', 'signify-pro' ),
					'section'       => 'signify_service',
					'template-part' => 'template-parts/service/display-service',
				),
				'collection'              => array(
					'label'         => esc_html__( 'Collection', 'signify-pro' ),
					'section'       => 'signify_collection',
					'template-part' => 'template-parts/collection/display-collection',
				),
				'hero-content'          => array(
					'label'         => esc_html__( 'Hero Content', 'signify-pro' ),
					'section'       => 'signify_hero_content_options',
					'template-part' => 'template-parts/hero-content/content-hero',
				),
				'testimonial'           => array(
					'label'         => esc_html__( 'Testimonial', 'signify-pro' ),
					'section'       => 'signify_testimonials',
					'template-part' => 'template-parts/testimonial/display-testimonial',
				),
				'gallery'               => array(
					'label'         => esc_html__( 'Gallery', 'signify-pro' ),
					'section'       => 'signify_gallery_options',
					'template-part' => 'template-parts/gallery/content-gallery',
				),
				'events'                => array(
					'label'         => esc_html__( 'Events', 'signify-pro' ),
					'section'       => 'signify_events',
					'template-part' => 'template-parts/events/content-event',
				),
				'timeline'              => array(
					'label'         => esc_html__( 'Timeline', 'signify-pro' ),
					'section'       => 'signify_timeline',
					'template-part' => 'template-parts/timeline/content-timeline',
				),
				'woo-products-showcase' => array(
					'label'         => esc_html__( 'Woo Products Showcase', 'signify-pro' ),
					'section'       => 'signify_woo_products_showcase',
					'template-part' => 'template-parts/woo-products-showcase/display-products',
				),
				'portfolio'             => array(
					'label'         => esc_html__( 'Portfolio', 'signify-pro' ),
					'section'       => 'signify_portfolio',
					'template-part' => 'template-parts/portfolio/display-portfolio',
				),
				'stats'                 => array(
					'label'         => esc_html__( 'Stats', 'signify-pro' ),
					'section'       => 'signify_stats',
					'template-part' => 'template-parts/stats/display-stats',
				),
				'team'                  => array(
					'label'         => esc_html__( 'Team', 'signify-pro' ),
					'section'       => 'signify_team',
					'template-part' => 'template-parts/team/display-team',
				),
				'playlist'                  => array(
					'label'         => esc_html__( 'Playlist', 'signify-pro' ),
					'section'       => 'signify_playlist',
					'template-part' => 'template-parts/playlist/content-playlist',
				),
				'promotion-headline'    => array(
					'label'         => esc_html__( 'Promotion headline', 'signify-pro' ),
					'section'       => 'signify_promotion_headline',
					'template-part' => 'template-parts/promotion-headline/content-promotion',
				),
				'main-content'          => array(
					'label' => esc_html__( 'Main Content', 'signify-pro' ),
				),
				'recent-posts'          => array(
					'label'         => esc_html__( 'Recent Posts ( Only on homepage )', 'signify-pro' ),
					'section'       => 'signify_homepage_options',
					'template-part' => 'template-parts/recent-posts/front-recent-posts',
				),
				'featured-content'      => array(
					'label'         => esc_html__( 'Featured Content', 'signify-pro' ),
					'section'       => 'signify_featured_content',
					'template-part' => 'template-parts/featured-content/display-featured',
				),
				'logo-slider'           => array(
					'label'         => esc_html__( 'Logo Slider', 'signify-pro' ),
					'section'       => 'signify_logo_slider',
					'template-part' => 'template-parts/logo-slider/display-logo-slider',
				),
				'newsletter'            => array(
					'label'         => esc_html__( 'Newsletter', 'signify-pro' ),
					'section'       => 'sidebar-widgets-sidebar-newsletter',
					'template-part' => 'template-parts/footer/footer-newsletter',
				),
				'contact'               => array(
					'label'         => esc_html__( 'Contact Section', 'signify-pro' ),
					'section'       => 'signify_contact',
					'template-part' => 'template-parts/contact/display-contact',
				),
				'instagram'             => array(
					'label'         => esc_html__( 'Instagram', 'signify-pro' ),
					'section'       => 'sidebar-widgets-sidebar-instagram',
					'template-part' => 'template-parts/footer/footer-instagram',
				),
		);

		return $sortable_sections;
	}
endif;

if ( ! function_exists( 'signify_team_social_links' ) ) :
	/**
	 * Displays team social links html
	 */
	function signify_team_social_links( $counter ) {

		$social_link_one   = get_theme_mod( 'signify_team_social_link_one_' . $counter );
		$social_link_two   = get_theme_mod( 'signify_team_social_link_two_' . $counter );
		$social_link_three = get_theme_mod( 'signify_team_social_link_three_' . $counter );
		$social_link_four  = get_theme_mod( 'signify_team_social_link_four_' . $counter );

		if ( $social_link_one || $social_link_two || $social_link_three || $social_link_four  ): ?>
			<div class="artist-social-profile">
				<nav class="social-navigation" role="navigation" aria-label="Social Menu">
					<div class="menu-social-container">
						<ul id="menu-social-menu" class="social-links-menu">
							<?php if ( $social_link_one ) : ?>
								<li class="menu-item-one">
									<a target="_blank" rel="nofollow" href="<?php echo esc_url( $social_link_one ); ?>"></a>
								</li>
							<?php endif;

							if ( $social_link_two ): ?>
								<li class="menu-item-two">
									<a target="_blank" rel="nofollow" href="<?php echo esc_url( $social_link_two ); ?>"></a>
								</li>
							<?php endif;

							if ( $social_link_three ): ?>
								<li class="menu-item-three">
									<a target="_blank" rel="nofollow" href="<?php echo esc_url( $social_link_three ); ?>"></a>
								</li>
							<?php endif;

							if ( $social_link_four ): ?>
								<li class="menu-item-four">
									<a target="_blank" rel="nofollow" href="<?php echo esc_url( $social_link_four ); ?>"></a>
								</li>
							<?php endif; ?>
						</ul>
					</div>
				</nav>
			</div><!-- .artist-social-profile -->
		<?php endif;
	}
endif;

if ( ! function_exists( 'signify_post_thumbnail' ) ) :
	/**
	 * $image_size post thumbnail size
	 * $type html, html-with-bg, url
	 * $echo echo true/false
	 * $no_thumb display no-thumb image or not
	 */
	function signify_post_thumbnail( $image_size = 'post-thumbnail', $type = 'html', $echo = true, $no_thumb = false ) {
		$image = $image_url = '';

		if ( has_post_thumbnail() ) {
			$image_url = get_the_post_thumbnail_url( get_the_ID(), $image_size );
			$image     = get_the_post_thumbnail( get_the_ID(), $image_size );
		} else {
			if ( is_array( $image_size ) && $no_thumb ) {
				$image_url  = trailingslashit( get_template_directory_uri() ) . 'images/no-thumb-1160x670.jpg';

				$image      = '<img src="' . esc_url( $image_url ) . '" alt="" />';
			} elseif ( $no_thumb ) {
				global $_wp_additional_image_sizes;

				$image_url  = trailingslashit( get_template_directory_uri() ) . 'images/no-thumb-1160x670.jpg';

				if ( array_key_exists( $image_size, $_wp_additional_image_sizes ) ) {
					$image_url  = trailingslashit( get_template_directory_uri() ) . 'images/no-thumb-' . $_wp_additional_image_sizes[ $image_size ]['width'] . 'x' . $_wp_additional_image_sizes[ $image_size ]['height'] . '.jpg';
				}

				$image = '<img src="' . esc_url( $image_url ) . '" alt="" />';
			}

			// Get the first image in page, returns false if there is no image.
			$first_image_url = signify_get_first_image( get_the_ID(), $image_size, '', true );

			// Set value of image as first image if there is an image present in the page.
			if ( $first_image_url ) {
				$image_url = $first_image_url;
				$image = '<img class="wp-post-image" src="'. esc_url( $image_url ) .'">';
			}
		}

		if ( ! $image_url ) {
			// Bail if there is no image url at this stage.
			return;
		}

		if ( 'url' === $type ) {
			return $image_url;
		}

		$output = '<div';

		if ( 'html-with-bg' === $type ) {
			$output .= ' class="post-thumbnail-background" style="background-image: url( ' . esc_url( $image_url ) . ' )"';
		} else {
			$output .= ' class="post-thumbnail"';
		}

		$output .= '>';

		if ( 'html-with-bg' === $type ) {
			$output .= '<a class="cover-link" href="' . esc_url( get_the_permalink() ) . '" title="' . the_title_attribute( 'echo=0' ) . '">';
		} else {
			$output .= '<a href="' . esc_url( get_the_permalink() ) . '" title="' . the_title_attribute( 'echo=0' ) . '">';
		}

		if ( 'html-with-bg' !== $type ) {
			$output .= $image;
		}

		$output .= '</a></div><!-- .post-thumbnail -->';

		if ( ! $echo ) {
			return $output;
		}

		echo $output;
	}
endif;

if ( ! function_exists( 'signify_testimonial_posts_args' ) ) :

	function signify_testimonial_posts_args() {
		$number = get_theme_mod( 'signify_testimonial_number', 5 );

		$args = array(
			'ignore_sticky_posts' => 1 // ignore sticky posts
		);

		$type = get_theme_mod( 'signify_testimonial_type', 'category' );

		$post_list  = array();// list of valid post/page ids


		if ( 'post' === $type || 'jetpack-testimonial' === $type || 'page' === $type  ) {
			$args['post_type'] = $type;

			for ( $i = 1; $i <= $number; $i++ ) {
				$post_id = '';

				if ( 'post' === $type ) {
					$post_id = get_theme_mod( 'signify_testimonial_post_' . $i );
				} elseif ( 'page' === $type ) {
					$post_id = get_theme_mod( 'signify_testimonial_page_' . $i );
				} elseif ( 'jetpack-testimonial' === $type ) {
					$post_id =  get_theme_mod( 'signify_testimonial_cpt_' . $i );
				}

				if ( $post_id && '' !== $post_id ) {
					// Polylang Support.
					if ( class_exists( 'Polylang' ) ) {
						$post_id = pll_get_post( $post_id, pll_current_language() );
					}

					$post_list = array_merge( $post_list, array( $post_id ) );

				}
			}

			$args['post__in'] = $post_list;
			$args['orderby'] = 'post__in';
		} elseif ( 'category' === $type ) {
			$no_of_post = $number;

			if ( get_theme_mod( 'signify_testimonial_select_category' ) ) {
				$args['category__in'] = (array) get_theme_mod( 'signify_testimonial_select_category' );
			}

			$args['post_type'] = 'post';
		} elseif ( 'tag' === $type ) {
			$no_of_post = $number;

			if ( get_theme_mod( 'signify_testimonial_select_tag' ) ) {
				$args['tag__in'] = (array) get_theme_mod( 'signify_testimonial_select_tag' );
			}

			$args['post_type'] = 'post';
		}

		$args['posts_per_page'] = $number;

		return $args;
	}
endif;

/**
 * Function to reset date with respect to condition
 */
function signify_reset_data() {
	if ( get_theme_mod( 'signify_reset_all_settings' ) ) {
		remove_theme_mods();

		return;
	}

	if ( get_theme_mod( 'signify_reset_footer_content' ) ) {
		remove_theme_mod( 'signify_footer_copyright_text' );
		remove_theme_mod( 'signify_footer_powered_by' );
		remove_theme_mod( 'signify_reset_footer_content' );
	}

	if ( get_theme_mod( 'signify_reset_typography' ) ) {
		//Reset Font Family Options
		$font_family_options = signify_font_family_options();

		foreach ( $font_family_options as $key => $value ) {
			 remove_theme_mod( $key );
		}

		remove_theme_mod( 'signify_reset_typography' );
	}
}
add_action( 'customize_save_after', 'signify_reset_data' );

function signify_get_posts_columns() {
	$columns = get_theme_mod( 'signify_blog_post_layout', 'layout-one' );

	if ( is_front_page() ) {
		$columns = get_theme_mod( 'signify_homepage_post_layout', 'layout-one' );
	}

	return $columns;
}

if ( ! function_exists( 'signify_section_header' ) ) :
	/**
	 * Display header of a section
	 */
	function signify_section_header( $tagline, $title, $description ) {
		if ( $title || $tagline || $description ) : ?>
			<div class="section-heading-wrapper">
				<?php if ( $tagline ) : ?>
					<div class="section-subtitle">
						<?php echo wp_kses_post( $tagline); ?>
					</div><!-- .section-description-wrapper -->
				<?php endif; ?>

				<?php if ( $title ) : ?>
					<div class="section-title-wrapper">
						<h2 class="section-title"><?php echo wp_kses_post( $title ); ?></h2>
					</div>
				<?php endif; ?>

				<?php if ( $description ) : ?>
					<div class="section-description">
						<p><?php echo wp_kses_post( $description ); ?></p>
					</div><!-- .section-description-wrapper -->
				<?php endif; ?>
			</div>
		<?php endif;
	}
endif;

if ( ! function_exists( 'signify_get_no_thumb_image' ) ) :
	/**
	 * $image_size post thumbnail size
	 * $type image, src
	 */
	function signify_get_no_thumb_image( $image_size = 'post-thumbnail', $type = 'image' ) {
		$image = $image_url = '';

		global $_wp_additional_image_sizes;

		$size = $_wp_additional_image_sizes['post-thumbnail'];

		if ( isset( $_wp_additional_image_sizes[ $image_size ] ) ) {
			$size = $_wp_additional_image_sizes[ $image_size ];
		}

		$image_url  = trailingslashit( get_template_directory_uri() ) . 'assets/images/no-thumb.jpg';

		if ( 'post-thumbnail' !== $image_size ) {
			$image_url  = trailingslashit( get_template_directory_uri() ) . 'assets/images/no-thumb-' . $size['width'] . 'x' . $size['height'] . '.jpg';
		}

		if ( 'src' === $type ) {
			return $image_url;
		}

		return '<img class="no-thumb ' . esc_attr( $image_size ) . '" src="' . esc_url( $image_url ) . '" />';
	}
endif;

if ( ! function_exists( 'signify_entry_header' ) ) :
	/**
	 * Prints HTML with meta information for the date and author
	 *
	 * Create your own signify_entry_header() function to override in a child theme.
	 */
	function signify_entry_header() {
		echo '<div class="entry-meta">';

		$portfolio_categories_list = get_the_term_list( get_the_ID(), 'jetpack-portfolio-type', '<span class="portfolio-entry-meta entry-meta">', esc_html_x( ', ', 'Used between list items, there is a space after the comma.', 'signify-pro' ), '</span>' );

		if ( 'jetpack-portfolio' === get_post_type() ) {
			printf( '<span class="cat-links"><span class="cat-label">%1$s: </span>%2$s</span>',
				sprintf( _x( 'Categories', 'Used before category names.', 'signify-pro' ) ),
				$portfolio_categories_list
			);
		}

		$categories_list = get_the_category_list( _x( ' ', 'Used between list items, there is a space after the comma.', 'signify-pro' ) );
		if ( $categories_list && signify_categorized_blog( ) ) {
			printf( '<span class="cat-links"><span class="cat-label screen-reader-text">%1$s</span>%2$s</span>',
				sprintf( _x( 'Categories', 'Used before category names.', 'signify-pro' ) ),
				$categories_list
			);
		}

		// Get the author name; wrap it in a link.
		printf(
			/* translators: %s: post author */
			__( '<span class="byline screen-reader-text"> <span class="author-label screen-reader-text">By </span>%s', 'signify-pro' ),
			'<span class="author vcard screen-reader-text"><a class="url fn n screen-reader-text" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . get_the_author() . '</a></span></span>'
		);

		// Comments.
		if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
			echo '<span class="comments-link screen-reader-text">';
			comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'signify-pro' ), get_the_title() ) );
			echo '</span>';
		}

		echo '</div><!-- .entry-meta -->';
	}
endif;

/**
 * Adds playlist background CSS
 */
function signify_playlist_bg_css() {
	$enable   = get_theme_mod( 'signify_playlist_visibility', 'disabled' );

	if ( ! signify_check_section( $enable ) ) {
		// Bail if contact section is disabled.
		return;
	}

	$css = '';

	$background = get_theme_mod( 'signify_playlist_bg_image' );

	if ( $background ) {
		$css = '#playlist-section { background-image: url("' . esc_url( $background ) . '"); }';
	}

	wp_add_inline_style( 'signify-style', $css );
}
add_action( 'wp_enqueue_scripts', 'signify_playlist_bg_css', 11 );
