Wordpress 3.6
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-ms-themes-list-table.php
1 <?php
2 /**
3  * MS Themes List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_MS_Themes_List_Table extends WP_List_Table {
11
12         var $site_id;
13         var $is_site_themes;
14
15         function __construct( $args = array() ) {
16                 global $status, $page;
17
18                 parent::__construct( array(
19                         'plural' => 'themes',
20                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
21                 ) );
22
23                 $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
24                 if ( !in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken' ) ) )
25                         $status = 'all';
26
27                 $page = $this->get_pagenum();
28
29                 $this->is_site_themes = ( 'site-themes-network' == $this->screen->id ) ? true : false;
30
31                 if ( $this->is_site_themes )
32                         $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
33         }
34
35         function get_table_classes() {
36                 return array( 'widefat', 'plugins' );   // todo: remove and add CSS for .themes
37         }
38
39         function ajax_user_can() {
40                 if ( $this->is_site_themes )
41                         return current_user_can( 'manage_sites' );
42                 else
43                         return current_user_can( 'manage_network_themes' );
44         }
45
46         function prepare_items() {
47                 global $status, $totals, $page, $orderby, $order, $s;
48
49                 wp_reset_vars( array( 'orderby', 'order', 's' ) );
50
51                 $themes = array(
52                         'all' => apply_filters( 'all_themes', wp_get_themes() ),
53                         'search' => array(),
54                         'enabled' => array(),
55                         'disabled' => array(),
56                         'upgrade' => array(),
57                         'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
58                 );
59
60                 if ( $this->is_site_themes ) {
61                         $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
62                         $allowed_where = 'site';
63                 } else {
64                         $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
65                         $allowed_where = 'network';
66                 }
67
68                 $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current = get_site_transient( 'update_themes' );
69
70                 foreach ( (array) $themes['all'] as $key => $theme ) {
71                         if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
72                                 unset( $themes['all'][ $key ] );
73                                 continue;
74                         }
75
76                         if ( $maybe_update && isset( $current->response[ $key ] ) ) {
77                                 $themes['all'][ $key ]->update = true;
78                                 $themes['upgrade'][ $key ] = $themes['all'][ $key ];
79                         }
80
81                         $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
82                         $themes[ $filter ][ $key ] = $themes['all'][ $key ];
83                 }
84
85                 if ( $s ) {
86                         $status = 'search';
87                         $themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( &$this, '_search_callback' ) );
88                 }
89
90                 $totals = array();
91                 foreach ( $themes as $type => $list )
92                         $totals[ $type ] = count( $list );
93
94                 if ( empty( $themes[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
95                         $status = 'all';
96
97                 $this->items = $themes[ $status ];
98                 WP_Theme::sort_by_name( $this->items );
99
100                 $this->has_items = ! empty( $themes['all'] );
101                 $total_this_page = $totals[ $status ];
102
103                 if ( $orderby ) {
104                         $orderby = ucfirst( $orderby );
105                         $order = strtoupper( $order );
106
107                         if ( $orderby == 'Name' ) {
108                                 if ( 'ASC' == $order )
109                                         $this->items = array_reverse( $this->items );
110                         } else {
111                                 uasort( $this->items, array( &$this, '_order_callback' ) );
112                         }
113                 }
114
115                 $start = ( $page - 1 ) * $themes_per_page;
116
117                 if ( $total_this_page > $themes_per_page )
118                         $this->items = array_slice( $this->items, $start, $themes_per_page, true );
119
120                 $this->set_pagination_args( array(
121                         'total_items' => $total_this_page,
122                         'per_page' => $themes_per_page,
123                 ) );
124         }
125
126         function _search_callback( $theme ) {
127                 static $term;
128                 if ( is_null( $term ) )
129                         $term = wp_unslash( $_REQUEST['s'] );
130
131                 foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
132                         // Don't mark up; Do translate.
133                         if ( false !== stripos( $theme->display( $field, false, true ), $term ) )
134                                 return true;
135                 }
136
137                 if ( false !== stripos( $theme->get_stylesheet(), $term ) )
138                         return true;
139
140                 if ( false !== stripos( $theme->get_template(), $term ) )
141                         return true;
142
143                 return false;
144         }
145
146         // Not used by any core columns.
147         function _order_callback( $theme_a, $theme_b ) {
148                 global $orderby, $order;
149
150                 $a = $theme_a[ $orderby ];
151                 $b = $theme_b[ $orderby ];
152
153                 if ( $a == $b )
154                         return 0;
155
156                 if ( 'DESC' == $order )
157                         return ( $a < $b ) ? 1 : -1;
158                 else
159                         return ( $a < $b ) ? -1 : 1;
160         }
161
162         function no_items() {
163                 if ( ! $this->has_items )
164                         _e( 'No themes found.' );
165                 else
166                         _e( 'You do not appear to have any themes available at this time.' );
167         }
168
169         function get_columns() {
170                 global $status;
171
172                 return array(
173                         'cb'          => '<input type="checkbox" />',
174                         'name'        => __( 'Theme' ),
175                         'description' => __( 'Description' ),
176                 );
177         }
178
179         function get_sortable_columns() {
180                 return array(
181                         'name'         => 'name',
182                 );
183         }
184
185         function get_views() {
186                 global $totals, $status;
187
188                 $status_links = array();
189                 foreach ( $totals as $type => $count ) {
190                         if ( !$count )
191                                 continue;
192
193                         switch ( $type ) {
194                                 case 'all':
195                                         $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' );
196                                         break;
197                                 case 'enabled':
198                                         $text = _n( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', $count );
199                                         break;
200                                 case 'disabled':
201                                         $text = _n( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', $count );
202                                         break;
203                                 case 'upgrade':
204                                         $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
205                                         break;
206                                 case 'broken' :
207                                         $text = _n( 'Broken <span class="count">(%s)</span>', 'Broken <span class="count">(%s)</span>', $count );
208                                         break;
209                         }
210
211                         if ( $this->is_site_themes )
212                                 $url = 'site-themes.php?id=' . $this->site_id;
213                         else
214                                 $url = 'themes.php';
215
216                         if ( 'search' != $type ) {
217                                 $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
218                                         esc_url( add_query_arg('theme_status', $type, $url) ),
219                                         ( $type == $status ) ? ' class="current"' : '',
220                                         sprintf( $text, number_format_i18n( $count ) )
221                                 );
222                         }
223                 }
224
225                 return $status_links;
226         }
227
228         function get_bulk_actions() {
229                 global $status;
230
231                 $actions = array();
232                 if ( 'enabled' != $status )
233                         $actions['enable-selected'] = $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' );
234                 if ( 'disabled' != $status )
235                         $actions['disable-selected'] = $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' );
236                 if ( ! $this->is_site_themes ) {
237                         if ( current_user_can( 'update_themes' ) )
238                                 $actions['update-selected'] = __( 'Update' );
239                         if ( current_user_can( 'delete_themes' ) )
240                                 $actions['delete-selected'] = __( 'Delete' );
241                 }
242                 return $actions;
243         }
244
245         function display_rows() {
246                 foreach ( $this->items as $theme )
247                         $this->single_row( $theme );
248         }
249
250         function single_row( $theme ) {
251                 global $status, $page, $s, $totals;
252
253                 $context = $status;
254
255                 if ( $this->is_site_themes ) {
256                         $url = "site-themes.php?id={$this->site_id}&amp;";
257                         $allowed = $theme->is_allowed( 'site', $this->site_id );
258                 } else {
259                         $url = 'themes.php?';
260                         $allowed = $theme->is_allowed( 'network' );
261                 }
262
263                 // preorder
264                 $actions = array(
265                         'enable' => '',
266                         'disable' => '',
267                         'edit' => '',
268                         'delete' => ''
269                 );
270
271                 $stylesheet = $theme->get_stylesheet();
272                 $theme_key = urlencode( $stylesheet );
273
274                 if ( ! $allowed ) {
275                         if ( ! $theme->errors() )
276                                 $actions['enable'] = '<a href="' . esc_url( wp_nonce_url($url . 'action=enable&amp;theme=' . $theme_key . '&amp;paged=' . $page . '&amp;s=' . $s, 'enable-theme_' . $stylesheet ) ) . '" title="' . esc_attr__('Enable this theme') . '" class="edit">' . ( $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' ) ) . '</a>';
277                 } else {
278                         $actions['disable'] = '<a href="' . esc_url( wp_nonce_url($url . 'action=disable&amp;theme=' . $theme_key . '&amp;paged=' . $page . '&amp;s=' . $s, 'disable-theme_' . $stylesheet ) ) . '" title="' . esc_attr__('Disable this theme') . '">' . ( $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' ) ) . '</a>';
279                 }
280
281                 if ( current_user_can('edit_themes') )
282                         $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>';
283
284                 if ( ! $allowed && current_user_can( 'delete_themes' ) && ! $this->is_site_themes && $stylesheet != get_option( 'stylesheet' ) && $stylesheet != get_option( 'template' ) )
285                         $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>';
286
287                 $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $stylesheet, $theme, $context );
288                 $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $stylesheet, $theme, $context );
289
290                 $class = ! $allowed ? 'inactive' : 'active';
291                 $checkbox_id = "checkbox_" . md5( $theme->get('Name') );
292                 $checkbox = "<input type='checkbox' name='checked[]' value='" . esc_attr( $stylesheet ) . "' id='" . $checkbox_id . "' /><label class='screen-reader-text' for='" . $checkbox_id . "' >" . __('Select') . " " . $theme->display('Name') . "</label>";
293
294                 $id = sanitize_html_class( $theme->get_stylesheet() );
295
296                 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) )
297                         $class .= ' update';
298
299                 echo "<tr id='$id' class='$class'>";
300
301                 list( $columns, $hidden ) = $this->get_column_info();
302
303                 foreach ( $columns as $column_name => $column_display_name ) {
304                         $style = '';
305                         if ( in_array( $column_name, $hidden ) )
306                                 $style = ' style="display:none;"';
307
308                         switch ( $column_name ) {
309                                 case 'cb':
310                                         echo "<th scope='row' class='check-column'>$checkbox</th>";
311                                         break;
312                                 case 'name':
313                                         echo "<td class='theme-title'$style><strong>" . $theme->display('Name') . "</strong>";
314                                         echo $this->row_actions( $actions, true );
315                                         echo "</td>";
316                                         break;
317                                 case 'description':
318                                         echo "<td class='column-description desc'$style>";
319                                         if ( $theme->errors() ) {
320                                                 $pre = $status == 'broken' ? '' : __( 'Broken Theme:' ) . ' ';
321                                                 echo '<p><strong class="attention">' . $pre . $theme->errors()->get_error_message() . '</strong></p>';
322                                         }
323                                         echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
324                                                 <div class='$class second theme-version-author-uri'>";
325
326                                         $theme_meta = array();
327
328                                         if ( $theme->get('Version') )
329                                                 $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display('Version') );
330
331                                         $theme_meta[] = sprintf( __( 'By %s' ), $theme->display('Author') );
332
333                                         if ( $theme->get('ThemeURI') )
334                                                 $theme_meta[] = '<a href="' . $theme->display('ThemeURI') . '" title="' . esc_attr__( 'Visit theme homepage' ) . '">' . __( 'Visit Theme Site' ) . '</a>';
335
336                                         $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
337                                         echo implode( ' | ', $theme_meta );
338
339                                         echo "</div></td>";
340                                         break;
341
342                                 default:
343                                         echo "<td class='$column_name column-$column_name'$style>";
344                                         do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
345                                         echo "</td>";
346                         }
347                 }
348
349                 echo "</tr>";
350
351                 if ( $this->is_site_themes )
352                         remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
353                 do_action( 'after_theme_row', $stylesheet, $theme, $status );
354                 do_action( "after_theme_row_$stylesheet", $stylesheet, $theme, $status );
355         }
356 }