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
73 public function prepare_items() {
74 include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
76 global $tabs, $tab, $paged, $type, $term;
78 wp_reset_vars( array( 'tab' ) );
80 $paged = $this->get_pagenum();
84 // These are the tabs which are shown on the page
87 if ( 'search' === $tab ) {
88 $tabs['search'] = __( 'Search Results' );
90 if ( $tab === 'beta' || false !== strpos( get_bloginfo( 'version' ), '-' ) ) {
91 $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
93 $tabs['featured'] = _x( 'Featured', 'Plugin Installer' );
94 $tabs['popular'] = _x( 'Popular', 'Plugin Installer' );
95 $tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' );
96 $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
97 if ( current_user_can( 'upload_plugins' ) ) {
98 // No longer a real tab. Here for filter compatibility.
99 // Gets skipped in get_views().
100 $tabs['upload'] = __( 'Upload Plugin' );
103 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
106 * Filters the tabs shown on the Plugin Install screen.
110 * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
111 * 'recommended', 'favorites', and 'upload'.
113 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
116 * Filters tabs not associated with a menu item on the Plugin Install screen.
120 * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
122 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
124 // If a non-valid menu tab has been selected, And it's not a non-menu action.
125 if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
130 'per_page' => $per_page,
132 'last_updated' => true,
134 'active_installs' => true
136 // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
137 'locale' => get_user_locale(),
138 'installed_plugins' => $this->get_installed_plugin_slugs(),
143 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
144 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
148 $args['tag'] = sanitize_title_with_dashes( $term );
151 $args['search'] = $term;
154 $args['author'] = $term;
161 $args['fields']['group'] = true;
162 $this->orderby = 'group';
168 $args['browse'] = $tab;
172 $action = 'save_wporg_username_' . get_current_user_id();
173 if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action ) ) {
174 $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
175 update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
177 $user = get_user_option( 'wporg_favorites' );
180 $args['user'] = $user;
184 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
193 * Filters API request arguments for each Plugin Install screen tab.
195 * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
196 * Default tabs include 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
200 * @param array|bool $args Plugin Install API arguments.
202 $args = apply_filters( "install_plugins_table_api_args_{$tab}", $args );
207 $api = plugins_api( 'query_plugins', $args );
209 if ( is_wp_error( $api ) ) {
214 $this->items = $api->plugins;
216 if ( $this->orderby ) {
217 uasort( $this->items, array( $this, 'order_callback' ) );
220 $this->set_pagination_args( array(
221 'total_items' => $api->info['results'],
222 'per_page' => $args['per_page'],
225 if ( isset( $api->info['groups'] ) ) {
226 $this->groups = $api->info['groups'];
233 public function no_items() {
234 if ( isset( $this->error ) ) {
235 $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>';
237 $message = __( 'No plugins match your request.' );
239 echo '<div class="no-plugin-results">' . $message . '</div>';
244 * @global array $tabs
245 * @global string $tab
249 protected function get_views() {
252 $display_tabs = array();
253 foreach ( (array) $tabs as $action => $text ) {
254 $class = ( $action === $tab ) ? ' current' : '';
255 $href = self_admin_url('plugin-install.php?tab=' . $action);
256 $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
258 // No longer a real tab.
259 unset( $display_tabs['plugin-install-upload'] );
261 return $display_tabs;
265 * Override parent views so we can use the filter bar display.
267 public function views() {
268 $views = $this->get_views();
270 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
271 $views = apply_filters( "views_{$this->screen->id}", $views );
273 $this->screen->render_screen_reader_content( 'heading_views' );
275 <div class="wp-filter">
276 <ul class="filter-links">
278 if ( ! empty( $views ) ) {
279 foreach ( $views as $class => $view ) {
280 $views[ $class ] = "\t<li class='$class'>$view";
282 echo implode( " </li>\n", $views ) . "</li>\n";
287 <?php install_search_form(); ?>
293 * Override the parent display() so we can provide a different container.
295 public function display() {
296 $singular = $this->_args['singular'];
301 $data_attr = " data-wp-lists='list:$singular'";
304 $this->display_tablenav( 'top' );
307 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
309 $this->screen->render_screen_reader_content( 'heading_list' );
311 <div id="the-list"<?php echo $data_attr; ?>>
312 <?php $this->display_rows_or_placeholder(); ?>
316 $this->display_tablenav( 'bottom' );
320 * @global string $tab
322 * @param string $which
324 protected function display_tablenav( $which ) {
325 if ( $GLOBALS['tab'] === 'featured' ) {
329 if ( 'top' === $which ) {
332 <div class="tablenav top">
333 <div class="alignleft actions">
336 * Fires before the Plugin Install table header pagination is displayed.
340 do_action( 'install_plugins_table_header' ); ?>
342 <?php $this->pagination( $which ); ?>
346 <div class="tablenav bottom">
347 <?php $this->pagination( $which ); ?>
357 protected function get_table_classes() {
358 return array( 'widefat', $this->_args['plural'] );
364 public function get_columns() {
369 * @param object $plugin_a
370 * @param object $plugin_b
373 private function order_callback( $plugin_a, $plugin_b ) {
374 $orderby = $this->orderby;
375 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
379 $a = $plugin_a->$orderby;
380 $b = $plugin_b->$orderby;
386 if ( 'DESC' === $this->order ) {
387 return ( $a < $b ) ? 1 : -1;
389 return ( $a < $b ) ? -1 : 1;
393 public function display_rows() {
394 $plugins_allowedtags = array(
395 'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
396 'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
397 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
398 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
401 $plugins_group_titles = array(
402 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
403 'Social' => _x( 'Social', 'Plugin installer group title' ),
404 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
409 foreach ( (array) $this->items as $plugin ) {
410 if ( is_object( $plugin ) ) {
411 $plugin = (array) $plugin;
414 // Display the group heading if there is one
415 if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
416 if ( isset( $this->groups[ $plugin['group'] ] ) ) {
417 $group_name = $this->groups[ $plugin['group'] ];
418 if ( isset( $plugins_group_titles[ $group_name ] ) ) {
419 $group_name = $plugins_group_titles[ $group_name ];
422 $group_name = $plugin['group'];
425 // Starting a new group, close off the divs of the last one
426 if ( ! empty( $group ) ) {
430 echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
431 // needs an extra wrapping div for nth-child selectors to work
432 echo '<div class="plugin-items">';
434 $group = $plugin['group'];
436 $title = wp_kses( $plugin['name'], $plugins_allowedtags );
438 // Remove any HTML from the description.
439 $description = strip_tags( $plugin['short_description'] );
440 $version = wp_kses( $plugin['version'], $plugins_allowedtags );
442 $name = strip_tags( $title . ' ' . $version );
444 $author = wp_kses( $plugin['author'], $plugins_allowedtags );
445 if ( ! empty( $author ) ) {
446 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
449 $action_links = array();
451 if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
452 $status = install_plugin_install_status( $plugin );
454 switch ( $status['status'] ) {
456 if ( $status['url'] ) {
457 /* translators: 1: Plugin name and version. */
458 $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>';
462 case 'update_available':
463 if ( $status['url'] ) {
464 /* translators: 1: Plugin name and version */
465 $action_links[] = '<a class="update-now button aria-button-if-js" 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>';
469 case 'latest_installed':
470 case 'newer_installed':
471 if ( is_plugin_active( $status['file'] ) ) {
472 $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Active', 'plugin' ) . '</button>';
473 } elseif ( current_user_can( 'activate_plugins' ) ) {
474 $button_text = __( 'Activate' );
475 /* translators: %s: Plugin name */
476 $button_label = _x( 'Activate %s', 'plugin' );
477 $activate_url = add_query_arg( array(
478 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
479 'action' => 'activate',
480 'plugin' => $status['file'],
481 ), network_admin_url( 'plugins.php' ) );
483 if ( is_network_admin() ) {
484 $button_text = __( 'Network Activate' );
485 /* translators: %s: Plugin name */
486 $button_label = _x( 'Network Activate %s', 'plugin' );
487 $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
490 $action_links[] = sprintf(
491 '<a href="%1$s" class="button activate-now" aria-label="%2$s">%3$s</a>',
492 esc_url( $activate_url ),
493 esc_attr( sprintf( $button_label, $plugin['name'] ) ),
497 $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Installed', 'plugin' ) . '</button>';
503 $details_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
504 '&TB_iframe=true&width=600&height=550' );
506 /* translators: 1: Plugin name and version. */
507 $action_links[] = '<a href="' . esc_url( $details_link ) . '" class="thickbox open-plugin-details-modal" aria-label="' . esc_attr( sprintf( __( 'More information about %s' ), $name ) ) . '" data-title="' . esc_attr( $name ) . '">' . __( 'More Details' ) . '</a>';
509 if ( !empty( $plugin['icons']['svg'] ) ) {
510 $plugin_icon_url = $plugin['icons']['svg'];
511 } elseif ( !empty( $plugin['icons']['2x'] ) ) {
512 $plugin_icon_url = $plugin['icons']['2x'];
513 } elseif ( !empty( $plugin['icons']['1x'] ) ) {
514 $plugin_icon_url = $plugin['icons']['1x'];
516 $plugin_icon_url = $plugin['icons']['default'];
520 * Filters the install action links for a plugin.
524 * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
525 * @param array $plugin The plugin currently being listed.
527 $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
529 $last_updated_timestamp = strtotime( $plugin['last_updated'] );
531 <div class="plugin-card plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
532 <div class="plugin-card-top">
533 <div class="name column-name">
535 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox open-plugin-details-modal">
536 <?php echo $title; ?>
537 <img src="<?php echo esc_attr( $plugin_icon_url ) ?>" class="plugin-icon" alt="">
541 <div class="action-links">
543 if ( $action_links ) {
544 echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
548 <div class="desc column-description">
549 <p><?php echo $description; ?></p>
550 <p class="authors"><?php echo $author; ?></p>
553 <div class="plugin-card-bottom">
554 <div class="vers column-rating">
555 <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
556 <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
558 <div class="column-updated">
559 <strong><?php _e( 'Last Updated:' ); ?></strong> <?php printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) ); ?>
561 <div class="column-downloaded">
563 if ( $plugin['active_installs'] >= 1000000 ) {
564 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
565 } elseif ( 0 == $plugin['active_installs'] ) {
566 $active_installs_text = _x( 'Less Than 10', 'Active plugin installs' );
568 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
570 printf( __( '%s Active Installs' ), $active_installs_text );
573 <div class="column-compatibility">
575 $wp_version = get_bloginfo( 'version' );
577 if ( ! empty( $plugin['tested'] ) && version_compare( substr( $wp_version, 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
578 echo '<span class="compatibility-untested">' . __( 'Untested with your version of WordPress' ) . '</span>';
579 } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $wp_version, 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
580 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
582 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
591 // Close off the group divs of the last one
592 if ( ! empty( $group ) ) {