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