Make WordPress Core

Ticket #11215: 11215-code-get_current_theme.patch

File 11215-code-get_current_theme.patch, 2.2 KB (added by hakre, 14 years ago)

code refactored in functions get_current_theme, get_themes and get_theme_roots (bit off-topic, stumbled over it while reviewing the ticket)

  • wp-includes/theme.php

    ### Eclipse Workspace Patch 1.0
    #P wordpress-trunk
     
    263263
    264264        asort( $theme_files );
    265265
    266         $wp_themes = array();
     266        $theme_roots = array();
     267        $wp_themes   = array();
    267268
    268269        foreach ( (array) $theme_files as $theme_file ) {
    269270                $theme_root = $theme_file['theme_root'];
     
    427428
    428429/**
    429430 * Retrieve theme roots.
     431 *
     432 * Theme roots are pathes to theme directories.
    430433 *
    431434 * @since 2.9.0
    432435 *
    433  * @return array Theme roots
     436 * @return array directories of all theme-roots keyed with their stylesheet value
    434437 */
    435438function get_theme_roots() {
    436439        $theme_roots = get_site_transient( 'theme_roots' );
    437440        if ( false === $theme_roots ) {
    438441                get_themes();
    439                 $theme_roots = get_site_transient( 'theme_roots' ); // this is set in get_theme()
     442                $theme_roots = get_site_transient( 'theme_roots' ); // set in get_themes()
    440443        }
    441         return $theme_roots;
     444        return (array) $theme_roots;
    442445}
    443446
    444447/**
     
    470473 * @return string
    471474 */
    472475function get_current_theme() {
     476        // magic number
     477        $default_theme      = 'WordPress Default';
     478
    473479        if ( $theme = get_option('current_theme') )
    474480                return $theme;
    475481
    476482        $themes = get_themes();
    477         $theme_names = array_keys($themes);
    478         $current_template = get_option('template');
    479         $current_stylesheet = get_option('stylesheet');
    480         $current_theme = 'WordPress Default';
     483        if ( ! is_array($themes) )
     484                return $default_theme;
    481485
    482         if ( $themes ) {
    483                 foreach ( (array) $theme_names as $theme_name ) {
    484                         if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
    485                                         $themes[$theme_name]['Template'] == $current_template ) {
    486                                 $current_theme = $themes[$theme_name]['Name'];
    487                                 break;
    488                         }
     486        $theme      = $default_theme;
     487        $template   = get_option('template');
     488        $stylesheet = get_option('stylesheet');
     489
     490        foreach ( $themes as $value ) {
     491                if ( $value['Stylesheet']  == $stylesheet
     492                     && $value['Template'] == $template ) {
     493                        $theme = $value['Name'];
     494                        break;
    489495                }
    490496        }
    491497
    492         update_option('current_theme', $current_theme);
    493 
    494         return $current_theme;
     498        update_option('current_theme', $theme);
     499        return $theme;
    495500}
    496501
    497502/**