3 * List Table API: WP_Plugin_Install_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying plugins to install in a list table.
18 class WP_Plugin_Install_List_Table extends WP_List_Table {
20 public $order = 'ASC';
21 public $orderby = null;
22 public $groups = array();
30 public function ajax_user_can() {
31 return current_user_can('install_plugins');
35 * Return a list of slugs of installed plugins, if known.
37 * Uses the transient data from the updates API to determine the slugs of
38 * known installed plugins. This might be better elsewhere, perhaps even
39 * within get_plugins().
46 protected function get_installed_plugin_slugs() {
49 $plugin_info = get_site_transient( 'update_plugins' );
50 if ( isset( $plugin_info->no_update ) ) {
51 foreach ( $plugin_info->no_update as $plugin ) {
52 $slugs[] = $plugin->slug;
56 if ( isset( $plugin_info->response ) ) {
57 foreach ( $plugin_info->response as $plugin ) {
58 $slugs[] = $plugin->slug;
70 * @global string $type
71 * @global string $term
72 * @global string $wp_version
74 public function prepare_items() {
75 include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
77 global $tabs, $tab, $paged, $type, $term;
79 wp_reset_vars( array( 'tab' ) );
81 $paged = $this->get_pagenum();
85 // These are the tabs which are shown on the page
88 if ( 'search' === $tab ) {
89 $tabs['search'] = __( 'Search Results' );
91 $tabs['featured'] = _x( 'Featured', 'Plugin Installer' );
92 $tabs['popular'] = _x( 'Popular', 'Plugin Installer' );
93 $tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' );
94 $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
95 if ( $tab === 'beta' || false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
96 $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
98 if ( current_user_can( 'upload_plugins' ) ) {
99 // No longer a real tab. Here for filter compatibility.
100 // Gets skipped in get_views().
101 $tabs['upload'] = __( 'Upload Plugin' );
104 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
107 * Filter the tabs shown on the Plugin Install screen.
111 * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
112 * 'recommended', 'favorites', and 'upload'.
114 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
117 * Filter tabs not associated with a menu item on the Plugin Install screen.
121 * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
123 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
125 // If a non-valid menu tab has been selected, And it's not a non-menu action.
126 if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
131 'per_page' => $per_page,
133 'last_updated' => true,
135 'active_installs' => true
137 // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
138 'locale' => get_locale(),
139 'installed_plugins' => $this->get_installed_plugin_slugs(),
144 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
145 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
149 $args['tag'] = sanitize_title_with_dashes( $term );
152 $args['search'] = $term;
155 $args['author'] = $term;
162 $args['fields']['group'] = true;
163 $this->orderby = 'group';
169 $args['browse'] = $tab;
173 $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
174 update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
176 $args['user'] = $user;
180 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
189 * Filter API request arguments for each Plugin Install screen tab.
191 * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
192 * Default tabs include 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
196 * @param array|bool $args Plugin Install API arguments.
198 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
203 $api = plugins_api( 'query_plugins', $args );
205 if ( is_wp_error( $api ) ) {
210 $this->items = $api->plugins;
212 if ( $this->orderby ) {
213 uasort( $this->items, array( $this, 'order_callback' ) );
216 $this->set_pagination_args( array(
217 'total_items' => $api->info['results'],
218 'per_page' => $args['per_page'],
221 if ( isset( $api->info['groups'] ) ) {
222 $this->groups = $api->info['groups'];
229 public function no_items() {
230 if ( isset( $this->error ) ) {
231 $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>';
233 $message = __( 'No plugins match your request.' );
235 echo '<div class="no-plugin-results">' . $message . '</div>';
240 * @global array $tabs
241 * @global string $tab
245 protected function get_views() {
248 $display_tabs = array();
249 foreach ( (array) $tabs as $action => $text ) {
250 $class = ( $action === $tab ) ? ' current' : '';
251 $href = self_admin_url('plugin-install.php?tab=' . $action);
252 $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
254 // No longer a real tab.
255 unset( $display_tabs['plugin-install-upload'] );
257 return $display_tabs;
261 * Override parent views so we can use the filter bar display.
263 public function views() {
264 $views = $this->get_views();
266 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
267 $views = apply_filters( "views_{$this->screen->id}", $views );
269 $this->screen->render_screen_reader_content( 'heading_views' );
271 <div class="wp-filter">
272 <ul class="filter-links">
274 if ( ! empty( $views ) ) {
275 foreach ( $views as $class => $view ) {
276 $views[ $class ] = "\t<li class='$class'>$view";
278 echo implode( " </li>\n", $views ) . "</li>\n";
283 <?php install_search_form( isset( $views['plugin-install-search'] ) ); ?>
289 * Override the parent display() so we can provide a different container.
291 public function display() {
292 $singular = $this->_args['singular'];
297 $data_attr = " data-wp-lists='list:$singular'";
300 $this->display_tablenav( 'top' );
303 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
305 $this->screen->render_screen_reader_content( 'heading_list' );
307 <div id="the-list"<?php echo $data_attr; ?>>
308 <?php $this->display_rows_or_placeholder(); ?>
312 $this->display_tablenav( 'bottom' );
316 * @global string $tab
318 * @param string $which
320 protected function display_tablenav( $which ) {
321 if ( $GLOBALS['tab'] === 'featured' ) {
325 if ( 'top' === $which ) {
328 <div class="tablenav top">
329 <div class="alignleft actions">
332 * Fires before the Plugin Install table header pagination is displayed.
336 do_action( 'install_plugins_table_header' ); ?>
338 <?php $this->pagination( $which ); ?>
342 <div class="tablenav bottom">
343 <?php $this->pagination( $which ); ?>
353 protected function get_table_classes() {
354 return array( 'widefat', $this->_args['plural'] );
360 public function get_columns() {
365 * @param object $plugin_a
366 * @param object $plugin_b
369 private function order_callback( $plugin_a, $plugin_b ) {
370 $orderby = $this->orderby;
371 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
375 $a = $plugin_a->$orderby;
376 $b = $plugin_b->$orderby;
382 if ( 'DESC' === $this->order ) {
383 return ( $a < $b ) ? 1 : -1;
385 return ( $a < $b ) ? -1 : 1;
390 * @global string $wp_version
392 public function display_rows() {
393 $plugins_allowedtags = array(
394 'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
395 'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
396 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
397 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
400 $plugins_group_titles = array(
401 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
402 'Social' => _x( 'Social', 'Plugin installer group title' ),
403 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
408 foreach ( (array) $this->items as $plugin ) {
409 if ( is_object( $plugin ) ) {
410 $plugin = (array) $plugin;
413 // Display the group heading if there is one
414 if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
415 if ( isset( $this->groups[ $plugin['group'] ] ) ) {
416 $group_name = $this->groups[ $plugin['group'] ];
417 if ( isset( $plugins_group_titles[ $group_name ] ) ) {
418 $group_name = $plugins_group_titles[ $group_name ];
421 $group_name = $plugin['group'];
424 // Starting a new group, close off the divs of the last one
425 if ( ! empty( $group ) ) {
429 echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
430 // needs an extra wrapping div for nth-child selectors to work
431 echo '<div class="plugin-items">';
433 $group = $plugin['group'];
435 $title = wp_kses( $plugin['name'], $plugins_allowedtags );
437 // Remove any HTML from the description.
438 $description = strip_tags( $plugin['short_description'] );
439 $version = wp_kses( $plugin['version'], $plugins_allowedtags );
441 $name = strip_tags( $title . ' ' . $version );
443 $author = wp_kses( $plugin['author'], $plugins_allowedtags );
444 if ( ! empty( $author ) ) {
445 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
448 $action_links = array();
450 if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
451 $status = install_plugin_install_status( $plugin );
453 switch ( $status['status'] ) {
455 if ( $status['url'] ) {
456 /* translators: 1: Plugin name and version. */
457 $action_links[] = '<a class="install-now button" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Install Now' ) . '</a>';
461 case 'update_available':
462 if ( $status['url'] ) {
463 /* translators: 1: Plugin name and version */
464 $action_links[] = '<a class="update-now button" data-plugin="' . esc_attr( $status['file'] ) . '" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Update Now' ) . '</a>';
468 case 'latest_installed':
469 case 'newer_installed':
470 $action_links[] = '<span class="button button-disabled" title="' . esc_attr__( 'This plugin is already installed and is up to date' ) . ' ">' . _x( 'Installed', 'plugin' ) . '</span>';
475 $details_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
476 '&TB_iframe=true&width=600&height=550' );
478 /* translators: 1: Plugin name and version. */
479 $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>';
481 if ( !empty( $plugin['icons']['svg'] ) ) {
482 $plugin_icon_url = $plugin['icons']['svg'];
483 } elseif ( !empty( $plugin['icons']['2x'] ) ) {
484 $plugin_icon_url = $plugin['icons']['2x'];
485 } elseif ( !empty( $plugin['icons']['1x'] ) ) {
486 $plugin_icon_url = $plugin['icons']['1x'];
488 $plugin_icon_url = $plugin['icons']['default'];
492 * Filter the install action links for a plugin.
496 * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
497 * @param array $plugin The plugin currently being listed.
499 $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
501 $date_format = __( 'M j, Y @ H:i' );
502 $last_updated_timestamp = strtotime( $plugin['last_updated'] );
504 <div class="plugin-card plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
505 <div class="plugin-card-top">
506 <div class="name column-name">
508 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox">
509 <?php echo $title; ?>
510 <img src="<?php echo esc_attr( $plugin_icon_url ) ?>" class="plugin-icon" alt="">
514 <div class="action-links">
516 if ( $action_links ) {
517 echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
521 <div class="desc column-description">
522 <p><?php echo $description; ?></p>
523 <p class="authors"><?php echo $author; ?></p>
526 <div class="plugin-card-bottom">
527 <div class="vers column-rating">
528 <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
529 <span class="num-ratings">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
531 <div class="column-updated">
532 <strong><?php _e( 'Last Updated:' ); ?></strong> <span title="<?php echo esc_attr( date_i18n( $date_format, $last_updated_timestamp ) ); ?>">
533 <?php printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) ); ?>
536 <div class="column-downloaded">
538 if ( $plugin['active_installs'] >= 1000000 ) {
539 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
541 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
543 printf( __( '%s Active Installs' ), $active_installs_text );
546 <div class="column-compatibility">
548 if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
549 echo '<span class="compatibility-untested">' . __( 'Untested with your version of WordPress' ) . '</span>';
550 } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
551 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
553 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
562 // Close off the group divs of the last one
563 if ( ! empty( $group ) ) {