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/class-wp-ms-themes-list-table.php

    r20022 r20029  
    5151
    5252    function prepare_items() {
    53         global $status, $themes, $totals, $page, $orderby, $order, $s;
     53        global $status, $totals, $page, $orderby, $order, $s;
    5454
    5555        wp_reset_vars( array( 'orderby', 'order', 's' ) );
    5656
    5757        $themes = array(
    58             'all' => apply_filters( 'all_themes', get_themes() ),
     58            'all' => apply_filters( 'all_themes', wp_get_themes() ),
    5959            'search' => array(),
    6060            'enabled' => array(),
     
    6363        );
    6464
    65         $site_allowed_themes = get_site_allowed_themes();
    66         if ( !$this->is_site_themes ) {
    67             $allowed_themes = $site_allowed_themes;
     65        if ( $this->is_site_themes ) {
     66            $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
     67            $allowed_where = 'site';
     68        } else {
    6869            $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
    69         } else {
    70             $allowed_themes = wpmu_get_blog_allowedthemes( $this->site_id );
    71             $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
    72         }
    73 
    74         $current = get_site_transient( 'update_themes' );
     70            $allowed_where = 'network';
     71        }
     72
     73        $current = current_user_can( 'update_themes' ) && ! $this->is_site_themes && get_site_transient( 'update_themes' );
    7574
    7675        foreach ( (array) $themes['all'] as $key => $theme ) {
    77             $theme_key = $theme['Stylesheet'];
    78 
    79             if ( isset( $allowed_themes [ $theme_key ] ) )  {
    80                 $themes['all'][$key]['enabled'] = true;
    81                 $themes['enabled'][$key] = $themes['all'][$key];
    82             }
    83             else {
    84                 $themes['all'][$key]['enabled'] = false;
    85                 $themes['disabled'][$key] = $themes['all'][$key];
    86             }
    87             if ( isset( $current->response[ $theme['Template'] ] ) )
    88                 $themes['upgrade'][$key] = $themes['all'][$key];
    89 
    90             if ( $this->is_site_themes && isset( $site_allowed_themes[$theme_key] ) ) {
    91                 unset( $themes['all'][$key] );
    92                 unset( $themes['enabled'][$key] );
    93                 unset( $themes['disabled'][$key] );
    94             }
    95         }
    96 
    97         if ( !current_user_can( 'update_themes' ) || $this->is_site_themes )
    98             $themes['upgrade'] = array();
     76            if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
     77                unset( $themes['all'][ $key ] );
     78                continue;
     79            }
     80
     81            $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
     82            $themes[ $filter ][ $key ] = $themes['all'][ $key ];
     83
     84            if ( $current && isset( $current->response[ $key ] ) )
     85                $themes['upgrade'][ $key ] = $themes['all'][ $key ];
     86        }
    9987
    10088        if ( $s ) {
     
    11199
    112100        $this->items = $themes[ $status ];
     101        WP_Theme::sort_by_name( $this->items );
     102
     103        $this->has_items = ! empty( $themes['all'] );
    113104        $total_this_page = $totals[ $status ];
    114105
     
    117108            $order = strtoupper( $order );
    118109
    119             uasort( $this->items, array( &$this, '_order_callback' ) );
     110            if ( $orderby == 'Name' ) {
     111                if ( 'ASC' == $order )
     112                    $this->items = array_reverse( $this->items );
     113            } else {
     114                uasort( $this->items, array( &$this, '_order_callback' ) );
     115            }
    120116        }
    121117
     
    123119
    124120        if ( $total_this_page > $themes_per_page )
    125             $this->items = array_slice( $this->items, $start, $themes_per_page );
     121            $this->items = array_slice( $this->items, $start, $themes_per_page, true );
    126122
    127123        $this->set_pagination_args( array(
     
    136132            $term = stripslashes( $_REQUEST['s'] );
    137133
    138         $search_fields = array( 'Name', 'Title', 'Description', 'Author', 'Author Name', 'Author URI', 'Template', 'Stylesheet' );
    139         foreach ( $search_fields as $field )
    140             if ( stripos( $theme[ $field ], $term ) !== false )
     134        foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
     135            // Don't mark up; Do translate.
     136            if ( false !== stripos( $theme->display( $field, false, true ), $term ) )
    141137                return true;
     138        }
     139
     140        if ( false !== stripos( $theme->get_stylesheet(), $term ) )
     141            return true;
     142
     143        if ( false !== stripos( $theme->get_template(), $term ) )
     144            return true;
    142145
    143146        return false;
    144147    }
    145148
     149    // Not used by any core columns.
    146150    function _order_callback( $theme_a, $theme_b ) {
    147151        global $orderby, $order;
    148152
    149         $a = $theme_a[$orderby];
    150         $b = $theme_b[$orderby];
     153        $a = $theme_a[ $orderby ];
     154        $b = $theme_b[ $orderby ];
    151155
    152156        if ( $a == $b )
     
    160164
    161165    function no_items() {
    162         global $themes;
    163 
    164         if ( !empty( $themes['all'] ) )
     166        if ( ! $$this->has_items )
    165167            _e( 'No themes found.' );
    166168        else
     
    260262        $context = $status;
    261263
    262         if ( $this->is_site_themes )
     264        if ( $this->is_site_themes ) {
    263265            $url = "site-themes.php?id={$this->site_id}&";
    264         else
     266            $allowed = $theme->is_allowed( 'site', $this->site_id );
     267        } else {
    265268            $url = 'themes.php?';
     269            $allowed = $theme->is_allowed( 'network' );
     270        }
    266271
    267272        // preorder
     
    273278        );
    274279
    275         $theme_key = $theme['Stylesheet'];
    276 
    277         if ( empty( $theme['enabled'] ) )
     280        $theme_key = $theme->get_stylesheet();
     281
     282        if ( ! $allowed )
    278283            $actions['enable'] = '<a href="' . esc_url( wp_nonce_url($url . 'action=enable&amp;theme=' . $theme_key . '&amp;paged=' . $page . '&amp;s=' . $s, 'enable-theme_' . $theme_key) ) . '" title="' . esc_attr__('Enable this theme') . '" class="edit">' . ( $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' ) ) . '</a>';
    279284        else
     
    281286
    282287        if ( current_user_can('edit_themes') )
    283             $actions['edit'] = '<a href="' . esc_url('theme-editor.php?theme=' . urlencode( $theme['Name'] )) . '" title="' . esc_attr__('Open this theme in the Theme Editor') . '" class="edit">' . __('Edit') . '</a>';
    284 
    285         if ( empty( $theme['enabled'] ) && current_user_can( 'delete_themes' ) && ! $this->is_site_themes && $theme_key != get_option( 'stylesheet' ) && $theme_key != get_option( 'template' ) )
     288            $actions['edit'] = '<a href="' . esc_url('theme-editor.php?theme=' .  $theme_key ) . '" title="' . esc_attr__('Open this theme in the Theme Editor') . '" class="edit">' . __('Edit') . '</a>';
     289
     290        if ( ! $allowed && current_user_can( 'delete_themes' ) && ! $this->is_site_themes && $theme_key != get_option( 'stylesheet' ) && $theme_key != get_option( 'template' ) )
    286291            $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( 'themes.php?action=delete-selected&amp;checked[]=' . $theme_key . '&amp;theme_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bulk-themes' ) ) . '" title="' . esc_attr__( 'Delete this theme' ) . '" class="delete">' . __( 'Delete' ) . '</a>';
    287292
     
    289294        $actions = apply_filters( "theme_action_links_$theme_key", $actions, $theme_key, $theme, $context );
    290295
    291         $class = empty( $theme['enabled'] ) ? 'inactive' : 'active';
    292         $checkbox_id = "checkbox_" . md5($theme['Name']);
    293         $checkbox = "<input type='checkbox' name='checked[]' value='" . esc_attr( $theme_key ) . "' id='" . $checkbox_id . "' /><label class='screen-reader-text' for='" . $checkbox_id . "' >" . __('Select') . " " . $theme['Name'] . "</label>";
    294 
    295         $description = '<p>' . $theme['Description'] . '</p>';
    296         $theme_name = $theme['Name'];
    297 
    298         $id = sanitize_title( $theme_name );
     296        $class = ! $allowed ? 'inactive' : 'active';
     297        $checkbox_id = "checkbox_" . md5( $theme->get('Name') );
     298        $checkbox = "<input type='checkbox' name='checked[]' value='" . esc_attr( $theme_key ) . "' id='" . $checkbox_id . "' /><label class='screen-reader-text' for='" . $checkbox_id . "' >" . __('Select') . " " . $theme->display('Name') . "</label>";
     299
     300        $description = '<p>' . $theme->display( 'Description' ) . '</p>';
     301
     302        $id = sanitize_html_class( $theme->get_stylesheet() );
    299303
    300304        echo "<tr id='$id' class='$class'>";
     
    312316                    break;
    313317                case 'name':
    314                     echo "<td class='theme-title'$style><strong>$theme_name</strong>";
     318                    echo "<td class='theme-title'$style><strong>" . $theme->display('Name') . "</strong>";
    315319                    echo $this->row_actions( $actions, true );
    316320                    echo "</td>";
     
    318322                case 'description':
    319323                    echo "<td class='column-description desc'$style>
    320                         <div class='theme-description'>$description</div>
     324                        <div class='theme-description'>" . $theme->display( 'Description' ) . "</div>
    321325                        <div class='$class second theme-version-author-uri'>";
    322326
    323327                    $theme_meta = array();
    324328
    325                     if ( !empty( $theme['Version'] ) )
    326                         $theme_meta[] = sprintf( __( 'Version %s' ), $theme['Version'] );
    327 
    328                     if ( !empty( $theme['Author'] ) )
    329                         $theme_meta[] = sprintf( __( 'By %s' ), $theme['Author'] );
    330 
    331                     if ( !empty( $theme['Theme URI'] ) )
    332                         $theme_meta[] = '<a href="' . $theme['Theme URI'] . '" title="' . esc_attr__( 'Visit theme homepage' ) . '">' . __( 'Visit Theme Site' ) . '</a>';
     329                    if ( $theme->get('Version') )
     330                        $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display('Version') );
     331
     332                    if ( $theme->get('Author') )
     333                        $theme_meta[] = sprintf( __( 'By %s' ), $theme->display('Author') );
     334
     335                    if ( $theme->get('ThemeURI') )
     336                        $theme_meta[] = '<a href="' . $theme->display('ThemeURI') . '" title="' . esc_attr__( 'Visit theme homepage' ) . '">' . __( 'Visit Theme Site' ) . '</a>';
    333337
    334338                    $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $theme_key, $theme, $status );
Note: See TracChangeset for help on using the changeset viewer.