]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-themes-list-table.php
Wordpress 3.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-themes-list-table.php
1 <?php
2 /**
3  * Themes List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_Themes_List_Table extends WP_List_Table {
11
12         var $search = array();
13         var $features = array();
14
15         function ajax_user_can() {
16                 // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
17                 return current_user_can('switch_themes');
18         }
19
20         function prepare_items() {
21                 global $ct;
22
23                 $ct = current_theme_info();
24
25                 $themes = get_allowed_themes();
26
27                 $search = !empty( $_REQUEST['s'] ) ? trim( stripslashes( $_REQUEST['s'] ) ) : '';
28
29                 if ( '' !== $search ) {
30                         $this->search = array_merge( $this->search, array_filter( array_map( 'trim', explode( ',', $search ) ) ) );
31                         $this->search = array_unique( $this->search );
32                 }
33
34                 if ( !empty( $_REQUEST['features'] ) ) {
35                         $this->features = $_REQUEST['features'];
36                         $this->features = array_map( 'trim', $this->features );
37                         $this->features = array_map( 'sanitize_title_with_dashes', $this->features );
38                         $this->features = array_unique( $this->features );
39                 }
40
41                 if ( $this->search || $this->features ) {
42                         foreach ( $themes as $key => $theme ) {
43                                 if ( !$this->search_theme( $theme ) )
44                                         unset( $themes[ $key ] );
45                         }
46                 }
47
48                 unset( $themes[$ct->name] );
49                 uksort( $themes, "strnatcasecmp" );
50
51                 $per_page = 15;
52                 $page = $this->get_pagenum();
53
54                 $start = ( $page - 1 ) * $per_page;
55
56                 $this->items = array_slice( $themes, $start, $per_page );
57
58                 $this->set_pagination_args( array(
59                         'total_items' => count( $themes ),
60                         'per_page' => $per_page,
61                 ) );
62         }
63
64         function no_items() {
65                 if ( $this->search || $this->features ) {
66                         _e( 'No items found.' );
67                         return;
68                 }
69
70                 if ( is_multisite() ) {
71                         if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
72                                 printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> or <a href="%2$s">install</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $GLOBALS['blog_id'] ), network_admin_url( 'theme-install.php' ) );
73
74                                 return;
75                         } elseif ( current_user_can( 'manage_network_themes' ) ) {
76                                 printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $GLOBALS['blog_id'] ) );
77
78                                 return;
79                         }
80                         // else, fallthrough. install_themes doesn't help if you can't enable it.
81                 } else {
82                         if ( current_user_can( 'install_themes' ) ) {
83                                 printf( __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress.org Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ), admin_url( 'theme-install.php' ) );
84
85                                 return;
86                         }
87                 }
88                 // Fallthrough.
89                 printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
90         }
91
92         function tablenav( $which = 'top' ) {
93                 if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
94                         return;
95                 ?>
96                 <div class="tablenav <?php echo $which; ?>">
97                         <?php $this->pagination( $which ); ?>
98                    <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading list-ajax-loading" alt="" />
99                   <br class="clear" />
100                 </div>
101                 <?php
102         }
103
104         function display() {
105                 // wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
106 ?>
107                 <?php $this->tablenav( 'top' ); ?>
108
109                 <table id="availablethemes" cellspacing="0" cellpadding="0">
110                         <tbody id="the-list" class="list:themes">
111                                 <?php $this->display_rows_or_placeholder(); ?>
112                         </tbody>
113                 </table>
114
115                 <?php $this->tablenav( 'bottom' ); ?>
116 <?php
117         }
118
119         function get_columns() {
120                 return array();
121         }
122
123         function display_rows() {
124                 $themes = $this->items;
125                 $theme_names = array_keys( $themes );
126                 natcasesort( $theme_names );
127
128                 $table = array();
129                 $rows = ceil( count( $theme_names ) / 3 );
130                 for ( $row = 1; $row <= $rows; $row++ )
131                         for ( $col = 1; $col <= 3; $col++ )
132                                 $table[$row][$col] = array_shift( $theme_names );
133
134                 foreach ( $table as $row => $cols ) {
135 ?>
136 <tr>
137 <?php
138 foreach ( $cols as $col => $theme_name ) {
139         $class = array( 'available-theme' );
140         if ( $row == 1 ) $class[] = 'top';
141         if ( $col == 1 ) $class[] = 'left';
142         if ( $row == $rows ) $class[] = 'bottom';
143         if ( $col == 3 ) $class[] = 'right';
144 ?>
145         <td class="<?php echo join( ' ', $class ); ?>">
146 <?php if ( !empty( $theme_name ) ) :
147         $template = $themes[$theme_name]['Template'];
148         $stylesheet = $themes[$theme_name]['Stylesheet'];
149         $title = $themes[$theme_name]['Title'];
150         $version = $themes[$theme_name]['Version'];
151         $description = $themes[$theme_name]['Description'];
152         $author = $themes[$theme_name]['Author'];
153         $screenshot = $themes[$theme_name]['Screenshot'];
154         $stylesheet_dir = $themes[$theme_name]['Stylesheet Dir'];
155         $template_dir = $themes[$theme_name]['Template Dir'];
156         $parent_theme = $themes[$theme_name]['Parent Theme'];
157         $theme_root = $themes[$theme_name]['Theme Root'];
158         $theme_root_uri = $themes[$theme_name]['Theme Root URI'];
159         $preview_link = esc_url( get_option( 'home' ) . '/' );
160         if ( is_ssl() )
161                 $preview_link = str_replace( 'http://', 'https://', $preview_link );
162         $preview_link = htmlspecialchars( add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ), $preview_link ) );
163         $preview_text = esc_attr( sprintf( __( 'Preview of &#8220;%s&#8221;' ), $title ) );
164         $tags = $themes[$theme_name]['Tags'];
165         $thickbox_class = 'thickbox thickbox-preview';
166         $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=".urlencode( $template )."&amp;stylesheet=".urlencode( $stylesheet ), 'switch-theme_' . $template );
167         $activate_text = esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) );
168         $actions = array();
169         $actions[] = '<a href="' . $activate_link .  '" class="activatelink" title="' . $activate_text . '">' . __( 'Activate' ) . '</a>';
170         $actions[] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $theme_name ) ) . '">' . __( 'Preview' ) . '</a>';
171         if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
172                 $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>';
173         $actions = apply_filters( 'theme_action_links', $actions, $themes[$theme_name] );
174
175         $actions = implode ( ' | ', $actions );
176 ?>
177                 <a href="<?php echo $preview_link; ?>" class="<?php echo $thickbox_class; ?> screenshot">
178 <?php if ( $screenshot ) : ?>
179                         <img src="<?php echo $theme_root_uri . '/' . $stylesheet . '/' . $screenshot; ?>" alt="" />
180 <?php endif; ?>
181                 </a>
182 <h3><?php
183         /* translators: 1: theme title, 2: theme version, 3: theme author */
184         printf( __( '%1$s %2$s by %3$s' ), $title, $version, $author ) ; ?></h3>
185 <p class="description"><?php echo $description; ?></p>
186 <span class='action-links'><?php echo $actions ?></span>
187         <?php if ( current_user_can( 'edit_themes' ) && $parent_theme ) {
188         /* translators: 1: theme title, 2:  template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?>
189         <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>
190 <?php } else { ?>
191         <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>
192 <?php } ?>
193 <?php if ( $tags ) : ?>
194 <p><?php _e( 'Tags:' ); ?> <?php echo join( ', ', $tags ); ?></p>
195 <?php endif; ?>
196                 <?php theme_update_available( $themes[$theme_name] ); ?>
197 <?php endif; // end if not empty theme_name ?>
198         </td>
199 <?php } // end foreach $cols ?>
200 </tr>
201 <?php } // end foreach $table
202         }
203
204         function search_theme( $theme ) {
205                 $matched = 0;
206
207                 // Match all phrases
208                 if ( count( $this->search ) > 0 ) {
209                         foreach ( $this->search as $word ) {
210                                 $matched = 0;
211
212                                 // In a tag?
213                                 if ( in_array( $word, array_map( 'sanitize_title_with_dashes', $theme['Tags'] ) ) )
214                                         $matched = 1;
215
216                                 // In one of the fields?
217                                 foreach ( array( 'Name', 'Title', 'Description', 'Author', 'Template', 'Stylesheet' ) AS $field ) {
218                                         if ( stripos( $theme[$field], $word ) !== false )
219                                                 $matched++;
220                                 }
221
222                                 if ( $matched == 0 )
223                                         return false;
224                         }
225                 }
226
227                 // Now search the features
228                 if ( count( $this->features ) > 0 ) {
229                         foreach ( $this->features as $word ) {
230                                 // In a tag?
231                                 if ( !in_array( $word, array_map( 'sanitize_title_with_dashes', $theme['Tags'] ) ) )
232                                         return false;
233                         }
234                 }
235
236                 // Only get here if each word exists in the tags or one of the fields
237                 return true;
238         }
239 }
240
241 ?>