Make WordPress Core


Ignore:
Timestamp:
02/28/2012 09:24:44 PM (13 years ago)
Author:
nacin
Message:

Introduce WP_Theme, wp_get_themes(), and wp_get_theme() to replace get_themes(), get_theme(), get_theme_data(), current_theme_info(), and others.

  • Getters and Helpers: Introduces a series of methods to allow for easy generation of headers for display, and other theme metadata, including page templates.
  • Screenshots: Handles support for multiple screenshots. (see # Additional screenshots must be PNG and start with screenshot-2.png, and be sequential to be counted. see #19816.
  • Error Handling: Broken themes have a WP_Error object attached to them.
  • Caching: Introduces a wp_cache_themes_persistently filter (also in [20020]) to enable persistent caching of all filesystem and sanitization operations normally handled by WP_Theme (and formerly get_file_data() and get_themes()). Themes are cached individually and across five different cache keys for different data pieces.
  • Compatibility: A WP_Theme object is backwards compatible with a theme's array formerly returned by get_themes() and get_theme(), and an stdClass object formerly returned by current_theme_info().
  • i18n/L10n: Theme headers are now localizable with proper Text Domain and Domain Path headers, like plugins. (Language packs may remove the requirement for headers.) For page templates, see #6007 (not fixed yet, but will be easy now). For headers, fixes #15858.
  • PHP and CSS files: New methods that fetch a list of theme files (for the theme editor) only on demand, rather than only loading them into memory. fixes #11214.

Functions deprecated:

  • get_themes(), get_allowed_themes() and get_broken_themes() -- use wp_get_themes()
  • get_theme() and current_theme_info() -- use wp_get_theme()
  • get_site_allowed_themes() -- use WP_Theme::get_allowed_on_network()
  • wpmu_get_blog_allowedthemes() -- use WP_theme::get_allowed_on_site()

see also [20016], [20018], [20019], [20020], [20021], [20022], [20025], [20026], [20027]. also fixes #19244.

see #20103.

File:
1 edited

Legend:

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

    r20025 r20029  
    66 * @subpackage Administration
    77 */
    8 
    9 /**
    10  * {@internal Missing Short Description}}
    11  *
    12  * @since 2.0.0
    13  *
    14  * @return unknown
    15  */
    16 function current_theme_info() {
    17     $themes = get_themes();
    18     $current_theme = get_current_theme();
    19 
    20     if ( ! $themes ) {
    21         $ct = new stdClass;
    22         $ct->name = $current_theme;
    23         return $ct;
    24     }
    25 
    26     if ( ! isset( $themes[$current_theme] ) ) {
    27         delete_option( 'current_theme' );
    28         $current_theme = get_current_theme();
    29     }
    30 
    31     $ct = new stdClass;
    32     $ct->name = $current_theme;
    33     $ct->title = $themes[$current_theme]['Title'];
    34     $ct->version = $themes[$current_theme]['Version'];
    35     $ct->parent_theme = $themes[$current_theme]['Parent Theme'];
    36     $ct->template_dir = $themes[$current_theme]['Template Dir'];
    37     $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
    38     $ct->template = $themes[$current_theme]['Template'];
    39     $ct->stylesheet = $themes[$current_theme]['Stylesheet'];
    40     $ct->screenshot = $themes[$current_theme]['Screenshot'];
    41     $ct->description = $themes[$current_theme]['Description'];
    42     $ct->author = $themes[$current_theme]['Author'];
    43     $ct->tags = $themes[$current_theme]['Tags'];
    44     $ct->theme_root = $themes[$current_theme]['Theme Root'];
    45     $ct->theme_root_uri = $themes[$current_theme]['Theme Root URI'];
    46     return $ct;
    47 }
    488
    499/**
     
    11575
    11676/**
    117  * {@internal Missing Short Description}}
     77 * Get the Page Templates available in this theme
    11878 *
    11979 * @since 1.5.0
    12080 *
    121  * @return unknown
    122  */
    123 function get_broken_themes() {
    124     global $wp_broken_themes;
    125 
    126     get_themes();
    127     return $wp_broken_themes;
    128 }
    129 
    130 /**
    131  * Get the allowed themes for the current blog.
    132  *
    133  * @since 3.0.0
    134  *
    135  * @uses get_themes()
    136  * @uses current_theme_info()
    137  * @uses get_site_allowed_themes()
    138  * @uses wpmu_get_blog_allowedthemes
    139  *
    140  * @return array $themes Array of allowed themes.
    141  */
    142 function get_allowed_themes() {
    143     if ( !is_multisite() )
    144         return get_themes();
    145 
    146     $themes = get_themes();
    147     $ct = current_theme_info();
    148     $allowed_themes = apply_filters("allowed_themes", get_site_allowed_themes() );
    149     if ( $allowed_themes == false )
    150         $allowed_themes = array();
    151 
    152     $blog_allowed_themes = wpmu_get_blog_allowedthemes();
    153     if ( is_array( $blog_allowed_themes ) )
    154         $allowed_themes = array_merge( $allowed_themes, $blog_allowed_themes );
    155 
    156     if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
    157         $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
    158 
    159     reset( $themes );
    160     foreach ( $themes as $key => $theme ) {
    161         if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
    162             unset( $themes[ $key ] );
    163     }
    164     reset( $themes );
    165 
    166     return $themes;
    167 }
    168 
    169 /**
    170  * Get the Page Templates available in this theme
    171  *
    172  * @since 1.5.0
    173  *
    17481 * @return array Key is the template name, value is the filename of the template
    17582 */
    17683function get_page_templates() {
    177     $themes = get_themes();
    178     $theme = get_current_theme();
    179     $templates = $themes[$theme]['Template Files'];
    180     $page_templates = array();
    181 
    182     if ( is_array( $templates ) ) {
    183         $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
    184 
    185         foreach ( $templates as $template ) {
    186             $basename = str_replace($base, '', $template);
    187 
    188             // don't allow template files in subdirectories
    189             if ( false !== strpos($basename, '/') )
    190                 continue;
    191 
    192             if ( 'functions.php' == $basename )
    193                 continue;
    194 
    195             $template_data = implode( '', file( $template ));
    196 
    197             $name = '';
    198             if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
    199                 $name = _cleanup_header_comment($name[1]);
    200 
    201             if ( !empty( $name ) ) {
    202                 $page_templates[trim( $name )] = $basename;
    203             }
    204         }
    205     }
    206 
    207     return $page_templates;
     84    return wp_get_theme()->get_page_templates();
    20885}
    20986
     
    241118        $themes_update = get_site_transient('update_themes');
    242119
    243     if ( is_object($theme) && isset($theme->stylesheet) )
    244         $stylesheet = $theme->stylesheet;
    245     elseif ( is_array($theme) && isset($theme['Stylesheet']) )
    246         $stylesheet = $theme['Stylesheet'];
    247     else
    248         return false; //No valid info passed.
     120    if ( ! is_a( $theme, 'WP_Theme' ) )
     121        return;
     122
     123    $stylesheet = $theme->get_stylesheet();
    249124
    250125    if ( isset($themes_update->response[ $stylesheet ]) ) {
    251126        $update = $themes_update->response[ $stylesheet ];
    252         $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
     127        $theme_name = $theme->get('Name');
    253128        $details_url = add_query_arg(array('TB_iframe' => 'true', 'width' => 1024, 'height' => 800), $update['url']); //Theme browser inside WP? replace this, Also, theme preview JS will override this on the available list.
    254129        $update_url = wp_nonce_url('update.php?action=upgrade-theme&theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
Note: See TracChangeset for help on using the changeset viewer.