Making WordPress.org

Changeset 7045


Ignore:
Timestamp:
04/06/2018 02:41:48 AM (7 years ago)
Author:
dd32
Message:

Plugin Direcyory: Add a route (/plugins/$slug/geopattern-icon/$color) to serve a plugins default geopattern icon.
This will be served via the CDN and implemented in a further commit.

See #3265.

File:
1 edited

Legend:

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

    r6855 r7045  
    4242        add_action( 'template_redirect', array( $this, 'prevent_canonical_for_plugins' ), 9 );
    4343        add_action( 'template_redirect', array( $this, 'custom_redirects' ), 1 );
     44        add_action( 'template_redirect', array( $this, 'geopattern_icon_route' ), 0 );
    4445        add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
    4546        add_filter( 'single_term_title', array( $this, 'filter_single_term_title' ) );
     
    504505        add_rewrite_rule( '^([^/]+)/advanced/?$', 'index.php?name=$matches[1]&plugin_advanced=1', 'top' );
    505506
     507        // Add a rule for generated plugin icons
     508        add_rewrite_rule( '^([^/]+)/geopattern-icon(/([^/]*))?/?$', 'index.php?name=$matches[1]&geopattern_icon=$matches[3]', 'top' );
     509
    506510        // Handle the old plugin tabs URLs.
    507511        add_rewrite_rule( '^([^/]+)/(installation|faq|screenshots|changelog|stats|developers|other_notes)/?$', 'index.php?redirect_plugin=$matches[1]&redirect_plugin_tab=$matches[2]', 'top' );
     
    919923
    920924    /**
    921      * Returns the requested page's content, translated.
     925     * Returns the requested page's title, translated.
    922926     *
    923927     * @param string $title
     
    928932        $post = get_post( $post_id );
    929933
     934        // The $post_id passed may be a Support Forum post ID, which thankfully is much higher than plugins ID's for now.
    930935        if ( $post instanceof \WP_Post ) {
    931936            $title = Plugin_I18n::instance()->translate( 'title', $title, [ 'post_id' => $post ] );
     
    10541059        $vars[] = 'redirect_plugin_tab';
    10551060        $vars[] = 'plugin_advanced';
     1061        $vars[] = 'geopattern_icon';
    10561062
    10571063        return $vars;
     
    11991205        }
    12001206
     1207    }
     1208
     1209    /**
     1210     * Output a SVG Geopattern for a given plugin.
     1211     */
     1212    function geopattern_icon_route() {
     1213        global $wp;
     1214
     1215        if ( ! isset( $wp->query_vars['name'], $wp->query_vars['geopattern_icon'] ) ) {
     1216            return;
     1217        }
     1218
     1219        $slug  = get_query_var( 'name' );
     1220        $color = get_query_var( 'geopattern_icon' );
     1221
     1222        $icon = new Plugin_Geopattern();
     1223        $icon->setString( $slug );
     1224        if ( strlen( $color ) === 6 && strspn( $color, 'abcdef0123456789' ) === 6 ) {
     1225            $icon->setColor( '#' . $color );
     1226        }
     1227
     1228        status_header( 200 );
     1229        header( 'Content-Type: image/svg+xml' );
     1230        echo $icon->toSVG();
     1231        die();
    12011232    }
    12021233
Note: See TracChangeset for help on using the changeset viewer.