WordPress 4.4
[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                 $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' );
97                 }
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                  * Filter 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                  * Filter 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                                 $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
174                                 update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
175                                 if ( $user )
176                                         $args['user'] = $user;
177                                 else
178                                         $args = false;
179
180                                 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
181                                 break;
182
183                         default:
184                                 $args = false;
185                                 break;
186                 }
187
188                 /**
189                  * Filter API request arguments for each Plugin Install screen tab.
190                  *
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'.
193                  *
194                  * @since 3.7.0
195                  *
196                  * @param array|bool $args Plugin Install API arguments.
197                  */
198                 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
199
200                 if ( !$args )
201                         return;
202
203                 $api = plugins_api( 'query_plugins', $args );
204
205                 if ( is_wp_error( $api ) ) {
206                         $this->error = $api;
207                         return;
208                 }
209
210                 $this->items = $api->plugins;
211
212                 if ( $this->orderby ) {
213                         uasort( $this->items, array( $this, 'order_callback' ) );
214                 }
215
216                 $this->set_pagination_args( array(
217                         'total_items' => $api->info['results'],
218                         'per_page' => $args['per_page'],
219                 ) );
220
221                 if ( isset( $api->info['groups'] ) ) {
222                         $this->groups = $api->info['groups'];
223                 }
224         }
225
226         /**
227          * @access public
228          */
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>';
232                 } else {
233                         $message = __( 'No plugins match your request.' );
234                 }
235                 echo '<div class="no-plugin-results">' . $message . '</div>';
236         }
237
238         /**
239          *
240          * @global array $tabs
241          * @global string $tab
242          *
243          * @return array
244          */
245         protected function get_views() {
246                 global $tabs, $tab;
247
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>";
253                 }
254                 // No longer a real tab.
255                 unset( $display_tabs['plugin-install-upload'] );
256
257                 return $display_tabs;
258         }
259
260         /**
261          * Override parent views so we can use the filter bar display.
262          */
263         public function views() {
264                 $views = $this->get_views();
265
266                 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
267                 $views = apply_filters( "views_{$this->screen->id}", $views );
268
269                 $this->screen->render_screen_reader_content( 'heading_views' );
270 ?>
271 <div class="wp-filter">
272         <ul class="filter-links">
273                 <?php
274                 if ( ! empty( $views ) ) {
275                         foreach ( $views as $class => $view ) {
276                                 $views[ $class ] = "\t<li class='$class'>$view";
277                         }
278                         echo implode( " </li>\n", $views ) . "</li>\n";
279                 }
280                 ?>
281         </ul>
282
283         <?php install_search_form( isset( $views['plugin-install-search'] ) ); ?>
284 </div>
285 <?php
286         }
287
288         /**
289          * Override the parent display() so we can provide a different container.
290          */
291         public function display() {
292                 $singular = $this->_args['singular'];
293
294                 $data_attr = '';
295
296                 if ( $singular ) {
297                         $data_attr = " data-wp-lists='list:$singular'";
298                 }
299
300                 $this->display_tablenav( 'top' );
301
302 ?>
303 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
304 <?php
305         $this->screen->render_screen_reader_content( 'heading_list' );
306 ?>
307         <div id="the-list"<?php echo $data_attr; ?>>
308                 <?php $this->display_rows_or_placeholder(); ?>
309         </div>
310 </div>
311 <?php
312                 $this->display_tablenav( 'bottom' );
313         }
314
315         /**
316          * @global string $tab
317          *
318          * @param string $which
319          */
320         protected function display_tablenav( $which ) {
321                 if ( $GLOBALS['tab'] === 'featured' ) {
322                         return;
323                 }
324
325                 if ( 'top' === $which ) {
326                         wp_referer_field();
327                 ?>
328                         <div class="tablenav top">
329                                 <div class="alignleft actions">
330                                         <?php
331                                         /**
332                                          * Fires before the Plugin Install table header pagination is displayed.
333                                          *
334                                          * @since 2.7.0
335                                          */
336                                         do_action( 'install_plugins_table_header' ); ?>
337                                 </div>
338                                 <?php $this->pagination( $which ); ?>
339                                 <br class="clear" />
340                         </div>
341                 <?php } else { ?>
342                         <div class="tablenav bottom">
343                                 <?php $this->pagination( $which ); ?>
344                                 <br class="clear" />
345                         </div>
346                 <?php
347                 }
348         }
349
350         /**
351          * @return array
352          */
353         protected function get_table_classes() {
354                 return array( 'widefat', $this->_args['plural'] );
355         }
356
357         /**
358          * @return array
359          */
360         public function get_columns() {
361                 return array();
362         }
363
364         /**
365          * @param object $plugin_a
366          * @param object $plugin_b
367          * @return int
368          */
369         private function order_callback( $plugin_a, $plugin_b ) {
370                 $orderby = $this->orderby;
371                 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
372                         return 0;
373                 }
374
375                 $a = $plugin_a->$orderby;
376                 $b = $plugin_b->$orderby;
377
378                 if ( $a == $b ) {
379                         return 0;
380                 }
381
382                 if ( 'DESC' === $this->order ) {
383                         return ( $a < $b ) ? 1 : -1;
384                 } else {
385                         return ( $a < $b ) ? -1 : 1;
386                 }
387         }
388
389         /**
390          * @global string $wp_version
391          */
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()
398                 );
399
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' ),
404                 );
405
406                 $group = null;
407
408                 foreach ( (array) $this->items as $plugin ) {
409                         if ( is_object( $plugin ) ) {
410                                 $plugin = (array) $plugin;
411                         }
412
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 ];
419                                         }
420                                 } else {
421                                         $group_name = $plugin['group'];
422                                 }
423
424                                 // Starting a new group, close off the divs of the last one
425                                 if ( ! empty( $group ) ) {
426                                         echo '</div></div>';
427                                 }
428
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">';
432
433                                 $group = $plugin['group'];
434                         }
435                         $title = wp_kses( $plugin['name'], $plugins_allowedtags );
436
437                         // Remove any HTML from the description.
438                         $description = strip_tags( $plugin['short_description'] );
439                         $version = wp_kses( $plugin['version'], $plugins_allowedtags );
440
441                         $name = strip_tags( $title . ' ' . $version );
442
443                         $author = wp_kses( $plugin['author'], $plugins_allowedtags );
444                         if ( ! empty( $author ) ) {
445                                 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
446                         }
447
448                         $action_links = array();
449
450                         if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
451                                 $status = install_plugin_install_status( $plugin );
452
453                                 switch ( $status['status'] ) {
454                                         case 'install':
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>';
458                                                 }
459
460                                                 break;
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>';
465                                                 }
466
467                                                 break;
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>';
471                                                 break;
472                                 }
473                         }
474
475                         $details_link   = self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
476                                                                 '&amp;TB_iframe=true&amp;width=600&amp;height=550' );
477
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>';
480
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'];
487                         } else {
488                                 $plugin_icon_url = $plugin['icons']['default'];
489                         }
490
491                         /**
492                          * Filter the install action links for a plugin.
493                          *
494                          * @since 2.7.0
495                          *
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.
498                          */
499                         $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
500
501                         $date_format = __( 'M j, Y @ H:i' );
502                         $last_updated_timestamp = strtotime( $plugin['last_updated'] );
503                 ?>
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">
507                                         <h3>
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="">
511                                                 </a>
512                                         </h3>
513                                 </div>
514                                 <div class="action-links">
515                                         <?php
516                                                 if ( $action_links ) {
517                                                         echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
518                                                 }
519                                         ?>
520                                 </div>
521                                 <div class="desc column-description">
522                                         <p><?php echo $description; ?></p>
523                                         <p class="authors"><?php echo $author; ?></p>
524                                 </div>
525                         </div>
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>
530                                 </div>
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 ) ); ?>
534                                         </span>
535                                 </div>
536                                 <div class="column-downloaded">
537                                         <?php
538                                         if ( $plugin['active_installs'] >= 1000000 ) {
539                                                 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
540                                         } else {
541                                                 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
542                                         }
543                                         printf( __( '%s Active Installs' ), $active_installs_text );
544                                         ?>
545                                 </div>
546                                 <div class="column-compatibility">
547                                         <?php
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>';
552                                         } else {
553                                                 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
554                                         }
555                                         ?>
556                                 </div>
557                         </div>
558                 </div>
559                 <?php
560                 }
561
562                 // Close off the group divs of the last one
563                 if ( ! empty( $group ) ) {
564                         echo '</div></div>';
565                 }
566         }
567 }