Last active
March 19, 2019 17:48
-
-
Save kostiantyn-petlia/969fdda77e3e56555ccf9a216432c068 to your computer and use it in GitHub Desktop.
Functions for inline SVG with adding class (WP, ACF PRO Image)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
/** | |
* Convert file url to path | |
* | |
* @param string $url Link to file | |
* | |
* @return bool|mixed|string | |
*/ | |
function convert_url_to_path( $url ) { | |
if ( ! $url ) { | |
return false; | |
} | |
$url = str_replace( array( 'https://', 'http://' ), '', $url ); | |
$home_url = str_replace( array( 'https://', 'http://' ), '', get_home_url() ); | |
$root_dir = ( defined( 'ROOT_DIR' ) && ! empty( ROOT_DIR ) ) ? ROOT_DIR : ABSPATH; | |
$file_part = $root_dir . str_replace( $home_url, '', $url ); | |
$file_part = str_replace( '//', '/', $file_part ); | |
if ( file_exists( $file_part ) ) { | |
return $file_part; | |
} | |
return false; | |
} | |
/** | |
* Add classes to the tag into the HTML | |
* | |
* Note: | |
* [1] & [2] - to disable error reporting - https://stackoverflow.com/questions/6090667/php-domdocument-errors-warnings-on-html5-tags | |
* | |
* @param bool $class - an adding class, like 'logo' | |
* @param bool $tag - a targeted tag, like 'svg' | |
* @param string $html - HTML, for example from file_get_contents() | |
* | |
* @return string - changed HTML | |
*/ | |
function add_class_to_tag_in_html( $class = false, $tag = false, $html = '' ) { | |
if ( empty( $class ) || empty( $tag ) ) { | |
return $html; | |
} | |
if ( class_exists( 'DOMDocument' ) ) { | |
// Build PHP DOM-object | |
$dom = new \DOMDocument(); | |
\libxml_use_internal_errors( true ); // [1] | |
$dom->loadHTML( $html ); | |
\libxml_clear_errors(); // [2] | |
// Add class to the tag instances | |
foreach ( $dom->getElementsByTagName( (string) $tag ) as $element ) { | |
$class_exists = $element->getAttribute( 'class' ); | |
$class = esc_attr( $class ); | |
$class = ! empty( $class_exists ) ? ( implode( ' ', array_unique( explode( ' ', $class . ' ' . $class_exists ) ) ) ) : $class; | |
$element->setAttribute( 'class', $class ); | |
} | |
$dom->saveHTML(); | |
$html = $dom->saveHTML(); | |
} | |
return $html; | |
} | |
function get_img_url( $file_name = null ) { | |
$dir = get_template_directory_uri() . '/images/'; | |
return ( ! empty( $file_name ) && is_string( $file_name ) ) ? ( $dir . $file_name ) : null; | |
} | |
/** | |
* Return/Output SVG inline as HTML | |
* | |
* @param array|string $img Image link or array | |
* @param string $class Additional class attribute for img tag | |
* @param string $size Image size if $img is array | |
* | |
* @return void | |
*/ | |
function the_svgimg( $img, $class = '', $size = 'medium' ) { | |
echo get_svgimg( $img, $class, $size ); | |
} | |
function get_svgimg( $img, $class = '', $size = 'medium' ) { | |
if ( ! $img ) { | |
return ''; | |
} | |
// ACF array or URL string | |
$img_url = is_array( $img ) ? $img['url'] : (string) $img; | |
// If URL is a filename like 'logo.svg' we add "url/to/images/" | |
$img_url = ( strpos( $img_url, '/' ) === false ) ? get_img_url( $img_url ) : $img_url; | |
$file_info = pathinfo( $img_url ); | |
if ( $file_info['extension'] == 'svg' ): | |
$arrContextOptions = array( | |
"ssl" => array( | |
"verify_peer" => false, | |
"verify_peer_name" => false, | |
), | |
); | |
$image = file_get_contents( convert_url_to_path( $img_url ), false, stream_context_create( $arrContextOptions ) ); | |
$image = add_class_to_tag_in_html( $class, 'svg', $image ); | |
elseif ( is_array( $img ) ): | |
$image = '<img class="' . esc_attr( $class ) . '" src="' . $img['sizes'][ $size ] . '" alt="' . $img['alt'] . '" />'; | |
else : | |
$image = '<img class="' . esc_attr( $class ) . '" src="' . esc_url( (string) $img ) . '" />'; | |
endif; | |
return $image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment