| 1 |
<?php |
| 2 |
// If this file is called directly, abort. |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
//Sanitizes a comma separated CSS selectors with class and id names to ensure it only contains valid characters. |
| 8 |
//Complex selectors (for ex. [name*="value"]) are not allowed. |
| 9 |
//Allowed characters: A-Z, a-z, 0-9, _, -, .(dot), >, (space), #, ,(comma) |
| 10 |
function borderless_svg_sanitize_css_selectors( $selectors ) { |
| 11 |
$selectors = htmlspecialchars_decode( $selectors ); |
| 12 |
|
| 13 |
//Strip out any % encoded octets |
| 14 |
$sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $selectors ); |
| 15 |
|
| 16 |
//Limit to A-Z, a-z, 0-9, _, -, .(dot), >, (space), #, ,(comma) |
| 17 |
$sanitized = preg_replace( '/[^A-Za-z0-9 _,.#>-]/', '', $sanitized ); |
| 18 |
|
| 19 |
//convert the ">" (greater-than) sign to > for storing in a database |
| 20 |
$sanitized = htmlspecialchars($sanitized); |
| 21 |
|
| 22 |
return apply_filters( 'borderless_svg_sanitize_css_selectors', $sanitized ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
//Allow SVG through WordPress Media Uploader |
| 27 |
function borderless_svg_cc_mime_types($mimes) { |
| 28 |
$mimes['svg'] = 'image/svg+xml'; |
| 29 |
$mimes['svgz'] = 'image/svg+xml'; |
| 30 |
return $mimes; |
| 31 |
} |
| 32 |
add_filter('upload_mimes', 'borderless_svg_cc_mime_types'); |
| 33 |
|
| 34 |
/** |
| 35 |
* TEMP FIX FOR 4.7.1 |
| 36 |
* Issue should be fixed in 4.7.2 in which case this will be deleted. |
| 37 |
*/ |
| 38 |
function borderless_svgs_disable_real_mime_check( $data, $file, $filename, $mimes ) { |
| 39 |
$wp_filetype = wp_check_filetype( $filename, $mimes ); |
| 40 |
|
| 41 |
$ext = $wp_filetype['ext']; |
| 42 |
$type = $wp_filetype['type']; |
| 43 |
$proper_filename = $data['proper_filename']; |
| 44 |
|
| 45 |
return compact( 'ext', 'type', 'proper_filename' ); |
| 46 |
} |
| 47 |
add_filter( 'wp_check_filetype_and_ext', 'borderless_svgs_disable_real_mime_check', 10, 4 ); |
| 48 |
|
| 49 |
//Sanitize SVG code of a file during uploading into media library: remove all JavaScript tags and attributes. |
| 50 |
function borderless_sanitize_svg( $file ) { |
| 51 |
if( $file['type'] == 'image/svg+xml' ) { |
| 52 |
|
| 53 |
require_once 'sanitizer.php'; |
| 54 |
|
| 55 |
$svg = new BORDERLESS_SvgSanitizer(); |
| 56 |
|
| 57 |
$svg->load_svg( $file['tmp_name'] ); |
| 58 |
$svg->borderless_sanitize_svg(); |
| 59 |
$sanitized_svg = $svg->save_svg(); |
| 60 |
|
| 61 |
global $wp_filesystem; |
| 62 |
$creds = request_filesystem_credentials( admin_url(), '', FALSE, FALSE, array() ); |
| 63 |
if ( ! WP_Filesystem( $creds ) ) { |
| 64 |
request_filesystem_credentials( admin_url(), '', TRUE, FALSE, NULL ); |
| 65 |
} |
| 66 |
|
| 67 |
$replaced = $wp_filesystem->put_contents( $file['tmp_name'], $sanitized_svg, FS_CHMOD_FILE ); |
| 68 |
} |
| 69 |
|
| 70 |
return $file; |
| 71 |
} |
| 72 |
add_filter( 'wp_handle_upload_prefilter', 'borderless_sanitize_svg' ); |
| 73 |
|
| 74 |
|
| 75 |
//Fixing SVG width and height attributes to show correctly in TinyMCE editor |
| 76 |
function borderless_svg_fix_svg_size_attributes( $out, $id ) { |
| 77 |
$image_url = wp_get_attachment_url( $id ); |
| 78 |
$file_ext = pathinfo( $image_url, PATHINFO_EXTENSION ); |
| 79 |
if ( ! is_admin() || 'svg' !== $file_ext ) |
| 80 |
{ |
| 81 |
return false; |
| 82 |
} |
| 83 |
return array( $image_url, null, null, false ); |
| 84 |
} |
| 85 |
add_filter( 'image_downsize', 'borderless_svg_fix_svg_size_attributes', 10, 2 ); |
| 86 |
|
| 87 |
|
| 88 |
//Fixing SVG width and height attributes to show correctly in Media Library in grid mode |
| 89 |
function borderless_svg_prepare_attachment_for_js_filter($response, $attachment, $meta){ |
| 90 |
if( $response['mime'] == 'image/svg+xml' && empty($response['sizes']) ){ |
| 91 |
$svg_file_path = get_attached_file( $attachment->ID ); |
| 92 |
|
| 93 |
$orig_size = borderless_svg_get_original_svg_size( $svg_file_path ); |
| 94 |
|
| 95 |
$response['sizes'] = array( |
| 96 |
'full' => array( |
| 97 |
'url' => $response['url'], |
| 98 |
'width' => $orig_size[0], |
| 99 |
'height' => $orig_size[1] |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
$arr = array( 'width' => $orig_size[0], 'height' => $orig_size[1] ); |
| 104 |
wp_update_attachment_metadata( $attachment->ID, $arr ); |
| 105 |
} |
| 106 |
return $response; |
| 107 |
} |
| 108 |
//get width and height attributes of uploded SVG |
| 109 |
function borderless_svg_get_original_svg_size($file) { |
| 110 |
$arr = array(); |
| 111 |
$xml_get = simplexml_load_file($file); |
| 112 |
$xml_attrs = $xml_get->attributes(); |
| 113 |
|
| 114 |
$width = (string) $xml_attrs->width; |
| 115 |
if ( empty($width) ) { |
| 116 |
$width = '100%'; |
| 117 |
} |
| 118 |
|
| 119 |
$height = (string) $xml_attrs->height; |
| 120 |
if ( empty($height) ) { |
| 121 |
$height = '100%'; |
| 122 |
} |
| 123 |
|
| 124 |
$arr[] = $width; |
| 125 |
$arr[] = $height; |
| 126 |
|
| 127 |
return $arr; |
| 128 |
} |
| 129 |
add_filter('wp_prepare_attachment_for_js', 'borderless_svg_prepare_attachment_for_js_filter', 10, 3); |
| 130 |
|
| 131 |
//Define styles and scripts for site's front-end |
| 132 |
|
| 133 |
function borderless_svg_scripts() { |
| 134 |
wp_enqueue_script( 'borderless_svg_js', plugins_url( '/svg.min.js', __FILE__ ), array('jquery'), BORDERLESS__VERSION, true ); |
| 135 |
|
| 136 |
wp_enqueue_script( 'borderless_svg_js' ); |
| 137 |
} |
| 138 |
add_action('wp_enqueue_scripts', 'borderless_svg_scripts'); |
| 139 |
|
| 140 |
|
| 141 |
?> |