Skip to content

Commit 79685af

Browse files
committed
fix: lazy-resolve Post model source URLs
1 parent 929f109 commit 79685af

File tree

2 files changed

+74
-25
lines changed

2 files changed

+74
-25
lines changed

src/Model/Post.php

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
* @property string $pingStatus
3636
* @property string $slug
3737
* @property array $template
38-
* @property bool $isFrontPage
39-
* @property bool $isPrivacyPage
40-
* @property bool $isPostsPage
41-
* @property bool $isPreview
42-
* @property bool $isRevision
43-
* @property bool $isSticky
38+
* @property bool $isFrontPage
39+
* @property bool $isPrivacyPage
40+
* @property bool $isPostsPage
41+
* @property bool $isPreview
42+
* @property bool $isRevision
43+
* @property bool $isSticky
4444
* @property string $toPing
4545
* @property string $pinged
4646
* @property string $modified
@@ -68,11 +68,10 @@
6868
* @property string $descriptionRaw
6969
* @property string $descriptionRendered
7070
* @property string $mediaType
71+
* @property ?string $mediaItemUrl
7172
* @property string $sourceUrl
7273
* @property string $mimeType
7374
* @property array $mediaDetails
74-
*
75-
* @package WPGraphQL\Model
7675
*/
7776
class Post extends Model {
7877

@@ -104,6 +103,17 @@ class Post extends Model {
104103
*/
105104
protected $wp_query;
106105

106+
/**
107+
* Stores the resolved image `sourceUrl`s keyed by size.
108+
*
109+
* This is used to prevent multiple calls to `wp_get_attachment_image_src`.
110+
*
111+
* If no source URL is found for a size, the value will be `null`.
112+
*
113+
* @var array<string,?string>
114+
*/
115+
protected $source_urls_by_size = [];
116+
107117
/**
108118
* Post constructor.
109119
*
@@ -794,23 +804,26 @@ protected function init() {
794804
return wp_attachment_is_image( $this->data->ID ) ? 'image' : 'file';
795805
},
796806
'mediaItemUrl' => function () {
797-
return wp_get_attachment_url( $this->data->ID );
807+
return wp_get_attachment_url( $this->data->ID ) ?: null;
798808
},
799809
'sourceUrl' => function () {
800-
$source_url = wp_get_attachment_image_src( $this->data->ID, 'full' );
801-
802-
return ! empty( $source_url ) ? $source_url[0] : null;
810+
return $this->get_source_url_by_size( 'full' );
803811
},
804812
'sourceUrlsBySize' => function () {
813+
_doing_it_wrong(
814+
__METHOD__,
815+
'`sourceUrlsBySize` is deprecated. Use the `sourceUrlBySize` callable instead.',
816+
'@todo'
817+
);
818+
805819
/**
806820
* This returns an empty array on the VIP Go platform.
807821
*/
808822
$sizes = get_intermediate_image_sizes(); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_intermediate_image_sizes_get_intermediate_image_sizes
809823
$urls = [];
810824
if ( ! empty( $sizes ) && is_array( $sizes ) ) {
811825
foreach ( $sizes as $size ) {
812-
$img_src = wp_get_attachment_image_src( $this->data->ID, $size );
813-
$urls[ $size ] = ! empty( $img_src ) ? $img_src[0] : null;
826+
$urls[ $size ] = $this->get_source_url_by_size( $size );
814827
}
815828
}
816829

@@ -845,4 +858,27 @@ protected function init() {
845858
}
846859
}
847860
}
861+
862+
/**
863+
* Gets the source URL for an image attachment by size.
864+
*
865+
* This method caches the source URL for a given size to prevent multiple calls to `wp_get_attachment_image_src`.
866+
*
867+
* @param ?string $size The size of the image to get the source URL for. `full` by default.
868+
*/
869+
public function get_source_url_by_size( ?string $size = 'full' ): ?string {
870+
// If size is not set, default to 'full'.
871+
if ( ! $size ) {
872+
$size = 'full';
873+
}
874+
875+
// Resolve the source URL for the size if it hasn't been resolved yet.
876+
if ( ! array_key_exists( $size, $this->source_urls_by_size ) ) {
877+
$src = wp_get_attachment_image_src( $this->data->ID, $size );
878+
879+
$this->source_urls_by_size[ $size ] = ! empty( $src ) ? $src[0] : null;
880+
}
881+
882+
return $this->source_urls_by_size[ $size ];
883+
}
848884
}

src/Registry/Utils/PostObject.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,15 @@ private static function register_attachment_fields( WP_Post_Type $post_type_obje
574574
],
575575
],
576576
'resolve' => static function ( $image, $args ) {
577-
// @codingStandardsIgnoreLine.
578-
$size = null;
579-
if ( isset( $args['size'] ) ) {
580-
$size = ( 'full' === $args['size'] ) ? 'large' : $args['size'];
577+
if ( empty( $args['size'] ) ) {
578+
return $image->sourceUrl;
581579
}
582580

583-
return ! empty( $size ) ? $image->sourceUrlsBySize[ $size ] : $image->sourceUrl;
581+
// @todo why do we coerce full to large?
582+
$size = 'full' === $args['size'] ? 'large' : $args['size'];
583+
584+
/** @var \WPGraphQL\Model\Post $image */
585+
return $image->get_source_url_by_size( $size );
584586
},
585587
],
586588
'fileSize' => [
@@ -593,15 +595,26 @@ private static function register_attachment_fields( WP_Post_Type $post_type_obje
593595
],
594596
],
595597
'resolve' => static function ( $image, $args ) {
596-
597-
// @codingStandardsIgnoreLine.
598-
$size = null;
599-
if ( isset( $args['size'] ) ) {
598+
/**
599+
* By default, use the mediaItemUrl.
600+
*
601+
* @var \WPGraphQL\Model\Post $image
602+
*/
603+
$source_url = $image->mediaItemUrl;
604+
605+
// If there's a url for the provided size, use that instead.
606+
if ( ! empty( $args['size'] ) ) {
600607
$size = ( 'full' === $args['size'] ) ? 'large' : $args['size'];
608+
609+
$source_url = $image->get_source_url_by_size( $size ) ?: $source_url;
610+
}
611+
612+
// If there's no source_url, return null.
613+
if ( empty( $source_url ) ) {
614+
return null;
601615
}
602616

603-
$sourceUrl = ! empty( $size ) ? $image->sourceUrlsBySize[ $size ] : $image->mediaItemUrl;
604-
$path_parts = pathinfo( $sourceUrl );
617+
$path_parts = pathinfo( $source_url );
605618
$original_file = get_attached_file( absint( $image->databaseId ) );
606619
$filesize_path = ! empty( $original_file ) ? path_join( dirname( $original_file ), $path_parts['basename'] ) : null;
607620

0 commit comments

Comments
 (0)