Make WordPress Core

Ticket #30180: 30180.2.2.diff

File 30180.2.2.diff, 12.1 KB (added by antpb, 6 years ago)

Fixes tests and undefined variable of $src

  • src/wp-includes/general-template.php

    diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
    index 28d0e39..5ef07a0 100644
    function get_custom_logo( $blog_id = 0 ) { 
    966966                 * If the logo alt attribute is empty, get the site title and explicitly
    967967                 * pass it to the attributes used by wp_get_attachment_image().
    968968                 */
    969                 $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
     969                $image_alt = wp_get_attachment_image_attr( $custom_logo_id, 'alt' );
    970970                if ( empty( $image_alt ) ) {
    971971                        $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
    972972                }
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 4117b78..58a6d09 100644
    function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon 
    968968}
    969969
    970970/**
     971 * Retrieve an image attribute value.
     972 *
     973 * Retrieve either a single image attribute value or all relevant attribute values. The possible
     974 * values are `alt`, `description`, `caption`, `src`, `width`, `height`, `srcset`, `sizes`, `class`. When
     975 * retrieving src, width, height, srcset, sizes, or class, you can specify the size of the image to retrieve
     976 * in the args parameter. Passing in an empty attribute value will return all relevant values.
     977 *
     978 * @since 5.X
     979 *
     980 * @param int          $attachment_id Image attachment ID.
     981 * @param string|array $attribute     Image attribute to retrieve. An empty value or `all` will retrieve all values.
     982 * @param array        $args          Optional. Array of attributes for the function. Currently only
     983 *                                    supports 'size' for the image source.
     984 * @return null|string|array          Returns the value of the attribute or attributes requested. A single
     985 *                                    attribute will return a single value. An array of attributes or no
     986 *                                    attribute will return an array of values.
     987 */
     988function wp_get_attachment_image_attr( $attachment_id, $attribute = array(), $args = array() ) {
     989        $image = get_post( $attachment_id );
     990
     991        // If this isn't an image, bail early with null.
     992        if ( empty( $image ) ) {
     993                return null;
     994        }
     995
     996        $values = array();
     997        $names  = array();
     998
     999        if ( empty( $attribute ) || 'all' === $attribute ) {
     1000                // If an empty value is passed for the attr name, return all our attributes.
     1001                $names = array(
     1002                        'alt',
     1003                        'description',
     1004                        'caption',
     1005                        'src',
     1006                        'width',
     1007                        'height',
     1008                        'srcset',
     1009                        'sizes',
     1010                        'class',
     1011                );
     1012        } elseif ( is_string( $attribute ) ) {
     1013                // If the attribute name is a string, convert it to an array.
     1014                $names = array( $attribute );
     1015        }
     1016
     1017        // Get a valid args array.
     1018        $default_args = array(
     1019                'icon' => false,
     1020                'size' => null,
     1021        );
     1022
     1023        $args = wp_parse_args( $args, $default_args );
     1024
     1025        // Loop through our names and get the value for each one.
     1026        foreach ( $names as $name ) {
     1027                switch ( $name ) {
     1028                        case 'alt':
     1029                                $values[ $name ] = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
     1030                                break;
     1031
     1032                        case 'description':
     1033                                $values[ $name ] = $image->post_content;
     1034                                break;
     1035
     1036                        case 'caption':
     1037                                $values[ $name ] = $image->post_excerpt;
     1038                                break;
     1039
     1040                        case 'src':
     1041                                if ( ! isset( $image_src ) ) {
     1042                                        $image_src = wp_get_attachment_image_src( $attachment_id, $args['size'], $args['icon'] );
     1043                                }
     1044
     1045                                $values[ $name ] = $image_src[0];
     1046                                break;
     1047
     1048                        case 'width':
     1049                                if ( ! isset( $image_src ) ) {
     1050                                        $image_src = wp_get_attachment_image_src( $attachment_id, $args['size'], $args['icon'] );
     1051                                }
     1052
     1053                                $values[ $name ] = $image_src[1];
     1054                                break;
     1055
     1056                        case 'height':
     1057                                if ( ! isset( $image_src ) ) {
     1058                                        $image_src = wp_get_attachment_image_src( $attachment_id, $args['size'], $args['icon'] );
     1059                                }
     1060
     1061                                $values[ $name ] = $image_src[2];
     1062                                break;
     1063
     1064                        case 'srcset':
     1065                                $values[ $name ] = wp_get_attachment_image_srcset( $attachment_id, $args['size'] );
     1066                                break;
     1067
     1068                        case 'sizes':
     1069                                $values[ $name ] = wp_get_attachment_image_sizes( $attachment_id, $args['size'] );
     1070                                break;
     1071
     1072                        case 'class':
     1073                                $size_class = $args['size'];
     1074
     1075                                if ( is_array( $size_class ) ) {
     1076                                        $size_class = join( 'x', $size_class );
     1077                                }
     1078
     1079                                $values[ $name ] = 'attachment-' . $size_class . ' size-' . $size_class;
     1080                                break;
     1081                }
     1082        }
     1083
     1084        /**
     1085         * Filters the image attr result.
     1086         *
     1087         * Will apply to all image attribute types.
     1088         *
     1089         * @since 5.x
     1090         *
     1091         * @param string|int $values         Value of the attribute.
     1092         * @param int        $attachment_id Image attachment ID.
     1093         * @param string     $name     Name of the attribute being retrieved.
     1094         * @param array      $args          Arguments passed into the function.
     1095         */
     1096        $values = apply_filters( 'wp_get_attachment_image_attr', $values, $attachment_id, $attribute, $args );
     1097
     1098        // If we didn't request a single attribute, we're done.
     1099        if ( is_array( $attribute ) || empty( $attribute ) || 'all' === $attribute ) {
     1100                return $values;
     1101        }
     1102
     1103        if ( ! empty( $values[ $attribute ] ) ) {
     1104                $value = $values[ $attribute ];
     1105        } else {
     1106                $value = null;
     1107        }
     1108
     1109        /**
     1110         * Filters the image attr result.
     1111         *
     1112         * This is a dynamic filter that will generate a distinct filter for each attribute type.
     1113         * It only runs if the requested attribute is a string.
     1114         *
     1115         * <code>
     1116         * <?php
     1117         * function filter_alt_text( $alt ) {
     1118         *     $alt = $alt . ' Extra content';
     1119         *     return $alt;
     1120         * }
     1121         * add_filter( 'wp_get_attachment_image_attr_alt', 'filter_alt_text' );
     1122         * </code>
     1123         *
     1124         * @since 5.x
     1125         *
     1126         * @param string|int $value         Value of the attribute.
     1127         * @param int        $attachment_id Image attachment ID.
     1128         * @param array      $args          Arguments passed into the function.
     1129         */
     1130        $value = apply_filters( 'wp_get_attachment_image_attr_' . $attribute, $value, $attachment_id, $args );
     1131
     1132        return $value;
     1133}
     1134
     1135/**
    9711136 * Get an HTML img element representing an image attachment
    9721137 *
    9731138 * While `$size` will accept an array, it is better to register a size with
    function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon 
    9851150 * @return string HTML img element or empty string on failure.
    9861151 */
    9871152function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
    988         $html  = '';
    989         $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
    990         if ( $image ) {
    991                 list($src, $width, $height) = $image;
    992                 $hwstring                   = image_hwstring( $width, $height );
    993                 $size_class                 = $size;
    994                 if ( is_array( $size_class ) ) {
    995                         $size_class = join( 'x', $size_class );
    996                 }
    997                 $attachment   = get_post( $attachment_id );
    998                 $default_attr = array(
    999                         'src'   => $src,
    1000                         'class' => "attachment-$size_class size-$size_class",
    1001                         'alt'   => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
    1002                 );
     1153        $html = '';
    10031154
    1004                 $attr = wp_parse_args( $attr, $default_attr );
     1155        $image_attr_args = array(
     1156                'size' => $size,
     1157                'icon' => $icon,
     1158        );
    10051159
    1006                 // Generate 'srcset' and 'sizes' if not already present.
    1007                 if ( empty( $attr['srcset'] ) ) {
    1008                         $image_meta = wp_get_attachment_metadata( $attachment_id );
     1160        $image_attr = wp_get_attachment_image_attr( $attachment_id, 'all', $image_attr_args );
    10091161
    1010                         if ( is_array( $image_meta ) ) {
    1011                                 $size_array = array( absint( $width ), absint( $height ) );
    1012                                 $srcset     = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
    1013                                 $sizes      = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
     1162        // If the image attribute fetch failed, return an empty string.
     1163        if ( empty( $image_attr ) ) {
     1164                return $html;
     1165        }
    10141166
    1015                                 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    1016                                         $attr['srcset'] = $srcset;
     1167        $hwstring     = image_hwstring( $image_attr['width'], $image_attr['height'] );
     1168        $attachment   = get_post( $attachment_id );
     1169        $default_attr = array(
     1170                'src'   => $image_attr['src'],
     1171                'class' => $image_attr['class'],
     1172                'alt'   => trim( strip_tags( $image_attr['alt'] ) ),
     1173        );
    10171174
    1018                                         if ( empty( $attr['sizes'] ) ) {
    1019                                                 $attr['sizes'] = $sizes;
    1020                                         }
    1021                                 }
    1022                         }
    1023                 }
     1175        $attr = wp_parse_args( $attr, $default_attr );
    10241176
    1025                 /**
    1026                  * Filters the list of attachment image attributes.
    1027                  *
    1028                  * @since 2.8.0
    1029                  *
    1030                  * @param array        $attr       Attributes for the image markup.
    1031                  * @param WP_Post      $attachment Image attachment post.
    1032                  * @param string|array $size       Requested size. Image size or array of width and height values
    1033                  *                                 (in that order). Default 'thumbnail'.
    1034                  */
    1035                 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
    1036                 $attr = array_map( 'esc_attr', $attr );
    1037                 $html = rtrim( "<img $hwstring" );
    1038                 foreach ( $attr as $name => $value ) {
    1039                         $html .= " $name=" . '"' . $value . '"';
     1177        // Generate 'srcset' and 'sizes' if not already present.
     1178        if ( empty( $attr['srcset'] ) &&
     1179                ! empty( $image_attr['srcset'] ) &&
     1180                ( ! empty( $image_attr['sizes'] ) || ! empty( $attr['sizes'] ) ) ) {
     1181
     1182                $attr['srcset'] = $image_attr['srcset'];
     1183
     1184                if ( empty( $attr['sizes'] ) ) {
     1185                        $attr['sizes'] = $image_attr['sizes'];
    10401186                }
    1041                 $html .= ' />';
    10421187        }
    10431188
     1189        /**
     1190         * Filters the list of attachment image attributes.
     1191         *
     1192         * @since 2.8.0
     1193         *
     1194         * @param array        $attr       Attributes for the image markup.
     1195         * @param WP_Post      $attachment Image attachment post.
     1196         * @param string|array $size       Requested size. Image size or array of width and height values
     1197         *                                 (in that order). Default 'thumbnail'.
     1198         */
     1199        $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
     1200        $attr = array_map( 'esc_attr', $attr );
     1201        $html = rtrim( "<img $hwstring" );
     1202
     1203        foreach ( $attr as $name => $value ) {
     1204                $html .= ' ' .  $name . '="' . $value . '"';
     1205        }
     1206
     1207        $html .= ' />';
     1208
    10441209        return $html;
    10451210}
    10461211
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index c1be06a..341917f 100644
    class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    321321                }
    322322
    323323                if ( in_array( 'alt_text', $fields, true ) ) {
    324                         $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
     324                        $data['alt_text'] = wp_get_attachment_image_attr( $post->ID, 'alt' );
    325325                }
    326326
    327327                if ( in_array( 'media_type', $fields, true ) ) {
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index e4cc880..d67bcbd 100644
    function get_uploaded_header_images() { 
    12971297                $header_images[ $header_index ]['attachment_id']     = $header->ID;
    12981298                $header_images[ $header_index ]['url']               = $url;
    12991299                $header_images[ $header_index ]['thumbnail_url']     = $url;
    1300                 $header_images[ $header_index ]['alt_text']          = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
     1300                $header_images[ $header_index ]['alt_text']          = wp_get_attachment_image_attr( $header->ID, 'alt' );
    13011301                $header_images[ $header_index ]['attachment_parent'] = isset( $header_data['attachment_parent'] ) ? $header_data['attachment_parent'] : '';
    13021302
    13031303                if ( isset( $header_data['width'] ) ) {
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git tests/qunit/fixtures/wp-api-generated.js tests/qunit/fixtures/wp-api-generated.js
    index 47f925f..05ddcb3 100644
    mockedApiResponse.MediaCollection = [ 
    55655565        "caption": {
    55665566            "rendered": "<p>A sample caption</p>\n"
    55675567        },
    5568         "alt_text": "",
     5568        "alt_text": null,
    55695569        "media_type": "image",
    55705570        "mime_type": "image/jpeg",
    55715571        "media_details": {},
    mockedApiResponse.MediaModel = { 
    56265626    "caption": {
    56275627        "rendered": "<p>A sample caption</p>\n"
    56285628    },
    5629     "alt_text": "",
     5629    "alt_text": null,
    56305630    "media_type": "image",
    56315631    "mime_type": "image/jpeg",
    56325632    "media_details": {},