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