]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-plugin-install-list-table.php
Wordpress 4.5.3-scripts
[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                  * 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                                 $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                  * Filter 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( isset( $views['plugin-install-search'] ) ); ?>
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
465                                                 break;
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
472                                                 break;
473                                         case 'latest_installed':
474                                         case 'newer_installed':
475                                                 $action_links[] = '<span class="button button-disabled">' . _x( 'Installed', 'plugin' ) . '</span>';
476                                                 break;
477                                 }
478                         }
479
480                         $details_link   = self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
481                                                                 '&amp;TB_iframe=true&amp;width=600&amp;height=550' );
482
483                         /* translators: 1: Plugin name and version. */
484                         $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>';
485
486                         if ( !empty( $plugin['icons']['svg'] ) ) {
487                                 $plugin_icon_url = $plugin['icons']['svg'];
488                         } elseif ( !empty( $plugin['icons']['2x'] ) ) {
489                                 $plugin_icon_url = $plugin['icons']['2x'];
490                         } elseif ( !empty( $plugin['icons']['1x'] ) ) {
491                                 $plugin_icon_url = $plugin['icons']['1x'];
492                         } else {
493                                 $plugin_icon_url = $plugin['icons']['default'];
494                         }
495
496                         /**
497                          * Filter the install action links for a plugin.
498                          *
499                          * @since 2.7.0
500                          *
501                          * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
502                          * @param array $plugin       The plugin currently being listed.
503                          */
504                         $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
505
506                         $last_updated_timestamp = strtotime( $plugin['last_updated'] );
507                 ?>
508                 <div class="plugin-card plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
509                         <div class="plugin-card-top">
510                                 <div class="name column-name">
511                                         <h3>
512                                                 <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox open-plugin-details-modal">
513                                                 <?php echo $title; ?>
514                                                 <img src="<?php echo esc_attr( $plugin_icon_url ) ?>" class="plugin-icon" alt="">
515                                                 </a>
516                                         </h3>
517                                 </div>
518                                 <div class="action-links">
519                                         <?php
520                                                 if ( $action_links ) {
521                                                         echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
522                                                 }
523                                         ?>
524                                 </div>
525                                 <div class="desc column-description">
526                                         <p><?php echo $description; ?></p>
527                                         <p class="authors"><?php echo $author; ?></p>
528                                 </div>
529                         </div>
530                         <div class="plugin-card-bottom">
531                                 <div class="vers column-rating">
532                                         <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
533                                         <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
534                                 </div>
535                                 <div class="column-updated">
536                                         <strong><?php _e( 'Last Updated:' ); ?></strong> <?php printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) ); ?>
537                                 </div>
538                                 <div class="column-downloaded">
539                                         <?php
540                                         if ( $plugin['active_installs'] >= 1000000 ) {
541                                                 $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
542                                         } else {
543                                                 $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
544                                         }
545                                         printf( __( '%s Active Installs' ), $active_installs_text );
546                                         ?>
547                                 </div>
548                                 <div class="column-compatibility">
549                                         <?php
550                                         if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
551                                                 echo '<span class="compatibility-untested">' . __( 'Untested with your version of WordPress' ) . '</span>';
552                                         } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
553                                                 echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
554                                         } else {
555                                                 echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
556                                         }
557                                         ?>
558                                 </div>
559                         </div>
560                 </div>
561                 <?php
562                 }
563
564                 // Close off the group divs of the last one
565                 if ( ! empty( $group ) ) {
566                         echo '</div></div>';
567                 }
568         }
569 }