WordPress 3.9
[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                         /**
53                          * Filter the full array of WP_Theme objects to list in the Multisite
54                          * themes list table.
55                          *
56                          * @since 3.1.0
57                          *
58                          * @param array $all An array of WP_Theme objects to display in the list table.
59                          */
60                         'all' => apply_filters( 'all_themes', wp_get_themes() ),
61                         'search' => array(),
62                         'enabled' => array(),
63                         'disabled' => array(),
64                         'upgrade' => array(),
65                         'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
66                 );
67
68                 if ( $this->is_site_themes ) {
69                         $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
70                         $allowed_where = 'site';
71                 } else {
72                         $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
73                         $allowed_where = 'network';
74                 }
75
76                 $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current = get_site_transient( 'update_themes' );
77
78                 foreach ( (array) $themes['all'] as $key => $theme ) {
79                         if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
80                                 unset( $themes['all'][ $key ] );
81                                 continue;
82                         }
83
84                         if ( $maybe_update && isset( $current->response[ $key ] ) ) {
85                                 $themes['all'][ $key ]->update = true;
86                                 $themes['upgrade'][ $key ] = $themes['all'][ $key ];
87                         }
88
89                         $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
90                         $themes[ $filter ][ $key ] = $themes['all'][ $key ];
91                 }
92
93                 if ( $s ) {
94                         $status = 'search';
95                         $themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( $this, '_search_callback' ) );
96                 }
97
98                 $totals = array();
99                 foreach ( $themes as $type => $list )
100                         $totals[ $type ] = count( $list );
101
102                 if ( empty( $themes[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
103                         $status = 'all';
104
105                 $this->items = $themes[ $status ];
106                 WP_Theme::sort_by_name( $this->items );
107
108                 $this->has_items = ! empty( $themes['all'] );
109                 $total_this_page = $totals[ $status ];
110
111                 if ( $orderby ) {
112                         $orderby = ucfirst( $orderby );
113                         $order = strtoupper( $order );
114
115                         if ( $orderby == 'Name' ) {
116                                 if ( 'ASC' == $order )
117                                         $this->items = array_reverse( $this->items );
118                         } else {
119                                 uasort( $this->items, array( $this, '_order_callback' ) );
120                         }
121                 }
122
123                 $start = ( $page - 1 ) * $themes_per_page;
124
125                 if ( $total_this_page > $themes_per_page )
126                         $this->items = array_slice( $this->items, $start, $themes_per_page, true );
127
128                 $this->set_pagination_args( array(
129                         'total_items' => $total_this_page,
130                         'per_page' => $themes_per_page,
131                 ) );
132         }
133
134         function _search_callback( $theme ) {
135                 static $term;
136                 if ( is_null( $term ) )
137                         $term = wp_unslash( $_REQUEST['s'] );
138
139                 foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
140                         // Don't mark up; Do translate.
141                         if ( false !== stripos( $theme->display( $field, false, true ), $term ) )
142                                 return true;
143                 }
144
145                 if ( false !== stripos( $theme->get_stylesheet(), $term ) )
146                         return true;
147
148                 if ( false !== stripos( $theme->get_template(), $term ) )
149                         return true;
150
151                 return false;
152         }
153
154         // Not used by any core columns.
155         function _order_callback( $theme_a, $theme_b ) {
156                 global $orderby, $order;
157
158                 $a = $theme_a[ $orderby ];
159                 $b = $theme_b[ $orderby ];
160
161                 if ( $a == $b )
162                         return 0;
163
164                 if ( 'DESC' == $order )
165                         return ( $a < $b ) ? 1 : -1;
166                 else
167                         return ( $a < $b ) ? -1 : 1;
168         }
169
170         function no_items() {
171                 if ( ! $this->has_items )
172                         _e( 'No themes found.' );
173                 else
174                         _e( 'You do not appear to have any themes available at this time.' );
175         }
176
177         function get_columns() {
178                 global $status;
179
180                 return array(
181                         'cb'          => '<input type="checkbox" />',
182                         'name'        => __( 'Theme' ),
183                         'description' => __( 'Description' ),
184                 );
185         }
186
187         function get_sortable_columns() {
188                 return array(
189                         'name'         => 'name',
190                 );
191         }
192
193         function get_views() {
194                 global $totals, $status;
195
196                 $status_links = array();
197                 foreach ( $totals as $type => $count ) {
198                         if ( !$count )
199                                 continue;
200
201                         switch ( $type ) {
202                                 case 'all':
203                                         $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' );
204                                         break;
205                                 case 'enabled':
206                                         $text = _n( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', $count );
207                                         break;
208                                 case 'disabled':
209                                         $text = _n( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', $count );
210                                         break;
211                                 case 'upgrade':
212                                         $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
213                                         break;
214                                 case 'broken' :
215                                         $text = _n( 'Broken <span class="count">(%s)</span>', 'Broken <span class="count">(%s)</span>', $count );
216                                         break;
217                         }
218
219                         if ( $this->is_site_themes )
220                                 $url = 'site-themes.php?id=' . $this->site_id;
221                         else
222                                 $url = 'themes.php';
223
224                         if ( 'search' != $type ) {
225                                 $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
226                                         esc_url( add_query_arg('theme_status', $type, $url) ),
227                                         ( $type == $status ) ? ' class="current"' : '',
228                                         sprintf( $text, number_format_i18n( $count ) )
229                                 );
230                         }
231                 }
232
233                 return $status_links;
234         }
235
236         function get_bulk_actions() {
237                 global $status;
238
239                 $actions = array();
240                 if ( 'enabled' != $status )
241                         $actions['enable-selected'] = $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' );
242                 if ( 'disabled' != $status )
243                         $actions['disable-selected'] = $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' );
244                 if ( ! $this->is_site_themes ) {
245                         if ( current_user_can( 'update_themes' ) )
246                                 $actions['update-selected'] = __( 'Update' );
247                         if ( current_user_can( 'delete_themes' ) )
248                                 $actions['delete-selected'] = __( 'Delete' );
249                 }
250                 return $actions;
251         }
252
253         function display_rows() {
254                 foreach ( $this->items as $theme )
255                         $this->single_row( $theme );
256         }
257
258         function single_row( $theme ) {
259                 global $status, $page, $s, $totals;
260
261                 $context = $status;
262
263                 if ( $this->is_site_themes ) {
264                         $url = "site-themes.php?id={$this->site_id}&amp;";
265                         $allowed = $theme->is_allowed( 'site', $this->site_id );
266                 } else {
267                         $url = 'themes.php?';
268                         $allowed = $theme->is_allowed( 'network' );
269                 }
270
271                 // preorder
272                 $actions = array(
273                         'enable' => '',
274                         'disable' => '',
275                         'edit' => '',
276                         'delete' => ''
277                 );
278
279                 $stylesheet = $theme->get_stylesheet();
280                 $theme_key = urlencode( $stylesheet );
281
282                 if ( ! $allowed ) {
283                         if ( ! $theme->errors() )
284                                 $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>';
285                 } else {
286                         $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>';
287                 }
288
289                 if ( current_user_can('edit_themes') )
290                         $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>';
291
292                 if ( ! $allowed && current_user_can( 'delete_themes' ) && ! $this->is_site_themes && $stylesheet != get_option( 'stylesheet' ) && $stylesheet != get_option( 'template' ) )
293                         $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>';
294
295                 /**
296                  * Filter the action links displayed for each theme in the Multisite
297                  * themes list table.
298                  *
299                  * The action links displayed are determined by the theme's status, and
300                  * which Multisite themes list table is being displayed - the Network
301                  * themes list table (themes.php), which displays all installed themes,
302                  * or the Site themes list table (site-themes.php), which displays the
303                  * non-network enabled themes when editing a site in the Network admin.
304                  *
305                  * The default action links for the Network themes list table include
306                  * 'Network Enable', 'Network Disable', 'Edit', and 'Delete'.
307                  *
308                  * The default action links for the Site themes list table include
309                  * 'Enable', 'Disable', and 'Edit'.
310                  *
311                  * @since 2.8.0
312                  *
313                  * @param array    $actions An array of action links.
314                  * @param WP_Theme $theme   The current WP_Theme object.
315                  * @param string   $context Status of the theme.
316                  */
317                 $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme, $context );
318
319                 /**
320                  * Filter the action links of a specific theme in the Multisite themes
321                  * list table.
322                  *
323                  * The dynamic portion of the hook name, $stylesheet, refers to the
324                  * directory name of the theme, which in most cases is synonymous
325                  * with the template name.
326                  *
327                  * @since 3.1.0
328                  *
329                  * @param array    $actions An array of action links.
330                  * @param WP_Theme $theme   The current WP_Theme object.
331                  * @param string   $context Status of the theme.
332                  */
333                 $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme, $context );
334
335                 $class = ! $allowed ? 'inactive' : 'active';
336                 $checkbox_id = "checkbox_" . md5( $theme->get('Name') );
337                 $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>";
338
339                 $id = sanitize_html_class( $theme->get_stylesheet() );
340
341                 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) )
342                         $class .= ' update';
343
344                 echo "<tr id='$id' class='$class'>";
345
346                 list( $columns, $hidden ) = $this->get_column_info();
347
348                 foreach ( $columns as $column_name => $column_display_name ) {
349                         $style = '';
350                         if ( in_array( $column_name, $hidden ) )
351                                 $style = ' style="display:none;"';
352
353                         switch ( $column_name ) {
354                                 case 'cb':
355                                         echo "<th scope='row' class='check-column'>$checkbox</th>";
356                                         break;
357                                 case 'name':
358                                         echo "<td class='theme-title'$style><strong>" . $theme->display('Name') . "</strong>";
359                                         echo $this->row_actions( $actions, true );
360                                         echo "</td>";
361                                         break;
362                                 case 'description':
363                                         echo "<td class='column-description desc'$style>";
364                                         if ( $theme->errors() ) {
365                                                 $pre = $status == 'broken' ? __( 'Broken Theme:' ) . ' ' : '';
366                                                 echo '<p><strong class="attention">' . $pre . $theme->errors()->get_error_message() . '</strong></p>';
367                                         }
368                                         echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
369                                                 <div class='$class second theme-version-author-uri'>";
370
371                                         $theme_meta = array();
372
373                                         if ( $theme->get('Version') )
374                                                 $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display('Version') );
375
376                                         $theme_meta[] = sprintf( __( 'By %s' ), $theme->display('Author') );
377
378                                         if ( $theme->get('ThemeURI') )
379                                                 $theme_meta[] = '<a href="' . $theme->display('ThemeURI') . '" title="' . esc_attr__( 'Visit theme homepage' ) . '">' . __( 'Visit Theme Site' ) . '</a>';
380
381                                         /**
382                                          * Filter the array of row meta for each theme in the Multisite themes
383                                          * list table.
384                                          *
385                                          * @since 3.1.0
386                                          *
387                                          * @param array    $theme_meta An array of the theme's metadata,
388                                          *                             including the version, author, and
389                                          *                             theme URI.
390                                          * @param string   $stylesheet Directory name of the theme.
391                                          * @param WP_Theme $theme      WP_Theme object.
392                                          * @param string   $status     Status of the theme.
393                                          */
394                                         $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
395                                         echo implode( ' | ', $theme_meta );
396
397                                         echo "</div></td>";
398                                         break;
399
400                                 default:
401                                         echo "<td class='$column_name column-$column_name'$style>";
402
403                                         /**
404                                          * Fires inside each custom column of the Multisite themes list table.
405                                          *
406                                          * @since 3.1.0
407                                          *
408                                          * @param string   $column_name Name of the column.
409                                          * @param string   $stylesheet  Directory name of the theme.
410                                          * @param WP_Theme $theme       Current WP_Theme object.
411                                          */
412                                         do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
413                                         echo "</td>";
414                         }
415                 }
416
417                 echo "</tr>";
418
419                 if ( $this->is_site_themes )
420                         remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
421
422                 /**
423                  * Fires after each row in the Multisite themes list table.
424                  *
425                  * @since 3.1.0
426                  *
427                  * @param string   $stylesheet Directory name of the theme.
428                  * @param WP_Theme $theme      Current WP_Theme object.
429                  * @param string   $status     Status of the theme.
430                  */
431                 do_action( 'after_theme_row', $stylesheet, $theme, $status );
432
433                 /**
434                  * Fires after each specific row in the Multisite themes list table.
435                  *
436                  * The dynamic portion of the hook name, $stylesheet, refers to the
437                  * directory name of the theme, most often synonymous with the template
438                  * name of the theme.
439                  *
440                  * @since 3.5.0
441                  *
442                  * @param string   $stylesheet Directory name of the theme.
443                  * @param WP_Theme $theme      Current WP_Theme object.
444                  * @param string   $status     Status of the theme.
445                  */
446                 do_action( "after_theme_row_$stylesheet", $stylesheet, $theme, $status );
447         }
448 }