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