From 567c4be047b36013ec1c0eb6ebf415fe147c982b Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Mon, 22 Jul 2024 20:40:48 +0200 Subject: [PATCH 1/8] Update how custom svg dimesions are applied Update how custom svg dimesions are applied for `get_image_tag` and `wp_get_attachment_image` functions Separate the logic for defining custom dimensions in a `set_svg_dimension` function --- safe-svg.php | 54 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index 7ee08c92..fd2ae7d9 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -395,11 +395,12 @@ 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( $attachment_id ); + $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); + $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); - if ( $dimensions ) { - $image[1] = $dimensions['width']; - $image[2] = $dimensions['height']; + if ( $height && $width ) { + $image[1] = $width; + $image[2] = $height; } else { $image[1] = 100; $image[2] = 100; @@ -450,19 +451,9 @@ public function load_custom_admin_style() { */ public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id ); - if ( 'image/svg+xml' === $mime ) { - if ( is_array( $size ) ) { - $width = $size[0]; - $height = $size[1]; - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure - } elseif ( 'full' === $size && $dimensions = $this->svg_dimensions( $id ) ) { - $width = $dimensions['width']; - $height = $dimensions['height']; - } else { - $width = get_option( "{$size}_size_w", false ); - $height = get_option( "{$size}_size_h", false ); - } + $width = $this->set_svg_dimension( 'width', $size, $id ); + $height = $this->set_svg_dimension( 'height', $size, $id ); if ( $height && $width ) { $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html ); @@ -728,6 +719,37 @@ protected function str_ends_with( $haystack, $needle ) { return 0 === substr_compare( $haystack, $needle, -$len, $len ); } + /** + * Set custom width or height of the SVG image. + * + * @param string $dimension Dimension quality of the image. Either 'width' or 'height'. + * @param string|array $size Size of image. Image size or array of width and height values + * (in that order). Default 'thumbnail'. + * @param int $id Image attachment ID. + * + * @return int|false Width or height of the SVG image, or false if not found. + */ + public function set_svg_dimension( $dimension, $size, $id ) { + $dimensions = $this->svg_dimensions( $id ); + + if ( is_array( $size ) ) { + $width = $size[0]; + $height = $size[1]; + } elseif ( 'full' === $size && $dimensions ) { + $width = $dimensions['width']; + $height = $dimensions['height']; + } else { + $width = get_option( "{$size}_size_w", false ); + $height = get_option( "{$size}_size_h", false ); + } + + if ($dimension === 'width') { + return $width ?: false; + } else { + return $height ?: false; + } + } + } } From e270b3804193f88bbb9ec992b5ab568e72961460 Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Mon, 22 Jul 2024 20:59:56 +0200 Subject: [PATCH 2/8] PHPCS fixes --- safe-svg.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index fd2ae7d9..43a01d43 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -395,8 +395,8 @@ 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' ) { - $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); - $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); + $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); + $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); if ( $height && $width ) { $image[1] = $width; @@ -452,8 +452,8 @@ public function load_custom_admin_style() { public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id ); if ( 'image/svg+xml' === $mime ) { - $width = $this->set_svg_dimension( 'width', $size, $id ); - $height = $this->set_svg_dimension( 'height', $size, $id ); + $width = $this->set_svg_dimension( 'width', $size, $id ); + $height = $this->set_svg_dimension( 'height', $size, $id ); if ( $height && $width ) { $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html ); @@ -743,10 +743,12 @@ public function set_svg_dimension( $dimension, $size, $id ) { $height = get_option( "{$size}_size_h", false ); } - if ($dimension === 'width') { - return $width ?: false; + if ( 'width' === $dimension ) { + return $width; + } elseif ( 'height' === $dimension ) { + return $height; } else { - return $height ?: false; + return false; } } From 4525a81356b0bdca197edc6f138196dc7250b490 Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Mon, 22 Jul 2024 21:02:11 +0200 Subject: [PATCH 3/8] PHPCS fixes --- safe-svg.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index 43a01d43..90a859a6 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -395,7 +395,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' ) { - $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); + $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); if ( $height && $width ) { @@ -452,8 +452,8 @@ public function load_custom_admin_style() { public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id ); if ( 'image/svg+xml' === $mime ) { - $width = $this->set_svg_dimension( 'width', $size, $id ); - $height = $this->set_svg_dimension( 'height', $size, $id ); + $width = $this->set_svg_dimension( 'width', $size, $id ); + $height = $this->set_svg_dimension( 'height', $size, $id ); if ( $height && $width ) { $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html ); @@ -743,7 +743,7 @@ public function set_svg_dimension( $dimension, $size, $id ) { $height = get_option( "{$size}_size_h", false ); } - if ( 'width' === $dimension ) { + if ( 'width' === $dimension ) { return $width; } elseif ( 'height' === $dimension ) { return $height; From 493793fad520091f75abc6972627e25445e74a8c Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Mon, 22 Jul 2024 21:03:07 +0200 Subject: [PATCH 4/8] PHPCS fixes --- safe-svg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safe-svg.php b/safe-svg.php index 90a859a6..22bd4928 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -396,7 +396,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' ) { $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); - $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); + $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); if ( $height && $width ) { $image[1] = $width; From 79fb26b06e190fe1c7788b104e43441b17b71f0c Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Mon, 22 Jul 2024 21:42:54 +0200 Subject: [PATCH 5/8] Refactor of implementation and tests --- safe-svg.php | 31 ++++++++++++++----------------- tests/unit/test-safe-svg.php | 2 +- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index 22bd4928..24eeb65f 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -395,12 +395,11 @@ 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' ) { - $width = $this->set_svg_dimension( 'width', $size, $attachment_id ); - $height = $this->set_svg_dimension( 'height', $size, $attachment_id ); + $dimensions = $this->set_svg_dimensions( $size, $attachment_id ); - if ( $height && $width ) { - $image[1] = $width; - $image[2] = $height; + if ( $dimensions && $dimensions['height'] && $dimensions['width'] ) { + $image[1] = $dimensions['width']; + $image[2] = $dimensions['height']; } else { $image[1] = 100; $image[2] = 100; @@ -452,12 +451,11 @@ public function load_custom_admin_style() { public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id ); if ( 'image/svg+xml' === $mime ) { - $width = $this->set_svg_dimension( 'width', $size, $id ); - $height = $this->set_svg_dimension( 'height', $size, $id ); + $dimensions = $this->set_svg_dimensions( $size, $id ); - if ( $height && $width ) { - $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html ); - $html = str_replace( 'height="1" ', sprintf( 'height="%s" ', $height ), $html ); + if ( $dimensions['height'] && $dimensions['width'] ) { + $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $dimensions['width'] ), $html ); + $html = str_replace( 'height="1" ', sprintf( 'height="%s" ', $dimensions['height'] ), $html ); } else { $html = str_replace( 'width="1" ', '', $html ); $html = str_replace( 'height="1" ', '', $html ); @@ -729,7 +727,7 @@ protected function str_ends_with( $haystack, $needle ) { * * @return int|false Width or height of the SVG image, or false if not found. */ - public function set_svg_dimension( $dimension, $size, $id ) { + protected function set_svg_dimensions( $size, $id ) { $dimensions = $this->svg_dimensions( $id ); if ( is_array( $size ) ) { @@ -743,13 +741,12 @@ public function set_svg_dimension( $dimension, $size, $id ) { $height = get_option( "{$size}_size_h", false ); } - if ( 'width' === $dimension ) { - return $width; - } elseif ( 'height' === $dimension ) { - return $height; - } else { - return false; + if ( $dimensions ) { + $dimensions['width'] = $width; + $dimensions['height'] = $height; } + + return $dimensions; } } diff --git a/tests/unit/test-safe-svg.php b/tests/unit/test-safe-svg.php index 061c175b..f655e891 100644 --- a/tests/unit/test-safe-svg.php +++ b/tests/unit/test-safe-svg.php @@ -270,7 +270,7 @@ public function test_one_pixel_fix() { ); // Test SVG Dimensions - $image_sizes = $this->instance->one_pixel_fix( array(), 1, 'thumbnail', false ); + $image_sizes = $this->instance->one_pixel_fix( array(), 1, 'full', false ); if ( ! empty( $image_sizes ) ) { $image_sizes = array_map( 'intval', $image_sizes ); } From b6300314489e399370ae30cf5389ff37a1a5428b Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Mon, 22 Jul 2024 21:44:57 +0200 Subject: [PATCH 6/8] PHPCS fixes --- safe-svg.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index 24eeb65f..33a497c4 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -395,7 +395,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->set_svg_dimensions( $size, $attachment_id ); + $dimensions = $this->set_svg_dimensions( $attachment_id, $size ); if ( $dimensions && $dimensions['height'] && $dimensions['width'] ) { $image[1] = $dimensions['width']; @@ -451,7 +451,7 @@ public function load_custom_admin_style() { public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id ); if ( 'image/svg+xml' === $mime ) { - $dimensions = $this->set_svg_dimensions( $size, $id ); + $dimensions = $this->set_svg_dimensions( $id, $size ); if ( $dimensions['height'] && $dimensions['width'] ) { $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $dimensions['width'] ), $html ); @@ -720,14 +720,13 @@ protected function str_ends_with( $haystack, $needle ) { /** * Set custom width or height of the SVG image. * - * @param string $dimension Dimension quality of the image. Either 'width' or 'height'. - * @param string|array $size Size of image. Image size or array of width and height values + * @param int $id Image attachment ID. + * @param string|array $size Size of image. Image size or array of width and height values * (in that order). Default 'thumbnail'. - * @param int $id Image attachment ID. * * @return int|false Width or height of the SVG image, or false if not found. */ - protected function set_svg_dimensions( $size, $id ) { + protected function set_svg_dimensions( $id, $size ) { $dimensions = $this->svg_dimensions( $id ); if ( is_array( $size ) ) { From fdea6e062706dcdfc2e4c53630ff2393d9c6ebee Mon Sep 17 00:00:00 2001 From: gabriel-glo Date: Fri, 30 Aug 2024 17:54:22 +0200 Subject: [PATCH 7/8] Update based on CR feedback --- safe-svg.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/safe-svg.php b/safe-svg.php index 33a497c4..8d5fa747 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -395,7 +395,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->set_svg_dimensions( $attachment_id, $size ); + $dimensions = $this->get_svg_dimensions( $attachment_id, $size ); if ( $dimensions && $dimensions['height'] && $dimensions['width'] ) { $image[1] = $dimensions['width']; @@ -451,7 +451,7 @@ public function load_custom_admin_style() { public function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) { $mime = get_post_mime_type( $id ); if ( 'image/svg+xml' === $mime ) { - $dimensions = $this->set_svg_dimensions( $id, $size ); + $dimensions = $this->get_svg_dimensions( $id, $size ); if ( $dimensions['height'] && $dimensions['width'] ) { $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $dimensions['width'] ), $html ); @@ -718,21 +718,21 @@ protected function str_ends_with( $haystack, $needle ) { } /** - * Set custom width or height of the SVG image. + * Return custom width or height of the SVG image. * * @param int $id Image attachment ID. * @param string|array $size Size of image. Image size or array of width and height values * (in that order). Default 'thumbnail'. * - * @return int|false Width or height of the SVG image, or false if not found. + * @return array|bool Width or height of the SVG image, or false if not found. */ - protected function set_svg_dimensions( $id, $size ) { + protected function get_svg_dimensions( $id, $size ) { $dimensions = $this->svg_dimensions( $id ); if ( is_array( $size ) ) { $width = $size[0]; $height = $size[1]; - } elseif ( 'full' === $size && $dimensions ) { + } elseif ( 'full' === $size && is_array( $dimensions ) && isset( $dimensions['width'], $dimensions['height'] ) ) { $width = $dimensions['width']; $height = $dimensions['height']; } else { From 6c701db271499b3ed23d7ef33f63f2bfa2b938bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Glogo=C5=A1ki?= Date: Sun, 8 Sep 2024 20:30:21 +0200 Subject: [PATCH 8/8] Update safe-svg.php Co-authored-by: Darin Kotter --- safe-svg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safe-svg.php b/safe-svg.php index 1d721eb3..074f4372 100644 --- a/safe-svg.php +++ b/safe-svg.php @@ -733,7 +733,7 @@ protected function get_svg_dimensions( $id, $size ) { if ( is_array( $size ) ) { $width = $size[0]; $height = $size[1]; - } elseif ( 'full' === $size && is_array( $dimensions ) && isset( $dimensions['width'], $dimensions['height'] ) ) { + } elseif ( 'full' === $size && is_array( $dimensions ) && isset( $dimensions['width'], $dimensions['height'] ) ) { $width = $dimensions['width']; $height = $dimensions['height']; } else {