Skip to content

Commit de7d55f

Browse files
committed
Refactor to guarantee wp_get_attachment_image_src() always returns expected type
1 parent 1a343b0 commit de7d55f

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

src/wp-includes/media.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,16 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
10351035
* an array of width and height values in pixels (in that order).
10361036
* @param bool $icon Whether the image should be treated as an icon.
10371037
*/
1038-
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
1038+
$source = apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
1039+
if ( is_array( $source ) && isset( $source[0] ) && is_string( $source[0] ) ) {
1040+
return array(
1041+
$source[0],
1042+
(int) ( $source[1] ?? 0 ),
1043+
(int) ( $source[2] ?? 0 ),
1044+
(bool) ( $source[3] ?? false ),
1045+
);
1046+
}
1047+
return false;
10391048
}
10401049

10411050
/**
@@ -3244,18 +3253,18 @@ function wp_playlist_shortcode( $attr ) {
32443253
$image_src_full = wp_get_attachment_image_src( $thumb_id, 'full' );
32453254
if ( is_array( $image_src_full ) ) {
32463255
$track['image'] = array(
3247-
'src' => (string) ( $image_src_full[0] ?? '' ),
3248-
'width' => (int) ( $image_src_full[1] ?? 0 ),
3249-
'height' => (int) ( $image_src_full[2] ?? 0 ),
3256+
'src' => $image_src_full[0],
3257+
'width' => $image_src_full[1],
3258+
'height' => $image_src_full[2],
32503259
);
32513260
}
32523261

32533262
$image_src_thumb = wp_get_attachment_image_src( $thumb_id, 'thumbnail' );
32543263
if ( is_array( $image_src_thumb ) ) {
32553264
$track['thumb'] = array(
3256-
'src' => (string) ( $image_src_thumb[0] ?? '' ),
3257-
'width' => (int) ( $image_src_thumb[1] ?? 0 ),
3258-
'height' => (int) ( $image_src_thumb[2] ?? 0 ),
3265+
'src' => $image_src_thumb[0],
3266+
'width' => $image_src_thumb[1],
3267+
'height' => $image_src_thumb[2],
32593268
);
32603269
}
32613270
} else {
@@ -4738,18 +4747,18 @@ function wp_prepare_attachment_for_js( $attachment ) {
47384747
$response_image_full = wp_get_attachment_image_src( $id, 'full' );
47394748
if ( is_array( $response_image_full ) ) {
47404749
$response['image'] = array(
4741-
'src' => (string) ( $response_image_full[0] ?? '' ),
4742-
'width' => (int) ( $response_image_full[1] ?? 0 ),
4743-
'height' => (int) ( $response_image_full[2] ?? 0 ),
4750+
'src' => $response_image_full[0],
4751+
'width' => $response_image_full[1],
4752+
'height' => $response_image_full[2],
47444753
);
47454754
}
47464755

47474756
$response_image_thumb = wp_get_attachment_image_src( $id, 'thumbnail' );
47484757
if ( is_array( $response_image_thumb ) ) {
47494758
$response['thumb'] = array(
4750-
'src' => (string) ( $response_image_thumb[0] ?? '' ),
4751-
'width' => (int) ( $response_image_thumb[1] ?? 0 ),
4752-
'height' => (int) ( $response_image_thumb[2] ?? 0 ),
4759+
'src' => $response_image_thumb[0],
4760+
'width' => $response_image_thumb[1],
4761+
'height' => $response_image_thumb[2],
47534762
);
47544763
}
47554764
} else {

0 commit comments

Comments
 (0)