From 97f4700f1e126b6660de597dd0a3f6d936f0b7a9 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Fri, 7 Jan 2022 15:34:23 +0530 Subject: [PATCH 1/4] #15 - fix file path in image meta - remove slash from file path --- safe-svg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safe-svg.php b/safe-svg.php index 0dcb86bc..f987d915 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -327,7 +327,7 @@ function skip_svg_regeneration( $metadata, $attachment_id ) { $svg_path = get_attached_file( $attachment_id ); $upload_dir = wp_upload_dir(); // get the path relative to /uploads/ - found no better way: - $relative_path = str_replace( $upload_dir['basedir'], '', $svg_path ); + $relative_path = str_replace( trailingslashit( $upload_dir['basedir'] ), '', $svg_path ); $filename = basename( $svg_path ); $dimensions = $this->svg_dimensions( $svg_path ); From 640fefb6cfdb516a5bd8d226342bfe603e255a4f Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Fri, 7 Jan 2022 16:10:36 +0530 Subject: [PATCH 2/4] #15 - fix file path in image meta for old svg uploads --- safe-svg.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/safe-svg.php b/safe-svg.php index f987d915..3c5572ec 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -49,6 +49,7 @@ function __construct() { add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_upgrade_link' ) ); add_filter( 'wp_get_attachment_metadata', array( $this, 'metadata_error_fix' ), 10, 2 ); add_filter( 'wp_get_attachment_image_attributes', array( $this, 'fix_direct_image_output' ), 10, 3 ); + add_filter( 'wp_calculate_image_srcset_meta', array( $this, 'fix_svg_image_meta' ), 10 ); } /** @@ -482,6 +483,20 @@ public function fix_direct_image_output( $attr, $attachment, $size = 'thumbnail' return $attr; } + /** + * Fix svg file path in attachment meta for old uploads. + * + * @param array $image_meta Attachment meta. + * + * @return array Attachment meta. + */ + public function fix_svg_image_meta( $image_meta ) { + if ( ! empty( $image_meta['file'] ) && '.svg' === substr( $image_meta['file'], -4 ) && false !== strpos( $image_meta['file'], '/', 1 ) ) { + $image_meta['file'] = substr( $image_meta['file'], 1 ); + } + return $image_meta; + } + } } From 06b439934ea9687db04c95c8fdf3d4f8668a7304 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 18 Jan 2022 15:54:13 +0530 Subject: [PATCH 3/4] #15 - fix svg image width and height typecast to number/float --- safe-svg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safe-svg.php b/safe-svg.php index 3c5572ec..8ebd43ad 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -427,7 +427,7 @@ protected function svg_dimensions( $svg ) { $height = 0; if ( $svg ) { $attributes = $svg->attributes(); - if ( isset( $attributes->width, $attributes->height ) && is_numeric( $attributes->width ) && is_numeric( $attributes->height ) ) { + if ( isset( $attributes->width, $attributes->height ) && is_numeric( (float)$attributes->width ) && is_numeric( (float)$attributes->height ) ) { $width = floatval( $attributes->width ); $height = floatval( $attributes->height ); } elseif ( isset( $attributes->viewBox ) ) { From 10bb4d611c112f30cc3b6b412377c363659463b5 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 18 Jan 2022 15:59:54 +0530 Subject: [PATCH 4/4] #15 - fix for old svg uploads removed --- safe-svg.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index 8ebd43ad..fa6d9583 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -49,7 +49,6 @@ function __construct() { add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_upgrade_link' ) ); add_filter( 'wp_get_attachment_metadata', array( $this, 'metadata_error_fix' ), 10, 2 ); add_filter( 'wp_get_attachment_image_attributes', array( $this, 'fix_direct_image_output' ), 10, 3 ); - add_filter( 'wp_calculate_image_srcset_meta', array( $this, 'fix_svg_image_meta' ), 10 ); } /** @@ -482,21 +481,6 @@ public function fix_direct_image_output( $attr, $attachment, $size = 'thumbnail' return $attr; } - - /** - * Fix svg file path in attachment meta for old uploads. - * - * @param array $image_meta Attachment meta. - * - * @return array Attachment meta. - */ - public function fix_svg_image_meta( $image_meta ) { - if ( ! empty( $image_meta['file'] ) && '.svg' === substr( $image_meta['file'], -4 ) && false !== strpos( $image_meta['file'], '/', 1 ) ) { - $image_meta['file'] = substr( $image_meta['file'], 1 ); - } - return $image_meta; - } - } }