]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-plugins-list-table.php
Wordpress 3.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-plugins-list-table.php
1 <?php
2 /**
3  * Plugins List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_Plugins_List_Table extends WP_List_Table {
11
12         function __construct() {
13                 global $status, $page;
14
15                 $default_status = get_user_option( 'plugins_last_view' );
16                 if ( empty( $default_status ) )
17                         $default_status = 'all';
18                 $status = isset( $_REQUEST['plugin_status'] ) ? $_REQUEST['plugin_status'] : $default_status;
19                 if ( !in_array( $status, array( 'all', 'active', 'inactive', 'recently_activated', 'upgrade', 'network', 'mustuse', 'dropins', 'search' ) ) )
20                         $status = 'all';
21                 if ( $status != $default_status && 'search' != $status )
22                         update_user_meta( get_current_user_id(), 'plugins_last_view', $status );
23
24
25                 if ( isset($_REQUEST['s']) )
26                         $_SERVER['REQUEST_URI'] = add_query_arg('s', stripslashes($_REQUEST['s']) );
27
28                 $page = $this->get_pagenum();
29
30                 parent::__construct( array(
31                         'plural' => 'plugins',
32                 ) );
33         }
34
35         function get_table_classes() {
36                 return array( 'widefat', $this->_args['plural'] );
37         }
38
39         function ajax_user_can() {
40                 if ( is_multisite() ) {
41                         $menu_perms = get_site_option( 'menu_items', array() );
42
43                         if ( empty( $menu_perms['plugins'] ) && ! is_super_admin() )
44                                 return false;
45                 }
46
47                 return current_user_can('activate_plugins');
48         }
49
50         function prepare_items() {
51                 global $status, $plugins, $totals, $page, $orderby, $order, $s;
52
53                 wp_reset_vars( array( 'orderby', 'order', 's' ) );
54
55                 $plugins = array(
56                         'all' => apply_filters( 'all_plugins', get_plugins() ),
57                         'search' => array(),
58                         'active' => array(),
59                         'inactive' => array(),
60                         'recently_activated' => array(),
61                         'upgrade' => array(),
62                         'mustuse' => array(),
63                         'dropins' => array()
64                 );
65
66                 $screen = get_current_screen();
67
68                 if ( ! is_multisite() || ( $screen->is_network && current_user_can('manage_network_plugins') ) ) {
69                         if ( apply_filters( 'show_advanced_plugins', true, 'mustuse' ) )
70                                 $plugins['mustuse'] = get_mu_plugins();
71                         if ( apply_filters( 'show_advanced_plugins', true, 'dropins' ) )
72                                 $plugins['dropins'] = get_dropins();
73
74                         $current = get_site_transient( 'update_plugins' );
75                         foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
76                                 if ( isset( $current->response[ $plugin_file ] ) )
77                                         $plugins['upgrade'][ $plugin_file ] = $plugin_data;
78                         }
79                 }
80
81                 set_transient( 'plugin_slugs', array_keys( $plugins['all'] ), 86400 );
82
83                 $recently_activated = get_option( 'recently_activated', array() );
84
85                 $one_week = 7*24*60*60;
86                 foreach ( $recently_activated as $key => $time )
87                         if ( $time + $one_week < time() )
88                                 unset( $recently_activated[$key] );
89                 update_option( 'recently_activated', $recently_activated );
90
91                 foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
92                         // Filter into individual sections
93                         if ( is_multisite() && is_network_only_plugin( $plugin_file ) && !$screen->is_network ) {
94                                 unset( $plugins['all'][ $plugin_file] );
95                         } elseif ( is_plugin_active_for_network($plugin_file) && !$screen->is_network ) {
96                                 unset( $plugins['all'][ $plugin_file ] );
97                         } elseif ( is_multisite() && is_network_only_plugin( $plugin_file ) && !current_user_can( 'manage_network_plugins' ) ) {
98                                 $plugins['network'][ $plugin_file ] = $plugin_data;
99                         } elseif ( ( !$screen->is_network && is_plugin_active( $plugin_file ) )
100                                 || ( $screen->is_network && is_plugin_active_for_network( $plugin_file ) ) ) {
101                                 $plugins['active'][ $plugin_file ] = $plugin_data;
102                         } else {
103                                 if ( !$screen->is_network && isset( $recently_activated[ $plugin_file ] ) ) // Was the plugin recently activated?
104                                         $plugins['recently_activated'][ $plugin_file ] = $plugin_data;
105                                 $plugins['inactive'][ $plugin_file ] = $plugin_data;
106                         }
107                 }
108
109                 if ( !current_user_can( 'update_plugins' ) )
110                         $plugins['upgrade'] = array();
111
112                 if ( $s ) {
113                         $status = 'search';
114                         $plugins['search'] = array_filter( $plugins['all'], array( &$this, '_search_callback' ) );
115                 }
116
117                 $totals = array();
118                 foreach ( $plugins as $type => $list )
119                         $totals[ $type ] = count( $list );
120
121                 if ( empty( $plugins[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
122                         $status = 'all';
123
124                 $this->items = array();
125                 foreach ( $plugins[ $status ] as $plugin_file => $plugin_data ) {
126                         // Translate, Don't Apply Markup, Sanitize HTML
127                         $this->items[$plugin_file] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true );
128                 }
129
130                 $total_this_page = $totals[ $status ];
131
132                 if ( $orderby ) {
133                         $orderby = ucfirst( $orderby );
134                         $order = strtoupper( $order );
135
136                         uasort( $this->items, array( &$this, '_order_callback' ) );
137                 }
138
139                 $plugins_per_page = $this->get_items_per_page( str_replace( '-', '_', $screen->id . '_per_page' ), 999 );
140
141                 $start = ( $page - 1 ) * $plugins_per_page;
142
143                 if ( $total_this_page > $plugins_per_page )
144                         $this->items = array_slice( $this->items, $start, $plugins_per_page );
145
146                 $this->set_pagination_args( array(
147                         'total_items' => $total_this_page,
148                         'per_page' => $plugins_per_page,
149                 ) );
150         }
151
152         function _search_callback( $plugin ) {
153                 static $term;
154                 if ( is_null( $term ) )
155                         $term = stripslashes( $_REQUEST['s'] );
156
157                 foreach ( $plugin as $value )
158                         if ( stripos( $value, $term ) !== false )
159                                 return true;
160
161                 return false;
162         }
163
164         function _order_callback( $plugin_a, $plugin_b ) {
165                 global $orderby, $order;
166
167                 $a = $plugin_a[$orderby];
168                 $b = $plugin_b[$orderby];
169
170                 if ( $a == $b )
171                         return 0;
172
173                 if ( 'DESC' == $order )
174                         return ( $a < $b ) ? 1 : -1;
175                 else
176                         return ( $a < $b ) ? -1 : 1;
177         }
178
179         function no_items() {
180                 global $plugins;
181
182                 if ( !empty( $plugins['all'] ) )
183                         _e( 'No plugins found.' );
184                 else
185                         _e( 'You do not appear to have any plugins available at this time.' );
186         }
187
188         function get_columns() {
189                 global $status;
190
191                 return array(
192                         'cb'          => !in_array( $status, array( 'mustuse', 'dropins' ) ) ? '<input type="checkbox" />' : '',
193                         'name'        => __( 'Plugin' ),
194                         'description' => __( 'Description' ),
195                 );
196         }
197
198         function get_sortable_columns() {
199                 return array();
200         }
201
202         function get_views() {
203                 global $totals, $status;
204
205                 $status_links = array();
206                 foreach ( $totals as $type => $count ) {
207                         if ( !$count )
208                                 continue;
209
210                         switch ( $type ) {
211                                 case 'all':
212                                         $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins' );
213                                         break;
214                                 case 'active':
215                                         $text = _n( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', $count );
216                                         break;
217                                 case 'recently_activated':
218                                         $text = _n( 'Recently Active <span class="count">(%s)</span>', 'Recently Active <span class="count">(%s)</span>', $count );
219                                         break;
220                                 case 'inactive':
221                                         $text = _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', $count );
222                                         break;
223                                 case 'network':
224                                         $text = _n( 'Network <span class="count">(%s)</span>', 'Network <span class="count">(%s)</span>', $count );
225                                         break;
226                                 case 'mustuse':
227                                         $text = _n( 'Must-Use <span class="count">(%s)</span>', 'Must-Use <span class="count">(%s)</span>', $count );
228                                         break;
229                                 case 'dropins':
230                                         $text = _n( 'Drop-ins <span class="count">(%s)</span>', 'Drop-ins <span class="count">(%s)</span>', $count );
231                                         break;
232                                 case 'upgrade':
233                                         $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
234                                         break;
235                         }
236
237                         if ( 'search' != $type ) {
238                                 $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
239                                         add_query_arg('plugin_status', $type, 'plugins.php'),
240                                         ( $type == $status ) ? ' class="current"' : '',
241                                         sprintf( $text, number_format_i18n( $count ) )
242                                         );
243                         }
244                 }
245
246                 return $status_links;
247         }
248
249         function get_bulk_actions() {
250                 global $status;
251
252                 $actions = array();
253
254                 $screen = get_current_screen();
255
256                 if ( 'active' != $status ) {
257                         $action = $screen->is_network ? 'network-activate-selected' : 'activate-selected';
258                         $actions[ $action ] = __( 'Activate' );
259                 }
260
261                 if ( 'inactive' != $status && 'recent' != $status )
262                         $actions['deactivate-selected'] = __( 'Deactivate' );
263
264                 if ( !is_multisite() || $screen->is_network ) {
265                         if ( current_user_can( 'update_plugins' ) )
266                                 $actions['update-selected'] = __( 'Update' );
267                         if ( current_user_can( 'delete_plugins' ) && ( 'active' != $status ) )
268                                 $actions['delete-selected'] = __( 'Delete' );
269                 }
270
271                 return $actions;
272         }
273
274         function bulk_actions( $which ) {
275                 global $status;
276
277                 if ( in_array( $status, array( 'mustuse', 'dropins' ) ) )
278                         return;
279
280                 parent::bulk_actions( $which );
281         }
282
283         function extra_tablenav( $which ) {
284                 global $status;
285
286                 if ( ! in_array($status, array('recently_activated', 'mustuse', 'dropins') ) )
287                         return;
288
289                 echo '<div class="alignleft actions">';
290
291                 if ( 'recently_activated' == $status )
292                         submit_button( __( 'Clear List' ), 'secondary', 'clear-recent-list', false );
293                 elseif ( 'top' == $which && 'mustuse' == $status )
294                         echo '<p>' . sprintf( __( 'Files in the <code>%s</code> directory are executed automatically.' ), str_replace( ABSPATH, '/', WPMU_PLUGIN_DIR ) ) . '</p>';
295                 elseif ( 'top' == $which && 'dropins' == $status )
296                         echo '<p>' . sprintf( __( 'Drop-ins are advanced plugins in the <code>%s</code> directory that replace WordPress functionality when present.' ), str_replace( ABSPATH, '', WP_CONTENT_DIR ) ) . '</p>';
297
298                 echo '</div>';
299         }
300
301         function current_action() {
302                 if ( isset($_POST['clear-recent-list']) )
303                         return 'clear-recent-list';
304
305                 return parent::current_action();
306         }
307
308         function display_rows() {
309                 global $status;
310
311                 $screen = get_current_screen();
312
313                 if ( is_multisite() && !$screen->is_network && in_array( $status, array( 'mustuse', 'dropins' ) ) )
314                         return;
315
316                 foreach ( $this->items as $plugin_file => $plugin_data )
317                         $this->single_row( $plugin_file, $plugin_data );
318         }
319
320         function single_row( $plugin_file, $plugin_data ) {
321                 global $status, $page, $s;
322
323                 $context = $status;
324
325                 $screen = get_current_screen();
326
327                 // preorder
328                 $actions = array(
329                         'network_deactivate' => '', 'deactivate' => '',
330                         'network_only' => '', 'activate' => '',
331                         'network_activate' => '',
332                         'edit' => '',
333                         'delete' => '',
334                 );
335
336                 if ( 'mustuse' == $context ) {
337                         $is_active = true;
338                 } elseif ( 'dropins' == $context ) {
339                         $dropins = _get_dropins();
340                         $plugin_name = $plugin_file;
341                         if ( $plugin_file != $plugin_data['Name'] )
342                                 $plugin_name .= '<br/>' . $plugin_data['Name'];
343                         if ( true === ( $dropins[ $plugin_file ][1] ) ) { // Doesn't require a constant
344                                 $is_active = true;
345                                 $description = '<p><strong>' . $dropins[ $plugin_file ][0] . '</strong></p>';
346                         } elseif ( constant( $dropins[ $plugin_file ][1] ) ) { // Constant is true
347                                 $is_active = true;
348                                 $description = '<p><strong>' . $dropins[ $plugin_file ][0] . '</strong></p>';
349                         } else {
350                                 $is_active = false;
351                                 $description = '<p><strong>' . $dropins[ $plugin_file ][0] . ' <span class="attention">' . __('Inactive:') . '</span></strong> ' . sprintf( __( 'Requires <code>%s</code> in <code>wp-config.php</code>.' ), "define('" . $dropins[ $plugin_file ][1] . "', true);" ) . '</p>';
352                         }
353                         if ( $plugin_data['Description'] )
354                                 $description .= '<p>' . $plugin_data['Description'] . '</p>';
355                 } else {
356                         $is_active_for_network = is_plugin_active_for_network($plugin_file);
357                         if ( $screen->is_network )
358                                 $is_active = $is_active_for_network;
359                         else
360                                 $is_active = is_plugin_active( $plugin_file );
361
362                         if ( $is_active_for_network && !is_super_admin() && !$screen->is_network )
363                                 return;
364
365                         if ( $screen->is_network ) {
366                                 if ( $is_active_for_network ) {
367                                         if ( current_user_can( 'manage_network_plugins' ) )
368                                                 $actions['network_deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&amp;networkwide=1&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Network Deactivate') . '</a>';
369                                 } else {
370                                         if ( current_user_can( 'manage_network_plugins' ) )
371                                                 $actions['network_activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin for all sites in this network') . '" class="edit">' . __('Network Activate') . '</a>';
372                                         if ( current_user_can( 'delete_plugins' ) && ! is_plugin_active( $plugin_file ) )
373                                                 $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&amp;checked[]=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
374                                 }
375                         } else {
376                                 if ( $is_active ) {
377                                         $actions['deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Deactivate') . '</a>';
378                                 } else {
379                                         $actions['activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" class="edit">' . __('Activate') . '</a>';
380
381                                         if ( ! is_multisite() && current_user_can('delete_plugins') )
382                                                 $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&amp;checked[]=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
383                                 } // end if $is_active
384                          } // end if $screen->is_network
385
386                         if ( ( ! is_multisite() || $screen->is_network ) && current_user_can('edit_plugins') && is_writable(WP_PLUGIN_DIR . '/' . $plugin_file) )
387                                 $actions['edit'] = '<a href="plugin-editor.php?file=' . $plugin_file . '" title="' . esc_attr__('Open this file in the Plugin Editor') . '" class="edit">' . __('Edit') . '</a>';
388                 } // end if $context
389
390                 $prefix = $screen->is_network ? 'network_admin_' : '';
391                 $actions = apply_filters( $prefix . 'plugin_action_links', array_filter( $actions ), $plugin_file, $plugin_data, $context );
392                 $actions = apply_filters( $prefix . "plugin_action_links_$plugin_file", $actions, $plugin_file, $plugin_data, $context );
393
394                 $class = $is_active ? 'active' : 'inactive';
395                 $checkbox_id =  "checkbox_" . md5($plugin_data['Name']);
396                 $checkbox = in_array( $status, array( 'mustuse', 'dropins' ) ) ? '' : "<input type='checkbox' name='checked[]' value='" . esc_attr( $plugin_file ) . "' id='" . $checkbox_id . "' /><label class='screen-reader-text' for='" . $checkbox_id . "' >" . __('Select') . " " . $plugin_data['Name'] . "</label>";
397                 if ( 'dropins' != $context ) {
398                         $description = '<p>' . ( $plugin_data['Description'] ? $plugin_data['Description'] : '&nbsp;' ) . '</p>';
399                         $plugin_name = $plugin_data['Name'];
400                 }
401
402                 $id = sanitize_title( $plugin_name );
403
404                 echo "<tr id='$id' class='$class'>";
405
406                 list( $columns, $hidden ) = $this->get_column_info();
407
408                 foreach ( $columns as $column_name => $column_display_name ) {
409                         $style = '';
410                         if ( in_array( $column_name, $hidden ) )
411                                 $style = ' style="display:none;"';
412
413                         switch ( $column_name ) {
414                                 case 'cb':
415                                         echo "<th scope='row' class='check-column'>$checkbox</th>";
416                                         break;
417                                 case 'name':
418                                         echo "<td class='plugin-title'$style><strong>$plugin_name</strong>";
419                                         echo $this->row_actions( $actions, true );
420                                         echo "</td>";
421                                         break;
422                                 case 'description':
423                                         echo "<td class='column-description desc'$style>
424                                                 <div class='plugin-description'>$description</div>
425                                                 <div class='$class second plugin-version-author-uri'>";
426
427                                         $plugin_meta = array();
428                                         if ( !empty( $plugin_data['Version'] ) )
429                                                 $plugin_meta[] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
430                                         if ( !empty( $plugin_data['Author'] ) ) {
431                                                 $author = $plugin_data['Author'];
432                                                 if ( !empty( $plugin_data['AuthorURI'] ) )
433                                                         $author = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
434                                                 $plugin_meta[] = sprintf( __( 'By %s' ), $author );
435                                         }
436                                         if ( ! empty( $plugin_data['PluginURI'] ) )
437                                                 $plugin_meta[] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin site' ) . '">' . __( 'Visit plugin site' ) . '</a>';
438
439                                         $plugin_meta = apply_filters( 'plugin_row_meta', $plugin_meta, $plugin_file, $plugin_data, $status );
440                                         echo implode( ' | ', $plugin_meta );
441
442                                         echo "</div></td>";
443                                         break;
444                                 default:
445                                         echo "<td class='$column_name column-$column_name'$style>";
446                                         do_action( 'manage_plugins_custom_column', $column_name, $plugin_file, $plugin_data );
447                                         echo "</td>";
448                         }
449                 }
450
451                 echo "</tr>";
452
453                 do_action( 'after_plugin_row', $plugin_file, $plugin_data, $status );
454                 do_action( "after_plugin_row_$plugin_file", $plugin_file, $plugin_data, $status );
455         }
456 }
457
458 ?>