feat: introduce new fields for getting mediaItem files and filePaths#3294
Merged
jasonbahl merged 5 commits intoFeb 10, 2025
Merged
Conversation
…iaItem and allows a size to be specified. - update resolver for MediaDetails.file to return just the filename as the description states. Previously it was returning a path such as /2022/02/filename.jpg which doesn't match the description of the field. This is a bugfix but could be a breaking change for users that were expecting the path. - introduce a MediaSize.filePath field which returns the path to the file for the referenced size (default is full) - add tests for the new and updated fields
…sizes-file-field # Conflicts: # tests/wpunit/MediaItemQueriesTest.php
…rectory - update tests to accomondate for the change
| $metadata = wp_get_attachment_metadata( $source->databaseId ); | ||
|
|
||
| if ( ! empty( $metadata['sizes'][ $size ]['file'] ) ) { | ||
| return $metadata['sizes'][ $size ]['file']; |
There was a problem hiding this comment.
Avoid too many return statements within this method.
|
|
||
| // Default to original file | ||
| $attached_file = get_post_meta( $source->databaseId, '_wp_attached_file', true ); | ||
| return ! empty( $attached_file ) ? basename( $attached_file ) : null; |
There was a problem hiding this comment.
Avoid too many return statements within this method.
|
|
||
| if ( ! empty( $metadata['sizes'][ $size ]['file'] ) ) { | ||
| $file_path = $metadata['file']; | ||
| return path_join( $relative_upload_path, dirname( $file_path ) . '/' . $metadata['sizes'][ $size ]['file'] ); |
There was a problem hiding this comment.
Avoid too many return statements within this method.
| return null; | ||
| } | ||
|
|
||
| return path_join( $relative_upload_path, $attached_file ); |
There was a problem hiding this comment.
Avoid too many return statements within this method.
|
Code Climate has analyzed commit 90d6ca5 and detected 9 issues on this pull request. Here's the issue category breakdown:
View more on Code Climate. |
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this implement/fix? Explain your changes.
This introduces some new fields for retrieving MediaItem file values:
MediaDetails.filePath: The path to the mediaItem relative to the uploads directoryMediaItem.file: The filename of the mediaItem for the specified size (default size is full)MediaItem.filePath: The path to the original file relative to the uploads directoryMediaSize.filePathThe path of the file for the referenced size (default size is full)Does this close any currently open issues?
related: #2982
Any other comments?
In #3293 we fixed a bug in how the MediaDetails.file field resolved. This introduces some new fields to access both the filename and filePath to accommodate various use cases.
ex:
Standard site with standard uploads directory
Bedrock site with a different uploads directory