Make WordPress Core

Changeset 55821

Timestamp:
05/17/2023 06:29:41 PM (15 months ago)
Author:
flixos90
Message:

Media: Introduce wp_get_attachment_image_context filter.

Since WordPress 5.9, a "context" value of "wp_get_attachment_image" has been used in the wp_get_attachment_image() function to provide context to underlying functions where that is relevant, e.g. wp_get_loading_attr_default(). Since that value used to be not customizable, it required a workaround in get_the_post_thumbnail() to avoid calling those functions in wp_get_attachment_image(), which resulted in unnecessary complexity and was prone to errors.

This changeset introduces a wp_get_attachment_image_context filter and leverages it with private filter callback functions that are leveraged by default when get_the_post_thumbnail() is called. This avoids the need for the previous workaround and furthermore provides flexibility for other callers of wp_get_attachment_image() to provide their own contexts.

Props flixos90, costdev, thekt12, westonruter, spacedmonkey.
Fixes #58212.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r55695 r55821  
    445445add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
    446446
    447 // Post Thumbnail CSS class filtering.
     447// Post Thumbnail filtering.
    448448add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
    449449add_action( 'end_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_remove' );
     450
     451
    450452
    451453// Redirect old slugs.
  • trunk/src/wp-includes/media.php

    r55816 r55821  
    10511051        );
    10521052
     1053
     1054
     1055
     1056
     1057
     1058
     1059
     1060
     1061
    10531062        // Add `loading` attribute.
    1054         if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
    1055             $default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
     1063        if ( wp_lazy_loading_enabled( 'img', ) ) {
     1064            $default_attr['loading'] = wp_get_loading_attr_default( );
    10561065        }
    10571066
     
    21692178function _wp_post_thumbnail_class_filter_remove( $attr ) {
    21702179    remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
     2180
     2181
     2182
     2183
     2184
     2185
     2186
     2187
     2188
     2189
     2190
     2191
     2192
     2193
     2194
     2195
     2196
     2197
     2198
     2199
     2200
     2201
     2202
     2203
     2204
     2205
     2206
     2207
     2208
     2209
     2210
     2211
     2212
     2213
     2214
     2215
     2216
     2217
     2218
     2219
     2220
    21712221}
    21722222
  • trunk/src/wp-includes/post-thumbnail-template.php

    r55093 r55821  
    187187        }
    188188
    189         // Add `loading` attribute.
    190         if ( wp_lazy_loading_enabled( 'img', 'the_post_thumbnail' ) ) {
    191             // Get the 'loading' attribute value to use as default, taking precedence over the default from
    192             // `wp_get_attachment_image()`.
    193             $loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
    194 
    195             // Add the default to the given attributes unless they already include a 'loading' directive.
    196             if ( empty( $attr ) ) {
    197                 $attr = array( 'loading' => $loading );
    198             } elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
    199                 $attr['loading'] = $loading;
    200             } elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
    201                 $attr .= '&loading=' . $loading;
    202             }
    203         }
    204 
    205189        $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
    206190
  • trunk/tests/phpunit/tests/media.php

    r55816 r55821  
    39653965
    39663966    /**
     3967
     3968
     3969
     3970
     3971
     3972
     3973
     3974
     3975
     3976
     3977
     3978
     3979
     3980
     3981
     3982
     3983
     3984
     3985
     3986
     3987
     3988
     3989
     3990
     3991
     3992
     3993
     3994
     3995
     3996
     3997
     3998
     3999
     4000
     4001
     4002
     4003
     4004
     4005
     4006
     4007
     4008
     4009
     4010
     4011
     4012
     4013
     4014
     4015
     4016
     4017
     4018
     4019
     4020
     4021
     4022
    39674023     * Add threshold to create a `-scaled` output image for testing.
    39684024     */
  • trunk/tests/phpunit/tests/post/thumbnails.php

    r55093 r55821  
    451451    }
    452452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
    453582    public function filter_post_thumbnail_size( $size, $post_id ) {
    454583        if ( is_array( $this->current_size_filter_data ) && isset( $this->current_size_filter_data[ $post_id ] ) ) {
Note: See TracChangeset for help on using the changeset viewer.