Making WordPress.org


Ignore:
Timestamp:
02/17/2015 11:18:19 PM (12 years ago)
Author:
obenland
Message:

WP.org Themes: Use main query instead of Themes API for server output.

  • Can now deal properly with /browse/*/ URL structure.
  • Adds single theme navigation.
  • Adds Photon support for server output.
  • Brings single theme view closer to melchoyce's mockups.

See #745.

File:
1 edited

Legend:

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

    r1220 r1274  
    2020// Load uploader.
    2121include_once plugin_dir_path( __FILE__ ) . 'upload.php';
     22
     23register_activation_hook( __FILE__, 'flush_rewrite_rules' );
     24register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
    2225
    2326/**
     
    5760                'query_var'           => true,
    5861                'can_export'          => true,
    59                 'rewrite'             => true,
     62                'rewrite'             => array( 'slug' => '/' ),
    6063                'capability_type'     => 'post',
    6164        );
     
    6568                register_post_type( 'repopackage', $args );
    6669        }
     70
     71        add_rewrite_tag( '%browse%', '(featured|popular|new)' );
     72
     73        // Single themes.
     74        add_rewrite_rule( '(.?.+?)(/[0-9]+)?/?$', 'index.php?post_type=repopackage&name=$matches[1]', 'top' );
     75
     76        // Browse views.
     77        add_rewrite_rule( 'browse/(featured|popular|new)/?$', 'index.php?post_type=repopackage&browse=$matches[1]', 'top' );
     78
     79        // Paginated browse views.
     80        add_rewrite_rule( 'browse/(featured|popular|new)/page/?([0-9]{1,})/?$', 'index.php?post_type=repopackage&browse=$matches[1]&paged=$matches[2]', 'top' );
    6781}
    6882add_action( 'init', 'wporg_themes_init' );
     83
     84/**
     85 * Adjusts query to account for custom views.
     86 *
     87 * @param WP_Query $wp_query
     88 * @return WP_Query
     89 */
     90function wporg_themes_set_up_query( $wp_query ) {
     91        if ( is_admin() || in_array( $wp_query->get( 'pagename' ), array( 'upload', 'commercial' ) ) || 'nav_menu_item' == $wp_query->get( 'post_type' ) ) {
     92                return $wp_query;
     93        }
     94
     95        $wp_query->set( 'post_type', 'repopackage' );
     96
     97        if ( $wp_query->is_home() ) {
     98                $wp_query->set( 'browse', 'featured' );
     99        }
     100
     101        if ( $wp_query->get( 'browse' ) ) {
     102                switch ( $wp_query->get( 'browse' ) ) {
     103                        case 'featured':
     104                                $wp_query->set( 'paged', 1 );
     105                                $wp_query->set( 'posts_per_page', 15 );
     106                                $wp_query->set( 'date_query', array(
     107                                        array(
     108                                                'column' => 'post_modified_gmt',
     109                                                'after'  => '-1 year',
     110                                        ),
     111                                ) );
     112                                $wp_query->set( 'orderby', 'rand' );
     113                                break;
     114
     115                        case 'popular':
     116                                //TODO: Sort by popularity.
     117                                break;
     118
     119                        case 'new':
     120                                break;
     121                }
     122        }
     123
     124        return $wp_query;
     125}
     126add_filter( 'pre_get_posts', 'wporg_themes_set_up_query' );
     127
     128/**
     129 * Adjusts the amount of found posts when browsing featured themes.
     130 *
     131 * @param string   $found_posts
     132 * @param WP_Query $wp_query
     133 * @return string
     134 */
     135function wporg_themes_found_posts( $found_posts, $wp_query ) {
     136        if ( $wp_query->is_main_query() && 'featured' === $wp_query->get( 'browse' ) ) {
     137                $found_posts = $wp_query->get( 'posts_per_page' );
     138        }
     139
     140        return $found_posts;
     141}
     142add_filter( 'found_posts', 'wporg_themes_found_posts', 10, 2 );
    69143
    70144/**
     
    169243 * @return string
    170244 */
    171 function wporg_themes_post_thumbnail_html( $html, $post_id ) {
     245function wporg_themes_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size ) {
    172246        $post = get_post( $post_id );
    173247        if ( 'repopackage' == $post->post_type ) {
    174248                $theme = new WPORG_Themes_Repo_Package( $post );
    175                 // no size because we only have one (unknown) image size, so the theme needs to size with css
    176                 $html = '<img src="' . $theme->screenshot_url() . '"/>';
     249                $src   = add_query_arg( array( 'w' => $size, 'strip' => 'all' ), $theme->screen_shot_url() );
     250
     251                $html = '<img src="' . esc_url( $src ) . '"/>';
    177252        }
    178253
    179254        return $html;
    180255}
    181 add_filter( 'post_thumbnail_html', 'wporg_themes_post_thumbnail_html', 10, 2 );
     256add_filter( 'post_thumbnail_html', 'wporg_themes_post_thumbnail_html', 10, 5 );
     257
    182258
    183259/**
Note: See TracChangeset for help on using the changeset viewer.