Making WordPress.org

Ticket #3935: 3935.3.diff

File 3935.3.diff, 4.4 KB (added by ck3lee, 5 years ago)
  • wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    diff --git wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php
    index d407c1295..bf8a0ef50 100644
    class Import { 
    362362                $svn_assets_folder = SVN::ls( self::PLUGIN_SVN_BASE . "/{$plugin_slug}/assets/", true /* verbose */ );
    363363                if ( $svn_assets_folder ) { // /assets/ may not exist.
    364364                        foreach ( $svn_assets_folder as $asset ) {
    365                                 // screenshot-0(-rtl).(png|jpg|jpeg|gif)  ||  icon.svg
    366                                 if ( ! preg_match( '!^(?P<type>screenshot|banner|icon)(-(?P<resolution>[\dx]+)(-rtl)?\.(png|jpg|jpeg|gif)|\.svg)$!i', $asset['filename'], $m ) ) {
     365                                // screenshot-0(-rtl)(-de_DE).(png|jpg|jpeg|gif)  ||  icon.svg
     366                                if ( ! preg_match( '!^(?P<type>screenshot|banner|icon)(?:-(?P<resolution>[\dx]+)(-rtl)?(?:-(?P<locale>[a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?))?\.(png|jpg|jpeg|gif)|\.svg)$!i', $asset['filename'], $m ) ) {
    367367                                        continue;
    368368                                }
    369369
    class Import { 
    372372                                $revision   = $asset['revision'];
    373373                                $location   = 'assets';
    374374                                $resolution = isset( $m['resolution'] ) ? $m['resolution'] : false;
     375                                $locale     = isset( $m['locale'] ) ? $m['locale'] : false;
    375376
    376                                 $assets[ $type ][ $asset['filename'] ] = compact( 'filename', 'revision', 'resolution', 'location' );
     377                                $assets[ $type ][ $asset['filename'] ] = compact( 'filename', 'revision', 'resolution', 'location', 'locale' );
    377378                        }
    378379                }
    379380
  • wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-screenshots.php

    diff --git wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-screenshots.php wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-screenshots.php
    index 4bfe1400a..470e1ff1d 100644
    use WordPressdotorg\Plugin_Directory\Plugin_i18n; 
    1111 */
    1212class Screenshots {
    1313
     14        /**
     15         * Reduce the screenshots metadata to group and sort the best matched locale screenshots
     16         *
     17         * @param array  $screen_shots An array with screenshots metadata for the plugin.
     18         * @param string $user_locale  The locale of the current request.
     19         * @return array screenshots grouped by resolution(ID), sorted by the best matched locale first.
     20         */
     21        private static function reduce_screenshots_with_locale( $screen_shots, $user_locale ) {
     22
     23                $reduced = array();
     24
     25                foreach ( $screen_shots as $image ) {
     26
     27                        if ( ! isset( $reduced[ $image['resolution'] ] ) ) {
     28                                $reduced[ $image['resolution'] ] = array();
     29                        }
     30
     31                        if ( get_locale() === $image['locale'] ) {
     32                                // if the locale is a full match, always insert to the first element (highest priority).
     33                                array_unshift( $reduced[ $image['resolution'] ], $image );
     34                        } elseif ( empty( $image['locale'] ) ) {
     35                                // if the image has no locale, always insert to the last element (lowerst priority).
     36                                $reduced[ $image['resolution'] ][] = $image;
     37                        } else {
     38                                $array1 = explode( '_', $image['locale'] );
     39                                $array2 = explode( '_', $user_locale );
     40                                // if only language is matched.
     41                                if ( $array1[0] === $array2[0] ) {
     42                                        // image with locale has a higher priority than image without locale.
     43                                        if ( '' === end( $reduced[ $image['resolution'] ] )['locale'] ) {
     44                                                array_splice( $reduced[ $image['resolution'] ], count( $reduced[ $image['resolution'] ] ), 0, array( $image ) );
     45                                        } else {
     46                                                $reduced[ $image['resolution'] ][] = $image;
     47                                        }
     48                                }
     49                        }
     50                }
     51
     52                return $reduced;
     53        }
     54
    1455        /**
    1556         * @return string
    1657         */
    class Screenshots { 
    2667                        return '';
    2768                }
    2869
    29                 ksort( $screen_shots, SORT_NATURAL );
     70                $reduced = self::reduce_screenshots_with_locale( $screen_shots, get_locale() );
     71
     72                ksort( $reduced, SORT_NATURAL );
    3073
    3174                /*
    3275                 * Find the image that corresponds with the text.
    3376                 * The image numbers are stored within the 'resolution' key.
     77                 * The same key can have more than one screenshot with different locales.
    3478                 */
    35                 foreach ( $screen_shots as $image ) {
     79                foreach ( $reduced as $screen_shots ) {
     80
     81                        $image = $screen_shots[0];
     82
    3683                        $screen_shot = sprintf(
    3784                                '<a href="%1$s" rel="nofollow"><img class="screenshot" src="%1$s" alt="" /></a>',
    3885                                esc_url( Template::get_asset_url( $plugin, $image ) )