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