<!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>[55252] trunk: Taxonomy: Implement `wp_cache_get_multiple` in `wp_queue_posts_for_term_meta_lazyload`.</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/55252">55252</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/55252","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-02-07 12:07:46 +0000 (Tue, 07 Feb 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'>Taxonomy: Implement `wp_cache_get_multiple` in `wp_queue_posts_for_term_meta_lazyload`.

In <a href="https://core.trac.wordpress.org/changeset/47938">[47938]</a> the `wp_cache_get_multiple` function was added to core. This function allows for multiple cache keys to be received from cache in a single function call. `wp_queue_posts_for_term_meta_lazyload` function does many calls to cache. To get taxonomy relationship for multiple posts and get all terms. Replace calls to `get_object_term_cache` with calls to `wp_cache_get_multiple` and `_prime_term_caches`. This improves performance on sites that implement the `wp_cache_get_multiple` in their object caching drop-in. 

Props spacedmonkey, ocean90, SergeyBiryukov, costdev, flixos90, joemcgill, 10upsimon. 
Fixes <a href="https://core.trac.wordpress.org/ticket/57150">#57150</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunitincludesabstracttestcasephp">trunk/tests/phpunit/includes/abstract-testcase.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsquerylazyLoadTermMetaphp">trunk/tests/phpunit/tests/query/lazyLoadTermMeta.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></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/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php  2023-02-07 12:06:33 UTC (rev 55251)
+++ trunk/src/wp-includes/class-wp-query.php    2023-02-07 12:07:46 UTC (rev 55252)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1948,6 +1948,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! isset( $q['lazy_load_term_meta'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $q['lazy_load_term_meta'] = $q['update_post_term_cache'];
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                } elseif ( $q['lazy_load_term_meta'] ) { // Lazy loading term meta only works if term caches are primed.
+                       $q['update_post_term_cache'] = true;
</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">                if ( ! isset( $q['update_post_meta_cache'] ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></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.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2023-02-07 12:06:33 UTC (rev 55251)
+++ trunk/src/wp-includes/post.php      2023-02-07 12:07:46 UTC (rev 55252)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7779,7 +7779,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_queue_posts_for_term_meta_lazyload( $posts ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $post_type_taxonomies = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $term_ids             = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prime_post_terms     = array();
</ins><span class="cx" style="display: block; padding: 0 10px">         foreach ( $posts as $post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! ( $post instanceof WP_Post ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        continue;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7790,16 +7790,52 @@
</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">                foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Term cache should already be primed by `update_post_term_cache()`.
-                       $terms = get_object_term_cache( $post->ID, $taxonomy );
-                       if ( false !== $terms ) {
-                               foreach ( $terms as $term ) {
-                                       if ( ! in_array( $term->term_id, $term_ids, true ) ) {
-                                               $term_ids[] = $term->term_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $prime_post_terms[ $taxonomy ][] = $post->ID;
+               }
+       }
+
+       $term_ids = array();
+       if ( $prime_post_terms ) {
+               $prime_term_ids     = array();
+               $prime_taxonomy_ids = array();
+               foreach ( $prime_post_terms as $taxonomy => $post_ids ) {
+                       $cached_term_ids = wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" );
+                       if ( is_array( $cached_term_ids ) ) {
+                               $cached_term_ids = array_filter( $cached_term_ids );
+                               foreach ( $cached_term_ids as $term_ids ) {
+                                       // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
+                                       foreach ( $term_ids as $term_id ) {
+                                               if ( is_numeric( $term_id ) ) {
+                                                       $prime_term_ids[]                  = (int) $term_id;
+                                                       $prime_taxonomy_ids[ $taxonomy ][] = (int) $term_id;
+                                               } elseif ( isset( $term_id->term_id ) ) {
+                                                       $prime_taxonomy_ids[ $taxonomy ][] = (int) $term_id->term_id;
+                                                       $prime_term_ids[]                  = (int) $term_id->term_id;
+                                               }
</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">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( $prime_term_ids ) {
+                       $prime_term_ids = array_unique( $prime_term_ids );
+                       // Do not prime term meta at this point, let the lazy loader take care of that.
+                       _prime_term_caches( $prime_term_ids, false );
+
+                       foreach ( $prime_taxonomy_ids as $taxonomy => $_term_ids ) {
+                               foreach ( $_term_ids as $term_id ) {
+                                       if ( in_array( $term_id, $term_ids, true ) ) {
+                                               continue;
+                                       }
+                                       $term = get_term( $term_id, $taxonomy );
+                                       if ( is_wp_error( $term ) ) {
+                                               continue;
+                                       }
+
+                                       $term_ids[] = $term_id;
+                               }
+                       }
+               }
</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">        if ( $term_ids ) {
</span></span></pre></div>
<a id="trunktestsphpunitincludesabstracttestcasephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/abstract-testcase.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/abstract-testcase.php        2023-02-07 12:06:33 UTC (rev 55251)
+++ trunk/tests/phpunit/includes/abstract-testcase.php  2023-02-07 12:07:46 UTC (rev 55252)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -194,6 +194,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->_restore_hooks();
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( 0 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $lazyloader = wp_metadata_lazyloader();
+               $lazyloader->reset_queue( 'term' );
+               $lazyloader->reset_queue( 'comment' );
</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">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsquerylazyLoadTermMetaphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/query/lazyLoadTermMeta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/lazyLoadTermMeta.php                              (rev 0)
+++ trunk/tests/phpunit/tests/query/lazyLoadTermMeta.php        2023-02-07 12:07:46 UTC (rev 55252)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,138 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group query
+ * @group taxonomy
+ * @group meta
+ */
+class Test_Lazy_Load_Term_Meta extends WP_UnitTestCase {
+       /**
+        * @var array
+        */
+       protected static $post_ids = array();
+       /**
+        * @var array
+        */
+       protected static $term_ids = array();
+
+       public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+               $post_type      = 'post';
+               self::$post_ids = $factory->post->create_many(
+                       3,
+                       array(
+                               'post_type'   => $post_type,
+                               'post_status' => 'publish',
+                       )
+               );
+               $taxonomies     = get_object_taxonomies( $post_type, 'object' );
+               foreach ( self::$post_ids  as $post_id ) {
+                       foreach ( $taxonomies as $taxonomy ) {
+                               if ( ! $taxonomy->_builtin ) {
+                                       continue;
+                               }
+                               $terms          = $factory->term->create_many( 3, array( 'taxonomy' => $taxonomy->name ) );
+                               self::$term_ids = array_merge( self::$term_ids, $terms );
+                               foreach ( $terms as $term ) {
+                                       add_term_meta( $term, wp_rand(), 'test' );
+                               }
+                               wp_set_object_terms( $post_id, $terms, $taxonomy->name );
+                       }
+               }
+       }
+
+       /**
+        * @ticket 57150
+        * @covers ::wp_queue_posts_for_term_meta_lazyload
+        */
+       public function test_wp_queue_posts_for_term_meta_lazyload() {
+               $filter = new MockAction();
+               add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
+               new WP_Query(
+                       array(
+                               'post__in'            => self::$post_ids,
+                               'lazy_load_term_meta' => true,
+                       )
+               );
+
+               get_term_meta( end( self::$term_ids ) );
+
+               $args     = $filter->get_args();
+               $first    = reset( $args );
+               $term_ids = end( $first );
+               $this->assertSameSets( $term_ids, self::$term_ids );
+       }
+
+       /**
+        * @ticket 57150
+        * @covers ::wp_queue_posts_for_term_meta_lazyload
+        */
+       public function test_wp_queue_posts_for_term_meta_lazyload_update_post_term_cache() {
+               $filter = new MockAction();
+               add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
+               new WP_Query(
+                       array(
+                               'post__in'               => self::$post_ids,
+                               'lazy_load_term_meta'    => true,
+                               'update_post_term_cache' => false,
+                       )
+               );
+
+               get_term_meta( end( self::$term_ids ) );
+
+               $args     = $filter->get_args();
+               $first    = reset( $args );
+               $term_ids = end( $first );
+               $this->assertSameSets( $term_ids, self::$term_ids );
+       }
+
+       /**
+        * @ticket 57150
+        * @covers ::wp_queue_posts_for_term_meta_lazyload
+        */
+       public function test_wp_queue_posts_for_term_meta_lazyload_false() {
+               $filter = new MockAction();
+               add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
+               new WP_Query(
+                       array(
+                               'post__in'            => self::$post_ids,
+                               'lazy_load_term_meta' => false,
+                       )
+               );
+
+               $term_id = end( self::$term_ids );
+               get_term_meta( $term_id );
+
+               $args     = $filter->get_args();
+               $first    = reset( $args );
+               $term_ids = end( $first );
+               $this->assertSameSets( $term_ids, array( $term_id ) );
+       }
+
+       /**
+        * @ticket 57150
+        * @covers ::wp_queue_posts_for_term_meta_lazyload
+        */
+       public function test_wp_queue_posts_for_term_meta_lazyload_delete_term() {
+               $filter = new MockAction();
+               add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
+
+               $remove_term_id = end( self::$term_ids );
+               $term           = get_term( $remove_term_id );
+               wp_delete_term( $remove_term_id, $term->taxonomy );
+
+               new WP_Query(
+                       array(
+                               'post__in'            => self::$post_ids,
+                               'lazy_load_term_meta' => true,
+                       )
+               );
+
+               $term_id = end( self::$term_ids );
+               get_term_meta( $term_id );
+
+               $args     = $filter->get_args();
+               $first    = reset( $args );
+               $term_ids = end( $first );
+               $this->assertNotContains( $remove_term_id, $term_ids );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/query/lazyLoadTermMeta.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>