-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix | Slow load times due to svg_dimensions #154
Changes from 4 commits
ebc0531
daadc14
cede2e8
b07176d
58ee0e8
d0b5b8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -560,19 +560,43 @@ 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 | ||
*/ | ||
protected function svg_dimensions( $svg ) { | ||
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; | ||
} | ||
|
||
$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'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sksaju Sorry, I know this is late and this PR is already merged but in looking at this prior to prepping the next release, on the surface it seems like this conditional is not correct. It checks if ( $svg && ! empty( $metadata['width'] ) && ! empty( $metadata['height'] ) ) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$width = floatval( $metadata['width'] ); | ||
$height = floatval( $metadata['height'] ); | ||
} elseif ( $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 +651,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 ); | ||
Comment on lines
+659
to
+668
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sksaju @10up/open-source-practice I like this filter, but what are your thoughts on introducing a My thinking is, if someone wants to override this value, forcing them through the overhead of loading and sizing the SVG seems a little odd. Something like the following at the top of this method to allow the short-circuit might be useful: /**
* 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 array $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', '__return_false' );
if ( false !== $short_circuit ) {
return $short_circuit;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @darylldoyle I've updated the PR, please take a look again and let me know your feedback. could you also let me know how to fix this https://github.com/10up/safe-svg/actions/runs/7004501147/job/19052370329?pr=154 UnitTest issue? Thanks |
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise (non-blocking): Nice work here!