Making WordPress.org


Ignore:
Timestamp:
05/03/2019 07:03:20 AM (6 years ago)
Author:
dd32
Message:

Plugin Directory: Introduce localised screenshots, allowing plugins to have translated screenshots on the website and within the wp-admin plugin installer.

This works by allowing screenshots located in the /assets/ folder to define the locale of the image, eg. screenshot-1-de_DE.png or screenshot-1-ary.png.

Props ck3lee, dd32.
See #3935.

File:
1 edited

Legend:

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

    r8546 r8729  
    365365            'developers',
    366366        );
    367         if ( ! get_post_meta( $plugin->ID, 'screenshots', true ) && ! get_post_meta( $plugin->ID, 'assets_screenshots', true ) ) {
     367        if ( ! get_post_meta( $plugin->ID, 'assets_screenshots', true ) ) {
    368368            unset( $default_sections[ array_search( 'screenshots', $default_sections ) ] );
    369369        }
     
    980980        return $link;
    981981    }
     982
     983    /**
     984     * Fetch plugin Screenshots, accounting for localised screenshots.
     985     *
     986     * @static
     987     *
     988     * @param object|int $plugin The plugin to fetch screenshots for. Optional.
     989     * @param string     $locale The locale requested. Optional.
     990     * @return array Screenshots for the plugin, localised if possible.
     991     */
     992    public static function get_screenshots( $plugin = null, $locale = null ) {
     993        $plugin = get_post( $plugin );
     994
     995        if ( ! $locale ) {
     996            $locale = get_locale();
     997        }
     998
     999        // All indexed from 1. The Image 'number' is stored in the 'resolution' key
     1000        $screen_shots = get_post_meta( $plugin->ID, 'assets_screenshots', true ) ?: array();
     1001        $descriptions = get_post_meta( $plugin->ID, 'screenshots', true ) ?: array();
     1002
     1003        if ( empty( $screen_shots ) ) {
     1004            return array();
     1005        }
     1006
     1007        $sorted = array();
     1008        foreach ( $screen_shots as $image ) {
     1009            if ( ! isset( $sorted[ $image['resolution'] ] ) ) {
     1010                $sorted[ $image['resolution'] ] = array();
     1011            }
     1012
     1013            if ( empty( $image['locale'] ) ) {
     1014                // if the image has no locale, always insert to the last element (lowerst priority).
     1015                $sorted[ $image['resolution'] ][] = $image;
     1016            } elseif ( $locale === $image['locale'] ) {
     1017                // if the locale is a full match, always insert to the first element (highest priority).
     1018                array_unshift( $sorted[ $image['resolution'] ], $image );
     1019            } else {
     1020                // TODO: de_DE_informal should probably fall back to de_DE before de_CH. Maybe this can wait until Core properly supports locale hierarchy.
     1021
     1022                $image_locale_parts = explode( '_', $image['locale'] );
     1023                $locale_parts       = explode( '_', $locale );
     1024                // if only the language matches.
     1025                if ( $image_locale_parts[0] === $locale_parts[0] ) {
     1026                    // image with locale has a higher priority than image without locale.
     1027                    $last_image = end( $sorted[ $image['resolution'] ] );
     1028                    if ( empty( $last_image['locale'] ) ) {
     1029                        array_splice( $sorted[ $image['resolution'] ], count( $sorted[ $image['resolution'] ] ), 0, array( $image ) );
     1030                    } else {
     1031                        $sorted[ $image['resolution'] ][] = $image;
     1032                    }
     1033                }
     1034            }
     1035        }
     1036
     1037        // Sort
     1038        ksort( $sorted, SORT_NATURAL );
     1039
     1040        // Reduce images to singulars and attach metadata
     1041        foreach ( $sorted as $index => $items ) {
     1042            // The highest priority image is the first.
     1043            $image = $items[0];
     1044
     1045            // Attach caption data
     1046            $image['caption'] = false;
     1047            if ( isset( $descriptions[ (int) $index ] ) ) {
     1048                $image['caption'] = $descriptions[ (int) $index ];
     1049                $image['caption'] = Plugin_I18n::instance()->translate(
     1050                    'screenshot-' . $image['resolution'],
     1051                    $image['caption'],
     1052                    [ 'post_id' => $plugin->ID ]
     1053                );
     1054            }
     1055
     1056            // Attach URL information for the asset
     1057            $image['src'] = Template::get_asset_url( $plugin, $image );
     1058
     1059            $sorted[ $index ] = $image;
     1060        }
     1061
     1062        return $sorted;
     1063    }
    9821064}
Note: See TracChangeset for help on using the changeset viewer.