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