<?php 


if( ! function_exists( 'jetpack_has_site_icon' ) ) :
function jetpack_has_site_icon( $blog_id = null ) {

	if( ! is_int( $blog_id ) )
		$blog_id = get_current_blog_id();

	if( jetpack_site_icon_url( $blog_id, 96, '' ) ) {
		return true;
	}

	return false;
}
endif;

if( ! function_exists( 'jetpack_get_site_icon' ) ) :
function jetpack_get_site_icon( $blog_id = null, $size = '96', $default = '', $alt = false ) {

	if( ! is_int( $blog_id ) )
		$blog_id = get_current_blog_id();

	$size  = esc_attr( $size );
	$class = "avatar avatar-$size";
	$alt = ( $alt ? esc_attr( $alt ) : __( 'Site Icon', 'jetpack' ) );
	$src = esc_url( jetpack_site_icon_url( $blog_id, $size, $default ) );
	$avatar = "<img alt='{$alt}' src='{$src}' class='$class' height='{$size}' width='{$size}' />";

	return apply_filters( 'jetpack-get_site_icon', $avatar, $blog_id, $size, $default, $alt );
}
endif; 

if( ! function_exists( 'jetpack_site_icon_url' ) ) :
function jetpack_site_icon_url( $blog_id = null, $size = '96', $default = false ) {
	$url = '';
	if( ! is_int( $blog_id ) )
		$blog_id = get_current_blog_id();

	if( function_exists( 'get_blog_option' ) ) {
		$site_icon_id = get_blog_option( $blog_id, 'site_icon_id' );
	} else {
		$site_icon_id = get_option( 'site_icon_id' );
	}
	
	if( ! $site_icon_id  ) {
		if( $default === false && defined( 'SITE_ICON_DEFAULT_URL' ) )
			$url =  SITE_ICON_DEFAULT_URL;
		else
			$url = $default;
	} else {

		$url_data = wp_get_attachment_image_src( $site_icon_id, array( $size, $size ) );
		$url = $url_data[0];
	}

	return $url;
}
endif; 