Skip to content

Commit

Permalink
Merge pull request #216 from 10up/6-svg-height-and-width-attributes
Browse files Browse the repository at this point in the history
Update how custom svg dimesions are applied
  • Loading branch information
dkotter authored Nov 20, 2024
2 parents fa36531 + fefd38b commit d5b1a52
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
53 changes: 36 additions & 17 deletions safe-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ 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 );
$dimensions = $this->get_svg_dimensions( $attachment_id, $size );

if ( $dimensions ) {
if ( is_array( $dimensions ) && isset( $dimensions['height'], $dimensions['width'] ) ) {
$image[1] = $dimensions['width'];
$image[2] = $dimensions['height'];
} else {
Expand Down Expand Up @@ -499,23 +499,12 @@ 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 );
}
$dimensions = $this->get_svg_dimensions( $id, $size );

if ( $height && $width ) {
$html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html );
$html = str_replace( 'height="1" ', sprintf( 'height="%s" ', $height ), $html );
if ( is_array( $dimensions ) && isset( $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 );
Expand Down Expand Up @@ -777,6 +766,36 @@ protected function str_ends_with( $haystack, $needle ) {
return 0 === substr_compare( $haystack, $needle, -$len, $len );
}

/**
* 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 array|bool Width or height of the SVG image, or false if not found.
*/
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 && is_array( $dimensions ) && isset( $dimensions['width'], $dimensions['height'] ) ) {
$width = $dimensions['width'];
$height = $dimensions['height'];
} else {
$width = get_option( "{$size}_size_w", false );
$height = get_option( "{$size}_size_h", false );
}

if ( $dimensions ) {
$dimensions['width'] = $width;
$dimensions['height'] = $height;
}

return $dimensions;
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion tests/unit/test-safe-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public function test_one_pixel_fix() {
'return_in_order' => array(
__DIR__ . '/files/svgCleanOne.svg',
__DIR__ . '/files/svgNoDimensions.svg',
__DIR__ . '/files/svgCleanOne.svg',
),
)
);
Expand Down Expand Up @@ -274,7 +275,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 );
}
Expand All @@ -298,6 +299,19 @@ public function test_one_pixel_fix() {
),
$image_sizes
);

// Test Custom Dimensions
$image_sizes = $this->instance->one_pixel_fix( array(), 1, [ 500, 500 ], false );
if ( ! empty( $image_sizes ) ) {
$image_sizes = array_map( 'intval', $image_sizes );
}
$this->assertSame(
array(
1 => 500,
2 => 500,
),
$image_sizes
);
}

/**
Expand Down

0 comments on commit d5b1a52

Please sign in to comment.