Making WordPress.org

Changeset 12474


Ignore:
Timestamp:
03/16/2023 04:42:12 AM (4 years ago)
Author:
dd32
Message:

Plugin Directory: Use Localised images for banners if available.

See #6627, #3935.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r12319 r12474  
    360360         * @param int|\WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
    361361         * @param string            $output Optional. Output type. 'html' or 'raw'. Default: 'raw'.
     362         * @param string            $locale Optional. Locale to use. Default: current locale.
    362363         * @return mixed
    363364         */
    364         public static function get_plugin_icon( $post = null, $output = 'raw' ) {
     365        public static function get_plugin_icon( $post = null, $output = 'raw', $locale = null ) {
    365366                $plugin = get_post( $post );
    366 
    367                 $raw_icons = get_post_meta( $plugin->ID, 'assets_icons', true ) ?: array();
     367                $locale = $locale ?: get_locale();
     368
     369                $all_icons = get_post_meta( $plugin->ID, 'assets_icons', true ) ?: [];
    368370                $icon      = $icon_1x = $icon_2x = $svg = $generated = false;
    369 
    370                 foreach ( $raw_icons as $file => $info ) {
    371                         switch ( $info['resolution'] ) {
    372                                 case '256x256':
    373                                         $icon_2x = self::get_asset_url( $plugin, $info );
    374                                         break;
    375 
    376                                 case '128x128':
    377                                         $icon_1x = self::get_asset_url( $plugin, $info );
    378                                         break;
    379 
    380                                 /* false = the resolution of the icon, this is NOT disabled */
    381                                 case false && 'icon.svg' == $file:
    382                                         $icon = $svg = self::get_asset_url( $plugin, $info );
    383                                         break;
    384                         }
    385                 }
    386 
    387                 // Fallback to 1x if it exists.
    388                 if ( ! $icon && $icon_1x ) {
    389                         $icon = $icon_1x;
    390                 }
    391 
    392                 // Fallback to 2x if it exists.
    393                 if ( ! $icon && $icon_2x ) {
    394                         $icon = $icon_2x;
    395                 }
     371                $svg       = self::find_best_asset( $plugin, $all_icons, false, $locale );
     372
     373                // SVG has priority.
     374                if ( $svg && 'icon.svg' === $svg['filename'] ) {
     375                        $icon   = $svg;
     376                } else {
     377                        // Look for the non-SVGs.
     378                        $svg     = false;
     379                        $icon_1x = self::find_best_asset( $plugin, $all_icons, '128x128', $locale );
     380                        $icon_2x = self::find_best_asset( $plugin, $all_icons, '256x256', $locale );
     381
     382                        $icon = ( $icon_1x ?: $icon_2x ) ?: false;
     383                }
     384
     385                // Resolve to URLs
     386                $icon    = $icon    ? self::get_asset_url( $plugin, $icon )    : false;
     387                $icon_2x = $icon_2x ? self::get_asset_url( $plugin, $icon_2x ) : false;
    396388
    397389                if ( ! $icon || 'publish' !== $plugin->post_status ) {
    398390                        $generated = true;
    399                         $icon_2x = false; // For the ! publish branch.
    400                         $icon = self::get_geopattern_icon_url( $plugin );
     391                        $icon_2x   = false; // For the ! publish branch.
     392                        $icon      = self::get_geopattern_icon_url( $plugin );
    401393                }
    402394
    … …  
    446438         * @param int|\WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
    447439         * @param string            $output Optional. Output type. 'html', 'raw', or 'raw_with_rtl'. Default: 'raw'.
     440         * @param string            $locale Optional. Locale to use. Defaults to current locale.
    448441         * @return mixed
    449442         */
    450         public static function get_plugin_banner( $post = null, $output = 'raw' ) {
     443        public static function get_plugin_banner( $post = null, $output = 'raw', $locale = null ) {
    451444                $plugin = get_post( $post );
     445                $locale = $locale ?: get_locale();
    452446
    453447                if ( in_array( $plugin->post_status, [ 'disabled', 'closed' ], true ) ) {
    … …  
    456450
    457451                $banner      = $banner_2x = $banner_rtl = $banner_2x_rtl = false;
    458                 $raw_banners = get_post_meta( $plugin->ID, 'assets_banners', true ) ?: array();
    459 
    460                 // Split in rtl and non-rtl banners.
    461                 $rtl_banners = array_filter( $raw_banners, function ( $info ) {
    462                         return (bool) stristr( $info['filename'], '-rtl' );
    463                 } );
    464                 $raw_banners = array_diff_key( $raw_banners, $rtl_banners );
    465 
    466                 // Default are non-rtl banners.
    467                 foreach ( $raw_banners as $info ) {
    468                         switch ( $info['resolution'] ) {
    469                                 case '1544x500':
    470                                         $banner_2x = self::get_asset_url( $plugin, $info );
    471                                         break;
    472 
    473                                 case '772x250':
    474                                         $banner = self::get_asset_url( $plugin, $info );
    475                                         break;
    476                         }
    477                 }
    478 
    479                 if ( is_rtl() || 'raw_with_rtl' == $output ) {
    480                         foreach ( $rtl_banners as $info ) {
    481                                 switch ( $info['resolution'] ) {
    482                                         case '1544x500':
    483                                                 $field = 'raw_with_rtl' == $output ? 'banner_2x_rtl' : 'banner_2x';
    484                                                 $$field = self::get_asset_url( $plugin, $info );
    485                                                 break;
    486 
    487                                         case '772x250':
    488                                                 $field = 'raw_with_rtl' == $output ? 'banner_rtl' : 'banner';
    489                                                 $$field = self::get_asset_url( $plugin, $info );
    490                                                 break;
    491                                 }
    492                         }
    493                 }
     452                $all_banners = get_post_meta( $plugin->ID, 'assets_banners', true ) ?: [];
     453
     454                $banner    = self::find_best_asset( $plugin, $all_banners, '772x250', $locale );
     455                $banner_2x = self::find_best_asset( $plugin, $all_banners, '1544x500', $locale );
    494456
    495457                if ( ! $banner ) {
    496458                        return false;
    497459                }
     460
     461                /*
     462                 * If we need both LTR and a RTL banners, fetch both..
     463                 * This doesn't use find_best_asset() as it's too complex to add the "RTL only" functionality to it.
     464                 */
     465                if ( 'raw_with_rtl' === $output ) {
     466                        $banner_rtl = array_filter(
     467                                wp_list_filter( $all_banners, array( 'resolution' => '772x250' ) ),
     468                                function( $info ) {
     469                                        return (bool) stristr( $info['filename'], '-rtl' );
     470                                }
     471                        );
     472
     473                        $banner_2x_rtl = array_filter(
     474                                wp_list_filter( $all_banners, array( 'resolution' => '1544x500' ) ),
     475                                function( $info ) {
     476                                        return (bool) stristr( $info['filename'], '-rtl' );
     477                                }
     478                        );
     479
     480                        $banner_rtl    = $banner_rtl    ? array_shift( $banner_rtl )    : false;
     481                        $banner_2x_rtl = $banner_2x_rtl ? array_shift( $banner_2x_rtl ) : false;
     482                }
     483
     484                // Resolve the URLs.
     485                $banner        = $banner        ? self::get_asset_url( $plugin, $banner )        : false;
     486                $banner_2x     = $banner_2x     ? self::get_asset_url( $plugin, $banner_2x )     : false;
     487                $banner_rtl    = $banner_rtl    ? self::get_asset_url( $plugin, $banner_rtl )    : false;
     488                $banner_2x_rtl = $banner_2x_rtl ? self::get_asset_url( $plugin, $banner_2x_rtl ) : false;
    498489
    499490                switch ( $output ) {
    … …  
    516507                                return compact( 'banner', 'banner_2x', 'banner_rtl', 'banner_2x_rtl' );
    517508                }
     509        }
     510
     511        /**
     512         * Retrieve the Plugin asset that matches the requested resolution, locale, and RTL.
     513         *
     514         * @static
     515         *
     516         * @param \WP_Post $plugin     The plugin.
     517         * @param array    $assets     The assets.
     518         * @param string   $resolution The resolution.
     519         * @param string   $locale     The locale.
     520         * @return string|false
     521         */
     522        public static function find_best_asset( $plugin, $assets, $resolution, $locale ) {
     523                // Asset matches resolution.
     524                $assets = wp_list_filter( $assets, [ 'resolution' => $resolution ] );
     525
     526                /*
     527                 * Filter the matching assets by locale.
     528                 * NOTE: en_US/'' must also go through this branch, to remove localised assets from the list.
     529                 * This also handles plugins which have specific english assets.
     530                 */
     531                if ( count( $assets ) > 1 ) {
     532                        // Locales, match [ `de_DE_formal`, `de_DE`, `de` ], prioritising the full locale before falling back to the partial match.
     533                        $locale_parts = explode( '_', $locale );
     534                        foreach ( range( count( $locale_parts ), 1 ) as $length ) {
     535                                $locale       = implode( '_', array_slice( $locale_parts, 0, $length ) );
     536                                $locale_asset = wp_list_filter( $assets, [ 'locale' => $locale ] );
     537                                if ( $locale_asset ) {
     538                                        break;
     539                                }
     540                        }
     541                        if ( ! $locale_asset ) {
     542                                // No locale match, filter to no-locale only.
     543                                $locale_asset = wp_list_filter( $assets, [ 'locale' => '' ] );
     544                        }
     545
     546                        $assets = $locale_asset ?: $assets;
     547                }
     548
     549                // Fetch RTL asset, if needed. This is only needed if there isn't a locale match.
     550                if ( count( $assets ) > 1 ) {
     551                        $direction_assets = array_filter(
     552                                $assets,
     553                                function( $info ) {
     554                                        // If we're on a RTL locale, we filter to RTL items else we remove them.
     555                                        $is_rtl_image = (bool) stristr( $info['filename'], '-rtl' );
     556
     557                                        if ( is_rtl() ) {
     558                                                return $is_rtl_image;
     559                                        } else {
     560                                                return ! $is_rtl_image;
     561                                        }
     562                                }
     563                        );
     564                        $assets           = $direction_assets ?: $assets;
     565                }
     566
     567                if ( ! $assets ) {
     568                        return false;
     569                }
     570
     571                return array_shift( $assets );
    518572        }
    519573
    … …  
    10211075        public static function get_screenshots( $plugin = null, $locale = null ) {
    10221076                $plugin = get_post( $plugin );
    1023 
    1024                 if ( ! $locale ) {
    1025                         $locale = get_locale();
    1026                 }
     1077                $locale = $locale ?: get_locale();
    10271078
    10281079                // All indexed from 1. The Image 'number' is stored in the 'resolution' key
    1029                 $screen_shots = get_post_meta( $plugin->ID, 'assets_screenshots', true ) ?: array();
    1030                 $descriptions = get_post_meta( $plugin->ID, 'screenshots', true ) ?: array();
    1031 
    1032                 if ( empty( $screen_shots ) ) {
    1033                         return array();
    1034                 }
    1035 
    1036                 $sorted = array();
    1037                 foreach ( $screen_shots as $image ) {
    1038                         if ( ! isset( $sorted[ $image['resolution'] ] ) ) {
    1039                                 $sorted[ $image['resolution'] ] = array();
     1080                $all_screenshots = get_post_meta( $plugin->ID, 'assets_screenshots', true ) ?: [];
     1081                $descriptions    = get_post_meta( $plugin->ID, 'screenshots', true )        ?: [];
     1082
     1083                if ( empty( $all_screenshots ) ) {
     1084                        return [];
     1085                }
     1086
     1087                $screenshot_nums = array_unique( wp_list_pluck( $all_screenshots, 'resolution') );
     1088                sort( $screenshot_nums, SORT_NATURAL );
     1089
     1090                foreach ( $screenshot_nums as $screenshot_num ) {
     1091                        $caption = $descriptions[ (int) $screenshot_num ] ?? '';
     1092
     1093                        $caption = Plugin_I18n::instance()->translate(
     1094                                'screenshot-' . $screenshot_num,
     1095                                $caption,
     1096                                [ 'post_id' => $plugin->ID ]
     1097                        );
     1098
     1099                        $asset = self::find_best_asset( $plugin, $all_screenshots, $screenshot_num, $locale );
     1100                        if ( ! $asset ) {
     1101                                continue;
    10401102                        }
    10411103
    1042                         if ( empty( $image['locale'] ) ) {
    1043                                 // if the image has no locale, always insert to the last element (lowerst priority).
    1044                                 $sorted[ $image['resolution'] ][] = $image;
    1045                         } elseif ( $locale === $image['locale'] ) {
    1046                                 // if the locale is a full match, always insert to the first element (highest priority).
    1047                                 array_unshift( $sorted[ $image['resolution'] ], $image );
    1048                         } else {
    1049                                 // TODO: de_DE_informal should probably fall back to de_DE before de_CH. Maybe this can wait until Core properly supports locale hierarchy.
    1050 
    1051                                 $image_locale_parts = explode( '_', $image['locale'] );
    1052                                 $locale_parts       = explode( '_', $locale );
    1053                                 // if only the language matches.
    1054                                 if ( $image_locale_parts[0] === $locale_parts[0] ) {
    1055                                         // image with locale has a higher priority than image without locale.
    1056                                         $last_image = end( $sorted[ $image['resolution'] ] );
    1057                                         if ( empty( $last_image['locale'] ) ) {
    1058                                                 array_splice( $sorted[ $image['resolution'] ], count( $sorted[ $image['resolution'] ] ), 0, array( $image ) );
    1059                                         } else {
    1060                                                 $sorted[ $image['resolution'] ][] = $image;
    1061                                         }
    1062                                 }
    1063                         }
    1064                 }
    1065 
    1066                 // Sort
    1067                 ksort( $sorted, SORT_NATURAL );
    1068 
    1069                 // Reduce images to singulars and attach metadata
    1070                 foreach ( $sorted as $index => $items ) {
    1071                         // The highest priority image is the first.
    1072                         $image = $items[0];
    1073 
    1074                         // Attach caption data
    1075                         $image['caption'] = false;
    1076                         if ( isset( $descriptions[ (int) $index ] ) ) {
    1077                                 $image['caption'] = $descriptions[ (int) $index ];
    1078                                 $image['caption'] = Plugin_I18n::instance()->translate(
    1079                                         'screenshot-' . $image['resolution'],
    1080                                         $image['caption'],
    1081                                         [ 'post_id' => $plugin->ID ]
    1082                                 );
    1083                         }
    1084 
    1085                         // Attach URL information for the asset
    1086                         $image['src'] = Template::get_asset_url( $plugin, $image );
    1087 
    1088                         $sorted[ $index ] = $image;
     1104                        $asset['caption'] = $caption;
     1105                        $asset['src']     = self::get_asset_url( $plugin, $asset );
     1106
     1107                        $sorted[ $screenshot_num ] = $asset;
    10891108                }
    10901109
Note: See TracChangeset for help on using the changeset viewer.