<?php

/**
 * Returns the logo in a link
 *
 * This function returns the logo image in an <a>
 * link tag with specified classes and id. If the
 * specified image can't be found, the function
 * will push a message to the developer console.
 *
 * @function the_logo()
 *
 * @uses get_template_directory_uri(), get_template_directory(), developer_console_message()
 *
 * @param (mixed) (string) $src the image source, $logo[ (string) class, (string) id ]
 *
 * @return (string) the specified image in a <a> tag linking to the home page
 *
 * @since 1.0.0 
 *
 */
function the_logo( $src, $logo = array( 'class' => 'logo', 'id' => 'logo' ), $args = array() ) {
	$url = get_template_directory_uri() . '/img/' . $src;
	$alt = ow_global_var( 'accountant_name' ) . ' Logo';
	$id = $logo['id'];
	$class = $logo['class'];
	$width = ( $args['width'] ) ? 'width="' . $args['width'] . 'px" ' : '';
	$height = ( $args['height'] ) ? 'height="' . $args['height'] . 'px" ' : '';
	if ( ! is_front_page() ) {

		$theLogo = '<a href="/"><img src="' . $url . '" alt="' . $alt . '" id="' . $id . '" class="' . $class . '" '.$width .$height .' /></a>';
	} else {

		$theLogo = '<img src="' . $url . '" alt="' . $alt . '" id="' . $id . '" class="' . $class . '" '.$width .$height .' />';	
	}
	if ( ! file_exists( get_template_directory() . '/img/' . $src ) ) {

		developer_console_message( 'Logo not found', 'alert' );
		return false;
	}
	return $theLogo;
}



/**
 * Returns an inline SVG
 *
 * This function returns an inline SVG or, if not
 * found, will push a message to the developer console.
 *
 * @function svg()
 *
 * @uses get_template_directory(), esc_attr(), developer_console_message()
 *
 * @param (string) $src  the SVG source
 * @param (array)  $rgs  An id and classname attributes to inject into the svg.
 *
 * @return (string) the specified SVG as an inline SVG element
 *
 * @since 1.2.0 
 *
 */

function svg( $src, $args = array() ) {
	$src = trim( $src );
	$path = get_template_directory() . '/img/' . $src . '.svg';
	if ( file_exists( $path ) ) {
		$args = (object) wp_parse_args( $args, array('class' => 'svg','id' => '',) );
		$id = $args->id ? 'id="' . esc_attr( $args->id ) . '" ' : '';
		$alt = $args->alt ? 'alt="' . esc_attr( $args->alt ) . '" ' : '';
		$class = $args->class ? 'class="' . esc_attr( $args->class ) . '" ' : '';
		echo substr_replace( file_get_contents( $path ), $alt .$id . $class, 5, 0 );
	}
	else {
		developer_console_message( $src . ' not found', 'alert' );
	}
}




/**
 * Returns an image
 *
 * This function returns a image or if not
 * found, will push a message to the developer console.
 *
 * @function image()
 *
 * @uses get_template_directory_uri(), get_template_directory(), developer_console_message()
 *
 * @param (mixed) (string) $src the image source, $logo[ (string) alt, (string) class, (string) id ]
 *
 * @return (string) the specified image
 *
 * @since 1.3.2 
 *
 */
function image( $src, $args = array() ) {
	
	$args = array_merge( array(
		'alt'   => '',
		'class' => '',
		'id'    => '',
	), $args );
	
	$path = get_template_directory() . '/img/' . $src;
	$url = get_template_directory_uri() . '/img/' . $src;
	
	if ( file_exists( $path ) ) {
		
		$alt = ( $args['alt'] ) ? 'alt="' . $args['alt'] . '" ' : 'alt="' . $src . '" ';
		$class = ( $args['class'] ) ? 'class="' . $args['class'] . '" ' : '';
		$id = ( $args['id'] ) ? 'id="' . $args['id'] . '" ' : '';
		
		echo '<img src="' . $url . '" ' . $alt . $class . $id . '/>';	
	} else {
		
		developer_console_message( $src . ' image not found', 'alert' );	
	}	
}



/**
 * Returns a PNG image
 *
 * This function returns a PNG image.
 *
 * @function png()
 *
 * @uses image()
 *
 * @param (mixed) (string) $src the image source, $logo[ (string) alt, (string) class, (string) id ]
 *
 * @return (string) the specified PNG image
 *
 * @since 1.3.2 
 *
 */
function png( $src, $args = array() ) {
	image( $src . '.png', $args );
}



/**
 * Returns a JPG image
 *
 * This function returns a JPG image.
 *
 * @function jpg()
 *
 * @uses image()
 *
 * @param (mixed) (string) $src the image source, $logo[ (string) alt, (string) class, (string) id ]
 *
 * @return (string) the specified JPG image
 *
 * @since 1.3.2 
 *
 */
function jpg( $src, $args = array() ) {
	image( $src . '.jpg', $args );
}



/**
 * Returns the IRIS OpenWebsite logo
 *
 * This function returns the IRIS OpenWebsite logo
 * with a <map> link to http://irisopenwebsite.co.uk and http://www.iris.co.uk
 *
 * @function iris_logo()
 *
 * @uses plugins_url()
 *
 * @param (array) $logo[ (string) class, (string) id ]
 *
 * @return (string) the IRIS OpenWebsite logo and <a> link
 *
 * @since 1.1.2 
 *
 */
function iris_logo( $logo = array( 'class' => 'iris-openwebsite-logo', 'id' => 'iris-openwebsite-logo' ) ) {
	
	$src = plugins_url( 'img/iris-webportal-logo-white.svg', dirname( __FILE__ ) );
	
	$irisLogo = "<img width='221' height='60' src='" . $src . "' alt='Powered by IRIS OpenWebPortal' class='" . $logo['class'] . "' id='" . $logo['id'] . "' usemap='#iris-logo-map' />";
	$irisLogo .= "<map name='iris-logo-map'><area shape='rect' coords='0,0,103,60' href='http://www.iris.co.uk' alt='IRIS Software' target='_blank' rel='external'><area shape='rect' coords='103,0,221,60' href='https://iriswebportal.co.uk' alt='Powered by IRIS OpenWebPortal' target='_blank' rel='external'></map>";
	
	echo $irisLogo;
}



/**
 * Returns the social icons in png format for the newsletter.
 *
 * @function ow_social_icon_png()
 *
 * @uses ow_global_var()
 *
 * @param (mixed) (string) $var the social name, (string) $url the social url, (string) $target the target
 *
 * @return (string) the social icon and link.
 *
 * @since 1.1.0 
 *
 */
function ow_social_icon_png( $var, $url = '', $target = '_blank' ) {
	
	$path = ABSPATH . '/img/social-icons/' . $var . '.png';
	$urlpath = get_site_url( NULL, 'img/social-icons/' . $var . '.png' );

	if ( file_exists( $path ) ) {

		$link = ( $url != '' ) ? $url : ow_global_var( $var );
		$img = '<img src="' . $urlpath . '" alt="' . $var . '"/>';
		return '<a href="https://' . $link . '" target="' . $target . '">' . $img . '</a>';		
	} else {
		
		developer_console_message( $var . ' social icon (png) not found', 'alert' );	
	}	
}