WordPress 4.2
[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         public function ajax_user_can() {
19                 return current_user_can('install_plugins');
20         }
21
22         /**
23          * Return a list of slugs of installed plugins, if known.
24          *
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().
28          *
29          * @since 4.0.0
30          * @access protected
31          */
32         protected function get_installed_plugin_slugs() {
33                 $slugs = array();
34
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;
39                         }
40                 }
41
42                 if ( isset( $plugin_info->response ) ) {
43                         foreach ( $plugin_info->response as $plugin ) {
44                                 $slugs[] = $plugin->slug;
45                         }
46                 }
47
48                 return $slugs;
49         }
50
51         public function prepare_items() {
52                 include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
53
54                 global $tabs, $tab, $paged, $type, $term;
55
56                 wp_reset_vars( array( 'tab' ) );
57
58                 $paged = $this->get_pagenum();
59
60                 $per_page = 30;
61
62                 // These are the tabs which are shown on the page
63                 $tabs = array();
64
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' );
73                 }
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' );
78                 }
79
80                 $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
81
82                 /**
83                  * Filter the tabs shown on the Plugin Install screen.
84                  *
85                  * @since 2.7.0
86                  *
87                  * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
88                  *                    'recommended', 'favorites', and 'upload'.
89                  */
90                 $tabs = apply_filters( 'install_plugins_tabs', $tabs );
91
92                 /**
93                  * Filter tabs not associated with a menu item on the Plugin Install screen.
94                  *
95                  * @since 2.7.0
96                  *
97                  * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
98                  */
99                 $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
100
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 ) ) )
103                         $tab = key( $tabs );
104
105                 $args = array(
106                         'page' => $paged,
107                         'per_page' => $per_page,
108                         'fields' => array(
109                                 'last_updated' => true,
110                                 'icons' => true,
111                                 'active_installs' => true
112                         ),
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(),
116                 );
117
118                 switch ( $tab ) {
119                         case 'search':
120                                 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
121                                 $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
122
123                                 switch ( $type ) {
124                                         case 'tag':
125                                                 $args['tag'] = sanitize_title_with_dashes( $term );
126                                                 break;
127                                         case 'term':
128                                                 $args['search'] = $term;
129                                                 break;
130                                         case 'author':
131                                                 $args['author'] = $term;
132                                                 break;
133                                 }
134
135                                 break;
136
137                         case 'featured':
138                                 $args['fields']['group'] = true;
139                                 $this->orderby = 'group';
140                                 // No break!
141                         case 'popular':
142                         case 'new':
143                         case 'beta':
144                         case 'recommended':
145                                 $args['browse'] = $tab;
146                                 break;
147
148                         case 'favorites':
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 );
151                                 if ( $user )
152                                         $args['user'] = $user;
153                                 else
154                                         $args = false;
155
156                                 add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
157                                 break;
158
159                         default:
160                                 $args = false;
161                                 break;
162                 }
163
164                 /**
165                  * Filter API request arguments for each Plugin Install screen tab.
166                  *
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'.
169                  *
170                  * @since 3.7.0
171                  *
172                  * @param array|bool $args Plugin Install API arguments.
173                  */
174                 $args = apply_filters( "install_plugins_table_api_args_$tab", $args );
175
176                 if ( !$args )
177                         return;
178
179                 $api = plugins_api( 'query_plugins', $args );
180
181                 if ( is_wp_error( $api ) ) {
182                         $this->error = $api;
183                         return;
184                 }
185
186                 $this->items = $api->plugins;
187
188                 if ( $this->orderby ) {
189                         uasort( $this->items, array( $this, 'order_callback' ) );
190                 }
191
192                 $this->set_pagination_args( array(
193                         'total_items' => $api->info['results'],
194                         'per_page' => $args['per_page'],
195                 ) );
196
197                 if ( isset( $api->info['groups'] ) ) {
198                         $this->groups = $api->info['groups'];
199                 }
200         }
201
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>';
205                 } else {
206                         $message = __( 'No plugins match your request.' );
207                 }
208                 echo '<div class="no-plugin-results">' . $message . '</div>';
209         }
210
211         protected function get_views() {
212                 global $tabs, $tab;
213
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>";
219                 }
220                 // No longer a real tab.
221                 unset( $display_tabs['plugin-install-upload'] );
222
223                 return $display_tabs;
224         }
225
226         /**
227          * Override parent views so we can use the filter bar display.
228          */
229         public function views() {
230                 $views = $this->get_views();
231
232                 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
233                 $views = apply_filters( "views_{$this->screen->id}", $views );
234
235 ?>
236 <div class="wp-filter">
237         <ul class="filter-links">
238                 <?php
239                 if ( ! empty( $views ) ) {
240                         foreach ( $views as $class => $view ) {
241                                 $views[ $class ] = "\t<li class='$class'>$view";
242                         }
243                         echo implode( " </li>\n", $views ) . "</li>\n";
244                 }
245                 ?>
246         </ul>
247
248         <?php install_search_form( isset( $views['plugin-install-search'] ) ); ?>
249 </div>
250 <?php
251         }
252
253         /**
254          * Override the parent display() so we can provide a different container.
255          */
256         public function display() {
257                 $singular = $this->_args['singular'];
258
259                 $data_attr = '';
260
261                 if ( $singular ) {
262                         $data_attr = " data-wp-lists='list:$singular'";
263                 }
264
265                 $this->display_tablenav( 'top' );
266
267 ?>
268 <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
269
270         <div id="the-list"<?php echo $data_attr; ?>>
271                 <?php $this->display_rows_or_placeholder(); ?>
272         </div>
273 </div>
274 <?php
275                 $this->display_tablenav( 'bottom' );
276         }
277
278         /**
279          * @param string $which
280          */
281         protected function display_tablenav( $which ) {
282                 if ( $GLOBALS['tab'] === 'featured' ) {
283                         return;
284                 }
285
286                 if ( 'top' ==  $which ) {
287                         wp_referer_field();
288                 ?>
289                         <div class="tablenav top">
290                                 <div class="alignleft actions">
291                                         <?php
292                                         /**
293                                          * Fires before the Plugin Install table header pagination is displayed.
294                                          *
295                                          * @since 2.7.0
296                                          */
297                                         do_action( 'install_plugins_table_header' ); ?>
298                                 </div>
299                                 <?php $this->pagination( $which ); ?>
300                                 <br class="clear" />
301                         </div>
302                 <?php } else { ?>
303                         <div class="tablenav bottom">
304                                 <?php $this->pagination( $which ); ?>
305                                 <br class="clear" />
306                         </div>
307                 <?php
308                 }
309         }
310
311         protected function get_table_classes() {
312                 return array( 'widefat', $this->_args['plural'] );
313         }
314
315         public function get_columns() {
316                 return array();
317         }
318
319         /**
320          * @param object $plugin_a
321          * @param object $plugin_b
322          * @return int
323          */
324         private function order_callback( $plugin_a, $plugin_b ) {
325                 $orderby = $this->orderby;
326                 if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
327                         return 0;
328                 }
329
330                 $a = $plugin_a->$orderby;
331                 $b = $plugin_b->$orderby;
332
333                 if ( $a == $b ) {
334                         return 0;
335                 }
336
337                 if ( 'DESC' == $this->order ) {
338                         return ( $a < $b ) ? 1 : -1;
339                 } else {
340                         return ( $a < $b ) ? -1 : 1;
341                 }
342         }
343
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()
350                 );
351
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' ),
356                 );
357
358                 $group = null;
359
360                 foreach ( (array) $this->items as $plugin ) {
361                         if ( is_object( $plugin ) ) {
362                                 $plugin = (array) $plugin;
363                         }
364
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 ];
371                                         }
372                                 } else {
373                                         $group_name = $plugin['group'];
374                                 }
375
376                                 // Starting a new group, close off the divs of the last one
377                                 if ( ! empty( $group ) ) {
378                                         echo '</div></div>';
379                                 }
380
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">';
384
385                                 $group = $plugin['group'];
386                         }
387                         $title = wp_kses( $plugin['name'], $plugins_allowedtags );
388
389                         // Remove any HTML from the description.
390                         $description = strip_tags( $plugin['short_description'] );
391                         $version = wp_kses( $plugin['version'], $plugins_allowedtags );
392
393                         $name = strip_tags( $title . ' ' . $version );
394
395                         $author = wp_kses( $plugin['author'], $plugins_allowedtags );
396                         if ( ! empty( $author ) ) {
397                                 $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
398                         }
399
400                         $action_links = array();
401
402                         if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
403                                 $status = install_plugin_install_status( $plugin );
404
405                                 switch ( $status['status'] ) {
406                                         case 'install':
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>';
410                                                 }
411
412                                                 break;
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>';
417                                                 }
418
419                                                 break;
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>';
423                                                 break;
424                                 }
425                         }
426
427                         $details_link   = self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
428                                                                 '&amp;TB_iframe=true&amp;width=600&amp;height=550' );
429
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>';
432
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'];
439                         } else {
440                                 $plugin_icon_url = $plugin['icons']['default'];
441                         }
442
443                         /**
444                          * Filter the install action links for a plugin.
445                          *
446                          * @since 2.7.0
447                          *
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.
450                          */
451                         $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
452
453                         $date_format = __( 'M j, Y @ H:i' );
454                         $last_updated_timestamp = strtotime( $plugin['last_updated'] );
455                 ?>
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>
461                                 </div>
462                                 <div class="action-links">
463                                         <?php
464                                                 if ( $action_links ) {
465                                                         echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
466                                                 }
467                                         ?>
468                                 </div>
469                                 <div class="desc column-description">
470                                         <p><?php echo $description; ?></p>
471                                         <p class="authors"><?php echo $author; ?></p>
472                                 </div>
473                         </div>
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>
478                                 </div>
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 ) ); ?>
482                                         </span>
483                                 </div>
484                                 <div class="column-downloaded">
485                                         <?php
486                                         if ( $plugin['active_installs'] >= 1000000 ) {
487                                                 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
488                                         } else {
489                                                 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
490                                         }
491                                         printf( __( '%s Active Installs' ), $active_installs_text );
492                                         ?>
493                                 </div>
494                                 <div class="column-compatibility">
495                                         <?php
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>';
500                                         } else {
501                                                 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
502                                         }
503                                         ?>
504                                 </div>
505                         </div>
506                 </div>
507                 <?php
508                 }
509
510                 // Close off the group divs of the last one
511                 if ( ! empty( $group ) ) {
512                         echo '</div></div>';
513                 }
514         }
515 }