Skip to content

Commit b441847

Browse files
Coding Standards: Reorder width and height for consistency in wp_get_attachment_image().
Includes correcting the placement of the `filter_wp_get_attachment_image()` helper function in unit tests. Follow-up to [60415], [60476]. See #63168. git-svn-id: https://develop.svn.wordpress.org/trunk@60477 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4ee3d8c commit b441847

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/wp-includes/media.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,8 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
10891089
*
10901090
* @param string $context The context. Default 'wp_get_attachment_image'.
10911091
*/
1092-
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
1092+
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
1093+
10931094
$attr = wp_parse_args( $attr, $default_attr );
10941095
$attr['width'] = $width;
10951096
$attr['height'] = $height;
@@ -1158,7 +1159,7 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
11581159
* Filters the list of attachment image attributes.
11591160
*
11601161
* @since 2.8.0
1161-
* @since 6.8.2 The `$attr` array includes `height` and `width` attributes.
1162+
* @since 6.8.2 The `$attr` array includes `width` and `height` attributes.
11621163
*
11631164
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
11641165
* See wp_get_attachment_image().
@@ -1168,13 +1169,14 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
11681169
*/
11691170
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
11701171

1171-
if ( isset( $attr['height'] ) && is_numeric( $attr['height'] ) ) {
1172-
$height = absint( $attr['height'] );
1173-
}
11741172
if ( isset( $attr['width'] ) && is_numeric( $attr['width'] ) ) {
11751173
$width = absint( $attr['width'] );
11761174
}
1177-
unset( $attr['height'], $attr['width'] );
1175+
if ( isset( $attr['height'] ) && is_numeric( $attr['height'] ) ) {
1176+
$height = absint( $attr['height'] );
1177+
}
1178+
unset( $attr['width'], $attr['height'] );
1179+
11781180
$attr = array_map( 'esc_attr', $attr );
11791181
$hwstring = image_hwstring( $width, $height );
11801182
$html = rtrim( "<img $hwstring" );

tests/phpunit/tests/media.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,10 +1583,14 @@ public function test_wp_get_attachment_image_filter_output() {
15831583
$this->assertSame( $expected, $output );
15841584
}
15851585

1586+
public function filter_wp_get_attachment_image() {
1587+
return 'Override wp_get_attachment_image';
1588+
}
1589+
15861590
/**
15871591
* @ticket 14110
15881592
*/
1589-
public function test_wp_get_attachment_image_filter_with_height_width() {
1593+
public function test_wp_get_attachment_image_filter_with_width_height() {
15901594
$mock_action = new MockAction();
15911595
add_filter( 'wp_get_attachment_image_attributes', array( $mock_action, 'filter' ) );
15921596
wp_get_attachment_image( self::$large_id );
@@ -1600,12 +1604,12 @@ public function test_wp_get_attachment_image_filter_with_height_width() {
16001604
/**
16011605
* @ticket 14110
16021606
*/
1603-
public function test_wp_get_attachment_image_filter_change_height_width() {
1607+
public function test_wp_get_attachment_image_filter_change_width_height() {
16041608
add_filter(
16051609
'wp_get_attachment_image_attributes',
16061610
static function ( $args ) {
1607-
$args['height'] = '999';
16081611
$args['width'] = '999';
1612+
$args['height'] = '999';
16091613
return $args;
16101614
}
16111615
);
@@ -1617,11 +1621,11 @@ static function ( $args ) {
16171621
/**
16181622
* @ticket 14110
16191623
*/
1620-
public function test_wp_get_attachment_image_filter_unset_height_width() {
1624+
public function test_wp_get_attachment_image_filter_unset_width_height() {
16211625
add_filter(
16221626
'wp_get_attachment_image_attributes',
16231627
static function ( $args ) {
1624-
unset( $args['height'], $args['width'] );
1628+
unset( $args['width'], $args['height'] );
16251629
return $args;
16261630
}
16271631
);
@@ -1630,10 +1634,6 @@ static function ( $args ) {
16301634
$this->assertStringContainsString( 'height="150"', $output, 'Height should not be changed.' );
16311635
}
16321636

1633-
public function filter_wp_get_attachment_image() {
1634-
return 'Override wp_get_attachment_image';
1635-
}
1636-
16371637
/**
16381638
* Test that `wp_get_attachment_image()` returns a proper alt value.
16391639
*

0 commit comments

Comments
 (0)