~canonical-sysadmins/wordpress/4.2.4

« back to all changes in this revision

Viewing changes to wp-admin/includes/class-wp-plugin-install-list-table.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
                        wp_referer_field();
 
278
                ?>
 
279
                        <div class="tablenav top">
 
280
                                <div class="alignleft actions">
 
281
                                        <?php
 
282
                                        /**
 
283
                                         * Fires before the Plugin Install table header pagination is displayed.
 
284
                                         *
 
285
                                         * @since 2.7.0
 
286
                                         */
 
287
                                        do_action( 'install_plugins_table_header' ); ?>
 
288
                                </div>
 
289
                                <?php $this->pagination( $which ); ?>
 
290
                                <br class="clear" />
 
291
                        </div>
 
292
                <?php } else { ?>
 
293
                        <div class="tablenav bottom">
 
294
                                <?php $this->pagination( $which ); ?>
 
295
                                <br class="clear" />
 
296
                        </div>
 
297
                <?php
 
298
                }
 
299
        }
 
300
 
 
301
        protected function get_table_classes() {
 
302
                return array( 'widefat', $this->_args['plural'] );
 
303
        }
 
304
 
 
305
        public function get_columns() {
 
306
                return array();
 
307
        }
 
308
 
 
309
        private function order_callback( $plugin_a, $plugin_b ) {
 
310
                $orderby = $this->orderby;
 
311
                if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
 
312
                        return 0;
 
313
                }
 
314
 
 
315
                $a = $plugin_a->$orderby;
 
316
                $b = $plugin_b->$orderby;
 
317
 
 
318
                if ( $a == $b ) {
 
319
                        return 0;
 
320
                }
 
321
 
 
322
                if ( 'DESC' == $this->order ) {
 
323
                        return ( $a < $b ) ? 1 : -1;
 
324
                } else {
 
325
                        return ( $a < $b ) ? -1 : 1;
 
326
                }
 
327
        }
 
328
 
 
329
        public function display_rows() {
 
330
                $plugins_allowedtags = array(
 
331
                        'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
 
332
                        'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
 
333
                        'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
 
334
                        'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
 
335
                );
 
336
 
 
337
                $plugins_group_titles = array(
 
338
                        'Performance' => _x( 'Performance', 'Plugin installer group title' ),
 
339
                        'Social'      => _x( 'Social',      'Plugin installer group title' ),
 
340
                        'Tools'       => _x( 'Tools',       'Plugin installer group title' ),
 
341
                );
 
342
 
 
343
                $group = null;
 
344
 
 
345
                foreach ( (array) $this->items as $plugin ) {
 
346
                        if ( is_object( $plugin ) ) {
 
347
                                $plugin = (array) $plugin;
 
348
                        }
 
349
 
 
350
                        // Display the group heading if there is one
 
351
                        if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
 
352
                                if ( isset( $this->groups[ $plugin['group'] ] ) ) {
 
353
                                        $group_name = $this->groups[ $plugin['group'] ];
 
354
                                        if ( isset( $plugins_group_titles[ $group_name ] ) ) {
 
355
                                                $group_name = $plugins_group_titles[ $group_name ];
 
356
                                        }
 
357
                                } else {
 
358
                                        $group_name = $plugin['group'];
 
359
                                }
 
360
 
 
361
                                // Starting a new group, close off the divs of the last one
 
362
                                if ( ! empty( $group ) ) {
 
363
                                        echo '</div></div>';
 
364
                                }
 
365
 
 
366
                                echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
 
367
                                // needs an extra wrapping div for nth-child selectors to work
 
368
                                echo '<div class="plugin-items">';
 
369
 
 
370
                                $group = $plugin['group'];
 
371
                        }
 
372
                        $title = wp_kses( $plugin['name'], $plugins_allowedtags );
 
373
 
 
374
                        // Remove any HTML from the description.
 
375
                        $description = strip_tags( $plugin['short_description'] );
 
376
                        $version = wp_kses( $plugin['version'], $plugins_allowedtags );
 
377
 
 
378
                        $name = strip_tags( $title . ' ' . $version );
 
379
 
 
380
                        $author = wp_kses( $plugin['author'], $plugins_allowedtags );
 
381
                        if ( ! empty( $author ) ) {
 
382
                                $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
 
383
                        }
 
384
 
 
385
                        $action_links = array();
 
386
 
 
387
                        if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
 
388
                                $status = install_plugin_install_status( $plugin );
 
389
 
 
390
                                switch ( $status['status'] ) {
 
391
                                        case 'install':
 
392
                                                if ( $status['url'] ) {
 
393
                                                        /* translators: 1: Plugin name and version. */
 
394
                                                        $action_links[] = '<a class="install-now button" href="' . $status['url'] . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now' ), $name ) ) . '">' . __( 'Install Now' ) . '</a>';
 
395
                                                }
 
396
 
 
397
                                                break;
 
398
                                        case 'update_available':
 
399
                                                if ( $status['url'] ) {
 
400
                                                        /* translators: 1: Plugin name and version */
 
401
                                                        $action_links[] = '<a class="button" href="' . $status['url'] . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now' ), $name ) ) . '">' . __( 'Update Now' ) . '</a>';
 
402
                                                }
 
403
 
 
404
                                                break;
 
405
                                        case 'latest_installed':
 
406
                                        case 'newer_installed':
 
407
                                                $action_links[] = '<span class="button button-disabled" title="' . esc_attr__( 'This plugin is already installed and is up to date' ) . ' ">' . _x( 'Installed', 'plugin' ) . '</span>';
 
408
                                                break;
 
409
                                }
 
410
                        }
 
411
 
 
412
                        $details_link   = self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
 
413
                                                                '&amp;TB_iframe=true&amp;width=600&amp;height=550' );
 
414
 
 
415
                        /* translators: 1: Plugin name and version. */
 
416
                        $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>';
 
417
 
 
418
                        if ( !empty( $plugin['icons']['svg'] ) ) {
 
419
                                $plugin_icon_url = $plugin['icons']['svg'];
 
420
                        } elseif ( !empty( $plugin['icons']['2x'] ) ) {
 
421
                                $plugin_icon_url = $plugin['icons']['2x'];
 
422
                        } elseif ( !empty( $plugin['icons']['1x'] ) ) {
 
423
                                $plugin_icon_url = $plugin['icons']['1x'];
 
424
                        } else {
 
425
                                $plugin_icon_url = $plugin['icons']['default'];
 
426
                        }
 
427
 
 
428
                        /**
 
429
                         * Filter the install action links for a plugin.
 
430
                         *
 
431
                         * @since 2.7.0
 
432
                         *
 
433
                         * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
 
434
                         * @param array $plugin       The plugin currently being listed.
 
435
                         */
 
436
                        $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
 
437
                ?>
 
438
                <div class="plugin-card">
 
439
                        <div class="plugin-card-top">
 
440
                                <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox plugin-icon"><img src="<?php echo esc_attr( $plugin_icon_url ) ?>" /></a>
 
441
                                <div class="name column-name">
 
442
                                        <h4><a href="<?php echo esc_url( $details_link ); ?>" class="thickbox"><?php echo $title; ?></a></h4>
 
443
                                </div>
 
444
                                <div class="action-links">
 
445
                                        <?php
 
446
                                                if ( $action_links ) {
 
447
                                                        echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
 
448
                                                }
 
449
                                        ?>
 
450
                                </div>
 
451
                                <div class="desc column-description">
 
452
                                        <p><?php echo $description; ?></p>
 
453
                                        <p class="authors"><?php echo $author; ?></p>
 
454
                                </div>
 
455
                        </div>
 
456
                        <div class="plugin-card-bottom">
 
457
                                <div class="vers column-rating">
 
458
                                        <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
 
459
                                        <span class="num-ratings">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
 
460
                                </div>
 
461
                                <div class="column-updated">
 
462
                                        <strong><?php _e( 'Last Updated:' ); ?></strong> <span title="<?php echo esc_attr( $plugin['last_updated'] ); ?>">
 
463
                                                <?php printf( __( '%s ago' ), human_time_diff( strtotime( $plugin['last_updated'] ) ) ); ?>
 
464
                                        </span>
 
465
                                </div>
 
466
                                <div class="column-downloaded">
 
467
                                        <?php echo sprintf( _n( '%s download', '%s downloads', $plugin['downloaded'] ), number_format_i18n( $plugin['downloaded'] ) ); ?>
 
468
                                </div>
 
469
                                <div class="column-compatibility">
 
470
                                        <?php
 
471
                                        if ( ! empty( $plugin['tested'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
 
472
                                                echo '<span class="compatibility-untested">' . __( '<strong>Untested</strong> with your version of WordPress' ) . '</span>';
 
473
                                        } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $GLOBALS['wp_version'], 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
 
474
                                                echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
 
475
                                        } else {
 
476
                                                echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
 
477
                                        }
 
478
                                        ?>
 
479
                                </div>
 
480
                        </div>
 
481
                </div>
 
482
                <?php
 
483
                }
 
484
 
 
485
                // Close off the group divs of the last one
 
486
                if ( ! empty( $group ) ) {
 
487
                        echo '</div></div>';
 
488
                }
 
489
        }
 
490
}