]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-theme-install-list-table.php
WordPress 4.4-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-theme-install-list-table.php
1 <?php
2 /**
3  * List Table API: WP_Theme_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 themes to install in a list table.
12  *
13  * @since 3.1.0
14  * @access private
15  *
16  * @see WP_Thenes_List_Table
17  */
18 class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
19
20         public $features = array();
21
22         /**
23          *
24          * @return bool
25          */
26         public function ajax_user_can() {
27                 return current_user_can( 'install_themes' );
28         }
29
30         /**
31          *
32          * @global array  $tabs
33          * @global string $tab
34          * @global int    $paged
35          * @global string $type
36          * @global array  $theme_field_defaults
37          */
38         public function prepare_items() {
39                 include( ABSPATH . 'wp-admin/includes/theme-install.php' );
40
41                 global $tabs, $tab, $paged, $type, $theme_field_defaults;
42                 wp_reset_vars( array( 'tab' ) );
43
44                 $search_terms = array();
45                 $search_string = '';
46                 if ( ! empty( $_REQUEST['s'] ) ){
47                         $search_string = strtolower( wp_unslash( $_REQUEST['s'] ) );
48                         $search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', $search_string ) ) ) );
49                 }
50
51                 if ( ! empty( $_REQUEST['features'] ) )
52                         $this->features = $_REQUEST['features'];
53
54                 $paged = $this->get_pagenum();
55
56                 $per_page = 36;
57
58                 // These are the tabs which are shown on the page,
59                 $tabs = array();
60                 $tabs['dashboard'] = __( 'Search' );
61                 if ( 'search' === $tab )
62                         $tabs['search'] = __( 'Search Results' );
63                 $tabs['upload'] = __( 'Upload' );
64                 $tabs['featured'] = _x( 'Featured', 'themes' );
65                 //$tabs['popular']  = _x( 'Popular', 'themes' );
66                 $tabs['new']      = _x( 'Latest', 'themes' );
67                 $tabs['updated']  = _x( 'Recently Updated', 'themes' );
68
69                 $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
70
71                 /** This filter is documented in wp-admin/theme-install.php */
72                 $tabs = apply_filters( 'install_themes_tabs', $tabs );
73
74                 /**
75                  * Filter tabs not associated with a menu item on the Install Themes screen.
76                  *
77                  * @since 2.8.0
78                  *
79                  * @param array $nonmenu_tabs The tabs that don't have a menu item on
80                  *                            the Install Themes screen.
81                  */
82                 $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
83
84                 // If a non-valid menu tab has been selected, And it's not a non-menu action.
85                 if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) )
86                         $tab = key( $tabs );
87
88                 $args = array( 'page' => $paged, 'per_page' => $per_page, 'fields' => $theme_field_defaults );
89
90                 switch ( $tab ) {
91                         case 'search':
92                                 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
93                                 switch ( $type ) {
94                                         case 'tag':
95                                                 $args['tag'] = array_map( 'sanitize_key', $search_terms );
96                                                 break;
97                                         case 'term':
98                                                 $args['search'] = $search_string;
99                                                 break;
100                                         case 'author':
101                                                 $args['author'] = $search_string;
102                                                 break;
103                                 }
104
105                                 if ( ! empty( $this->features ) ) {
106                                         $args['tag'] = $this->features;
107                                         $_REQUEST['s'] = implode( ',', $this->features );
108                                         $_REQUEST['type'] = 'tag';
109                                 }
110
111                                 add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
112                                 break;
113
114                         case 'featured':
115                         // case 'popular':
116                         case 'new':
117                         case 'updated':
118                                 $args['browse'] = $tab;
119                                 break;
120
121                         default:
122                                 $args = false;
123                                 break;
124                 }
125
126                 /**
127                  * Filter API request arguments for each Install Themes screen tab.
128                  *
129                  * The dynamic portion of the hook name, `$tab`, refers to the theme install
130                  * tabs. Default tabs are 'dashboard', 'search', 'upload', 'featured',
131                  * 'new', and 'updated'.
132                  *
133                  * @since 3.7.0
134                  *
135                  * @param array $args An array of themes API arguments.
136                  */
137                 $args = apply_filters( 'install_themes_table_api_args_' . $tab, $args );
138
139                 if ( ! $args )
140                         return;
141
142                 $api = themes_api( 'query_themes', $args );
143
144                 if ( is_wp_error( $api ) )
145                         wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
146
147                 $this->items = $api->themes;
148
149                 $this->set_pagination_args( array(
150                         'total_items' => $api->info['results'],
151                         'per_page' => $args['per_page'],
152                         'infinite_scroll' => true,
153                 ) );
154         }
155
156         /**
157          * @access public
158          */
159         public function no_items() {
160                 _e( 'No themes match your request.' );
161         }
162
163         /**
164          *
165          * @global array $tabs
166          * @global string $tab
167          * @return array
168          */
169         protected function get_views() {
170                 global $tabs, $tab;
171
172                 $display_tabs = array();
173                 foreach ( (array) $tabs as $action => $text ) {
174                         $class = ( $action === $tab ) ? ' class="current"' : '';
175                         $href = self_admin_url('theme-install.php?tab=' . $action);
176                         $display_tabs['theme-install-'.$action] = "<a href='$href'$class>$text</a>";
177                 }
178
179                 return $display_tabs;
180         }
181
182         /**
183          * @access public
184          */
185         public function display() {
186                 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
187 ?>
188                 <div class="tablenav top themes">
189                         <div class="alignleft actions">
190                                 <?php
191                                 /**
192                                  * Fires in the Install Themes list table header.
193                                  *
194                                  * @since 2.8.0
195                                  */
196                                 do_action( 'install_themes_table_header' );
197                                 ?>
198                         </div>
199                         <?php $this->pagination( 'top' ); ?>
200                         <br class="clear" />
201                 </div>
202
203                 <div id="availablethemes">
204                         <?php $this->display_rows_or_placeholder(); ?>
205                 </div>
206
207                 <?php
208                 $this->tablenav( 'bottom' );
209         }
210
211         /**
212          * @access public
213          */
214         public function display_rows() {
215                 $themes = $this->items;
216                 foreach ( $themes as $theme ) {
217                                 ?>
218                                 <div class="available-theme installable-theme"><?php
219                                         $this->single_row( $theme );
220                                 ?></div>
221                 <?php } // end foreach $theme_names
222
223                 $this->theme_installer();
224         }
225
226         /**
227          * Prints a theme from the WordPress.org API.
228          *
229          * @global array $themes_allowedtags
230          *
231          * @param object $theme An object that contains theme data returned by the WordPress.org API.
232          *
233          * Example theme data:
234          *   object(stdClass)[59]
235          *     public 'name' => string 'Magazine Basic'
236          *     public 'slug' => string 'magazine-basic'
237          *     public 'version' => string '1.1'
238          *     public 'author' => string 'tinkerpriest'
239          *     public 'preview_url' => string 'http://wp-themes.com/?magazine-basic'
240          *     public 'screenshot_url' => string 'http://wp-themes.com/wp-content/themes/magazine-basic/screenshot.png'
241          *     public 'rating' => float 80
242          *     public 'num_ratings' => int 1
243          *     public 'homepage' => string 'http://wordpress.org/themes/magazine-basic'
244          *     public 'description' => string 'A basic magazine style layout with a fully customizable layout through a backend interface. Designed by <a href="http://bavotasan.com">c.bavota</a> of <a href="http://tinkerpriestmedia.com">Tinker Priest Media</a>.'
245          *     public 'download_link' => string 'http://wordpress.org/themes/download/magazine-basic.1.1.zip'
246          */
247         public function single_row( $theme ) {
248                 global $themes_allowedtags;
249
250                 if ( empty( $theme ) )
251                         return;
252
253                 $name   = wp_kses( $theme->name,   $themes_allowedtags );
254                 $author = wp_kses( $theme->author, $themes_allowedtags );
255
256                 $preview_title = sprintf( __('Preview &#8220;%s&#8221;'), $name );
257                 $preview_url   = add_query_arg( array(
258                         'tab'   => 'theme-information',
259                         'theme' => $theme->slug,
260                 ), self_admin_url( 'theme-install.php' ) );
261
262                 $actions = array();
263
264                 $install_url = add_query_arg( array(
265                         'action' => 'install-theme',
266                         'theme'  => $theme->slug,
267                 ), self_admin_url( 'update.php' ) );
268
269                 $update_url = add_query_arg( array(
270                         'action' => 'upgrade-theme',
271                         'theme'  => $theme->slug,
272                 ), self_admin_url( 'update.php' ) );
273
274                 $status = $this->_get_theme_status( $theme );
275
276                 switch ( $status ) {
277                         case 'update_available':
278                                 $actions[] = '<a class="install-now" href="' . esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ) . '" title="' . esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ) . '">' . __( 'Update' ) . '</a>';
279                                 break;
280                         case 'newer_installed':
281                         case 'latest_installed':
282                                 $actions[] = '<span class="install-now" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
283                                 break;
284                         case 'install':
285                         default:
286                                 $actions[] = '<a class="install-now" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '" title="' . esc_attr( sprintf( __( 'Install %s' ), $name ) ) . '">' . __( 'Install Now' ) . '</a>';
287                                 break;
288                 }
289
290                 $actions[] = '<a class="install-theme-preview" href="' . esc_url( $preview_url ) . '" title="' . esc_attr( sprintf( __( 'Preview %s' ), $name ) ) . '">' . __( 'Preview' ) . '</a>';
291
292                 /**
293                  * Filter the install action links for a theme in the Install Themes list table.
294                  *
295                  * @since 3.4.0
296                  *
297                  * @param array    $actions An array of theme action hyperlinks. Defaults are
298                  *                          links to Install Now, Preview, and Details.
299                  * @param WP_Theme $theme   Theme object.
300                  */
301                 $actions = apply_filters( 'theme_install_actions', $actions, $theme );
302
303                 ?>
304                 <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
305                         <img src="<?php echo esc_url( $theme->screenshot_url ); ?>" width="150" alt="" />
306                 </a>
307
308                 <h3><?php echo $name; ?></h3>
309                 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
310
311                 <div class="action-links">
312                         <ul>
313                                 <?php foreach ( $actions as $action ): ?>
314                                         <li><?php echo $action; ?></li>
315                                 <?php endforeach; ?>
316                                 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
317                         </ul>
318                 </div>
319
320                 <?php
321                 $this->install_theme_info( $theme );
322         }
323
324         /**
325          * Prints the wrapper for the theme installer.
326          */
327         public function theme_installer() {
328                 ?>
329                 <div id="theme-installer" class="wp-full-overlay expanded">
330                         <div class="wp-full-overlay-sidebar">
331                                 <div class="wp-full-overlay-header">
332                                         <a href="#" class="close-full-overlay button-secondary"><?php _e( 'Close' ); ?></a>
333                                         <span class="theme-install"></span>
334                                 </div>
335                                 <div class="wp-full-overlay-sidebar-content">
336                                         <div class="install-theme-info"></div>
337                                 </div>
338                                 <div class="wp-full-overlay-footer">
339                                         <button type="button" class="collapse-sidebar button-secondary" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
340                                                 <span class="collapse-sidebar-arrow"></span>
341                                                 <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
342                                         </button>
343                                 </div>
344                         </div>
345                         <div class="wp-full-overlay-main"></div>
346                 </div>
347                 <?php
348         }
349
350         /**
351          * Prints the wrapper for the theme installer with a provided theme's data.
352          * Used to make the theme installer work for no-js.
353          *
354          * @param object $theme - A WordPress.org Theme API object.
355          */
356         public function theme_installer_single( $theme ) {
357                 ?>
358                 <div id="theme-installer" class="wp-full-overlay single-theme">
359                         <div class="wp-full-overlay-sidebar">
360                                 <?php $this->install_theme_info( $theme ); ?>
361                         </div>
362                         <div class="wp-full-overlay-main">
363                                 <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
364                         </div>
365                 </div>
366                 <?php
367         }
368
369         /**
370          * Prints the info for a theme (to be used in the theme installer modal).
371          *
372          * @global array $themes_allowedtags
373          *
374          * @param object $theme - A WordPress.org Theme API object.
375          */
376         public function install_theme_info( $theme ) {
377                 global $themes_allowedtags;
378
379                 if ( empty( $theme ) )
380                         return;
381
382                 $name   = wp_kses( $theme->name,   $themes_allowedtags );
383                 $author = wp_kses( $theme->author, $themes_allowedtags );
384
385                 $install_url = add_query_arg( array(
386                         'action' => 'install-theme',
387                         'theme'  => $theme->slug,
388                 ), self_admin_url( 'update.php' ) );
389
390                 $update_url = add_query_arg( array(
391                         'action' => 'upgrade-theme',
392                         'theme'  => $theme->slug,
393                 ), self_admin_url( 'update.php' ) );
394
395                 $status = $this->_get_theme_status( $theme );
396
397                 ?>
398                 <div class="install-theme-info"><?php
399                         switch ( $status ) {
400                                 case 'update_available':
401                                         echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ) . '" title="' . esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ) . '">' . __( 'Update' ) . '</a>';
402                                         break;
403                                 case 'newer_installed':
404                                 case 'latest_installed':
405                                         echo '<span class="theme-install" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
406                                         break;
407                                 case 'install':
408                                 default:
409                                         echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '">' . __( 'Install' ) . '</a>';
410                                         break;
411                         } ?>
412                         <h3 class="theme-name"><?php echo $name; ?></h3>
413                         <span class="theme-by"><?php printf( __( 'By %s' ), $author ); ?></span>
414                         <?php if ( isset( $theme->screenshot_url ) ): ?>
415                                 <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" alt="" />
416                         <?php endif; ?>
417                         <div class="theme-details">
418                                 <?php wp_star_rating( array( 'rating' => $theme->rating, 'type' => 'percent', 'number' => $theme->num_ratings ) ); ?>
419                                 <div class="theme-version">
420                                         <strong><?php _e('Version:') ?> </strong>
421                                         <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
422                                 </div>
423                                 <div class="theme-description">
424                                         <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
425                                 </div>
426                         </div>
427                         <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
428                 </div>
429                 <?php
430         }
431
432         /**
433          * Send required variables to JavaScript land
434          *
435          * @since 3.4.0
436          * @access public
437          *
438          * @global string $tab  Current tab within Themes->Install screen
439          * @global string $type Type of search.
440          *
441          * @param array $extra_args Unused.
442          */
443         public function _js_vars( $extra_args = array() ) {
444                 global $tab, $type;
445                 parent::_js_vars( compact( 'tab', 'type' ) );
446         }
447
448         /**
449          * Check to see if the theme is already installed.
450          *
451          * @since 3.4.0
452          * @access private
453          *
454          * @param object $theme - A WordPress.org Theme API object.
455          * @return string Theme status.
456          */
457         private function _get_theme_status( $theme ) {
458                 $status = 'install';
459
460                 $installed_theme = wp_get_theme( $theme->slug );
461                 if ( $installed_theme->exists() ) {
462                         if ( version_compare( $installed_theme->get('Version'), $theme->version, '=' ) )
463                                 $status = 'latest_installed';
464                         elseif ( version_compare( $installed_theme->get('Version'), $theme->version, '>' ) )
465                                 $status = 'newer_installed';
466                         else
467                                 $status = 'update_available';
468                 }
469
470                 return $status;
471         }
472 }