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['recommended'] = _x( 'Recommended', 'Plugin Installer' );
68 $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
69 if ( $tab === 'beta' || false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
70 $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
72 if ( current_user_can( 'upload_plugins' ) ) {
73 // No longer a real tab. Here for filter compatibility.
74 // Gets skipped in get_views().
75 $tabs['upload'] = __( 'Upload Plugin' );
78 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
81 * Filter the tabs shown on the Plugin Install screen.
85 * @param array $tabs The tabs shown on the Plugin Install screen. Defaults are 'dashboard', 'search',
86 * 'upload', 'featured', 'popular', 'new', and 'favorites'.
88 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
91 * Filter tabs not associated with a menu item on the Plugin Install screen.
95 * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
97 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
99 // If a non-valid menu tab has been selected, And it's not a non-menu action.
100 if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
105 'per_page' => $per_page,
106 'fields' => array( 'last_updated' => true, 'downloaded' => true, 'icons' => true ),
107 // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
108 'locale' => get_locale(),
109 'installed_plugins' => $this->get_installed_plugin_slugs(),
114 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
115 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
119 $args['tag'] = sanitize_title_with_dashes( $term );
122 $args['search'] = $term;
125 $args['author'] = $term;
132 $args['fields']['group'] = true;
133 $this->orderby = 'group';
139 $args['browse'] = $tab;
143 $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
144 update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
146 $args['user'] = $user;
150 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
159 * Filter API request arguments for each Plugin Install screen tab.
161 * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
162 * Default tabs are 'dashboard', 'search', 'upload', 'featured', 'popular', 'new',
167 * @param array|bool $args Plugin Install API arguments.
169 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
174 $api = plugins_api( 'query_plugins', $args );
176 if ( is_wp_error( $api ) ) {
181 $this->items = $api->plugins;
183 if ( $this->orderby ) {
184 uasort( $this->items, array( $this, 'order_callback' ) );
187 $this->set_pagination_args( array(
188 'total_items' => $api->info['results'],
189 'per_page' => $args['per_page'],
192 if ( isset( $api->info['groups'] ) ) {
193 $this->groups = $api->info['groups'];
197 public function no_items() {
198 if ( isset( $this->error ) ) {
199 $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>';
201 $message = __( 'No plugins match your request.' );
203 echo '<div class="no-plugin-results">' . $message . '</div>';
206 protected function get_views() {
209 $display_tabs = array();
210 foreach ( (array) $tabs as $action => $text ) {
211 $class = ( $action == $tab ) ? ' current' : '';
212 $href = self_admin_url('plugin-install.php?tab=' . $action);
213 $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
215 // No longer a real tab.
216 unset( $display_tabs['plugin-install-upload'] );
218 return $display_tabs;
222 * Override parent views so we can use the filter bar display.
224 public function views() {
225 $views = $this->get_views();
227 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
228 $views = apply_filters( "views_{$this->screen->id}", $views );
231 <div class="wp-filter">
232 <ul class="filter-links">
234 if ( ! empty( $views ) ) {
235 foreach ( $views as $class => $view ) {
236 $views[ $class ] = "\t<li class='$class'>$view";
238 echo implode( " </li>\n", $views ) . "</li>\n";
243 <?php install_search_form( isset( $views['plugin-install-search'] ) ); ?>
249 * Override the parent display() so we can provide a different container.
251 public function display() {
252 $singular = $this->_args['singular'];
257 $data_attr = " data-wp-lists='list:$singular'";
260 $this->display_tablenav( 'top' );
263 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
265 <div id="the-list"<?php echo $data_attr; ?>>
266 <?php $this->display_rows_or_placeholder(); ?>
270 $this->display_tablenav( 'bottom' );
274 * @param string $which
276 protected function display_tablenav( $which ) {
277 if ( $GLOBALS['tab'] === 'featured' ) {
281 if ( 'top' == $which ) {
284 <div class="tablenav top">
285 <div class="alignleft actions">
288 * Fires before the Plugin Install table header pagination is displayed.
292 do_action( 'install_plugins_table_header' ); ?>
294 <?php $this->pagination( $which ); ?>
298 <div class="tablenav bottom">
299 <?php $this->pagination( $which ); ?>
306 protected function get_table_classes() {
307 return array( 'widefat', $this->_args['plural'] );
310 public function get_columns() {
315 * @param object $plugin_a
316 * @param object $plugin_b
319 private function order_callback( $plugin_a, $plugin_b ) {
320 $orderby = $this->orderby;
321 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
325 $a = $plugin_a->$orderby;
326 $b = $plugin_b->$orderby;
332 if ( 'DESC' == $this->order ) {
333 return ( $a < $b ) ? 1 : -1;
335 return ( $a < $b ) ? -1 : 1;
339 public function display_rows() {
340 $plugins_allowedtags = array(
341 'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
342 'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
343 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
344 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
347 $plugins_group_titles = array(
348 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
349 'Social' => _x( 'Social', 'Plugin installer group title' ),
350 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
355 foreach ( (array) $this->items as $plugin ) {
356 if ( is_object( $plugin ) ) {
357 $plugin = (array) $plugin;
360 // Display the group heading if there is one
361 if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
362 if ( isset( $this->groups[ $plugin['group'] ] ) ) {
363 $group_name = $this->groups[ $plugin['group'] ];
364 if ( isset( $plugins_group_titles[ $group_name ] ) ) {
365 $group_name = $plugins_group_titles[ $group_name ];
368 $group_name = $plugin['group'];
371 // Starting a new group, close off the divs of the last one
372 if ( ! empty( $group ) ) {
376 echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
377 // needs an extra wrapping div for nth-child selectors to work
378 echo '<div class="plugin-items">';
380 $group = $plugin['group'];
382 $title = wp_kses( $plugin['name'], $plugins_allowedtags );
384 // Remove any HTML from the description.
385 $description = strip_tags( $plugin['short_description'] );
386 $version = wp_kses( $plugin['version'], $plugins_allowedtags );
388 $name = strip_tags( $title . ' ' . $version );
390 $author = wp_kses( $plugin['author'], $plugins_allowedtags );
391 if ( ! empty( $author ) ) {
392 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
395 $action_links = array();
397 if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
398 $status = install_plugin_install_status( $plugin );
400 switch ( $status['status'] ) {
402 if ( $status['url'] ) {
403 /* translators: 1: Plugin name and version. */
404 $action_links[] = '<a class="install-now button" href="' . $status['url'] . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now' ), $name ) ) . '">' . __( 'Install Now' ) . '</a>';
408 case 'update_available':
409 if ( $status['url'] ) {
410 /* translators: 1: Plugin name and version */
411 $action_links[] = '<a class="button" href="' . $status['url'] . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now' ), $name ) ) . '">' . __( 'Update Now' ) . '</a>';
415 case 'latest_installed':
416 case 'newer_installed':
417 $action_links[] = '<span class="button button-disabled" title="' . esc_attr__( 'This plugin is already installed and is up to date' ) . ' ">' . _x( 'Installed', 'plugin' ) . '</span>';
422 $details_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
423 '&TB_iframe=true&width=600&height=550' );
425 /* translators: 1: Plugin name and version. */
426 $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>';
428 if ( !empty( $plugin['icons']['svg'] ) ) {
429 $plugin_icon_url = $plugin['icons']['svg'];
430 } elseif ( !empty( $plugin['icons']['2x'] ) ) {
431 $plugin_icon_url = $plugin['icons']['2x'];
432 } elseif ( !empty( $plugin['icons']['1x'] ) ) {
433 $plugin_icon_url = $plugin['icons']['1x'];
435 $plugin_icon_url = $plugin['icons']['default'];
439 * Filter the install action links for a plugin.
443 * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
444 * @param array $plugin The plugin currently being listed.
446 $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
448 <div class="plugin-card">
449 <div class="plugin-card-top">
450 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox plugin-icon"><img src="<?php echo esc_attr( $plugin_icon_url ) ?>" /></a>
451 <div class="name column-name">
452 <h4><a href="<?php echo esc_url( $details_link ); ?>" class="thickbox"><?php echo $title; ?></a></h4>
454 <div class="action-links">
456 if ( $action_links ) {
457 echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
461 <div class="desc column-description">
462 <p><?php echo $description; ?></p>
463 <p class="authors"><?php echo $author; ?></p>
466 <div class="plugin-card-bottom">
467 <div class="vers column-rating">
468 <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
469 <span class="num-ratings">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
471 <div class="column-updated">
472 <strong><?php _e( 'Last Updated:' ); ?></strong> <span title="<?php echo esc_attr( $plugin['last_updated'] ); ?>">
473 <?php printf( __( '%s ago' ), human_time_diff( strtotime( $plugin['last_updated'] ) ) ); ?>
476 <div class="column-downloaded">
477 <?php echo sprintf( _n( '%s download', '%s downloads', $plugin['downloaded'] ), number_format_i18n( $plugin['downloaded'] ) ); ?>
479 <div class="column-compatibility">
481 if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
482 echo '<span class="compatibility-untested">' . __( 'Untested with your version of WordPress' ) . '</span>';
483 } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
484 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
486 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
495 // Close off the group divs of the last one
496 if ( ! empty( $group ) ) {