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