|
35 | 35 | * @property string $pingStatus |
36 | 36 | * @property string $slug |
37 | 37 | * @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 |
44 | 44 | * @property string $toPing |
45 | 45 | * @property string $pinged |
46 | 46 | * @property string $modified |
|
68 | 68 | * @property string $descriptionRaw |
69 | 69 | * @property string $descriptionRendered |
70 | 70 | * @property string $mediaType |
| 71 | + * @property ?string $mediaItemUrl |
71 | 72 | * @property string $sourceUrl |
72 | 73 | * @property string $mimeType |
73 | 74 | * @property array $mediaDetails |
74 | | - * |
75 | | - * @package WPGraphQL\Model |
76 | 75 | */ |
77 | 76 | class Post extends Model { |
78 | 77 |
|
@@ -104,6 +103,17 @@ class Post extends Model { |
104 | 103 | */ |
105 | 104 | protected $wp_query; |
106 | 105 |
|
| 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 | + |
107 | 117 | /** |
108 | 118 | * Post constructor. |
109 | 119 | * |
@@ -794,23 +804,26 @@ protected function init() { |
794 | 804 | return wp_attachment_is_image( $this->data->ID ) ? 'image' : 'file'; |
795 | 805 | }, |
796 | 806 | 'mediaItemUrl' => function () { |
797 | | - return wp_get_attachment_url( $this->data->ID ); |
| 807 | + return wp_get_attachment_url( $this->data->ID ) ?: null; |
798 | 808 | }, |
799 | 809 | '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' ); |
803 | 811 | }, |
804 | 812 | 'sourceUrlsBySize' => function () { |
| 813 | + _doing_it_wrong( |
| 814 | + __METHOD__, |
| 815 | + '`sourceUrlsBySize` is deprecated. Use the `sourceUrlBySize` callable instead.', |
| 816 | + '@todo' |
| 817 | + ); |
| 818 | + |
805 | 819 | /** |
806 | 820 | * This returns an empty array on the VIP Go platform. |
807 | 821 | */ |
808 | 822 | $sizes = get_intermediate_image_sizes(); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_intermediate_image_sizes_get_intermediate_image_sizes |
809 | 823 | $urls = []; |
810 | 824 | if ( ! empty( $sizes ) && is_array( $sizes ) ) { |
811 | 825 | 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 ); |
814 | 827 | } |
815 | 828 | } |
816 | 829 |
|
@@ -845,4 +858,27 @@ protected function init() { |
845 | 858 | } |
846 | 859 | } |
847 | 860 | } |
| 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 | + } |
848 | 884 | } |
0 commit comments