Skip to content

Commit

Permalink
Update how custom svg dimesions are applied
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gabriel-glo committed Jul 22, 2024
1 parent 1452db6 commit 567c4be
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions safe-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
}

}
}

Expand Down

0 comments on commit 567c4be

Please sign in to comment.