-
Notifications
You must be signed in to change notification settings - Fork 3.6k
REST API: Normalize non-integer attachment filesize metadata #12611
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
Closed
apermo
wants to merge
28
commits into
WordPress:trunk
from
apermo:trac-65670-attachment-filesize-typeerror
+203
−31
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1944ed1
fix(rest): cast string filesize meta to int
apermo ba9bf73
docs(rest): trim filesize guard comment per review
apermo dfb925e
Eliminate legacy args for create_object() and add assertions to fix P…
westonruter e694464
fix(rest): require positive integer filesize meta
apermo 91c0463
fix(rest): guard ctype_digit against non-string meta
apermo 12a4efa
Remove change to unrelated test
westonruter 6d2882e
Add typing to data provider
westonruter be49854
Remove extra line break
westonruter d27aa4b
Ensure wp_filesize() never returns a negative number
westonruter a6810a9
Reformat multi-line conditional
westonruter e87ede8
Merge branch 'trunk' into trac-65670-attachment-filesize-typeerror
westonruter b6280e0
Make it clear a non-negative-int is required for the short-circuit to…
westonruter 97bef96
Only cast numeric values to integer; treat others as zero
westonruter 32585a8
Add tests
westonruter 554a036
Improve formatting of arrow functions
westonruter 62be93a
Update filesize key in metadata alone
westonruter 001ee72
Use attachment's resolved path
westonruter c50756d
Relax attachment metadata filesize to include numeric values
westonruter 7d71ec8
Improve wording of since tag
westonruter 0808521
Improve name of test method
westonruter ec081cb
Indicate that filesize may be null in REST API response
westonruter bd24dfd
Improve data provider name
westonruter 3d7ddf4
Add tests for numeric-string as wp_filesize and pre_wp_filesize filte…
westonruter f556a44
Clarify numeric value vs numeric string
westonruter 026c8f0
Improve wording of wp_filesize() docblocks
westonruter 6122b77
Add negative value test for pre_wp_filesize
westonruter 866255d
Add data provider for negative pre_wp_filesize filter values
westonruter 7d68239
Improve param name
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1707,7 +1707,7 @@ public function get_item_schema() { | |
|
|
||
| $schema['properties']['filesize'] = array( | ||
| 'description' => __( 'Attachment file size in bytes.' ), | ||
| 'type' => 'integer', | ||
| 'type' => array( 'integer', 'null' ), | ||
|
Member
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. Claude pointed out that |
||
| 'context' => array( 'view', 'edit' ), | ||
| 'readonly' => true, | ||
| ); | ||
|
|
@@ -2359,12 +2359,13 @@ protected function get_attachment_filename( int $attachment_id ): ?string { | |
| * | ||
| * @param int $attachment_id Attachment ID. | ||
| * @return int|null Attachment file size in bytes, or null if not available. | ||
| * @phpstan-return non-negative-int|null | ||
| */ | ||
| protected function get_attachment_filesize( int $attachment_id ): ?int { | ||
| $meta = wp_get_attachment_metadata( $attachment_id ); | ||
|
|
||
| if ( isset( $meta['filesize'] ) ) { | ||
| return $meta['filesize']; | ||
| if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && $meta['filesize'] > 0 ) { | ||
| return (int) $meta['filesize']; | ||
| } | ||
|
|
||
| $original_path = wp_get_original_image_path( $attachment_id ); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
apermo marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I fixed the typing of this function as otherwise
get_attachment_filesize()would not be guaranteed to return anon-negative-int. This would warrant additional tests to ensure negative values get increased to zero.Could there be any reason any plugin would filter
pre_wp_filesizeorwp_filesizeto be negative?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.
Besides the obvious "Developer did a bad job, or had some quirky idea"?
I don't think so, but that alone is already bad enough.
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.
No technically legitimate reason, but definitely a reason.