From ebc053126485a5cb432e5bf86698c84e209c4358 Mon Sep 17 00:00:00 2001 From: sksaju Date: Tue, 26 Sep 2023 05:38:17 +0600 Subject: [PATCH 1/5] refactor svg_dimensions function to calculate dimensions using existing metadata --- safe-svg.php | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index c1602c2a..3b485940 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -328,7 +328,7 @@ protected function is_gzipped( $contents ) { public function fix_admin_preview( $response, $attachment, $meta ) { if ( 'image/svg+xml' === $response['mime'] ) { - $dimensions = $this->svg_dimensions( get_attached_file( $attachment->ID ) ); + $dimensions = $this->svg_dimensions( $attachment->ID ); if ( $dimensions ) { $response = array_merge( $response, $dimensions ); @@ -384,7 +384,7 @@ public function fix_admin_preview( $response, $attachment, $meta ) { */ public function one_pixel_fix( $image, $attachment_id, $size, $icon ) { if ( get_post_mime_type( $attachment_id ) === 'image/svg+xml' ) { - $dimensions = $this->svg_dimensions( get_attached_file( $attachment_id ) ); + $dimensions = $this->svg_dimensions( $attachment_id ); if ( $dimensions ) { $image[1] = $dimensions['width']; @@ -445,7 +445,7 @@ public function get_image_tag_override( $html, $id, $alt, $title, $align, $size $width = $size[0]; $height = $size[1]; // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure - } elseif ( 'full' === $size && $dimensions = $this->svg_dimensions( get_attached_file( $id ) ) ) { + } elseif ( 'full' === $size && $dimensions = $this->svg_dimensions( $id ) ) { $width = $dimensions['width']; $height = $dimensions['height']; } else { @@ -485,7 +485,7 @@ public function skip_svg_regeneration( $metadata, $attachment_id ) { $relative_path = str_replace( trailingslashit( $upload_dir['basedir'] ), '', $svg_path ); $filename = basename( $svg_path ); - $dimensions = $this->svg_dimensions( $svg_path ); + $dimensions = $this->svg_dimensions( $attachment_id ); if ( ! $dimensions ) { return $metadata; @@ -564,15 +564,22 @@ public function metadata_error_fix( $data, $post_id ) { * * @return array|bool */ - protected function svg_dimensions( $svg ) { + protected function svg_dimensions( $attachment_id ) { if ( ! function_exists( 'simplexml_load_file' ) ) { return false; } - $svg = @simplexml_load_file( $svg ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - $width = 0; - $height = 0; - if ( $svg ) { + $svg = get_attached_file( $attachment_id ); + $metadata = wp_get_attachment_metadata( $attachment_id ); + $width = 0; + $height = 0; + + if ( $svg && ! empty( $metadata['width'] ) && empty( $metadata['height'] ) ) { + $width = floatval( $metadata['width'] ); + $height = floatval( $metadata['height'] ); + } else if ( $svg ) { + $svg = @simplexml_load_file( $svg ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + $attributes = $svg->attributes(); if ( isset( $attributes->viewBox ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase @@ -627,11 +634,21 @@ protected function svg_dimensions( $svg ) { } } - return array( + $dimensions = array( 'width' => $width, 'height' => $height, 'orientation' => ( $width > $height ) ? 'landscape' : 'portrait', ); + + /** + * Calculate SVG dimensions and orientation. + * + * @param array $dimensions An array containing width, height, and orientation. + * @param string $svg The file path to the SVG. + * + * @return array An array of SVG dimensions and orientation. + */ + return apply_filters( 'safe_svg_dimensions', $dimensions, $svg ); } /** From daadc14e853ae4a91f3ddf277bf5f01f08136f36 Mon Sep 17 00:00:00 2001 From: sksaju Date: Mon, 27 Nov 2023 17:38:58 +0600 Subject: [PATCH 2/5] added safe_svg_pre_dimensions filter in the svg_dimensions function --- safe-svg.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/safe-svg.php b/safe-svg.php index 3b485940..f5a30c7c 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -565,6 +565,23 @@ public function metadata_error_fix( $data, $post_id ) { * @return array|bool */ protected function svg_dimensions( $attachment_id ) { + /** + * Calculate SVG dimensions and orientation. + * + * This filter allows you to implement your own sizing. By returning a non-false + * value, it will short-circuit this function and return your set value. + * + * @param boolean Default value of the filter. + * @param integer $attachment_id The attachment ID of the SVG being processed. + * + * @return array|false An array of SVG dimensions and orientation or false. + */ + $short_circuit = apply_filters( 'safe_svg_pre_dimensions', false, $attachment_id ); + + if ( false !== $short_circuit ) { + return $short_circuit; + } + if ( ! function_exists( 'simplexml_load_file' ) ) { return false; } From cede2e82f294f8796f204899e5b51ec501250990 Mon Sep 17 00:00:00 2001 From: sksaju Date: Mon, 27 Nov 2023 17:43:47 +0600 Subject: [PATCH 3/5] fix phpcs issue on fix/75-slow-load-times-due-to-svgdimensions --- safe-svg.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index f5a30c7c..aec97cd9 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -560,7 +560,7 @@ public function metadata_error_fix( $data, $post_id ) { /** * Get SVG size from the width/height or viewport. * - * @param string|false $svg The file path to where the SVG file should be, false otherwise. + * @param integer $attachment_id The attachment ID of the SVG being processed. * * @return array|bool */ @@ -594,7 +594,7 @@ protected function svg_dimensions( $attachment_id ) { if ( $svg && ! empty( $metadata['width'] ) && empty( $metadata['height'] ) ) { $width = floatval( $metadata['width'] ); $height = floatval( $metadata['height'] ); - } else if ( $svg ) { + } elseif ( $svg ) { $svg = @simplexml_load_file( $svg ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $attributes = $svg->attributes(); From 58ee0e8249eebf551f2c171e0c00c05c50093d03 Mon Sep 17 00:00:00 2001 From: sksaju Date: Tue, 12 Dec 2023 20:45:53 +0600 Subject: [PATCH 4/5] fix php unit test issue --- tests/unit/test-safe-svg.php | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/unit/test-safe-svg.php b/tests/unit/test-safe-svg.php index 93bfe203..ab56eae2 100644 --- a/tests/unit/test-safe-svg.php +++ b/tests/unit/test-safe-svg.php @@ -208,6 +208,64 @@ public function test_one_pixel_fix() { ) ); + \WP_Mock::userFunction( + 'wp_get_attachment_metadata', + array( + 'args' => 1, + 'return_in_order' => array( + array( + 'width' => 600, + 'height' => 600, + 'file' => __DIR__ . '/files/svgCleanOne.svg', + 'sizes' => array( + 'thumbnail' => array( + 'width' => 150, + 'height' => 150, + 'crop' => 1, + 'file' => 'svgCleanOne.svg', + 'mime-type' => 'image/svg+xml', + ), + 'medium' => array( + 'width' => 300, + 'height' => 300, + 'crop' => 0, // Set to 0 if you don't want to crop + 'file' => 'svgCleanOne.svg', + 'mime-type' => 'image/svg+xml', + ), + 'medium_large' => array( + 'width' => 768, + 'height' => 0, + 'crop' => 0, + 'file' => 'svgCleanOne.svg', + 'mime-type' => 'image/svg+xml', + ), + 'large' => array( + 'width' => 1024, + 'height' => 1024, + 'crop' => 0, + 'file' => 'svgCleanOne.svg', + 'mime-type' => 'image/svg+xml', + ), + '1536x1536' => array( + 'width' => 1536, + 'height' => 1536, + 'crop' => 0, + 'file' => 'svgCleanOne.svg', + 'mime-type' => 'image/svg+xml', + ), + '2048x2048' => array( + 'width' => 2048, + 'height' => 2048, + 'crop' => 0, + 'file' => 'svgCleanOne.svg', + 'mime-type' => 'image/svg+xml', + ), + ), + ), + ), + ) + ); + // Test SVG Dimensions $image_sizes = $this->instance->one_pixel_fix( array(), 1, 'thumbnail', false ); if ( ! empty( $image_sizes ) ) { From d0b5b8a2a7d9776bfa639f9b912ccf090686e01c Mon Sep 17 00:00:00 2001 From: sksaju Date: Tue, 12 Dec 2023 20:58:37 +0600 Subject: [PATCH 5/5] fix e2e test issue --- package-lock.json | 16 +++++++++++++++- package.json | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index a5eec80f..ace60594 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,8 @@ "@wordpress/env": "^5.14.0", "@wordpress/icons": "^9.20.0", "@wordpress/scripts": "^26.0.0", - "cypress": "^13.3.0" + "cypress": "^13.3.0", + "mochawesome-json-to-md": "^0.7.2" } }, "node_modules/@10up/cypress-wp-utils": { @@ -12791,6 +12792,19 @@ "mocha": ">=7" } }, + "node_modules/mochawesome-json-to-md": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/mochawesome-json-to-md/-/mochawesome-json-to-md-0.7.2.tgz", + "integrity": "sha512-dxh+o73bhC6nEph6fNky9wy35R+2oK3ueXwAlJ/COAanlFgu8GuvGzQ00VNO4PPYhYGDsO4vbt4QTcMA3lv25g==", + "deprecated": "🙌 Thanks for using it. We recommend upgrading to the newer version, 1.x.x. Check out https://www.npmjs.com/package/mochawesome-json-to-md for details.", + "dev": true, + "dependencies": { + "yargs": "^17.0.1" + }, + "bin": { + "mochawesome-json-to-md": "index.js" + } + }, "node_modules/mochawesome-merge": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/mochawesome-merge/-/mochawesome-merge-4.3.0.tgz", diff --git a/package.json b/package.json index 9d5ac0f4..07373800 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "@wordpress/env": "^5.14.0", "@wordpress/icons": "^9.20.0", "@wordpress/scripts": "^26.0.0", - "cypress": "^13.3.0" + "cypress": "^13.3.0", + "mochawesome-json-to-md": "^0.7.2" }, "dependencies": { "cypress-mochawesome-reporter": "^3.4.0",