<?php
/**
 * Title: Header Hooks
 *
 * Description: Defines actions/hooks for header content.
 *
 * Please do not edit this file. This file is part of the CyberChimps Framework and all modifications
 * should be made in a child theme.
 *
 * @category CyberChimps Framework
 * @package  Framework
 * @since    1.0
 * @author   CyberChimps
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v3.0 (or later)
 * @link     http://www.cyberchimps.com/
 */


// Function to display the site logo from the customizer options
function cyberchimps_header_logo() {
	$url = ( get_theme_mod( 'custom_logo_url' ) == '1' ) ? get_theme_mod( 'custom_logo_url_link' ) : esc_url( home_url() );

	$theme_options_main = get_option( 'theme_mods_' . get_template() );

	if ( has_custom_logo( $blog_id = 0 ) ) {
		$custom_logo_id = get_theme_mod( 'custom_logo' );
		$logo = wp_get_attachment_image_src( $custom_logo_id, 'full' );
		?>
		<div id="logo">
			<a href="<?php echo esc_url( $url ); ?>" title="<?php echo get_bloginfo( 'name' ); ?>"><img src="<?php echo esc_url( $logo[0] ) ? esc_url( $logo[0] ) : apply_filters( 'cyberchimps_default_logo' , esc_url( $url ) ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"></a>
		</div>
	<?php
	} else {
		if ( function_exists( 'cyberchimps_header_site_title_desc' ) ) {
			cyberchimps_header_site_title_desc();
		}
	}
}

// Function to display the site title and description if the logo is not set
function cyberchimps_header_site_title_desc() {

?>
	<div class="hgroup">
		<h2 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h2>
	</div>
	
	<?php $description = get_bloginfo( 'description' ); ?>
	<div class="blog-description">
		<p><?php echo $description; ?></p>
	</div>
<?php
}

/**
 * Social icons positioned in header and some theme's footer
 *
 * The key of the $social variable has to match the font icon you want to use. If that differs from the name you want displayed set the title key
 * e.g. $social['twitterbird']['title'] = 'twitter';
 *
 * styling is located in /lib/css/core.css
 * icon fonts are from http://drinchev.github.io/monosocialiconsfont/
 */
function cyberchimps_header_social_icons() {

	// get the design of the icons to apply the right class
	$design = get_theme_mod( 'theme_backgrounds', 'default' );

	// create array of social icons to loop through to check if they are set and add title key to
	// social networks with names different to key

	$social['twitterbird']['set']   = get_theme_mod( 'social_twitter', 'checked' );
	$social['twitterbird']['title'] = 'twitter';
	$social['twitterbird']['url']   = get_theme_mod( 'twitter_url' );
	$social['facebook']['set']      = get_theme_mod( 'social_facebook', 'checked' );
	$social['facebook']['url']      = get_theme_mod( 'facebook_url' );
	$social['googleplus']['set']    = get_theme_mod( 'social_google', 'checked' );
	$social['googleplus']['url']    = get_theme_mod( 'google_url' );
	$social['flickr']['set']        = get_theme_mod( 'social_flickr' );
	$social['flickr']['url']        = get_theme_mod( 'flickr_url' );
	$social['pinterest']['set']     = get_theme_mod( 'social_pinterest' );
	$social['pinterest']['url']     = get_theme_mod( 'pinterest_url' );
	$social['linkedin']['set']      = get_theme_mod( 'social_linkedin' );
	$social['linkedin']['url']      = get_theme_mod( 'linkedin_url' );
	$social['youtube']['set']       = get_theme_mod( 'social_youtube' );
	$social['youtube']['url']       = get_theme_mod( 'youtube_url' );
	$social['map']['set']           = get_theme_mod( 'social_googlemaps' );
	$social['map']['title']         = 'google maps';
	$social['map']['url']           = get_theme_mod( 'googlemaps_url' );
	$social['email']['set']         = get_theme_mod( 'social_email' );
	$social['email']['url']         = 'mailto:' . get_theme_mod( 'email_url' );
	$social['rss']['set']           = get_theme_mod( 'social_rss' );
	$social['rss']['url']           = get_theme_mod( 'rss_url' );
	$social['instagram']['set']     = get_theme_mod( 'social_instagram' );
	$social['instagram']['url']     = get_theme_mod( 'instagram_url' );

	$output = '';

	// get the blog title to add to link title
	$link_title = get_bloginfo( 'title' );

	// Loop through the $social variable
	foreach ( $social as $key => $value ) {

		// Check that the social icon has been set
		if ( ! empty( $value['set'] ) ) {

			// check if title is set and use it otherwise use key as title
			$title = ( isset( $social[ $key ]['title'] ) ) ? $social[ $key ]['title'] : $key;

			// Create the output
			$output .= '<a href="' . esc_url( $social[ $key ]['url'] ) . '"' . ( 'email' != $key ? ' target="_blank"' : '' )
				. ' title="' . esc_attr( $link_title . ' ' . ucwords( $title ) ) . '" class="symbol ' . $key . '"><span class="screen-reader-text">' . $key . ' - ' . __( ' Link will open in new window', 'cyberchimps_core' ) . '</span></a>';
		}
	}

	// Echo to the page
	?>
	<div id="social">
		<div class="<?php echo $design; ?>-icons">
			<?php echo $output; ?>
		</div>
	</div>

<?php
}
