Make WordPress Core


Ignore:
Timestamp:
02/28/2012 09:24:44 PM (15 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/class-wp-themes-list-table.php

    r20027 r20029  
    2525
    2626        function prepare_items() {
    27                 global $ct;
    28 
    29                 $ct = current_theme_info();
    30 
    31                 $themes = get_allowed_themes();
     27                $themes = wp_get_themes( array( 'allowed' => true ) );
    3228
    3329                if ( ! empty( $_REQUEST['s'] ) ) {
     
    4642                if ( $this->search || $this->features ) {
    4743                        foreach ( $themes as $key => $theme ) {
    48                                 if ( !$this->search_theme( $theme ) )
     44                                if ( ! $this->search_theme( $theme ) )
    4945                                        unset( $themes[ $key ] );
    5046                        }
    5147                }
    5248
    53                 unset( $themes[$ct->name] );
    54                 uksort( $themes, "strnatcasecmp" );
     49                unset( $themes[ get_option( 'stylesheet' ) ] );
     50                WP_Theme::sort_by_name( $themes );
    5551
    5652                $per_page = 999;
     
    126122        function display_rows() {
    127123                $themes = $this->items;
    128                 $theme_names = array_keys( $themes );
    129                 natcasesort( $theme_names );
    130 
    131         foreach ( $theme_names as $theme_name ) {
    132                 $class = array( 'available-theme' );
    133         ?>
    134         <div class="<?php echo join( ' ', $class ); ?>">
    135         <?php if ( !empty( $theme_name ) ) :
    136         $template = $themes[$theme_name]['Template'];
    137         $stylesheet = $themes[$theme_name]['Stylesheet'];
    138         $title = $themes[$theme_name]['Title'];
    139         $version = $themes[$theme_name]['Version'];
    140         $description = $themes[$theme_name]['Description'];
    141         $author = $themes[$theme_name]['Author'];
    142         $screenshot = $themes[$theme_name]['Screenshot'];
    143         $stylesheet_dir = $themes[$theme_name]['Stylesheet Dir'];
    144         $template_dir = $themes[$theme_name]['Template Dir'];
    145         $parent_theme = $themes[$theme_name]['Parent Theme'];
    146         $theme_root = $themes[$theme_name]['Theme Root'];
    147         $theme_root_uri = $themes[$theme_name]['Theme Root URI'];
    148         $preview_link = esc_url( add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ), home_url( '/' ) ) );
    149         $preview_text = esc_attr( sprintf( __( 'Preview of &#8220;%s&#8221;' ), $title ) );
    150         $tags = $themes[$theme_name]['Tags'];
    151         $thickbox_class = 'thickbox thickbox-preview';
    152         $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $template );
    153         $activate_text = esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) );
    154         $actions = array();
    155         $actions[] = '<a href="' . $activate_link . '" class="activatelink" title="' . $activate_text . '">' . __( 'Activate' ) . '</a>';
    156         $actions[] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $theme_name ) ) . '">' . __( 'Preview' ) . '</a>';
    157         if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
    158                 $actions[] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&amp;template=$stylesheet", 'delete-theme_' . $stylesheet ) . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $theme_name ) ) . "' );" . '">' . __( 'Delete' ) . '</a>';
    159         $actions = apply_filters( 'theme_action_links', $actions, $themes[$theme_name] );
    160 
    161         $actions = implode ( ' | ', $actions );
    162 ?>
    163                 <a href="<?php echo $preview_link; ?>" class="<?php echo $thickbox_class; ?> screenshot">
    164 <?php if ( $screenshot ) : ?>
    165                         <img src="<?php echo $theme_root_uri . '/' . $stylesheet . '/' . $screenshot; ?>" alt="" />
    166 <?php endif; ?>
    167                 </a>
    168 <h3><?php
    169         /* translators: 1: theme title, 2: theme version, 3: theme author */
    170         printf( __( '%1$s %2$s by %3$s' ), $title, $version, $author ) ; ?></h3>
    171 
    172 <span class='action-links'><?php echo $actions ?></span>
    173 <span class="separator hide-if-no-js">| </span><a href="#" class="theme-detail hide-if-no-js" tabindex='4'><?php _e('Details') ?></a>
    174 <div class="themedetaildiv hide-if-js">
    175 <p><?php echo $description; ?></p>
    176         <?php if ( current_user_can( 'edit_themes' ) && $parent_theme ) {
    177         /* translators: 1: theme title, 2:  template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?>
    178         <p><?php printf( __( 'The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.' ), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ), $title, $parent_theme ); ?></p>
    179 <?php } else { ?>
    180         <p><?php printf( __( 'All of this theme&#8217;s files are located in <code>%2$s</code>.' ), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ) ); ?></p>
    181 <?php } ?>
    182 <?php if ( $tags ) : ?>
    183 <p><?php _e( 'Tags:' ); ?> <?php echo join( ', ', $tags ); ?></p>
    184 <?php endif; ?>
    185 <?php endif; // end if not empty theme_name ?>
    186 </div>
    187         <?php theme_update_available( $themes[$theme_name] ); ?>
    188         </div>
    189 <?php } // end foreach $theme_names
     124
     125                foreach ( $themes as $theme ) {
     126                        echo '<div class="available-theme">';
     127
     128                        $template = $theme->get_template();
     129                        $stylesheet = $theme->get_stylesheet();
     130
     131                        $title = $theme->display('Name');
     132                        $version = $theme->display('Version');
     133                        $author = $theme->display('Author');
     134 
     135                        $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $template );
     136                        $preview_link = esc_url( add_query_arg(
     137                                array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ),
     138                                home_url( '/' ) ) );
     139 
     140                        $actions = array();
     141                        $actions[] = '<a href="' . $activate_link . '" class="activatelink" title="'
     142                                . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
     143                        $actions[] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="'
     144                                . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
     145                        if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
     146                                $actions[] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&amp;template=$stylesheet", 'delete-theme_' . $stylesheet )
     147                                        . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $title ) )
     148                                        . "' );" . '">' . __( 'Delete' ) . '</a>';
     149 
     150                        $actions = apply_filters( 'theme_action_links', $actions, $theme );
     151 
     152                        $actions = implode ( ' | ', $actions );
     153                        ?>
     154                        <a href="<?php echo $preview_link; ?>" class="thickbox thickbox-preview screenshot">
     155                        <?php if ( $theme->get_screenshot() ) : ?>
     156                                <img src="<?php echo esc_url( $theme->get_screenshot( 'absolute' ) ); ?>" alt="" />
     157                        <?php endif; ?>
     158                        </a>
     159                        <h3><?php
     160                        /* translators: 1: theme title, 2: theme version, 3: theme author */
     161                        printf( __( '%1$s %2$s by %3$s' ), $title, $version, $author ) ; ?></h3>
     162 
     163                        <span class='action-links'><?php echo $actions ?></span>
     164                        <span class="separator hide-if-no-js">| </span><a href="#" class="theme-detail hide-if-no-js" tabindex='4'><?php _e('Details') ?></a>
     165                        <div class="themedetaildiv hide-if-js">
     166                        <p><?php echo $theme->display('Description'); ?></p>
     167                        <?php if ( current_user_can( 'edit_themes' ) && $theme->parent() ) :
     168                                /* translators: 1: theme title, 2:  template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?>
     169                                <p><?php printf( __( 'The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.' ),
     170                                        $title, str_replace( WP_CONTENT_DIR, '', $theme->get_template_directory() ), str_replace( WP_CONTENT_DIR, '', $theme->get_stylesheet_directory() ), $title, $theme->parent()->display('Name') ); ?></p>
     171                        <?php else :
     172                                        /* translators: 1: theme title, 2:  template dir, 3: stylesheet_dir */ ?>
     173                                <p><?php printf( __( 'All of this theme&#8217;s files are located in <code>%2$s</code>.' ),
     174                                        $title, str_replace( WP_CONTENT_DIR, '', $theme->get_template_directory() ), str_replace( WP_CONTENT_DIR, '', $theme->get_stylesheet_directory() ) ); ?></p>
     175                        <?php endif; ?>
     176                        <?php
     177                        if ( $theme->get('Tags') )
     178                                printf( '<p>' . __( 'Tags: %s.' ) . '</p>', $theme->display('Tags') );
     179                        ?>
     180                        </div>
     181                        <?php theme_update_available( $theme ); ?>
     182                        </div>
     183                <?php
     184                }
    190185        }
    191186
     
    194189                if ( $this->features ) {
    195190                        foreach ( $this->features as $word ) {
    196                                 if ( ! in_array( $word, $theme['Tags'] ) )
     191                                if ( ! in_array( $word, $theme->get('Tags') ) )
    197192                                        return false;
    198193                        }
     
    202197                if ( $this->search ) {
    203198                        foreach ( $this->search as $word ) {
    204                                 if ( in_array( $word, $theme['Tags'] ) )
     199                                if ( in_array( $word, $theme->get('Tags') ) )
    205200                                        continue;
    206                         }
    207 
    208                         foreach ( array( 'Name', 'Title', 'Description', 'Author', 'Template', 'Stylesheet' ) as $header ) {
    209                                 if ( false !== stripos( $theme[ $header ], $word ) )
    210                                         continue 2;
    211                         }
    212 
    213                         return false;
     201
     202                                foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
     203                                        // Don't mark up; Do translate.
     204                                        if ( false !== stripos( $theme->display( $header, false, true ), $word ) )
     205                                                continue 2;
     206                                }
     207
     208                                if ( false !== stripos( $theme->get_stylesheet(), $word ) )
     209                                        continue;
     210
     211                                if ( false !== stripos( $theme->get_template(), $word ) )
     212                                        continue;
     213 
     214                                return false;
     215                        }
    214216                }
    215217
Note: See TracChangeset for help on using the changeset viewer.