Skip to content

Conversation

@jasonbahl
Copy link
Collaborator

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 directory
  • MediaItem.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 directory
  • MediaSize.filePath The 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:

query GetMediaItemFilePath($id: ID!) {
  mediaItem(id: $id, idType: DATABASE_ID) {
    file
    filePath
    mediaDetails {
      file
      filePath
      sizes {
        file
        filePath
      }
    }
  }
}

Standard site with standard uploads directory

CleanShot 2025-02-07 at 12 01 14

Bedrock site with a different uploads directory

CleanShot 2025-02-07 at 12 00 58

…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
@jasonbahl jasonbahl added the object type: media Relating to the MediaItem Type label Feb 7, 2025
@jasonbahl jasonbahl self-assigned this Feb 7, 2025
$metadata = wp_get_attachment_metadata( $source->databaseId );

if ( ! empty( $metadata['sizes'][ $size ]['file'] ) ) {
return $metadata['sizes'][ $size ]['file'];

Choose a reason for hiding this comment

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

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;

Choose a reason for hiding this comment

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

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'] );

Choose a reason for hiding this comment

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

Avoid too many return statements within this method.

return null;
}

return path_join( $relative_upload_path, $attached_file );

Choose a reason for hiding this comment

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

Avoid too many return statements within this method.

@qlty-cloud-legacy
Copy link

Code Climate has analyzed commit 90d6ca5 and detected 9 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 9

View more on Code Climate.

@coveralls
Copy link

Coverage Status

coverage: 83.203% (+0.009%) from 83.194%
when pulling 90d6ca5 on jasonbahl:feat/2982-media-file-fields
into eb824fd on wp-graphql:develop.

@jasonbahl jasonbahl merged commit 37a6647 into wp-graphql:develop Feb 10, 2025
35 of 36 checks passed
@jasonbahl jasonbahl mentioned this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

object type: media Relating to the MediaItem Type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants