Make WordPress Core

Ticket #30180: 30180.1.diff

File 30180.1.diff, 8.9 KB (added by ryanshoover, 6 years ago)

Writes a new function to assist with getting image attributes.

  • wp-admin/custom-header.php

    diff --git a/wp-admin/custom-header.php b/wp-admin/custom-header.php
    index 9460c80ca6..dd7306e1c2 100644
    a b endif; 
    14611461        public function get_uploaded_header_images() {
    14621462                $header_images = get_uploaded_header_images();
    14631463                $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
    1464                 $alt_text_key  = '_wp_attachment_image_alt';
    14651464
    14661465                foreach ( $header_images as &$header_image ) {
    1467                         $header_meta               = get_post_meta( $header_image['attachment_id'] );
    1468                         $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
    1469                         $header_image['alt_text']  = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
     1466                        $header_image['timestamp'] = get_post_meta( $header_image['attachment_id'], 'timestamp', true );
     1467                        $header_image['alt_text']  = wp_get_attachment_image_attr( $header_image['attachment_id'], 'alt' );
    14701468                }
    14711469
    14721470                return $header_images;
  • wp-admin/includes/media.php

    diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php
    index 93eceeca3a..b7839f0525 100644
    a b function get_attachment_fields_to_edit( $post, $errors = null ) { 
    13571357
    13581358        // This was formerly in image_attachment_fields_to_edit().
    13591359        if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
    1360                 $alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
     1360                $alt = wp_get_attachment_image_attr( $post->ID, 'alt' );
    13611361                if ( empty( $alt ) ) {
    13621362                        $alt = '';
    13631363                }
  • wp-includes/general-template.php

    diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php
    index 434e3245a1..4dbc759c2f 100644
    a b function get_custom_logo( $blog_id = 0 ) { 
    905905                 * If the logo alt attribute is empty, get the site title and explicitly
    906906                 * pass it to the attributes used by wp_get_attachment_image().
    907907                 */
    908                 $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
     908                $image_alt = wp_get_attachment_image_attr( $custom_logo_id, 'alt' );
    909909                if ( empty( $image_alt ) ) {
    910910                        $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
    911911                }
  • wp-includes/media.php

    diff --git a/wp-includes/media.php b/wp-includes/media.php
    index ec50703b33..fc100ecbdd 100644
    a b function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon 
    853853        return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
    854854}
    855855
     856/**
     857 * Retrieve an image attribute value.
     858 *
     859 * Retrieve either a single image attribute value or all relevant attribute values. The possible
     860 * values are `alt`, `description`, `caption`, `src`, `width`, `height`, `srcset`, `sizes`, `class`. When
     861 * retrieving src, width, height, srcset, sizes, or class, you can specify the size of the image to retrieve
     862 * in the args parameter. Passing in an empty attribute value will return all relevant values.
     863 *
     864 * @since 5.X
     865 *
     866 * @param int          $attachment_id Image attachment ID.
     867 * @param string|array $attribute     Image attribute to retrieve.
     868 * @param array        $args          Optional. Array of attributes for the function. Currently only
     869 *                                    supports 'size' for the image source.
     870 * @return string|array               Returns the value of the attribute or attributes requested. A single
     871 *                                    attribute will return a single value. An array of attributes or no
     872 *                                    attribute will return an array of values.
     873 */
     874function wp_get_attachment_image_attr( $attachment_id, $attribute = [], $args = [] ) {
     875        $image = get_post( $attachment_id );
     876
     877        // If this isn't an image, bail early with null.
     878        if ( empty( $image ) ) {
     879                return null;
     880        }
     881
     882        $values = [];
     883        $names  = [];
     884
     885        if ( empty( $attribute ) ) {
     886                // If an empty value is passed for the attr name, return all our attributes.
     887                $names = array(
     888                        'alt',
     889                        'description',
     890                        'caption',
     891                        'src',
     892                        'width',
     893                        'height',
     894                        'srcset',
     895                        'sizes',
     896                        'class',
     897                );
     898        } elseif ( is_string( $attribute ) ) {
     899                // If the attribute name is a string, convert it to an array.
     900                $names = array( $attribute );
     901        }
     902
     903        // Get a valid args array.
     904        $default_args = array(
     905                'size' => null,
     906        );
     907
     908        $args = wp_parse_args( $args, $default_args );
     909
     910        // Loop through our names and get the value for each one.
     911        foreach ( $names as $name ) {
     912                switch ( $name ) {
     913                        case 'alt':
     914                                $values[ $name ] = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
     915                                break;
     916
     917                        case 'description':
     918                                $values[ $name ] = $image->post_content;
     919                                break;
     920
     921                        case 'caption':
     922                                $values[ $name ] = $image->post_excerpt;
     923                                break;
     924
     925                        case 'src':
     926                                if ( ! isset( $image_src ) ) {
     927                                        $image_src       = image_downsize( $attachment_id, $args['size'] );
     928                                }
     929
     930                                $values[ $name ] = $image_src[0];
     931                                break;
     932
     933                        case 'width':
     934                                if ( ! isset( $image_src ) ) {
     935                                        $image_src       = image_downsize( $attachment_id, $args['size'] );
     936                                }
     937
     938                                $values[ $name ] = $image_src[1];
     939                                break;
     940
     941                        case 'height':
     942                                if ( ! isset( $image_src ) ) {
     943                                        $image_src       = image_downsize( $attachment_id, $args['size'] );
     944                                }
     945
     946                                $values[ $name ] = $image_src[2];
     947                                break;
     948
     949                        case 'srcset':
     950                                $values[ $name ] = wp_get_attachment_image_srcset( $attachment_id, $args['size'] );
     951                                break;
     952
     953                        case 'sizes':
     954                                $values[ $name ] = wp_get_attachment_image_sizes( $attachment_id, $args['size'] );
     955                                break;
     956
     957                        case 'class':
     958                                $size_class = $args['size'];
     959
     960                                if ( is_array( $size_class ) ) {
     961                                        $size_class = join( 'x', $size_class );
     962                                }
     963
     964                                $values[ $name ] = 'attachment-' . $size_class . ' size-' . $size_class;
     965                                break;
     966                }
     967        }
     968
     969        /**
     970         * Filters the image attr result.
     971         *
     972         * Will apply to all image attribute types.
     973         *
     974         * @since 5.x
     975         *
     976         * @param string|int $values         Value of the attribute.
     977         * @param int        $attachment_id Image attachment ID.
     978         * @param string     $name     Name of the attribute being retrieved.
     979         * @param array      $args          Arguments passed into the function.
     980         */
     981        $values = apply_filters( 'wp_get_attachment_image_attr', $values, $attachment_id, $attribute, $args );
     982
     983        // If we didn't request a single attribute, we're done.
     984        if ( is_array( $attribute ) || empty( $attribute ) ) {
     985                return $values;
     986        }
     987
     988        if ( ! empty( $values[ $attribute ] ) ) {
     989                $value = $values[ $attribute ];
     990        } else {
     991                $value = null;
     992        }
     993
     994        /**
     995         * Filters the image attr result.
     996         *
     997         * This is a dynamic filter that will generate a distinct filter for each attribute type.
     998         * It only runs if the requested attribute is a string.
     999         *
     1000         * <code>
     1001         * <?php
     1002         * function filter_alt_text( $alt ) {
     1003         *     $alt = $alt + ' Extra content';
     1004         *     return $alt;
     1005         * }
     1006         * add_filter( 'wp_get_attachment_image_attr_alt', 'filter_alt_text' );
     1007         * </code>
     1008         *
     1009         * @since 5.x
     1010         *
     1011         * @param string|int $value         Value of the attribute.
     1012         * @param int        $attachment_id Image attachment ID.
     1013         * @param array      $args          Arguments passed into the function.
     1014         */
     1015        $value = apply_filters( 'wp_get_attachment_image_attr_' . $attribute, $value, $attachment_id, $args );
     1016
     1017        return $value;
     1018}
     1019
    8561020/**
    8571021 * Get an HTML img element representing an image attachment
    8581022 *
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index e317f523aa..424f066025 100644
    a b class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    308308                }
    309309
    310310                if ( in_array( 'alt_text', $fields, true ) ) {
    311                         $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
     311                        $data['alt_text'] = wp_get_attachment_image_attr( $post->ID, 'alt' );
    312312                }
    313313
    314314                if ( in_array( 'media_type', $fields, true ) ) {
  • wp-includes/theme.php

    diff --git a/wp-includes/theme.php b/wp-includes/theme.php
    index 3d26800436..c45dfbdd40 100644
    a b function get_uploaded_header_images() { 
    12751275                $header_images[ $header_index ]['attachment_id']     = $header->ID;
    12761276                $header_images[ $header_index ]['url']               = $url;
    12771277                $header_images[ $header_index ]['thumbnail_url']     = $url;
    1278                 $header_images[ $header_index ]['alt_text']          = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
     1278                $header_images[ $header_index ]['alt_text']          = wp_get_attachment_image_attr( $header->ID, 'alt' );
    12791279                $header_images[ $header_index ]['attachment_parent'] = isset( $header_data['attachment_parent'] ) ? $header_data['attachment_parent'] : '';
    12801280
    12811281                if ( isset( $header_data['width'] ) ) {