Making WordPress.org

Changeset 2755


Ignore:
Timestamp:
03/17/2016 05:39:02 AM (10 years ago)
Author:
dd32
Message:

Themes Directory: Open-source the pre_get_posts logic used by the Themes Directory & add logic to handle the domain not matching the home_url.

This allows us to remove the 'placeholder' sites we use for the themes directory on internationalized sites (xx.wordpress.org/themes), and makes the query logic more transparent.

See #1630 (which this is the first part for)
See #1618 (Some work will still be needed to correctly trim the description)

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
1 added
7 edited

Legend:

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

    r2738 r2755  
    11<?php
    22/*
    3 Plugin Name: Theme Repository
    4 Plugin URI:
    5 Description: Transforms a WordPress site in The Official Theme Directory.
    6 Version: 0.1
    7 Author: wordpressdotorg
    8 Author URI: http://wordpress.org/
    9 Text Domain: wporg-themes
    10 License: GPLv2
    11 License URI: http://opensource.org/licenses/gpl-2.0.php
    12 */
     3 * Plugin Name: Theme Repository
     4 * Plugin URI: https://wordpress.org/themes/
     5 * Description: Transforms a WordPress site in The Official Theme Directory.
     6 * Version: 1.0
     7 * Author: wordpressdotorg
     8 * Author URI: http://wordpress.org/
     9 * Text Domain: wporg-themes
     10 * License: GPLv2
     11 * License URI: http://opensource.org/licenses/gpl-2.0.php
     12 */
    1313
    1414// Load base repo package.
    15 include_once plugin_dir_path( __FILE__ ) . 'class-repo-package.php';
     15include __DIR__ . '/class-repo-package.php';
    1616
    1717// Load theme repo package.
    18 include_once plugin_dir_path( __FILE__ ) . 'class-wporg-themes-repo-package.php';
     18include __DIR__ . '/class-wporg-themes-repo-package.php';
    1919
    2020// Load uploader.
    21 include_once plugin_dir_path( __FILE__ ) . 'upload.php';
     21include __DIR__ . '/upload.php';
    2222
    2323// Load Themes API adjustments.
    24 include_once plugin_dir_path( __FILE__ ) . 'themes-api.php';
     24include __DIR__ . '/themes-api.php';
    2525
    2626// Load adjustments to the edit.php screen for repopackage posts.
    27 include_once plugin_dir_path( __FILE__ ) . 'admin-edit.php';
     27include __DIR__ . '/admin-edit.php';
     28
     29// Load the query modifications needed for the directory.
     30include __DIR__ . '/query-modifications.php';
    2831
    2932/**
     
    148151
    149152        // Add the browse/* views
    150         add_rewrite_tag( '%browse%', '(featured|popular|new|favorites)' );
     153        add_rewrite_tag( '%browse%', '(featured|popular|new|updated|favorites)' );
    151154        add_permastruct( 'browse', 'browse/%browse%' );
     155        add_rewrite_tag( '%favorites_user%', '([^/]+)' );
     156        //add_permastruct( 'favorites_user', 'browse/favorites/%favorites_user%' ); // TODO: Implment in JS before enabling
    152157
    153158        if ( ! defined( 'WPORG_THEME_DIRECTORY_BLOGID' ) ) {
     
    744749}
    745750
     751function wporg_themes_theme_information( $slug ) {
     752        return wporg_themes_query_api( 'theme_information', array(
     753                'slug' => $slug,
     754                'fields' => array(
     755                        'description' => true,
     756                        'sections' => false,
     757                        'tested' => true,
     758                        'requires' => true,
     759                        'downloaded' => false,
     760                        'downloadlink' => true,
     761                        'last_updated' => true,
     762                        'homepage' => true,
     763                        'theme_url' => true,
     764                        'parent' => true,
     765                        'tags' => true,
     766                        'rating' => true,
     767                        'ratings' => true,
     768                        'num_ratings' => true,
     769                        'extended_author' => true,
     770                        'photon_screenshots' => true,
     771                        'active_installs' => true,
     772                )
     773        ) );
     774}
     775
    746776/**
    747777 * Makes a query against api.wordpress.org/themes/info/1.0/ without making a HTTP call
     
    751781        include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
    752782
    753         switch_to_blog( WPORG_THEME_DIRECTORY_BLOGID );
    754783        $api = new Themes_API( $method, $args );
    755         restore_current_blog();
    756784
    757785        return $api->response;
     
    966994
    967995/**
    968  * Correct the post type for theme queries to be "repopackage". This fixes the post type for embeds.
    969  */
    970 function wporg_themes_adjust_main_query( $query ) {
    971         if ( $query->is_main_query() && $query->get( 'name' ) && ! $query->is_404() ) {
    972                 $query->query_vars['post_type'] = 'repopackage';
    973         }
    974 }
    975 add_action( 'pre_get_posts', 'wporg_themes_adjust_main_query');
    976 
    977 
     996 * Filter the URLs to use the current localized domain name, rather than WordPress.org.
     997 *
     998 * The Theme Directory is available at multiple URLs (internationalised domains), this method allows
     999 * for the one blog (a single blog_id) to be presented at multiple URLs yet have correct localised links.
     1000 *
     1001 * This method works in conjunction with a filter in sunrise.php, duplicated here for transparency:
     1002 *
     1003 * // Make the Plugin Directory available at /plugins/ on all rosetta sites.
     1004 * function wporg_themes_on_rosetta_domains( $site, $domain, $path, $segments ) {
     1005 *     // All non-rosetta networks define DOMAIN_CURRENT_SITE in wp-config.php
     1006 *     if ( ! defined( 'DOMAIN_CURRENT_SITE' ) && 'wordpress.org' != $domain && '/themes/' == substr( $path . '/', 0, 8 ) ) {
     1007 *          $site = get_blog_details( WPORG_THEME_DIRECTORY_BLOGID );
     1008 *          if ( $site ) {
     1009 *              $site = clone $site;
     1010 *              // 6 = The Rosetta network, this causes the site to be loaded as part of the Rosetta network
     1011 *              $site->site_id = 6;
     1012 *              return $site;
     1013 *          }
     1014 *     }
     1015 *
     1016 *     return $site;
     1017 * }
     1018 * add_filter( 'pre_get_site_by_path', 'wporg_themes_on_rosetta_domains', 10, 4 );
     1019 *
     1020 * @param string $url The URL to be localized.
     1021 * @return string
     1022 */
     1023function wporg_themes_rosetta_network_localize_url( $url ) {
     1024        static $localized_url = null;
     1025
     1026        if ( get_current_blog_id() != WPORG_THEME_DIRECTORY_BLOGID ) {
     1027                return $url;
     1028        }
     1029
     1030        if ( is_null( $localized_url ) ) {
     1031                $localized_url = 'https://' . preg_replace( '![^a-z.-]+!', '', $_SERVER['HTTP_HOST'] );
     1032        }
     1033
     1034        return preg_replace( '!^[https]+://wordpress\.org!i', $localized_url, $url );
     1035}
     1036if ( 'wordpress.org' != $_SERVER['HTTP_HOST'] && defined( 'WPORG_THEME_DIRECTORY_BLOGID' ) ) {
     1037        add_filter( 'option_home',    'wporg_themes_rosetta_network_localize_url' );
     1038        add_filter( 'option_siteurl', 'wporg_themes_rosetta_network_localize_url' );
     1039}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/upload.php

    r1572 r2755  
    8282        }
    8383
    84         switch_to_blog( WPORG_THEME_DIRECTORY_BLOGID );
    85 
    8684        $upload = new WPORG_Themes_Upload;
    8785        $message = $upload->process_upload();
    8886
    89         restore_current_blog();
    90 
    9187        return $message;
    9288}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/embed.php

    r2190 r2755  
    2929if ( have_posts() ) :
    3030        while ( have_posts() ) : the_post();
    31 
    32         // setup the theme variable
    33 
    34         $themes = wporg_themes_get_themes_for_query();
    35         $theme = $themes['themes'][0];
    36 
    37         // note, $theme contains things like active installs and other data to be added eventually
     31                // setup the theme variable
     32                // note, $theme contains things like active installs and other data to be added eventually
     33                $theme = wporg_themes_theme_information( $post->post_name );
    3834
    3935        ?>
     
    4743                        <p class="wp-embed-heading">
    4844                                <a href="<?php the_permalink(); ?>" target="_top">
    49                                         <?php the_title(); ?>
     45                                        <?php echo esc_html( $theme->name ); ?>
    5046                                </a>
    5147                        </p>
    5248
    53                         <div class="wp-embed-excerpt"><?php the_excerpt_embed(); ?></div>
     49                        <div class="wp-embed-excerpt"><?php echo wp_trim_words( $theme->description, 55 ); ?></div>
    5450
    5551                        <?php
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/functions.php

    r2681 r2755  
    127127}
    128128add_filter( 'body_class', 'wporg_themes_body_class' );
    129 
    130 /**
    131  * Prevent 404 responses when we've got a theme via the API.
    132  */
    133 function wporg_themes_prevent_404() {
    134         global $wp_query;
    135         if ( ! is_404() ) {
    136                 return;
    137         }
    138         $themes = wporg_themes_get_themes_for_query();
    139         if ( $themes['total'] ) {
    140                 $wp_query->is_404 = false;
    141                 status_header( 200 );
    142         }
    143 }
    144 add_filter( 'template_redirect', 'wporg_themes_prevent_404' );
    145129
    146130/**
     
    252236        return $template;
    253237}
    254 add_filter('embed_template', 'wporg_themes_embed_template');
    255 
     238add_filter( 'embed_template', 'wporg_themes_embed_template' );
     239
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/header.php

    r2459 r2755  
    66 */
    77
    8 $GLOBALS['themes']    = wporg_themes_get_themes_for_query();
    98$GLOBALS['pagetitle'] = __( 'Theme Directory &mdash; Free WordPress Themes', 'wporg-themes' );
    109
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/index.php

    r1813 r2755  
    1212 */
    1313
    14 global $themes;
    15 
    1614get_header();
    1715?>
     
    1917                <div class="wp-filter">
    2018                        <div class="filter-count">
    21                                 <span class="count theme-count"><?php echo number_format_i18n( $themes['total'] ); ?></span>
     19                                <span class="count theme-count"><?php echo number_format_i18n( $wp_query->found_posts ); ?></span>
    2220                        </div>
    2321
     
    6765                                <?php
    6866                                if ( get_query_var('name') && !is_404() ) {
    69                                         $theme = reset( $themes['themes'] );
    70                                         include __DIR__ . '/theme-single.php';
     67                                        while ( have_posts() ) {
     68                                                the_post();
     69                                                $theme = wporg_themes_theme_information( $post->post_name );
     70                                                include __DIR__ . '/theme-single.php';
     71                                        }
    7172                                } else {
    72                                         foreach ( $themes['themes'] as $theme ) {
     73                                        while ( have_posts() ) {
     74                                                the_post();
     75                                                $theme = wporg_themes_theme_information( $post->post_name );
    7376                                                include __DIR__ . '/theme.php';
    7477                                        }
    7578
    7679                                        // Add the navigation between pages
    77                                         if ( $themes['pages'] > 1 ) {
    78                                                 echo '<nav class="posts-navigation">';
    79                                                 echo paginate_links( array(
    80                                                         'total' => $themes['pages'],
    81                                                         'mid_size' => 3,
    82                                                 ) );
    83                                                 echo '</nav>';
    84                                         }
     80                                        echo '<nav class="posts-navigation">';
     81                                        echo paginate_links( array(
     82                                                'mid_size' => 3,
     83                                        ) );
     84                                        echo '</nav>';
    8585                                }
    8686                                ?>
    8787                        </div>
    8888
     89                        <?php /* TODO: Don't display this for no-js queries where $wp_query->post_count > 0, but JS needs it too. */ ?>
    8990                        <p class="no-themes"><?php _e( 'No themes found. Try a different search.', 'wporg-themes' ); ?></p>
    9091                </div>
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/rss.php

    r1489 r2755  
    2121        <?php
    2222
    23         $themes = wporg_themes_get_themes_for_query();
    24         foreach ( $themes['themes'] as $theme ) :
     23        while ( have_posts() ) :
     24                the_post();
     25                $theme = wporg_themes_theme_information( $post->post_name );
    2526        ?>
    2627        <item>
     
    3637                <content:encoded><![CDATA[<?php echo esc_html( $theme->description ); ?>]]></content:encoded>
    3738        </item>
    38         <?php endforeach; ?>
     39        <?php endwhile; ?>
    3940</channel>
    4041</rss>
Note: See TracChangeset for help on using the changeset viewer.