Created
October 16, 2015 20:19
-
-
Save jchristopher/5e7285f7e8bc09871075 to your computer and use it in GitHub Desktop.
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
<?php | |
function iti_get_svg_markup( $svg_id = 0 ) { | |
// get the file name of the svg | |
$svg_id = absint( $svg_id ); | |
$svg_file = get_post_meta( $svg_id, '_wp_attached_file', true ); | |
// locate the file on disk | |
$upload_dir = wp_upload_dir(); | |
$upload_basedir = trailingslashit( $upload_dir['basedir'] ); | |
$svg_markup = iti_get_svg_content( $upload_basedir . $svg_file ); | |
return $svg_markup; | |
} | |
function iti_get_svg_content( $svg_file = '' ) { | |
// prep the filesystem | |
global $wp_filesystem; | |
include_once ABSPATH . 'wp-admin/includes/file.php'; | |
WP_Filesystem(); | |
// make sure at least the extension is proper | |
if ( '.svg' !== strtolower( substr( $svg_file, -4 ) ) ) { | |
return ''; | |
} | |
if ( ! file_exists( $svg_file ) ) { | |
return ''; | |
} | |
// retrieve the markup | |
$svg_markup = $wp_filesystem->get_contents( $svg_file ); | |
// filter the markup | |
$allowed = array( | |
'svg' => array( | |
'width' => array(), | |
'height' => array(), | |
'viewbox' => array(), | |
'version' => array(), | |
'xmlns' => array(), | |
'xmlns:xlink' => array(), | |
), | |
'g' => array( | |
'stroke' => array(), | |
'stroke-width' => array(), | |
// 'fill' => array(), | |
'fill-rule' => array(), | |
), | |
'path' => array( | |
'd' => array(), | |
'id' => array(), | |
), | |
); | |
return wp_kses( $svg_markup, $allowed ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment