Skip to content

Commit df70ae5

Browse files
committed
Add typing for attachment metadata added by Dominant Color Images
1 parent 30a3e5b commit df70ae5

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

plugins/dominant-color-images/hooks.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
*
1717
* @since 1.0.0
1818
*
19-
* @param array $metadata The attachment metadata.
20-
* @param int $attachment_id The attachment ID.
21-
* @return array $metadata The attachment metadata.
19+
* @param array|mixed $metadata The attachment metadata.
20+
* @param int $attachment_id The attachment ID.
21+
* @return array{ has_transparency?: bool, dominant_color?: string } The amended attachment metadata.
2222
*/
23-
function dominant_color_metadata( $metadata, $attachment_id ) {
23+
function dominant_color_metadata( $metadata, int $attachment_id ): array {
24+
if ( ! is_array( $metadata ) ) {
25+
$metadata = array();
26+
}
27+
2428
$dominant_color_data = dominant_color_get_dominant_color_data( $attachment_id );
2529
if ( ! is_wp_error( $dominant_color_data ) ) {
2630
if ( isset( $dominant_color_data['dominant_color'] ) ) {
@@ -36,6 +40,31 @@ function dominant_color_metadata( $metadata, $attachment_id ) {
3640
}
3741
add_filter( 'wp_generate_attachment_metadata', 'dominant_color_metadata', 10, 2 );
3842

43+
/**
44+
* Retrieves attachment metadata for attachment ID.
45+
*
46+
* This is a wrapper for {@see wp_get_attachment_metadata()} to add the typing which is augmented by the
47+
* `wp_generate_attachment_metadata` via {@see dominant_color_metadata()}.
48+
*
49+
* @since n.e.x.t
50+
*
51+
* @param int $attachment_id Attachment post ID. Defaults to global $post.
52+
* @return array{
53+
* width: int,
54+
* height: int,
55+
* file: string,
56+
* sizes: array,
57+
* image_meta: array,
58+
* filesize: int,
59+
* has_transparency?: bool,
60+
* dominant_color?: string
61+
* }|null Attachment metadata. Null on failure.
62+
*/
63+
function dominant_color_get_attachment_metadata( int $attachment_id = 0 ): ?array {
64+
$metadata = wp_get_attachment_metadata( $attachment_id );
65+
return is_array( $metadata ) ? $metadata : null;
66+
}
67+
3968
/**
4069
* Filters various image attributes to add the dominant color to the image.
4170
*
@@ -46,7 +75,7 @@ function dominant_color_metadata( $metadata, $attachment_id ) {
4675
* @return mixed $attr Attributes for the image markup.
4776
*/
4877
function dominant_color_update_attachment_image_attributes( $attr, $attachment ) {
49-
$image_meta = wp_get_attachment_metadata( $attachment->ID );
78+
$image_meta = dominant_color_get_attachment_metadata( $attachment->ID );
5079
if ( ! is_array( $image_meta ) ) {
5180
return $attr;
5281
}
@@ -100,7 +129,7 @@ function dominant_color_img_tag_add_dominant_color( $filtered_image, $context, $
100129
return $filtered_image;
101130
}
102131

103-
$image_meta = wp_get_attachment_metadata( $attachment_id );
132+
$image_meta = dominant_color_get_attachment_metadata( $attachment_id );
104133
if ( ! is_array( $image_meta ) ) {
105134
return $filtered_image;
106135
}

0 commit comments

Comments
 (0)