3 * Plugins List Table class.
6 * @subpackage List_Table
10 class WP_Plugins_List_Table extends WP_List_Table {
18 * @see WP_List_Table::__construct() for more information on default arguments.
20 * @param array $args An associative array of arguments.
22 public function __construct( $args = array() ) {
23 global $status, $page;
25 parent::__construct( array(
26 'plural' => 'plugins',
27 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
31 if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search' ) ) )
32 $status = $_REQUEST['plugin_status'];
34 if ( isset($_REQUEST['s']) )
35 $_SERVER['REQUEST_URI'] = add_query_arg('s', wp_unslash($_REQUEST['s']) );
37 $page = $this->get_pagenum();
40 protected function get_table_classes() {
41 return array( 'widefat', $this->_args['plural'] );
44 public function ajax_user_can() {
45 return current_user_can('activate_plugins');
48 public function prepare_items() {
49 global $status, $plugins, $totals, $page, $orderby, $order, $s;
51 wp_reset_vars( array( 'orderby', 'order', 's' ) );
54 * Filter the full array of plugins to list in the Plugins list table.
60 * @param array $plugins An array of plugins to display in the list table.
63 'all' => apply_filters( 'all_plugins', get_plugins() ),
66 'inactive' => array(),
67 'recently_activated' => array(),
73 $screen = $this->screen;
75 if ( ! is_multisite() || ( $screen->in_admin( 'network' ) && current_user_can( 'manage_network_plugins' ) ) ) {
78 * Filter whether to display the advanced plugins list table.
80 * There are two types of advanced plugins - must-use and drop-ins -
81 * which can be used in a single site or Multisite network.
83 * The $type parameter allows you to differentiate between the type of advanced
84 * plugins to filter the display of. Contexts include 'mustuse' and 'dropins'.
88 * @param bool $show Whether to show the advanced plugins for the specified
89 * plugin type. Default true.
90 * @param string $type The plugin type. Accepts 'mustuse', 'dropins'.
92 if ( apply_filters( 'show_advanced_plugins', true, 'mustuse' ) ) {
93 $plugins['mustuse'] = get_mu_plugins();
96 /** This action is documented in wp-admin/includes/class-wp-plugins-list-table.php */
97 if ( apply_filters( 'show_advanced_plugins', true, 'dropins' ) )
98 $plugins['dropins'] = get_dropins();
100 if ( current_user_can( 'update_plugins' ) ) {
101 $current = get_site_transient( 'update_plugins' );
102 foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
103 if ( isset( $current->response[ $plugin_file ] ) ) {
104 $plugins['all'][ $plugin_file ]['update'] = true;
105 $plugins['upgrade'][ $plugin_file ] = $plugins['all'][ $plugin_file ];
111 set_transient( 'plugin_slugs', array_keys( $plugins['all'] ), DAY_IN_SECONDS );
113 if ( ! $screen->in_admin( 'network' ) ) {
114 $recently_activated = get_option( 'recently_activated', array() );
116 foreach ( $recently_activated as $key => $time )
117 if ( $time + WEEK_IN_SECONDS < time() )
118 unset( $recently_activated[$key] );
119 update_option( 'recently_activated', $recently_activated );
122 $plugin_info = get_site_transient( 'update_plugins' );
124 foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
125 // Extra info if known. array_merge() ensures $plugin_data has precedence if keys collide.
126 if ( isset( $plugin_info->response[ $plugin_file ] ) ) {
127 $plugins['all'][ $plugin_file ] = $plugin_data = array_merge( (array) $plugin_info->response[ $plugin_file ], $plugin_data );
128 } elseif ( isset( $plugin_info->no_update[ $plugin_file ] ) ) {
129 $plugins['all'][ $plugin_file ] = $plugin_data = array_merge( (array) $plugin_info->no_update[ $plugin_file ], $plugin_data );
132 // Filter into individual sections
133 if ( is_multisite() && ! $screen->in_admin( 'network' ) && is_network_only_plugin( $plugin_file ) && ! is_plugin_active( $plugin_file ) ) {
134 // On the non-network screen, filter out network-only plugins as long as they're not individually activated
135 unset( $plugins['all'][ $plugin_file ] );
136 } elseif ( ! $screen->in_admin( 'network' ) && is_plugin_active_for_network( $plugin_file ) ) {
137 // On the non-network screen, filter out network activated plugins
138 unset( $plugins['all'][ $plugin_file ] );
139 } elseif ( ( ! $screen->in_admin( 'network' ) && is_plugin_active( $plugin_file ) )
140 || ( $screen->in_admin( 'network' ) && is_plugin_active_for_network( $plugin_file ) ) ) {
141 // On the non-network screen, populate the active list with plugins that are individually activated
142 // On the network-admin screen, populate the active list with plugins that are network activated
143 $plugins['active'][ $plugin_file ] = $plugin_data;
145 if ( ! $screen->in_admin( 'network' ) && isset( $recently_activated[ $plugin_file ] ) ) {
146 // On the non-network screen, populate the recently activated list with plugins that have been recently activated
147 $plugins['recently_activated'][ $plugin_file ] = $plugin_data;
149 // Populate the inactive list with plugins that aren't activated
150 $plugins['inactive'][ $plugin_file ] = $plugin_data;
156 $plugins['search'] = array_filter( $plugins['all'], array( $this, '_search_callback' ) );
160 foreach ( $plugins as $type => $list )
161 $totals[ $type ] = count( $list );
163 if ( empty( $plugins[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
166 $this->items = array();
167 foreach ( $plugins[ $status ] as $plugin_file => $plugin_data ) {
168 // Translate, Don't Apply Markup, Sanitize HTML
169 $this->items[$plugin_file] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true );
172 $total_this_page = $totals[ $status ];
175 $orderby = ucfirst( $orderby );
176 $order = strtoupper( $order );
178 uasort( $this->items, array( $this, '_order_callback' ) );
181 $plugins_per_page = $this->get_items_per_page( str_replace( '-', '_', $screen->id . '_per_page' ), 999 );
183 $start = ( $page - 1 ) * $plugins_per_page;
185 if ( $total_this_page > $plugins_per_page )
186 $this->items = array_slice( $this->items, $start, $plugins_per_page );
188 $this->set_pagination_args( array(
189 'total_items' => $total_this_page,
190 'per_page' => $plugins_per_page,
195 * @staticvar string $term
196 * @param array $plugin
199 public function _search_callback( $plugin ) {
201 if ( is_null( $term ) )
202 $term = wp_unslash( $_REQUEST['s'] );
204 foreach ( $plugin as $value ) {
205 if ( false !== stripos( strip_tags( $value ), $term ) ) {
214 * @global string $orderby
215 * @global string $order
216 * @param array $plugin_a
217 * @param array $plugin_b
220 public function _order_callback( $plugin_a, $plugin_b ) {
221 global $orderby, $order;
223 $a = $plugin_a[$orderby];
224 $b = $plugin_b[$orderby];
229 if ( 'DESC' == $order )
230 return ( $a < $b ) ? 1 : -1;
232 return ( $a < $b ) ? -1 : 1;
235 public function no_items() {
238 if ( !empty( $plugins['all'] ) )
239 _e( 'No plugins found.' );
241 _e( 'You do not appear to have any plugins available at this time.' );
244 public function get_columns() {
248 'cb' => !in_array( $status, array( 'mustuse', 'dropins' ) ) ? '<input type="checkbox" />' : '',
249 'name' => __( 'Plugin' ),
250 'description' => __( 'Description' ),
254 protected function get_sortable_columns() {
258 protected function get_views() {
259 global $totals, $status;
261 $status_links = array();
262 foreach ( $totals as $type => $count ) {
268 $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins' );
271 $text = _n( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', $count );
273 case 'recently_activated':
274 $text = _n( 'Recently Active <span class="count">(%s)</span>', 'Recently Active <span class="count">(%s)</span>', $count );
277 $text = _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', $count );
280 $text = _n( 'Must-Use <span class="count">(%s)</span>', 'Must-Use <span class="count">(%s)</span>', $count );
283 $text = _n( 'Drop-ins <span class="count">(%s)</span>', 'Drop-ins <span class="count">(%s)</span>', $count );
286 $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
290 if ( 'search' != $type ) {
291 $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
292 add_query_arg('plugin_status', $type, 'plugins.php'),
293 ( $type == $status ) ? ' class="current"' : '',
294 sprintf( $text, number_format_i18n( $count ) )
299 return $status_links;
302 protected function get_bulk_actions() {
307 if ( 'active' != $status )
308 $actions['activate-selected'] = $this->screen->in_admin( 'network' ) ? __( 'Network Activate' ) : __( 'Activate' );
310 if ( 'inactive' != $status && 'recent' != $status )
311 $actions['deactivate-selected'] = $this->screen->in_admin( 'network' ) ? __( 'Network Deactivate' ) : __( 'Deactivate' );
313 if ( !is_multisite() || $this->screen->in_admin( 'network' ) ) {
314 if ( current_user_can( 'update_plugins' ) )
315 $actions['update-selected'] = __( 'Update' );
316 if ( current_user_can( 'delete_plugins' ) && ( 'active' != $status ) )
317 $actions['delete-selected'] = __( 'Delete' );
324 * @global string $status
325 * @param string $which
328 public function bulk_actions( $which = '' ) {
331 if ( in_array( $status, array( 'mustuse', 'dropins' ) ) )
334 parent::bulk_actions( $which );
338 * @global string $status
339 * @param string $which
342 protected function extra_tablenav( $which ) {
345 if ( ! in_array($status, array('recently_activated', 'mustuse', 'dropins') ) )
348 echo '<div class="alignleft actions">';
350 if ( ! $this->screen->in_admin( 'network' ) && 'recently_activated' == $status )
351 submit_button( __( 'Clear List' ), 'button', 'clear-recent-list', false );
352 elseif ( 'top' == $which && 'mustuse' == $status )
353 echo '<p>' . sprintf( __( 'Files in the <code>%s</code> directory are executed automatically.' ), str_replace( ABSPATH, '/', WPMU_PLUGIN_DIR ) ) . '</p>';
354 elseif ( 'top' == $which && 'dropins' == $status )
355 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>';
360 public function current_action() {
361 if ( isset($_POST['clear-recent-list']) )
362 return 'clear-recent-list';
364 return parent::current_action();
367 public function display_rows() {
370 if ( is_multisite() && ! $this->screen->in_admin( 'network' ) && in_array( $status, array( 'mustuse', 'dropins' ) ) )
373 foreach ( $this->items as $plugin_file => $plugin_data )
374 $this->single_row( array( $plugin_file, $plugin_data ) );
378 * @global string $status
381 * @global array $totals
384 public function single_row( $item ) {
385 global $status, $page, $s, $totals;
387 list( $plugin_file, $plugin_data ) = $item;
389 $screen = $this->screen;
400 if ( 'mustuse' == $context ) {
402 } elseif ( 'dropins' == $context ) {
403 $dropins = _get_dropins();
404 $plugin_name = $plugin_file;
405 if ( $plugin_file != $plugin_data['Name'] )
406 $plugin_name .= '<br/>' . $plugin_data['Name'];
407 if ( true === ( $dropins[ $plugin_file ][1] ) ) { // Doesn't require a constant
409 $description = '<p><strong>' . $dropins[ $plugin_file ][0] . '</strong></p>';
410 } elseif ( defined( $dropins[ $plugin_file ][1] ) && constant( $dropins[ $plugin_file ][1] ) ) { // Constant is true
412 $description = '<p><strong>' . $dropins[ $plugin_file ][0] . '</strong></p>';
415 $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>';
417 if ( $plugin_data['Description'] )
418 $description .= '<p>' . $plugin_data['Description'] . '</p>';
420 if ( $screen->in_admin( 'network' ) )
421 $is_active = is_plugin_active_for_network( $plugin_file );
423 $is_active = is_plugin_active( $plugin_file );
425 if ( $screen->in_admin( 'network' ) ) {
427 if ( current_user_can( 'manage_network_plugins' ) )
428 $actions['deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&plugin=' . $plugin_file . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Network Deactivate') . '</a>';
430 if ( current_user_can( 'manage_network_plugins' ) )
431 $actions['activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&plugin=' . $plugin_file . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin for all sites in this network') . '" class="edit">' . __('Network Activate') . '</a>';
432 if ( current_user_can( 'delete_plugins' ) && ! is_plugin_active( $plugin_file ) )
433 $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&checked[]=' . $plugin_file . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
437 $actions['deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&plugin=' . $plugin_file . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Deactivate') . '</a>';
439 $actions['activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&plugin=' . $plugin_file . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" class="edit">' . __('Activate') . '</a>';
441 if ( ! is_multisite() && current_user_can('delete_plugins') )
442 $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&checked[]=' . $plugin_file . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
443 } // end if $is_active
445 } // end if $screen->in_admin( 'network' )
447 if ( ( ! is_multisite() || $screen->in_admin( 'network' ) ) && current_user_can('edit_plugins') && is_writable(WP_PLUGIN_DIR . '/' . $plugin_file) )
448 $actions['edit'] = '<a href="plugin-editor.php?file=' . $plugin_file . '" title="' . esc_attr__('Open this file in the Plugin Editor') . '" class="edit">' . __('Edit') . '</a>';
451 $prefix = $screen->in_admin( 'network' ) ? 'network_admin_' : '';
454 * Filter the action links displayed for each plugin in the Plugins list table.
456 * The dynamic portion of the hook name, `$prefix`, refers to the context the
457 * action links are displayed in. The 'network_admin_' prefix is used if the
458 * current screen is the Network plugins list table. The prefix is empty ('')
459 * if the current screen is the site plugins list table.
461 * The default action links for the Network plugins list table include
462 * 'Network Activate', 'Network Deactivate', 'Edit', and 'Delete'.
464 * The default action links for the site plugins list table include
465 * 'Activate', 'Deactivate', and 'Edit', for a network site, and
466 * 'Activate', 'Deactivate', 'Edit', and 'Delete' for a single site.
470 * @param array $actions An array of plugin action links.
471 * @param string $plugin_file Path to the plugin file.
472 * @param array $plugin_data An array of plugin data.
473 * @param string $context The plugin context. Defaults are 'All', 'Active',
474 * 'Inactive', 'Recently Activated', 'Upgrade',
475 * 'Must-Use', 'Drop-ins', 'Search'.
477 $actions = apply_filters( $prefix . 'plugin_action_links', array_filter( $actions ), $plugin_file, $plugin_data, $context );
480 * Filter the list of action links displayed for a specific plugin.
482 * The first dynamic portion of the hook name, $prefix, refers to the context
483 * the action links are displayed in. The 'network_admin_' prefix is used if the
484 * current screen is the Network plugins list table. The prefix is empty ('')
485 * if the current screen is the site plugins list table.
487 * The second dynamic portion of the hook name, $plugin_file, refers to the path
488 * to the plugin file, relative to the plugins directory.
492 * @param array $actions An array of plugin action links.
493 * @param string $plugin_file Path to the plugin file.
494 * @param array $plugin_data An array of plugin data.
495 * @param string $context The plugin context. Defaults are 'All', 'Active',
496 * 'Inactive', 'Recently Activated', 'Upgrade',
497 * 'Must-Use', 'Drop-ins', 'Search'.
499 $actions = apply_filters( $prefix . "plugin_action_links_$plugin_file", $actions, $plugin_file, $plugin_data, $context );
501 $class = $is_active ? 'active' : 'inactive';
502 $checkbox_id = "checkbox_" . md5($plugin_data['Name']);
503 if ( in_array( $status, array( 'mustuse', 'dropins' ) ) ) {
506 $checkbox = "<label class='screen-reader-text' for='" . $checkbox_id . "' >" . sprintf( __( 'Select %s' ), $plugin_data['Name'] ) . "</label>"
507 . "<input type='checkbox' name='checked[]' value='" . esc_attr( $plugin_file ) . "' id='" . $checkbox_id . "' />";
509 if ( 'dropins' != $context ) {
510 $description = '<p>' . ( $plugin_data['Description'] ? $plugin_data['Description'] : ' ' ) . '</p>';
511 $plugin_name = $plugin_data['Name'];
514 $id = sanitize_title( $plugin_name );
515 if ( ! empty( $totals['upgrade'] ) && ! empty( $plugin_data['update'] ) )
518 echo "<tr id='$id' class='$class'>";
520 list( $columns, $hidden ) = $this->get_column_info();
522 foreach ( $columns as $column_name => $column_display_name ) {
524 if ( in_array( $column_name, $hidden ) )
525 $style = ' style="display:none;"';
527 switch ( $column_name ) {
529 echo "<th scope='row' class='check-column'>$checkbox</th>";
532 echo "<td class='plugin-title'$style><strong>$plugin_name</strong>";
533 echo $this->row_actions( $actions, true );
537 echo "<td class='column-description desc'$style>
538 <div class='plugin-description'>$description</div>
539 <div class='$class second plugin-version-author-uri'>";
541 $plugin_meta = array();
542 if ( !empty( $plugin_data['Version'] ) )
543 $plugin_meta[] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
544 if ( !empty( $plugin_data['Author'] ) ) {
545 $author = $plugin_data['Author'];
546 if ( !empty( $plugin_data['AuthorURI'] ) )
547 $author = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
548 $plugin_meta[] = sprintf( __( 'By %s' ), $author );
551 // Details link using API info, if available
552 if ( isset( $plugin_data['slug'] ) && current_user_can( 'install_plugins' ) ) {
553 $plugin_meta[] = sprintf( '<a href="%s" class="thickbox" aria-label="%s" data-title="%s">%s</a>',
554 esc_url( network_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_data['slug'] .
555 '&TB_iframe=true&width=600&height=550' ) ),
556 esc_attr( sprintf( __( 'More information about %s' ), $plugin_name ) ),
557 esc_attr( $plugin_name ),
560 } elseif ( ! empty( $plugin_data['PluginURI'] ) ) {
561 $plugin_meta[] = sprintf( '<a href="%s">%s</a>',
562 esc_url( $plugin_data['PluginURI'] ),
563 __( 'Visit plugin site' )
568 * Filter the array of row meta for each plugin in the Plugins list table.
572 * @param array $plugin_meta An array of the plugin's metadata,
573 * including the version, author,
574 * author URI, and plugin URI.
575 * @param string $plugin_file Path to the plugin file, relative to the plugins directory.
576 * @param array $plugin_data An array of plugin data.
577 * @param string $status Status of the plugin. Defaults are 'All', 'Active',
578 * 'Inactive', 'Recently Activated', 'Upgrade', 'Must-Use',
579 * 'Drop-ins', 'Search'.
581 $plugin_meta = apply_filters( 'plugin_row_meta', $plugin_meta, $plugin_file, $plugin_data, $status );
582 echo implode( ' | ', $plugin_meta );
587 echo "<td class='$column_name column-$column_name'$style>";
590 * Fires inside each custom column of the Plugins list table.
594 * @param string $column_name Name of the column.
595 * @param string $plugin_file Path to the plugin file.
596 * @param array $plugin_data An array of plugin data.
598 do_action( 'manage_plugins_custom_column', $column_name, $plugin_file, $plugin_data );
606 * Fires after each row in the Plugins list table.
610 * @param string $plugin_file Path to the plugin file, relative to the plugins directory.
611 * @param array $plugin_data An array of plugin data.
612 * @param string $status Status of the plugin. Defaults are 'All', 'Active',
613 * 'Inactive', 'Recently Activated', 'Upgrade', 'Must-Use',
614 * 'Drop-ins', 'Search'.
616 do_action( 'after_plugin_row', $plugin_file, $plugin_data, $status );
619 * Fires after each specific row in the Plugins list table.
621 * The dynamic portion of the hook name, `$plugin_file`, refers to the path
622 * to the plugin file, relative to the plugins directory.
626 * @param string $plugin_file Path to the plugin file, relative to the plugins directory.
627 * @param array $plugin_data An array of plugin data.
628 * @param string $status Status of the plugin. Defaults are 'All', 'Active',
629 * 'Inactive', 'Recently Activated', 'Upgrade', 'Must-Use',
630 * 'Drop-ins', 'Search'.
632 do_action( "after_plugin_row_$plugin_file", $plugin_file, $plugin_data, $status );