Wordpress 4.6
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-plugin-install-list-table.php
1 <?php
2 /**
3  * List Table API: WP_Plugin_Install_List_Table class
4  *
5  * @package WordPress
6  * @subpackage Administration
7  * @since 3.1.0
8  */
9
10 /**
11  * Core class used to implement displaying plugins to install in a list table.
12  *
13  * @since 3.1.0
14  * @access private
15  *
16  * @see WP_List_Table
17  */
18 class WP_Plugin_Install_List_Table extends WP_List_Table {
19
20         public $order = 'ASC';
21         public $orderby = null;
22         public $groups = array();
23
24         private $error;
25
26         /**
27          *
28          * @return bool
29          */
30         public function ajax_user_can() {
31                 return current_user_can('install_plugins');
32         }
33
34         /**
35          * Return a list of slugs of installed plugins, if known.
36          *
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().
40          *
41          * @since 4.0.0
42          * @access protected
43          *
44          * @return array
45          */
46         protected function get_installed_plugin_slugs() {
47                 $slugs = array();
48
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;
53                         }
54                 }
55
56                 if ( isset( $plugin_info->response ) ) {
57                         foreach ( $plugin_info->response as $plugin ) {
58                                 $slugs[] = $plugin->slug;
59                         }
60                 }
61
62                 return $slugs;
63         }
64
65         /**
66          *
67          * @global array  $tabs
68          * @global string $tab
69          * @global int    $paged
70          * @global string $type
71          * @global string $term
72          * @global string $wp_version
73          */
74         public function prepare_items() {
75                 include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
76
77                 global $tabs, $tab, $paged, $type, $term;
78
79                 wp_reset_vars( array( 'tab' ) );
80
81                 $paged = $this->get_pagenum();
82
83                 $per_page = 30;
84
85                 // These are the tabs which are shown on the page
86                 $tabs = array();
87
88                 if ( 'search' === $tab ) {
89                         $tabs['search'] = __( 'Search Results' );
90                 }
91                 if ( $tab === 'beta' || false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
92                         $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
93                 }
94                 $tabs['featured']    = _x( 'Featured', 'Plugin Installer' );
95                 $tabs['popular']     = _x( 'Popular', 'Plugin Installer' );
96                 $tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' );
97                 $tabs['favorites']   = _x( 'Favorites', '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' );
102                 }
103
104                 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
105
106                 /**
107                  * Filters the tabs shown on the Plugin Install screen.
108                  *
109                  * @since 2.7.0
110                  *
111                  * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
112                  *                    'recommended', 'favorites', and 'upload'.
113                  */
114                 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
115
116                 /**
117                  * Filters tabs not associated with a menu item on the Plugin Install screen.
118                  *
119                  * @since 2.7.0
120                  *
121                  * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
122                  */
123                 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
124
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 ) ) )
127                         $tab = key( $tabs );
128
129                 $args = array(
130                         'page' => $paged,
131                         'per_page' => $per_page,
132                         'fields' => array(
133                                 'last_updated' => true,
134                                 'icons' => true,
135                                 'active_installs' => true
136                         ),
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(),
140                 );
141
142                 switch ( $tab ) {
143                         case 'search':
144                                 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
145                                 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
146
147                                 switch ( $type ) {
148                                         case 'tag':
149                                                 $args['tag'] = sanitize_title_with_dashes( $term );
150                                                 break;
151                                         case 'term':
152                                                 $args['search'] = $term;
153                                                 break;
154                                         case 'author':
155                                                 $args['author'] = $term;
156                                                 break;
157                                 }
158
159                                 break;
160
161                         case 'featured':
162                                 $args['fields']['group'] = true;
163                                 $this->orderby = 'group';
164                                 // No break!
165                         case 'popular':
166                         case 'new':
167                         case 'beta':
168                         case 'recommended':
169                                 $args['browse'] = $tab;
170                                 break;
171
172                         case 'favorites':
173                                 $action = 'save_wporg_username_' . get_current_user_id();
174                                 if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action ) ) {
175                                         $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
176                                         update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
177                                 } else {
178                                         $user = get_user_option( 'wporg_favorites' );
179                                 }
180                                 if ( $user )
181                                         $args['user'] = $user;
182                                 else
183                                         $args = false;
184
185                                 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
186                                 break;
187
188                         default:
189                                 $args = false;
190                                 break;
191                 }
192
193                 /**
194                  * Filters API request arguments for each Plugin Install screen tab.
195                  *
196                  * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
197                  * Default tabs include 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
198                  *
199                  * @since 3.7.0
200                  *
201                  * @param array|bool $args Plugin Install API arguments.
202                  */
203                 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
204
205                 if ( !$args )
206                         return;
207
208                 $api = plugins_api( 'query_plugins', $args );
209
210                 if ( is_wp_error( $api ) ) {
211                         $this->error = $api;
212                         return;
213                 }
214
215                 $this->items = $api->plugins;
216
217                 if ( $this->orderby ) {
218                         uasort( $this->items, array( $this, 'order_callback' ) );
219                 }
220
221                 $this->set_pagination_args( array(
222                         'total_items' => $api->info['results'],
223                         'per_page' => $args['per_page'],
224                 ) );
225
226                 if ( isset( $api->info['groups'] ) ) {
227                         $this->groups = $api->info['groups'];
228                 }
229         }
230
231         /**
232          * @access public
233          */
234         public function no_items() {
235                 if ( isset( $this->error ) ) {
236                         $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                 } else {
238                         $message = __( 'No plugins match your request.' );
239                 }
240                 echo '<div class="no-plugin-results">' . $message . '</div>';
241         }
242
243         /**
244          *
245          * @global array $tabs
246          * @global string $tab
247          *
248          * @return array
249          */
250         protected function get_views() {
251                 global $tabs, $tab;
252
253                 $display_tabs = array();
254                 foreach ( (array) $tabs as $action => $text ) {
255                         $class = ( $action === $tab ) ? ' current' : '';
256                         $href = self_admin_url('plugin-install.php?tab=' . $action);
257                         $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
258                 }
259                 // No longer a real tab.
260                 unset( $display_tabs['plugin-install-upload'] );
261
262                 return $display_tabs;
263         }
264
265         /**
266          * Override parent views so we can use the filter bar display.
267          */
268         public function views() {
269                 $views = $this->get_views();
270
271                 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
272                 $views = apply_filters( "views_{$this->screen->id}", $views );
273
274                 $this->screen->render_screen_reader_content( 'heading_views' );
275 ?>
276 <div class="wp-filter">
277         <ul class="filter-links">
278                 <?php
279                 if ( ! empty( $views ) ) {
280                         foreach ( $views as $class => $view ) {
281                                 $views[ $class ] = "\t<li class='$class'>$view";
282                         }
283                         echo implode( " </li>\n", $views ) . "</li>\n";
284                 }
285                 ?>
286         </ul>
287
288         <?php install_search_form(); ?>
289 </div>
290 <?php
291         }
292
293         /**
294          * Override the parent display() so we can provide a different container.
295          */
296         public function display() {
297                 $singular = $this->_args['singular'];
298
299                 $data_attr = '';
300
301                 if ( $singular ) {
302                         $data_attr = " data-wp-lists='list:$singular'";
303                 }
304
305                 $this->display_tablenav( 'top' );
306
307 ?>
308 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
309 <?php
310         $this->screen->render_screen_reader_content( 'heading_list' );
311 ?>
312         <div id="the-list"<?php echo $data_attr; ?>>
313                 <?php $this->display_rows_or_placeholder(); ?>
314         </div>
315 </div>
316 <?php
317                 $this->display_tablenav( 'bottom' );
318         }
319
320         /**
321          * @global string $tab
322          *
323          * @param string $which
324          */
325         protected function display_tablenav( $which ) {
326                 if ( $GLOBALS['tab'] === 'featured' ) {
327                         return;
328                 }
329
330                 if ( 'top' === $which ) {
331                         wp_referer_field();
332                 ?>
333                         <div class="tablenav top">
334                                 <div class="alignleft actions">
335                                         <?php
336                                         /**
337                                          * Fires before the Plugin Install table header pagination is displayed.
338                                          *
339                                          * @since 2.7.0
340                                          */
341                                         do_action( 'install_plugins_table_header' ); ?>
342                                 </div>
343                                 <?php $this->pagination( $which ); ?>
344                                 <br class="clear" />
345                         </div>
346                 <?php } else { ?>
347                         <div class="tablenav bottom">
348                                 <?php $this->pagination( $which ); ?>
349                                 <br class="clear" />
350                         </div>
351                 <?php
352                 }
353         }
354
355         /**
356          * @return array
357          */
358         protected function get_table_classes() {
359                 return array( 'widefat', $this->_args['plural'] );
360         }
361
362         /**
363          * @return array
364          */
365         public function get_columns() {
366                 return array();
367         }
368
369         /**
370          * @param object $plugin_a
371          * @param object $plugin_b
372          * @return int
373          */
374         private function order_callback( $plugin_a, $plugin_b ) {
375                 $orderby = $this->orderby;
376                 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
377                         return 0;
378                 }
379
380                 $a = $plugin_a->$orderby;
381                 $b = $plugin_b->$orderby;
382
383                 if ( $a == $b ) {
384                         return 0;
385                 }
386
387                 if ( 'DESC' === $this->order ) {
388                         return ( $a < $b ) ? 1 : -1;
389                 } else {
390                         return ( $a < $b ) ? -1 : 1;
391                 }
392         }
393
394         /**
395          * @global string $wp_version
396          */
397         public function display_rows() {
398                 $plugins_allowedtags = array(
399                         'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
400                         'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
401                         'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
402                         'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
403                 );
404
405                 $plugins_group_titles = array(
406                         'Performance' => _x( 'Performance', 'Plugin installer group title' ),
407                         'Social'      => _x( 'Social',      'Plugin installer group title' ),
408                         'Tools'       => _x( 'Tools',       'Plugin installer group title' ),
409                 );
410
411                 $group = null;
412
413                 foreach ( (array) $this->items as $plugin ) {
414                         if ( is_object( $plugin ) ) {
415                                 $plugin = (array) $plugin;
416                         }
417
418                         // Display the group heading if there is one
419                         if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
420                                 if ( isset( $this->groups[ $plugin['group'] ] ) ) {
421                                         $group_name = $this->groups[ $plugin['group'] ];
422                                         if ( isset( $plugins_group_titles[ $group_name ] ) ) {
423                                                 $group_name = $plugins_group_titles[ $group_name ];
424                                         }
425                                 } else {
426                                         $group_name = $plugin['group'];
427                                 }
428
429                                 // Starting a new group, close off the divs of the last one
430                                 if ( ! empty( $group ) ) {
431                                         echo '</div></div>';
432                                 }
433
434                                 echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
435                                 // needs an extra wrapping div for nth-child selectors to work
436                                 echo '<div class="plugin-items">';
437
438                                 $group = $plugin['group'];
439                         }
440                         $title = wp_kses( $plugin['name'], $plugins_allowedtags );
441
442                         // Remove any HTML from the description.
443                         $description = strip_tags( $plugin['short_description'] );
444                         $version = wp_kses( $plugin['version'], $plugins_allowedtags );
445
446                         $name = strip_tags( $title . ' ' . $version );
447
448                         $author = wp_kses( $plugin['author'], $plugins_allowedtags );
449                         if ( ! empty( $author ) ) {
450                                 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
451                         }
452
453                         $action_links = array();
454
455                         if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
456                                 $status = install_plugin_install_status( $plugin );
457
458                                 switch ( $status['status'] ) {
459                                         case 'install':
460                                                 if ( $status['url'] ) {
461                                                         /* translators: 1: Plugin name and version. */
462                                                         $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>';
463                                                 }
464                                                 break;
465
466                                         case 'update_available':
467                                                 if ( $status['url'] ) {
468                                                         /* translators: 1: Plugin name and version */
469                                                         $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>';
470                                                 }
471                                                 break;
472
473                                         case 'latest_installed':
474                                         case 'newer_installed':
475                                                 if ( is_plugin_active( $status['file'] ) ) {
476                                                         $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Active', 'plugin' ) . '</button>';
477                                                 } elseif ( current_user_can( 'activate_plugins' ) ) {
478                                                         $button_text  = __( 'Activate' );
479                                                         /* translators: %s: Plugin name */
480                                                         $button_label = _x( 'Activate %s', 'plugin' );
481                                                         $activate_url = add_query_arg( array(
482                                                                 '_wpnonce'    => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
483                                                                 'action'      => 'activate',
484                                                                 'plugin'      => $status['file'],
485                                                         ), network_admin_url( 'plugins.php' ) );
486
487                                                         if ( is_network_admin() ) {
488                                                                 $button_text  = __( 'Network Activate' );
489                                                                 /* translators: %s: Plugin name */
490                                                                 $button_label = _x( 'Network Activate %s', 'plugin' );
491                                                                 $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
492                                                         }
493
494                                                         $action_links[] = sprintf(
495                                                                 '<a href="%1$s" class="button activate-now button-secondary" aria-label="%2$s">%3$s</a>',
496                                                                 esc_url( $activate_url ),
497                                                                 esc_attr( sprintf( $button_label, $plugin['name'] ) ),
498                                                                 $button_text
499                                                         );
500                                                 } else {
501                                                         $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Installed', 'plugin' ) . '</button>';
502                                                 }
503                                                 break;
504                                 }
505                         }
506
507                         $details_link   = self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
508                                                                 '&amp;TB_iframe=true&amp;width=600&amp;height=550' );
509
510                         /* translators: 1: Plugin name and version. */
511                         $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>';
512
513                         if ( !empty( $plugin['icons']['svg'] ) ) {
514                                 $plugin_icon_url = $plugin['icons']['svg'];
515                         } elseif ( !empty( $plugin['icons']['2x'] ) ) {
516                                 $plugin_icon_url = $plugin['icons']['2x'];
517                         } elseif ( !empty( $plugin['icons']['1x'] ) ) {
518                                 $plugin_icon_url = $plugin['icons']['1x'];
519                         } else {
520                                 $plugin_icon_url = $plugin['icons']['default'];
521                         }
522
523                         /**
524                          * Filters the install action links for a plugin.
525                          *
526                          * @since 2.7.0
527                          *
528                          * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
529                          * @param array $plugin       The plugin currently being listed.
530                          */
531                         $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
532
533                         $last_updated_timestamp = strtotime( $plugin['last_updated'] );
534                 ?>
535                 <div class="plugin-card plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
536                         <div class="plugin-card-top">
537                                 <div class="name column-name">
538                                         <h3>
539                                                 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox open-plugin-details-modal">
540                                                 <?php echo $title; ?>
541                                                 <img src="<?php echo esc_attr( $plugin_icon_url ) ?>" class="plugin-icon" alt="">
542                                                 </a>
543                                         </h3>
544                                 </div>
545                                 <div class="action-links">
546                                         <?php
547                                                 if ( $action_links ) {
548                                                         echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
549                                                 }
550                                         ?>
551                                 </div>
552                                 <div class="desc column-description">
553                                         <p><?php echo $description; ?></p>
554                                         <p class="authors"><?php echo $author; ?></p>
555                                 </div>
556                         </div>
557                         <div class="plugin-card-bottom">
558                                 <div class="vers column-rating">
559                                         <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
560                                         <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
561                                 </div>
562                                 <div class="column-updated">
563                                         <strong><?php _e( 'Last Updated:' ); ?></strong> <?php printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) ); ?>
564                                 </div>
565                                 <div class="column-downloaded">
566                                         <?php
567                                         if ( $plugin['active_installs'] >= 1000000 ) {
568                                                 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
569                                         } else {
570                                                 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
571                                         }
572                                         printf( __( '%s Active Installs' ), $active_installs_text );
573                                         ?>
574                                 </div>
575                                 <div class="column-compatibility">
576                                         <?php
577                                         if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['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( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
580                                                 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
581                                         } else {
582                                                 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
583                                         }
584                                         ?>
585                                 </div>
586                         </div>
587                 </div>
588                 <?php
589                 }
590
591                 // Close off the group divs of the last one
592                 if ( ! empty( $group ) ) {
593                         echo '</div></div>';
594                 }
595         }
596 }