<!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>[12474] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php: Plugin Directory: Use Localised images for banners if available.</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="http://meta.trac.wordpress.org/changeset/12474">12474</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12474","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-03-16 04:42:12 +0000 (Thu, 16 Mar 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'>Plugin Directory: Use Localised images for banners if available.

See <a href="http://meta.trac.wordpress.org/ticket/6627">#6627</a>, <a href="http://meta.trac.wordpress.org/ticket/3935">#3935</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclasstemplatephp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclasstemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php      2023-03-16 03:18:26 UTC (rev 12473)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php        2023-03-16 04:42:12 UTC (rev 12474)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -359,45 +359,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param int|\WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string            $output Optional. Output type. 'html' or 'raw'. Default: 'raw'.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param string            $locale Optional. Locale to use. Default: current locale.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return mixed
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static function get_plugin_icon( $post = null, $output = 'raw' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function get_plugin_icon( $post = null, $output = 'raw', $locale = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $plugin = get_post( $post );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $locale = $locale ?: get_locale();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $raw_icons = get_post_meta( $plugin->ID, 'assets_icons', true ) ?: array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $all_icons = get_post_meta( $plugin->ID, 'assets_icons', true ) ?: [];
</ins><span class="cx" style="display: block; padding: 0 10px">                 $icon      = $icon_1x = $icon_2x = $svg = $generated = false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $svg       = self::find_best_asset( $plugin, $all_icons, false, $locale );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $raw_icons as $file => $info ) {
-                       switch ( $info['resolution'] ) {
-                               case '256x256':
-                                       $icon_2x = self::get_asset_url( $plugin, $info );
-                                       break;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // SVG has priority.
+               if ( $svg && 'icon.svg' === $svg['filename'] ) {
+                       $icon   = $svg;
+               } else {
+                       // Look for the non-SVGs.
+                       $svg     = false;
+                       $icon_1x = self::find_best_asset( $plugin, $all_icons, '128x128', $locale );
+                       $icon_2x = self::find_best_asset( $plugin, $all_icons, '256x256', $locale );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                case '128x128':
-                                       $icon_1x = self::get_asset_url( $plugin, $info );
-                                       break;
-
-                               /* false = the resolution of the icon, this is NOT disabled */
-                               case false && 'icon.svg' == $file:
-                                       $icon = $svg = self::get_asset_url( $plugin, $info );
-                                       break;
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $icon = ( $icon_1x ?: $icon_2x ) ?: false;
</ins><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">-                // Fallback to 1x if it exists.
-               if ( ! $icon && $icon_1x ) {
-                       $icon = $icon_1x;
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Resolve to URLs
+               $icon    = $icon    ? self::get_asset_url( $plugin, $icon )    : false;
+               $icon_2x = $icon_2x ? self::get_asset_url( $plugin, $icon_2x ) : false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Fallback to 2x if it exists.
-               if ( ! $icon && $icon_2x ) {
-                       $icon = $icon_2x;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( ! $icon || 'publish' !== $plugin->post_status ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $generated = true;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $icon_2x = false; // For the ! publish branch.
-                       $icon = self::get_geopattern_icon_url( $plugin );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $icon_2x   = false; // For the ! publish branch.
+                       $icon      = self::get_geopattern_icon_url( $plugin );
</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">                switch ( $output ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -445,10 +437,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param int|\WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string            $output Optional. Output type. 'html', 'raw', or 'raw_with_rtl'. Default: 'raw'.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param string            $locale Optional. Locale to use. Defaults to current locale.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return mixed
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static function get_plugin_banner( $post = null, $output = 'raw' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function get_plugin_banner( $post = null, $output = 'raw', $locale = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $plugin = get_post( $post );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $locale = $locale ?: get_locale();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( in_array( $plugin->post_status, [ 'disabled', 'closed' ], true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return false;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -455,47 +449,44 @@
</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">                $banner      = $banner_2x = $banner_rtl = $banner_2x_rtl = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $raw_banners = get_post_meta( $plugin->ID, 'assets_banners', true ) ?: array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $all_banners = get_post_meta( $plugin->ID, 'assets_banners', true ) ?: [];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Split in rtl and non-rtl banners.
-               $rtl_banners = array_filter( $raw_banners, function ( $info ) {
-                       return (bool) stristr( $info['filename'], '-rtl' );
-               } );
-               $raw_banners = array_diff_key( $raw_banners, $rtl_banners );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $banner    = self::find_best_asset( $plugin, $all_banners, '772x250', $locale );
+               $banner_2x = self::find_best_asset( $plugin, $all_banners, '1544x500', $locale );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Default are non-rtl banners.
-               foreach ( $raw_banners as $info ) {
-                       switch ( $info['resolution'] ) {
-                               case '1544x500':
-                                       $banner_2x = self::get_asset_url( $plugin, $info );
-                                       break;
-
-                               case '772x250':
-                                       $banner = self::get_asset_url( $plugin, $info );
-                                       break;
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $banner ) {
+                       return false;
</ins><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">-                if ( is_rtl() || 'raw_with_rtl' == $output ) {
-                       foreach ( $rtl_banners as $info ) {
-                               switch ( $info['resolution'] ) {
-                                       case '1544x500':
-                                               $field = 'raw_with_rtl' == $output ? 'banner_2x_rtl' : 'banner_2x';
-                                               $$field = self::get_asset_url( $plugin, $info );
-                                               break;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * If we need both LTR and a RTL banners, fetch both..
+                * This doesn't use find_best_asset() as it's too complex to add the "RTL only" functionality to it.
+                */
+               if ( 'raw_with_rtl' === $output ) {
+                       $banner_rtl = array_filter(
+                               wp_list_filter( $all_banners, array( 'resolution' => '772x250' ) ),
+                               function( $info ) {
+                                       return (bool) stristr( $info['filename'], '-rtl' );
+                               }
+                       );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        case '772x250':
-                                               $field = 'raw_with_rtl' == $output ? 'banner_rtl' : 'banner';
-                                               $$field = self::get_asset_url( $plugin, $info );
-                                               break;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $banner_2x_rtl = array_filter(
+                               wp_list_filter( $all_banners, array( 'resolution' => '1544x500' ) ),
+                               function( $info ) {
+                                       return (bool) stristr( $info['filename'], '-rtl' );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        }
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $banner ) {
-                       return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $banner_rtl    = $banner_rtl    ? array_shift( $banner_rtl )    : false;
+                       $banner_2x_rtl = $banner_2x_rtl ? array_shift( $banner_2x_rtl ) : false;
</ins><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">+                // Resolve the URLs.
+               $banner        = $banner        ? self::get_asset_url( $plugin, $banner )        : false;
+               $banner_2x     = $banner_2x     ? self::get_asset_url( $plugin, $banner_2x )     : false;
+               $banner_rtl    = $banner_rtl    ? self::get_asset_url( $plugin, $banner_rtl )    : false;
+               $banner_2x_rtl = $banner_2x_rtl ? self::get_asset_url( $plugin, $banner_2x_rtl ) : false;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 switch ( $output ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'html':
</span><span class="cx" style="display: block; padding: 0 10px">                                $id    = "plugin-banner-{$plugin->post_name}";
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -518,6 +509,69 @@
</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">+         * Retrieve the Plugin asset that matches the requested resolution, locale, and RTL.
+        *
+        * @static
+        *
+        * @param \WP_Post $plugin     The plugin.
+        * @param array    $assets     The assets.
+        * @param string   $resolution The resolution.
+        * @param string   $locale     The locale.
+        * @return string|false
+        */
+       public static function find_best_asset( $plugin, $assets, $resolution, $locale ) {
+               // Asset matches resolution.
+               $assets = wp_list_filter( $assets, [ 'resolution' => $resolution ] );
+
+               /*
+                * Filter the matching assets by locale.
+                * NOTE: en_US/'' must also go through this branch, to remove localised assets from the list.
+                * This also handles plugins which have specific english assets.
+                */
+               if ( count( $assets ) > 1 ) {
+                       // Locales, match [ `de_DE_formal`, `de_DE`, `de` ], prioritising the full locale before falling back to the partial match.
+                       $locale_parts = explode( '_', $locale );
+                       foreach ( range( count( $locale_parts ), 1 ) as $length ) {
+                               $locale       = implode( '_', array_slice( $locale_parts, 0, $length ) );
+                               $locale_asset = wp_list_filter( $assets, [ 'locale' => $locale ] );
+                               if ( $locale_asset ) {
+                                       break;
+                               }
+                       }
+                       if ( ! $locale_asset ) {
+                               // No locale match, filter to no-locale only.
+                               $locale_asset = wp_list_filter( $assets, [ 'locale' => '' ] );
+                       }
+
+                       $assets = $locale_asset ?: $assets;
+               }
+
+               // Fetch RTL asset, if needed. This is only needed if there isn't a locale match.
+               if ( count( $assets ) > 1 ) {
+                       $direction_assets = array_filter(
+                               $assets,
+                               function( $info ) {
+                                       // If we're on a RTL locale, we filter to RTL items else we remove them.
+                                       $is_rtl_image = (bool) stristr( $info['filename'], '-rtl' );
+
+                                       if ( is_rtl() ) {
+                                               return $is_rtl_image;
+                                       } else {
+                                               return ! $is_rtl_image;
+                                       }
+                               }
+                       );
+                       $assets           = $direction_assets ?: $assets;
+               }
+
+               if ( ! $assets ) {
+                       return false;
+               }
+
+               return array_shift( $assets );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Generates and returns the URL to a passed asset.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Assets can be screenshots, icons, banners, etc.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1020,72 +1074,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_screenshots( $plugin = null, $locale = null ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $plugin = get_post( $plugin );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $locale = $locale ?: get_locale();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $locale ) {
-                       $locale = get_locale();
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // All indexed from 1. The Image 'number' is stored in the 'resolution' key
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $screen_shots = get_post_meta( $plugin->ID, 'assets_screenshots', true ) ?: array();
-               $descriptions = get_post_meta( $plugin->ID, 'screenshots', true ) ?: array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $all_screenshots = get_post_meta( $plugin->ID, 'assets_screenshots', true ) ?: [];
+               $descriptions    = get_post_meta( $plugin->ID, 'screenshots', true )        ?: [];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $screen_shots ) ) {
-                       return array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $all_screenshots ) ) {
+                       return [];
</ins><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">-                $sorted = array();
-               foreach ( $screen_shots as $image ) {
-                       if ( ! isset( $sorted[ $image['resolution'] ] ) ) {
-                               $sorted[ $image['resolution'] ] = array();
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $screenshot_nums = array_unique( wp_list_pluck( $all_screenshots, 'resolution') );
+               sort( $screenshot_nums, SORT_NATURAL );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( empty( $image['locale'] ) ) {
-                               // if the image has no locale, always insert to the last element (lowerst priority).
-                               $sorted[ $image['resolution'] ][] = $image;
-                       } elseif ( $locale === $image['locale'] ) {
-                               // if the locale is a full match, always insert to the first element (highest priority).
-                               array_unshift( $sorted[ $image['resolution'] ], $image );
-                       } else {
-                               // TODO: de_DE_informal should probably fall back to de_DE before de_CH. Maybe this can wait until Core properly supports locale hierarchy.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $screenshot_nums as $screenshot_num ) {
+                       $caption = $descriptions[ (int) $screenshot_num ] ?? '';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $image_locale_parts = explode( '_', $image['locale'] );
-                               $locale_parts       = explode( '_', $locale );
-                               // if only the language matches.
-                               if ( $image_locale_parts[0] === $locale_parts[0] ) {
-                                       // image with locale has a higher priority than image without locale.
-                                       $last_image = end( $sorted[ $image['resolution'] ] );
-                                       if ( empty( $last_image['locale'] ) ) {
-                                               array_splice( $sorted[ $image['resolution'] ], count( $sorted[ $image['resolution'] ] ), 0, array( $image ) );
-                                       } else {
-                                               $sorted[ $image['resolution'] ][] = $image;
-                                       }
-                               }
-                       }
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $caption = Plugin_I18n::instance()->translate(
+                               'screenshot-' . $screenshot_num,
+                               $caption,
+                               [ 'post_id' => $plugin->ID ]
+                       );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Sort
-               ksort( $sorted, SORT_NATURAL );
-
-               // Reduce images to singulars and attach metadata
-               foreach ( $sorted as $index => $items ) {
-                       // The highest priority image is the first.
-                       $image = $items[0];
-
-                       // Attach caption data
-                       $image['caption'] = false;
-                       if ( isset( $descriptions[ (int) $index ] ) ) {
-                               $image['caption'] = $descriptions[ (int) $index ];
-                               $image['caption'] = Plugin_I18n::instance()->translate(
-                                       'screenshot-' . $image['resolution'],
-                                       $image['caption'],
-                                       [ 'post_id' => $plugin->ID ]
-                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $asset = self::find_best_asset( $plugin, $all_screenshots, $screenshot_num, $locale );
+                       if ( ! $asset ) {
+                               continue;
</ins><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">-                        // Attach URL information for the asset
-                       $image['src'] = Template::get_asset_url( $plugin, $image );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $asset['caption'] = $caption;
+                       $asset['src']     = self::get_asset_url( $plugin, $asset );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $sorted[ $index ] = $image;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $sorted[ $screenshot_num ] = $asset;
</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">                return $sorted;
</span></span></pre>
</div>
</div>

</body>
</html>