<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[55821] trunk: Media: Introduce `wp_get_attachment_image_context` filter.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55821">55821</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55821","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-05-17 18:29:41 +0000 (Wed, 17 May 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/58212">#58212</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunksrcwpincludespostthumbnailtemplatephp">trunk/src/wp-includes/post-thumbnail-template.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
<li><a href="#trunktestsphpunittestspostthumbnailsphp">trunk/tests/phpunit/tests/post/thumbnails.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2023-05-17 12:04:38 UTC (rev 55820)
+++ trunk/src/wp-includes/default-filters.php   2023-05-17 18:29:41 UTC (rev 55821)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -444,9 +444,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-// Post Thumbnail CSS class filtering.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Post Thumbnail specific image filtering.
</ins><span class="cx" style="display: block; padding: 0 10px"> add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'end_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_remove' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_context_filter_add' );
+add_action( 'end_fetch_post_thumbnail_html', '_wp_post_thumbnail_context_filter_remove' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> // Redirect old slugs.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'template_redirect', 'wp_old_slug_redirect' );
</span></span></pre></div>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php   2023-05-17 12:04:38 UTC (rev 55820)
+++ trunk/src/wp-includes/media.php     2023-05-17 18:29:41 UTC (rev 55821)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1050,9 +1050,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'decoding' => 'async',
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * Filters the context in which wp_get_attachment_image() is used.
+                *
+                * @since 6.3.0
+                *
+                * @param string $context The context. Default 'wp_get_attachment_image'.
+                */
+               $context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Add `loading` attribute.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
-                       $default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( wp_lazy_loading_enabled( 'img', $context ) ) {
+                       $default_attr['loading'] = wp_get_loading_attr_default( $context );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $attr = wp_parse_args( $attr, $default_attr );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2170,6 +2179,47 @@
</span><span class="cx" style="display: block; padding: 0 10px">        remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Overrides the context used in {@see wp_get_attachment_image()}. Internal use only.
+ *
+ * Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'}
+ * action hooks to dynamically add/remove itself so as to only filter post thumbnails.
+ *
+ * @ignore
+ * @since 6.3.0
+ * @access private
+ *
+ * @param string $context The context for rendering an attachment image.
+ * @return string Modified context set to 'the_post_thumbnail'.
+ */
+function _wp_post_thumbnail_context_filter( $context ) {
+       return 'the_post_thumbnail';
+}
+
+/**
+ * Adds the '_wp_post_thumbnail_context_filter' callback to the 'wp_get_attachment_image_context'
+ * filter hook. Internal use only.
+ *
+ * @ignore
+ * @since 6.3.0
+ * @access private
+ */
+function _wp_post_thumbnail_context_filter_add() {
+       add_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
+}
+
+/**
+ * Removes the '_wp_post_thumbnail_context_filter' callback from the 'wp_get_attachment_image_context'
+ * filter hook. Internal use only.
+ *
+ * @ignore
+ * @since 6.3.0
+ * @access private
+ */
+function _wp_post_thumbnail_context_filter_remove() {
+       remove_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> add_shortcode( 'wp_caption', 'img_caption_shortcode' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_shortcode( 'caption', 'img_caption_shortcode' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludespostthumbnailtemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post-thumbnail-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post-thumbnail-template.php 2023-05-17 12:04:38 UTC (rev 55820)
+++ trunk/src/wp-includes/post-thumbnail-template.php   2023-05-17 18:29:41 UTC (rev 55821)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -186,22 +186,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        update_post_thumbnail_cache();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Add `loading` attribute.
-               if ( wp_lazy_loading_enabled( 'img', 'the_post_thumbnail' ) ) {
-                       // Get the 'loading' attribute value to use as default, taking precedence over the default from
-                       // `wp_get_attachment_image()`.
-                       $loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
-
-                       // Add the default to the given attributes unless they already include a 'loading' directive.
-                       if ( empty( $attr ) ) {
-                               $attr = array( 'loading' => $loading );
-                       } elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
-                               $attr['loading'] = $loading;
-                       } elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
-                               $attr .= '&loading=' . $loading;
-                       }
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span></span></pre></div>
<a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php       2023-05-17 12:04:38 UTC (rev 55820)
+++ trunk/tests/phpunit/tests/media.php 2023-05-17 18:29:41 UTC (rev 55821)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3964,6 +3964,62 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Tests that `wp_get_attachment_image()` uses the correct default context.
+        *
+        * @ticket 58212
+        *
+        * @covers ::wp_get_attachment_image()
+        */
+       public function test_wp_get_attachment_image_context_filter_default() {
+               $last_context = '';
+               $this->track_last_attachment_image_context( $last_context );
+
+               wp_get_attachment_image( self::$large_id );
+               $this->assertSame( 'wp_get_attachment_image', $last_context );
+       }
+
+       /**
+        * Tests that `wp_get_attachment_image()` allows overriding the context via filter.
+        *
+        * @ticket 58212
+        *
+        * @covers ::wp_get_attachment_image()
+        */
+       public function test_wp_get_attachment_image_context_filter_value_is_passed_correctly() {
+               $last_context = '';
+               $this->track_last_attachment_image_context( $last_context );
+
+               // Add a filter that modifies the context.
+               add_filter(
+                       'wp_get_attachment_image_context',
+                       static function() {
+                               return 'my_custom_context';
+                       }
+               );
+
+               wp_get_attachment_image( self::$large_id );
+               $this->assertSame( 'my_custom_context', $last_context );
+       }
+
+       /**
+        * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
+        *
+        * The method parameter is passed by reference and therefore will always contain the last context value.
+        *
+        * @param mixed $last_context Variable to track last context. Passed by reference.
+        */
+       private function track_last_attachment_image_context( &$last_context ) {
+               add_filter(
+                       'wp_get_attachment_image_context',
+                       static function( $context ) use ( &$last_context ) {
+                               $last_context = $context;
+                               return $context;
+                       },
+                       11
+               );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Add threshold to create a `-scaled` output image for testing.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function add_big_image_size_threshold() {
</span></span></pre></div>
<a id="trunktestsphpunittestspostthumbnailsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/thumbnails.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/thumbnails.php     2023-05-17 12:04:38 UTC (rev 55820)
+++ trunk/tests/phpunit/tests/post/thumbnails.php       2023-05-17 18:29:41 UTC (rev 55821)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -450,6 +450,135 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Tests that `_wp_post_thumbnail_context_filter()` returns 'the_post_thumbnail'.
+        *
+        * @ticket 58212
+        *
+        * @covers::_wp_post_thumbnail_context_filter
+        */
+       public function test_wp_post_thumbnail_context_filter_should_return_the_post_thumbnail() {
+               $this->assertSame( 'the_post_thumbnail', _wp_post_thumbnail_context_filter( 'wp_get_attachment_image' ) );
+       }
+
+       /**
+        * Tests that `::_wp_post_thumbnail_context_filter_add` adds a filter to override the context
+        * used in `wp_get_attachment_image()`.
+        *
+        * @ticket 58212
+        *
+        * @covers ::_wp_post_thumbnail_context_filter_add
+        */
+       public function test_wp_post_thumbnail_context_filter_add_should_add_the_filter() {
+               $last_context = '';
+               $this->track_last_attachment_image_context( $last_context );
+
+               _wp_post_thumbnail_context_filter_add();
+               wp_get_attachment_image( self::$attachment_id );
+
+               $this->assertSame( 'the_post_thumbnail', $last_context );
+       }
+
+       /**
+        * Tests that `_wp_post_thumbnail_context_filter_remove()` removes a filter to override the context
+        * used in `wp_get_attachment_image()`.
+        *
+        * @ticket 58212
+        *
+        * @covers ::_wp_post_thumbnail_context_filter_remove
+        */
+       public function test_wp_post_thumbnail_context_filter_remove_should_remove_the_filter() {
+               $last_context = '';
+               $this->track_last_attachment_image_context( $last_context );
+
+               _wp_post_thumbnail_context_filter_add();
+               wp_get_attachment_image( self::$attachment_id );
+
+               // Verify that the filter has been added before testing that it has been removed.
+               $this->assertSame(
+                       'the_post_thumbnail',
+                       $last_context,
+                       'The filter was not added.'
+               );
+
+               _wp_post_thumbnail_context_filter_remove();
+
+               // The context should no longer be modified by the filter.
+               wp_get_attachment_image( self::$attachment_id );
+
+               $this->assertSame(
+                       'wp_get_attachment_image',
+                       $last_context,
+                       'The filter was not removed.'
+               );
+       }
+
+       /**
+        * Tests that `get_the_post_thumbnail()` uses the 'the_post_thumbnail' context.
+        *
+        * @ticket 58212
+        *
+        * @covers ::get_the_post_thumbnail
+        */
+       public function test_get_the_post_thumbnail_should_use_the_post_thumbnail_context() {
+               $last_context = '';
+               $this->track_last_attachment_image_context( $last_context );
+
+               set_post_thumbnail( self::$post, self::$attachment_id );
+               get_the_post_thumbnail( self::$post );
+
+               $this->assertSame( 'the_post_thumbnail', $last_context );
+       }
+
+       /**
+        * Tests that `get_the_post_thumbnail()` restores the context afterwards.
+        *
+        * @ticket 58212
+        *
+        * @covers ::get_the_post_thumbnail
+        */
+       public function test_get_the_post_thumbnail_should_remove_the_post_thumbnail_context_afterwards() {
+               $last_context = '';
+               $this->track_last_attachment_image_context( $last_context );
+
+               set_post_thumbnail( self::$post, self::$attachment_id );
+               get_the_post_thumbnail( self::$post );
+
+               // Verify that the context was overridden before testing that it has been restored.
+               $this->assertSame(
+                       'the_post_thumbnail',
+                       $last_context,
+                       'The context was not overridden.'
+               );
+
+               // The context should no longer be overridden.
+               wp_get_attachment_image( self::$attachment_id );
+
+               $this->assertSame(
+                       'wp_get_attachment_image',
+                       $last_context,
+                       'The context was not restored.'
+               );
+       }
+
+       /**
+        * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
+        *
+        * The method parameter is passed by reference and therefore will always contain the last context value.
+        *
+        * @param mixed $last_context Variable to track last context. Passed by reference.
+        */
+       private function track_last_attachment_image_context( &$last_context ) {
+               add_filter(
+                       'wp_get_attachment_image_context',
+                       static function( $context ) use ( &$last_context ) {
+                               $last_context = $context;
+                               return $context;
+                       },
+                       11
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function filter_post_thumbnail_size( $size, $post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_array( $this->current_size_filter_data ) && isset( $this->current_size_filter_data[ $post_id ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return $this->current_size_filter_data[ $post_id ];
</span></span></pre>
</div>
</div>

</body>
</html>