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