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