WordPress.org

Make WordPress Core

Changeset 20152


Ignore:
Timestamp:
03/08/12 07:46:39 (3 years ago)
Author:
nacin
Message:

In wp_get_themes() under multisite, filter out allowed themes before creating WP_Theme objects, rather than after. search_theme_directories() provides us the stylesheet of each theme which is all we need to do the proper intersection. This results in major performance gains on sites that have large numbers of themes unavailable to them. see #20103.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/theme.php

    r20118 r20152  
    2626    $args = wp_parse_args( $args, $defaults ); 
    2727 
    28     static $_themes; 
    29     if ( ! isset( $_themes ) ) { 
    30         $_themes = array(); 
    31         $theme_data = search_theme_directories(); 
    32         // Make sure the current theme wins out, in case search_theme_directories() picks the wrong 
    33         // one in the case of a conflict. (Normally, last registered theme root wins.) 
    34         $current_theme = get_stylesheet(); 
    35         $current_theme_root = get_raw_theme_root( $current_theme ); 
    36         if ( ! in_array( $current_theme_root, $wp_theme_directories ) ) 
    37             $current_theme_root = WP_CONTENT_DIR . $current_theme_root; 
    38         foreach ( (array) $theme_data as $theme_slug => $data ) { 
    39             if ( $current_theme == $theme_slug && $current_theme_root != $data['theme_root'] ) 
    40                 $_themes[ $theme_slug ] = new WP_Theme( $theme_slug, $current_theme_root ); 
    41             else 
    42                 $_themes[ $theme_slug ] = new WP_Theme( $theme_slug, $data['theme_root'] ); 
     28    static $_theme_directories, $_themes = array(); 
     29    if ( ! isset( $_theme_directories ) ) { 
     30        $_theme_directories = search_theme_directories(); 
     31        if ( count( $wp_theme_directories ) > 1 ) { 
     32            // Make sure the current theme wins out, in case search_theme_directories() picks the wrong 
     33            // one in the case of a conflict. (Normally, last registered theme root wins.) 
     34            $current_theme = get_stylesheet(); 
     35            $root_of_current_theme = get_raw_theme_root( $current_theme ); 
     36            if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) ) 
     37                $root_of_current_theme = WP_CONTENT_DIR . $current_theme_root; 
     38            $_theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme; 
    4339        } 
    4440    } 
    4541 
    46     $themes = $_themes; 
    47     if ( empty( $themes ) ) 
    48         return $themes; 
     42    if ( empty( $_theme_directories ) ) 
     43        return array(); 
     44 
     45    $theme_directories = $_theme_directories; 
     46 
     47    if ( is_multisite() && null !== $args['allowed'] ) { 
     48        $allowed = $args['allowed']; 
     49        if ( 'network' === $allowed ) 
     50            $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network( $args['blog_id'] ) ); 
     51        elseif ( 'site' === $allowed ) 
     52            $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) ); 
     53        elseif ( $allowed ) 
     54            $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); 
     55        else 
     56            $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); 
     57    } 
     58 
     59    $themes = array(); 
     60 
     61    foreach ( $theme_directories as $theme => $theme_root ) { 
     62        if ( isset( $_themes[ $theme ] ) ) 
     63            $themes[ $theme ] = $_themes[ $theme ]; 
     64        else 
     65            $themes[ $theme ] = $_themes[ $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] ); 
     66    } 
    4967 
    5068    if ( null !== $args['errors'] ) { 
    51         foreach ( $themes as $theme_slug => $theme ) { 
    52             if ( $theme->errors() != $args['errors'] ) 
    53                 unset( $themes[ $theme_slug ] ); 
    54         } 
    55     } 
    56  
    57     if ( is_multisite() && null !== $args['allowed'] ) { 
    58         if ( $allowed = $args['allowed'] ) { 
    59             if ( 'network' === $allowed ) 
    60                 $themes = array_intersect_key( $themes, WP_Theme::get_allowed_on_network( $args['blog_id'] ) ); 
    61             elseif ( 'site' === $allowed ) 
    62                 $themes = array_intersect_key( $themes, WP_Theme::get_allowed_on_site( $args['blog_id'] ) ); 
    63             else 
    64                 $themes = array_intersect_key( $themes, WP_Theme::get_allowed( $args['blog_id'] ) ); 
    65         } else { 
    66             $themes = array_diff_key( $themes, WP_Theme::get_allowed( $args['blog_id'] ) ); 
     69        foreach ( $themes as $theme => $wp_theme ) { 
     70            if ( $wp_theme->errors() != $args['errors'] ) 
     71                unset( $themes[ $theme ] ); 
    6772        } 
    6873    } 
Note: See TracChangeset for help on using the changeset viewer.