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