3 * Plugin Installer List Table class.
6 * @subpackage List_Table
10 class WP_Plugin_Install_List_Table extends WP_List_Table {
14 var $groups = array();
16 public function ajax_user_can() {
17 return current_user_can('install_plugins');
21 * Return a list of slugs of installed plugins, if known.
23 * Uses the transient data from the updates API to determine the slugs of
24 * known installed plugins. This might be better elsewhere, perhaps even
25 * within get_plugins().
30 protected function get_installed_plugin_slugs() {
33 $plugin_info = get_site_transient( 'update_plugins' );
34 if ( isset( $plugin_info->no_update ) ) {
35 foreach ( $plugin_info->no_update as $plugin ) {
36 $slugs[] = $plugin->slug;
40 if ( isset( $plugin_info->response ) ) {
41 foreach ( $plugin_info->response as $plugin ) {
42 $slugs[] = $plugin->slug;
49 public function prepare_items() {
50 include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
52 global $tabs, $tab, $paged, $type, $term;
54 wp_reset_vars( array( 'tab' ) );
56 $paged = $this->get_pagenum();
60 // These are the tabs which are shown on the page
63 if ( 'search' == $tab )
64 $tabs['search'] = __( 'Search Results' );
65 $tabs['featured'] = _x( 'Featured', 'Plugin Installer' );
66 $tabs['popular'] = _x( 'Popular', 'Plugin Installer' );
67 $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
68 if ( $tab === 'beta' || false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
69 $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
71 if ( current_user_can( 'upload_plugins' ) ) {
72 // No longer a real tab. Here for filter compatibility.
73 // Gets skipped in get_views().
74 $tabs['upload'] = __( 'Upload Plugin' );
77 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
80 * Filter the tabs shown on the Plugin Install screen.
84 * @param array $tabs The tabs shown on the Plugin Install screen. Defaults are 'dashboard', 'search',
85 * 'upload', 'featured', 'popular', 'new', and 'favorites'.
87 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
90 * Filter tabs not associated with a menu item on the Plugin Install screen.
94 * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
96 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
98 // If a non-valid menu tab has been selected, And it's not a non-menu action.
99 if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
104 'per_page' => $per_page,
105 'fields' => array( 'last_updated' => true, 'downloaded' => true, 'icons' => true ),
106 // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
107 'locale' => get_locale(),
108 'installed_plugins' => $this->get_installed_plugin_slugs(),
113 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
114 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
118 $args['tag'] = sanitize_title_with_dashes( $term );
121 $args['search'] = $term;
124 $args['author'] = $term;
131 $args['fields']['group'] = true;
132 $this->orderby = 'group';
137 $args['browse'] = $tab;
141 $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
142 update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
144 $args['user'] = $user;
148 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
157 * Filter API request arguments for each Plugin Install screen tab.
159 * The dynamic portion of the hook name, $tab, refers to the plugin install tabs.
160 * Default tabs are 'dashboard', 'search', 'upload', 'featured', 'popular', 'new',
165 * @param array|bool $args Plugin Install API arguments.
167 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
172 $api = plugins_api( 'query_plugins', $args );
174 if ( is_wp_error( $api ) ) {
179 $this->items = $api->plugins;
181 if ( $this->orderby ) {
182 uasort( $this->items, array( $this, 'order_callback' ) );
185 $this->set_pagination_args( array(
186 'total_items' => $api->info['results'],
187 'per_page' => $args['per_page'],
190 if ( isset( $api->info['groups'] ) ) {
191 $this->groups = $api->info['groups'];
195 public function no_items() {
196 if ( isset( $this->error ) ) {
197 $message = $this->error->get_error_message() . '<p class="hide-if-no-js"><a href="#" class="button" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a></p>';
199 $message = __( 'No plugins match your request.' );
201 echo '<div class="no-plugin-results">' . $message . '</div>';
204 protected function get_views() {
207 $display_tabs = array();
208 foreach ( (array) $tabs as $action => $text ) {
209 $class = ( $action == $tab ) ? ' current' : '';
210 $href = self_admin_url('plugin-install.php?tab=' . $action);
211 $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
213 // No longer a real tab.
214 unset( $display_tabs['plugin-install-upload'] );
216 return $display_tabs;
220 * Override parent views so we can use the filter bar display.
222 public function views() {
223 $views = $this->get_views();
225 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
226 $views = apply_filters( "views_{$this->screen->id}", $views );
229 <div class="wp-filter">
230 <ul class="filter-links">
232 if ( ! empty( $views ) ) {
233 foreach ( $views as $class => $view ) {
234 $views[ $class ] = "\t<li class='$class'>$view";
236 echo implode( " </li>\n", $views ) . "</li>\n";
241 <?php install_search_form( isset( $views['plugin-install-search'] ) ); ?>
247 * Override the parent display() so we can provide a different container.
249 public function display() {
250 $singular = $this->_args['singular'];
255 $data_attr = " data-wp-lists='list:$singular'";
258 $this->display_tablenav( 'top' );
261 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
263 <div id="the-list"<?php echo $data_attr; ?>>
264 <?php $this->display_rows_or_placeholder(); ?>
268 $this->display_tablenav( 'bottom' );
271 protected function display_tablenav( $which ) {
272 if ( $GLOBALS['tab'] === 'featured' ) {
276 if ( 'top' == $which ) { ?>
277 <div class="tablenav top">
278 <div class="alignleft actions">
281 * Fires before the Plugin Install table header pagination is displayed.
285 do_action( 'install_plugins_table_header' ); ?>
287 <?php $this->pagination( $which ); ?>
291 <div class="tablenav bottom">
292 <?php $this->pagination( $which ); ?>
299 protected function get_table_classes() {
300 return array( 'widefat', $this->_args['plural'] );
303 public function get_columns() {
307 private function order_callback( $plugin_a, $plugin_b ) {
308 $orderby = $this->orderby;
309 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
313 $a = $plugin_a->$orderby;
314 $b = $plugin_b->$orderby;
320 if ( 'DESC' == $this->order ) {
321 return ( $a < $b ) ? 1 : -1;
323 return ( $a < $b ) ? -1 : 1;
327 public function display_rows() {
328 $plugins_allowedtags = array(
329 'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
330 'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
331 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
332 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
335 $plugins_group_titles = array(
336 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
337 'Social' => _x( 'Social', 'Plugin installer group title' ),
338 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
343 foreach ( (array) $this->items as $plugin ) {
344 if ( is_object( $plugin ) ) {
345 $plugin = (array) $plugin;
348 // Display the group heading if there is one
349 if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
350 if ( isset( $this->groups[ $plugin['group'] ] ) ) {
351 $group_name = $this->groups[ $plugin['group'] ];
352 if ( isset( $plugins_group_titles[ $group_name ] ) ) {
353 $group_name = $plugins_group_titles[ $group_name ];
356 $group_name = $plugin['group'];
359 // Starting a new group, close off the divs of the last one
360 if ( ! empty( $group ) ) {
364 echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
365 // needs an extra wrapping div for nth-child selectors to work
366 echo '<div class="plugin-items">';
368 $group = $plugin['group'];
370 $title = wp_kses( $plugin['name'], $plugins_allowedtags );
372 // Remove any HTML from the description.
373 $description = strip_tags( $plugin['short_description'] );
374 $version = wp_kses( $plugin['version'], $plugins_allowedtags );
376 $name = strip_tags( $title . ' ' . $version );
378 $author = wp_kses( $plugin['author'], $plugins_allowedtags );
379 if ( ! empty( $author ) ) {
380 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
383 $action_links = array();
385 if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
386 $status = install_plugin_install_status( $plugin );
388 switch ( $status['status'] ) {
390 if ( $status['url'] ) {
391 /* translators: 1: Plugin name and version. */
392 $action_links[] = '<a class="install-now button" href="' . $status['url'] . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now' ), $name ) ) . '">' . __( 'Install Now' ) . '</a>';
396 case 'update_available':
397 if ( $status['url'] ) {
398 /* translators: 1: Plugin name and version */
399 $action_links[] = '<a class="button" href="' . $status['url'] . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now' ), $name ) ) . '">' . __( 'Update Now' ) . '</a>';
403 case 'latest_installed':
404 case 'newer_installed':
405 $action_links[] = '<span class="button button-disabled" title="' . esc_attr__( 'This plugin is already installed and is up to date' ) . ' ">' . _x( 'Installed', 'plugin' ) . '</span>';
410 $details_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
411 '&TB_iframe=true&width=600&height=550' );
413 /* translators: 1: Plugin name and version. */
414 $action_links[] = '<a href="' . esc_url( $details_link ) . '" class="thickbox" aria-label="' . esc_attr( sprintf( __( 'More information about %s' ), $name ) ) . '" data-title="' . esc_attr( $name ) . '">' . __( 'More Details' ) . '</a>';
416 if ( !empty( $plugin['icons']['svg'] ) ) {
417 $plugin_icon_url = $plugin['icons']['svg'];
418 } elseif ( !empty( $plugin['icons']['2x'] ) ) {
419 $plugin_icon_url = $plugin['icons']['2x'];
420 } elseif ( !empty( $plugin['icons']['1x'] ) ) {
421 $plugin_icon_url = $plugin['icons']['1x'];
423 $plugin_icon_url = $plugin['icons']['default'];
427 * Filter the install action links for a plugin.
431 * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
432 * @param array $plugin The plugin currently being listed.
434 $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
436 <div class="plugin-card">
437 <div class="plugin-card-top">
438 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox plugin-icon"><img src="<?php echo esc_attr( $plugin_icon_url ) ?>" /></a>
439 <div class="name column-name">
440 <h4><a href="<?php echo esc_url( $details_link ); ?>" class="thickbox"><?php echo $title; ?></a></h4>
442 <div class="action-links">
444 if ( $action_links ) {
445 echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
449 <div class="desc column-description">
450 <p><?php echo $description; ?></p>
451 <p class="authors"><?php echo $author; ?></p>
454 <div class="plugin-card-bottom">
455 <div class="vers column-rating">
456 <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
457 <span class="num-ratings">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
459 <div class="column-updated">
460 <strong><?php _e( 'Last Updated:' ); ?></strong> <span title="<?php echo esc_attr( $plugin['last_updated'] ); ?>">
461 <?php printf( __( '%s ago' ), human_time_diff( strtotime( $plugin['last_updated'] ) ) ); ?>
464 <div class="column-downloaded">
465 <?php echo sprintf( _n( '%s download', '%s downloads', $plugin['downloaded'] ), number_format_i18n( $plugin['downloaded'] ) ); ?>
467 <div class="column-compatibility">
469 if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
470 echo '<span class="compatibility-untested">' . __( '<strong>Untested</strong> with your version of WordPress' ) . '</span>';
471 } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
472 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
474 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
483 // Close off the group divs of the last one
484 if ( ! empty( $group ) ) {