Making WordPress.org


Ignore:
Timestamp:
02/23/2016 04:12:32 AM (8 years ago)
Author:
dd32
Message:

Plugins Directory: Implement basic Banner & Icon support (including porting the autotomatically generated icons plugin over) to give plugins a bit of individuality.
See #1584

File:
1 edited

Legend:

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

    r2554 r2555  
    135135        return $sections;
    136136    }
     137
     138    /**
     139     * Retrieve the Plugin Icon details for a plugin.
     140     *
     141     * @param WP_Post|string $plugin An instance of a Plugin post, or the plugin slug.
     142     * @return mixed
     143     */
     144    static function get_plugin_icon( $plugin, $output = 'raw' ) {
     145        global $wporg_plugin_directory;
     146        $plugin = $wporg_plugin_directory->get_or_create_plugin_post( $plugin );
     147        if ( is_wp_error( $plugin ) ) {
     148            return false;
     149        }
     150        $plugin_slug = $plugin->post_name;
     151
     152        $raw_icons = get_post_meta( $plugin->ID, 'assets_icons', true );
     153
     154        $icon = $icon_2x = $vector = $generated = false;
     155        foreach ( $raw_icons as $file => $info ) {
     156            switch ( $info['resolution'] ) {
     157                case '256x256':
     158                    $icon_2x = self::get_asset_url( $plugin_slug, $info );
     159                    break;
     160                case '128x128':
     161                    $icon = self::get_asset_url( $plugin_slug, $info );
     162                    break;
     163                case false && 'icon.svg' == $file:
     164                    $icon = self::get_asset_url( $plugin_slug, $info );
     165                    $vector = true;
     166                    break;
     167            }
     168        }
     169
     170        if ( ! $icon ) {
     171            $generated = true;
     172            if ( ! class_exists( 'WPorg_Plugin_Geopattern' ) ) {
     173                include __DIR__ . '/class-wporg-plugin-geopattern.php';
     174            }
     175            $icon = new WPorg_Plugin_Geopattern;
     176            $icon->setString( $plugin->post_name );
     177
     178            // Use the average color of the first known banner as the icon background color
     179            if ( $color = get_post_meta( $plugin->ID, 'assets_banners_color', true ) ) {
     180                if ( strlen( $color ) == 6 && strspn( $color, 'abcdef0123456789' ) == 6 ) {
     181                    $icon->setColor( '#' . $color );
     182                }
     183            }
     184
     185            $icon = $icon->toDataURI();
     186        }
     187
     188        switch ( $output ) {
     189            case 'html':
     190                $id = "plugin-icon-{$plugin_slug}";
     191                $html = "<style type='text/css'>";
     192                $html .= "#{$id} { width:128px; height:128px; background-image: url('{$icon}'); background-size:128px 128px; }";
     193                if ( ! empty( $icon_2x ) && ! $generated ) {
     194                    $html .= "@media only screen and (-webkit-min-device-pixel-ratio: 1.5) { #{$id} { background-image: url('{$icon_2x}'); } }";
     195                }
     196                $html .= "</style>";
     197                $html .= "<div class='plugin-icon' id='{$id}'></div>";
     198
     199                return $html;
     200                break;
     201            case 'raw':
     202            default:
     203                return compact( 'icon', 'icon_2x', 'vector', 'generated' );
     204        }
     205    }
     206
     207    /**
     208     * Retrieve the Plugin Icon details for a plugin.
     209     *
     210     * @param WP_Post|string $plugin An instance of a Plugin post, or the plugin slug.
     211     * @return mixed
     212     */
     213    static function get_plugin_banner( $plugin, $output = 'raw' ) {
     214        global $wporg_plugin_directory;
     215        $plugin = $wporg_plugin_directory->get_or_create_plugin_post( $plugin );
     216        if ( is_wp_error( $plugin ) ) {
     217            return false;
     218        }
     219        $plugin_slug = $plugin->post_name;
     220
     221        $raw_banners = get_post_meta( $plugin->ID, 'assets_banners', true );
     222
     223        $banner = $banner_2x = false;
     224        foreach ( $raw_banners as $file => $info ) {
     225            switch ( $info['resolution'] ) {
     226                case '1544x500':
     227                    $banner_2x = self::get_asset_url( $plugin_slug, $info );
     228                    break;
     229                case '772x250':
     230                    $banner = self::get_asset_url( $plugin_slug, $info );
     231                    break;
     232            }
     233        }
     234
     235        if ( ! $banner ) {
     236            return false;
     237        }
     238
     239        switch ( $output ) {
     240            case 'raw':
     241            default:
     242                return compact( 'banner', 'banner_2x' );
     243        }
     244    }
     245
     246    static function get_asset_url( $plugin, $asset ) {
     247        return esc_url( sprintf(
     248            'https://ps.w.org/%s/assets/%s?rev=%s',
     249            $plugin,
     250            $asset['filename'],
     251            $asset['revision']
     252        ) );
     253    }
    137254}
Note: See TracChangeset for help on using the changeset viewer.