3 * Plugin Installer List Table class.
6 * @subpackage List_Table
10 class WP_Plugin_Install_List_Table extends WP_List_Table {
12 public $order = 'ASC';
13 public $orderby = null;
14 public $groups = array();
18 public function ajax_user_can() {
19 return current_user_can('install_plugins');
23 * Return a list of slugs of installed plugins, if known.
25 * Uses the transient data from the updates API to determine the slugs of
26 * known installed plugins. This might be better elsewhere, perhaps even
27 * within get_plugins().
32 protected function get_installed_plugin_slugs() {
35 $plugin_info = get_site_transient( 'update_plugins' );
36 if ( isset( $plugin_info->no_update ) ) {
37 foreach ( $plugin_info->no_update as $plugin ) {
38 $slugs[] = $plugin->slug;
42 if ( isset( $plugin_info->response ) ) {
43 foreach ( $plugin_info->response as $plugin ) {
44 $slugs[] = $plugin->slug;
51 public function prepare_items() {
52 include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
54 global $tabs, $tab, $paged, $type, $term;
56 wp_reset_vars( array( 'tab' ) );
58 $paged = $this->get_pagenum();
62 // These are the tabs which are shown on the page
65 if ( 'search' == $tab )
66 $tabs['search'] = __( 'Search Results' );
67 $tabs['featured'] = _x( 'Featured', 'Plugin Installer' );
68 $tabs['popular'] = _x( 'Popular', 'Plugin Installer' );
69 $tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' );
70 $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
71 if ( $tab === 'beta' || false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
72 $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
74 if ( current_user_can( 'upload_plugins' ) ) {
75 // No longer a real tab. Here for filter compatibility.
76 // Gets skipped in get_views().
77 $tabs['upload'] = __( 'Upload Plugin' );
80 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
83 * Filter the tabs shown on the Plugin Install screen.
87 * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
88 * 'recommended', 'favorites', and 'upload'.
90 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
93 * Filter tabs not associated with a menu item on the Plugin Install screen.
97 * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
99 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
101 // If a non-valid menu tab has been selected, And it's not a non-menu action.
102 if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
107 'per_page' => $per_page,
109 'last_updated' => true,
111 'active_installs' => true
113 // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
114 'locale' => get_locale(),
115 'installed_plugins' => $this->get_installed_plugin_slugs(),
120 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
121 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
125 $args['tag'] = sanitize_title_with_dashes( $term );
128 $args['search'] = $term;
131 $args['author'] = $term;
138 $args['fields']['group'] = true;
139 $this->orderby = 'group';
145 $args['browse'] = $tab;
149 $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
150 update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
152 $args['user'] = $user;
156 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
165 * Filter API request arguments for each Plugin Install screen tab.
167 * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
168 * Default tabs include 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
172 * @param array|bool $args Plugin Install API arguments.
174 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
179 $api = plugins_api( 'query_plugins', $args );
181 if ( is_wp_error( $api ) ) {
186 $this->items = $api->plugins;
188 if ( $this->orderby ) {
189 uasort( $this->items, array( $this, 'order_callback' ) );
192 $this->set_pagination_args( array(
193 'total_items' => $api->info['results'],
194 'per_page' => $args['per_page'],
197 if ( isset( $api->info['groups'] ) ) {
198 $this->groups = $api->info['groups'];
202 public function no_items() {
203 if ( isset( $this->error ) ) {
204 $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>';
206 $message = __( 'No plugins match your request.' );
208 echo '<div class="no-plugin-results">' . $message . '</div>';
211 protected function get_views() {
214 $display_tabs = array();
215 foreach ( (array) $tabs as $action => $text ) {
216 $class = ( $action == $tab ) ? ' current' : '';
217 $href = self_admin_url('plugin-install.php?tab=' . $action);
218 $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
220 // No longer a real tab.
221 unset( $display_tabs['plugin-install-upload'] );
223 return $display_tabs;
227 * Override parent views so we can use the filter bar display.
229 public function views() {
230 $views = $this->get_views();
232 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
233 $views = apply_filters( "views_{$this->screen->id}", $views );
236 <div class="wp-filter">
237 <ul class="filter-links">
239 if ( ! empty( $views ) ) {
240 foreach ( $views as $class => $view ) {
241 $views[ $class ] = "\t<li class='$class'>$view";
243 echo implode( " </li>\n", $views ) . "</li>\n";
248 <?php install_search_form( isset( $views['plugin-install-search'] ) ); ?>
254 * Override the parent display() so we can provide a different container.
256 public function display() {
257 $singular = $this->_args['singular'];
262 $data_attr = " data-wp-lists='list:$singular'";
265 $this->display_tablenav( 'top' );
268 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
270 <div id="the-list"<?php echo $data_attr; ?>>
271 <?php $this->display_rows_or_placeholder(); ?>
275 $this->display_tablenav( 'bottom' );
279 * @param string $which
281 protected function display_tablenav( $which ) {
282 if ( $GLOBALS['tab'] === 'featured' ) {
286 if ( 'top' == $which ) {
289 <div class="tablenav top">
290 <div class="alignleft actions">
293 * Fires before the Plugin Install table header pagination is displayed.
297 do_action( 'install_plugins_table_header' ); ?>
299 <?php $this->pagination( $which ); ?>
303 <div class="tablenav bottom">
304 <?php $this->pagination( $which ); ?>
311 protected function get_table_classes() {
312 return array( 'widefat', $this->_args['plural'] );
315 public function get_columns() {
320 * @param object $plugin_a
321 * @param object $plugin_b
324 private function order_callback( $plugin_a, $plugin_b ) {
325 $orderby = $this->orderby;
326 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
330 $a = $plugin_a->$orderby;
331 $b = $plugin_b->$orderby;
337 if ( 'DESC' == $this->order ) {
338 return ( $a < $b ) ? 1 : -1;
340 return ( $a < $b ) ? -1 : 1;
344 public function display_rows() {
345 $plugins_allowedtags = array(
346 'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
347 'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
348 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
349 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
352 $plugins_group_titles = array(
353 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
354 'Social' => _x( 'Social', 'Plugin installer group title' ),
355 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
360 foreach ( (array) $this->items as $plugin ) {
361 if ( is_object( $plugin ) ) {
362 $plugin = (array) $plugin;
365 // Display the group heading if there is one
366 if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
367 if ( isset( $this->groups[ $plugin['group'] ] ) ) {
368 $group_name = $this->groups[ $plugin['group'] ];
369 if ( isset( $plugins_group_titles[ $group_name ] ) ) {
370 $group_name = $plugins_group_titles[ $group_name ];
373 $group_name = $plugin['group'];
376 // Starting a new group, close off the divs of the last one
377 if ( ! empty( $group ) ) {
381 echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
382 // needs an extra wrapping div for nth-child selectors to work
383 echo '<div class="plugin-items">';
385 $group = $plugin['group'];
387 $title = wp_kses( $plugin['name'], $plugins_allowedtags );
389 // Remove any HTML from the description.
390 $description = strip_tags( $plugin['short_description'] );
391 $version = wp_kses( $plugin['version'], $plugins_allowedtags );
393 $name = strip_tags( $title . ' ' . $version );
395 $author = wp_kses( $plugin['author'], $plugins_allowedtags );
396 if ( ! empty( $author ) ) {
397 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
400 $action_links = array();
402 if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
403 $status = install_plugin_install_status( $plugin );
405 switch ( $status['status'] ) {
407 if ( $status['url'] ) {
408 /* translators: 1: Plugin name and version. */
409 $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>';
413 case 'update_available':
414 if ( $status['url'] ) {
415 /* translators: 1: Plugin name and version */
416 $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>';
420 case 'latest_installed':
421 case 'newer_installed':
422 $action_links[] = '<span class="button button-disabled" title="' . esc_attr__( 'This plugin is already installed and is up to date' ) . ' ">' . _x( 'Installed', 'plugin' ) . '</span>';
427 $details_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
428 '&TB_iframe=true&width=600&height=550' );
430 /* translators: 1: Plugin name and version. */
431 $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>';
433 if ( !empty( $plugin['icons']['svg'] ) ) {
434 $plugin_icon_url = $plugin['icons']['svg'];
435 } elseif ( !empty( $plugin['icons']['2x'] ) ) {
436 $plugin_icon_url = $plugin['icons']['2x'];
437 } elseif ( !empty( $plugin['icons']['1x'] ) ) {
438 $plugin_icon_url = $plugin['icons']['1x'];
440 $plugin_icon_url = $plugin['icons']['default'];
444 * Filter the install action links for a plugin.
448 * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
449 * @param array $plugin The plugin currently being listed.
451 $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
453 $date_format = __( 'M j, Y @ H:i' );
454 $last_updated_timestamp = strtotime( $plugin['last_updated'] );
456 <div class="plugin-card plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
457 <div class="plugin-card-top">
458 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox plugin-icon"><img src="<?php echo esc_attr( $plugin_icon_url ) ?>" /></a>
459 <div class="name column-name">
460 <h4><a href="<?php echo esc_url( $details_link ); ?>" class="thickbox"><?php echo $title; ?></a></h4>
462 <div class="action-links">
464 if ( $action_links ) {
465 echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
469 <div class="desc column-description">
470 <p><?php echo $description; ?></p>
471 <p class="authors"><?php echo $author; ?></p>
474 <div class="plugin-card-bottom">
475 <div class="vers column-rating">
476 <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
477 <span class="num-ratings">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
479 <div class="column-updated">
480 <strong><?php _e( 'Last Updated:' ); ?></strong> <span title="<?php echo esc_attr( date_i18n( $date_format, $last_updated_timestamp ) ); ?>">
481 <?php printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) ); ?>
484 <div class="column-downloaded">
486 if ( $plugin['active_installs'] >= 1000000 ) {
487 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
489 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
491 printf( __( '%s Active Installs' ), $active_installs_text );
494 <div class="column-compatibility">
496 if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
497 echo '<span class="compatibility-untested">' . __( 'Untested with your version of WordPress' ) . '</span>';
498 } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
499 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
501 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
510 // Close off the group divs of the last one
511 if ( ! empty( $group ) ) {