Skip to content
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

Merged
merged 6 commits into from
Dec 12, 2023

Conversation

sksaju
Copy link
Member

@sksaju sksaju commented Sep 25, 2023

Description of the Change

We have refactored the svg_dimensions function to calculate dimensions using the existing attachment meta.

Closes #75 and #151

How to test the Change

Changelog Entry

Added - New feature
Changed - Existing functionality
Deprecated - Soon-to-be removed feature
Removed - Feature
Fixed - Bug fix
Security - Vulnerability

Credits

Props @username, @username2, ...

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@sksaju sksaju requested a review from a team as a code owner September 25, 2023 23:44
@sksaju sksaju requested review from Sidsector9 and removed request for a team September 25, 2023 23:44
@sksaju sksaju self-assigned this Sep 25, 2023
@sksaju sksaju marked this pull request as draft September 25, 2023 23:46
@jeffpaul jeffpaul added this to the 2.2.1 milestone Sep 26, 2023
@Sidsector9
Copy link
Member

@sksaju the PR is in draft, is this ready for review?

@sksaju
Copy link
Member Author

sksaju commented Sep 28, 2023

@sksaju the PR is in draft, is this ready for review?

Not yet. I just need to fix the unit test issue, which I can do early next week :)

Comment on lines +642 to +651

/**
* 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 );
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 safe_svg_pre_dimensions filter also?

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;
}

Copy link
Member Author

Choose a reason for hiding this comment

The 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

@jeffpaul jeffpaul linked an issue Oct 16, 2023 that may be closed by this pull request
1 task
@dkotter dkotter modified the milestones: 2.2.1, 2.3.0 Oct 19, 2023
@sksaju sksaju marked this pull request as ready for review November 27, 2023 11:55
@Sidsector9 Sidsector9 removed their request for review November 27, 2023 14:11
@jeffpaul
Copy link
Member

@darylldoyle back to you for review

darylldoyle
darylldoyle previously approved these changes Dec 12, 2023
Copy link
Collaborator

@darylldoyle darylldoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @sksaju!

The PHPUnit failure looks like it's because you haven't mocked wp_get_attachment_metadata() within the test. I didn't think it would be needed, but worth a try. See: https://github.com/10up/safe-svg/blob/develop/tests/unit/test-safe-svg.php#L191-L209

Once those tests are passing I'm good with merging this.

CC @jeffpaul

Comment on lines +568 to +583
/**
* 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;
}
Copy link
Collaborator

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!

darylldoyle
darylldoyle previously approved these changes Dec 12, 2023
Copy link
Collaborator

@darylldoyle darylldoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Ignoring E2E tests as it doesn't look like they're fully set-up

@sksaju
Copy link
Member Author

sksaju commented Dec 12, 2023

Looking good! Ignoring E2E tests as it doesn't look like they're fully set-up

Thanks @darylldoyle I already fixed the E@E issue here d0b5b8a :)

@jeffpaul jeffpaul merged commit fbcee2f into develop Dec 12, 2023
11 checks passed
@jeffpaul jeffpaul deleted the fix/75-slow-load-times-due-to-svgdimensions branch December 12, 2023 16:15
$width = 0;
$height = 0;

if ( $svg && ! empty( $metadata['width'] ) && empty( $metadata['height'] ) ) {
Copy link
Collaborator

@dkotter dkotter Jan 10, 2024

Choose a reason for hiding this comment

The 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 $svg isn't null and then checks if the width isn't empty and the height is empty. Unless I'm thinking of this wrong, shouldn't it be checking if both width and height aren't empty, so like this:

if ( $svg && ! empty( $metadata['width'] ) && ! empty( $metadata['height'] ) ) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkotter Yeah it should be ! empty, I've created a PR #174 to fix it, Apologies & Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function used by Save SVG is slowing down wordpress pages Slow load times due to svg_dimensions
5 participants