]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-upgrader-skins.php
WordPress 3.9-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-upgrader-skins.php
1 <?php
2 /**
3  * The User Interface "Skins" for the WordPress File Upgrader
4  *
5  * @package WordPress
6  * @subpackage Upgrader
7  * @since 2.8.0
8  */
9
10 /**
11  * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
12  *
13  * @package WordPress
14  * @subpackage Upgrader
15  * @since 2.8.0
16  */
17 class WP_Upgrader_Skin {
18
19         var $upgrader;
20         var $done_header = false;
21         var $result = false;
22
23         function __construct($args = array()) {
24                 $defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
25                 $this->options = wp_parse_args($args, $defaults);
26         }
27
28         function set_upgrader(&$upgrader) {
29                 if ( is_object($upgrader) )
30                         $this->upgrader =& $upgrader;
31                 $this->add_strings();
32         }
33
34         function add_strings() {
35         }
36
37         function set_result($result) {
38                 $this->result = $result;
39         }
40
41         function request_filesystem_credentials($error = false) {
42                 $url = $this->options['url'];
43                 $context = $this->options['context'];
44                 if ( !empty($this->options['nonce']) )
45                         $url = wp_nonce_url($url, $this->options['nonce']);
46                 return request_filesystem_credentials($url, '', $error, $context); //Possible to bring inline, Leaving as is for now.
47         }
48
49         function header() {
50                 if ( $this->done_header )
51                         return;
52                 $this->done_header = true;
53                 echo '<div class="wrap">';
54                 echo '<h2>' . $this->options['title'] . '</h2>';
55         }
56         function footer() {
57                 echo '</div>';
58         }
59
60         function error($errors) {
61                 if ( ! $this->done_header )
62                         $this->header();
63                 if ( is_string($errors) ) {
64                         $this->feedback($errors);
65                 } elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
66                         foreach ( $errors->get_error_messages() as $message ) {
67                                 if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) )
68                                         $this->feedback($message . ' ' . esc_html( strip_tags( $errors->get_error_data() ) ) );
69                                 else
70                                         $this->feedback($message);
71                         }
72                 }
73         }
74
75         function feedback($string) {
76                 if ( isset( $this->upgrader->strings[$string] ) )
77                         $string = $this->upgrader->strings[$string];
78
79                 if ( strpos($string, '%') !== false ) {
80                         $args = func_get_args();
81                         $args = array_splice($args, 1);
82                         if ( $args ) {
83                                 $args = array_map( 'strip_tags', $args );
84                                 $args = array_map( 'esc_html', $args );
85                                 $string = vsprintf($string, $args);
86                         }
87                 }
88                 if ( empty($string) )
89                         return;
90                 show_message($string);
91         }
92         function before() {}
93         function after() {}
94
95         /**
96          * Output JavaScript that calls function to decrement the update counts.
97          *
98          * @since 3.9.0
99          *
100          * @param string $type Type of update count to decrement. Likely values include 'plugin',
101          *                     'theme', 'translation', etc.
102          */
103         protected function decrement_update_count( $type ) {
104                 if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) {
105                         return;
106                 }
107                 echo '<script type="text/javascript">
108                                 (function( wp ) {
109                                         if ( wp && wp.updates.decrementCount ) {
110                                                 wp.updates.decrementCount( "' . $type . '" );
111                                         }
112                                 })( window.wp );
113                         </script>';
114         }
115 }
116
117 /**
118  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
119  *
120  * @package WordPress
121  * @subpackage Upgrader
122  * @since 2.8.0
123  */
124 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
125         var $plugin = '';
126         var $plugin_active = false;
127         var $plugin_network_active = false;
128
129         function __construct($args = array()) {
130                 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Update Plugin') );
131                 $args = wp_parse_args($args, $defaults);
132
133                 $this->plugin = $args['plugin'];
134
135                 $this->plugin_active = is_plugin_active( $this->plugin );
136                 $this->plugin_network_active = is_plugin_active_for_network( $this->plugin );
137
138                 parent::__construct($args);
139         }
140
141         function after() {
142                 $this->plugin = $this->upgrader->plugin_info();
143                 if ( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
144                         echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&networkwide=' . $this->plugin_network_active . '&plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin) .'"></iframe>';
145                 }
146
147                 $this->decrement_update_count( 'plugin' );
148
149                 $update_actions =  array(
150                         'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
151                         'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
152                 );
153                 if ( $this->plugin_active || ! $this->result || is_wp_error( $this->result ) || ! current_user_can( 'activate_plugins' ) )
154                         unset( $update_actions['activate_plugin'] );
155
156                 /**
157                  * Filter the list of action links available following a single plugin update.
158                  *
159                  * @since 2.7.0
160                  *
161                  * @param array  $update_actions Array of plugin action links.
162                  * @param string $plugin         Path to the plugin file.
163                  */
164                 $update_actions = apply_filters( 'update_plugin_complete_actions', $update_actions, $this->plugin );
165
166                 if ( ! empty($update_actions) )
167                         $this->feedback(implode(' | ', (array)$update_actions));
168         }
169 }
170
171 /**
172  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
173  *
174  * @package WordPress
175  * @subpackage Upgrader
176  * @since 3.0.0
177  */
178 class Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
179         var $in_loop = false;
180         var $error = false;
181
182         function __construct($args = array()) {
183                 $defaults = array( 'url' => '', 'nonce' => '' );
184                 $args = wp_parse_args($args, $defaults);
185
186                 parent::__construct($args);
187         }
188
189         function add_strings() {
190                 $this->upgrader->strings['skin_upgrade_start'] = __('The update process is starting. This process may take a while on some hosts, so please be patient.');
191                 $this->upgrader->strings['skin_update_failed_error'] = __('An error occurred while updating %1$s: <strong>%2$s</strong>');
192                 $this->upgrader->strings['skin_update_failed'] = __('The update of %1$s failed.');
193                 $this->upgrader->strings['skin_update_successful'] = __('%1$s updated successfully.').' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>'.__('Show Details').'</span><span class="hidden">'.__('Hide Details').'</span>.</a>';
194                 $this->upgrader->strings['skin_upgrade_end'] = __('All updates have been completed.');
195         }
196
197         function feedback($string) {
198                 if ( isset( $this->upgrader->strings[$string] ) )
199                         $string = $this->upgrader->strings[$string];
200
201                 if ( strpos($string, '%') !== false ) {
202                         $args = func_get_args();
203                         $args = array_splice($args, 1);
204                         if ( $args ) {
205                                 $args = array_map( 'strip_tags', $args );
206                                 $args = array_map( 'esc_html', $args );
207                                 $string = vsprintf($string, $args);
208                         }
209                 }
210                 if ( empty($string) )
211                         return;
212                 if ( $this->in_loop )
213                         echo "$string<br />\n";
214                 else
215                         echo "<p>$string</p>\n";
216         }
217
218         function header() {
219                 // Nothing, This will be displayed within a iframe.
220         }
221
222         function footer() {
223                 // Nothing, This will be displayed within a iframe.
224         }
225         function error($error) {
226                 if ( is_string($error) && isset( $this->upgrader->strings[$error] ) )
227                         $this->error = $this->upgrader->strings[$error];
228
229                 if ( is_wp_error($error) ) {
230                         foreach ( $error->get_error_messages() as $emessage ) {
231                                 if ( $error->get_error_data() && is_string( $error->get_error_data() ) )
232                                         $messages[] = $emessage . ' ' . esc_html( strip_tags( $error->get_error_data() ) );
233                                 else
234                                         $messages[] = $emessage;
235                         }
236                         $this->error = implode(', ', $messages);
237                 }
238                 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
239         }
240
241         function bulk_header() {
242                 $this->feedback('skin_upgrade_start');
243         }
244
245         function bulk_footer() {
246                 $this->feedback('skin_upgrade_end');
247         }
248
249         function before($title = '') {
250                 $this->in_loop = true;
251                 printf( '<h4>' . $this->upgrader->strings['skin_before_update_header'] . ' <span class="spinner waiting-' . $this->upgrader->update_current . '"></span></h4>',  $title, $this->upgrader->update_current, $this->upgrader->update_count);
252                 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').css("display", "inline-block");</script>';
253                 echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr($this->upgrader->update_current) . '"><p>';
254                 $this->flush_output();
255         }
256
257         function after($title = '') {
258                 echo '</p></div>';
259                 if ( $this->error || ! $this->result ) {
260                         if ( $this->error )
261                                 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed_error'], $title, $this->error) . '</p></div>';
262                         else
263                                 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed'], $title) . '</p></div>';
264
265                         echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
266                 }
267                 if ( $this->result && ! is_wp_error( $this->result ) ) {
268                         if ( ! $this->error )
269                                 echo '<div class="updated"><p>' . sprintf($this->upgrader->strings['skin_update_successful'], $title, 'jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').toggle();jQuery(\'span\', this).toggle(); return false;') . '</p></div>';
270                         echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
271                 }
272
273                 $this->reset();
274                 $this->flush_output();
275         }
276
277         function reset() {
278                 $this->in_loop = false;
279                 $this->error = false;
280         }
281
282         function flush_output() {
283                 wp_ob_end_flush_all();
284                 flush();
285         }
286
287         /**
288          * Output JavaScript that sends message to parent window to decrement the update counts.
289          *
290          * @since 3.9.0
291          *
292          * @param string $type Type of update count to decrement. Likely values include 'plugin',
293          *                     'theme', 'translation', etc.
294          */
295         protected function decrement_update_count( $type ) {
296                 if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) {
297                         return;
298                 }
299                 echo '<script type="text/javascript">
300                                 if ( window.postMessage && JSON ) {
301                                         window.parent.postMessage( JSON.stringify( { action: "decrementUpdateCount", upgradeType: "' . $type . '" } ), window.location.protocol + "//" + window.location.hostname );
302                                 }
303                         </script>';
304         }
305 }
306
307 class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin {
308         var $plugin_info = array(); // Plugin_Upgrader::bulk() will fill this in.
309
310         function __construct($args = array()) {
311                 parent::__construct($args);
312         }
313
314         function add_strings() {
315                 parent::add_strings();
316                 $this->upgrader->strings['skin_before_update_header'] = __('Updating Plugin %1$s (%2$d/%3$d)');
317         }
318
319         function before($title = '') {
320                 parent::before($this->plugin_info['Title']);
321         }
322
323         function after($title = '') {
324                 parent::after($this->plugin_info['Title']);
325                 $this->decrement_update_count( 'plugin' );
326         }
327         function bulk_footer() {
328                 parent::bulk_footer();
329                 $update_actions =  array(
330                         'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>',
331                         'updates_page' => '<a href="' . self_admin_url('update-core.php') . '" title="' . esc_attr__('Go to WordPress Updates page') . '" target="_parent">' . __('Return to WordPress Updates') . '</a>'
332                 );
333                 if ( ! current_user_can( 'activate_plugins' ) )
334                         unset( $update_actions['plugins_page'] );
335
336                 /**
337                  * Filter the list of action links available following bulk plugin updates.
338                  *
339                  * @since 3.0.0
340                  *
341                  * @param array $update_actions Array of plugin action links.
342                  * @param array $plugin_info    Array of information for the last-updated plugin.
343                  */
344                 $update_actions = apply_filters( 'update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
345
346                 if ( ! empty($update_actions) )
347                         $this->feedback(implode(' | ', (array)$update_actions));
348         }
349 }
350
351 class Bulk_Theme_Upgrader_Skin extends Bulk_Upgrader_Skin {
352         var $theme_info = array(); // Theme_Upgrader::bulk() will fill this in.
353
354         function __construct($args = array()) {
355                 parent::__construct($args);
356         }
357
358         function add_strings() {
359                 parent::add_strings();
360                 $this->upgrader->strings['skin_before_update_header'] = __('Updating Theme %1$s (%2$d/%3$d)');
361         }
362
363         function before($title = '') {
364                 parent::before( $this->theme_info->display('Name') );
365         }
366
367         function after($title = '') {
368                 parent::after( $this->theme_info->display('Name') );
369                 $this->decrement_update_count( 'theme' );
370         }
371
372         function bulk_footer() {
373                 parent::bulk_footer();
374                 $update_actions =  array(
375                         'themes_page' => '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Go to themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>',
376                         'updates_page' => '<a href="' . self_admin_url('update-core.php') . '" title="' . esc_attr__('Go to WordPress Updates page') . '" target="_parent">' . __('Return to WordPress Updates') . '</a>'
377                 );
378                 if ( ! current_user_can( 'switch_themes' ) && ! current_user_can( 'edit_theme_options' ) )
379                         unset( $update_actions['themes_page'] );
380
381                 /**
382                  * Filter the list of action links available following bulk theme updates.
383                  *
384                  * @since 3.0.0
385                  *
386                  * @param array $update_actions Array of theme action links.
387                  * @param array $theme_info     Array of information for the last-updated theme.
388                  */
389                 $update_actions = apply_filters( 'update_bulk_theme_complete_actions', $update_actions, $this->theme_info );
390
391                 if ( ! empty($update_actions) )
392                         $this->feedback(implode(' | ', (array)$update_actions));
393         }
394 }
395
396 /**
397  * Plugin Installer Skin for WordPress Plugin Installer.
398  *
399  * @package WordPress
400  * @subpackage Upgrader
401  * @since 2.8.0
402  */
403 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
404         var $api;
405         var $type;
406
407         function __construct($args = array()) {
408                 $defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
409                 $args = wp_parse_args($args, $defaults);
410
411                 $this->type = $args['type'];
412                 $this->api = isset($args['api']) ? $args['api'] : array();
413
414                 parent::__construct($args);
415         }
416
417         function before() {
418                 if ( !empty($this->api) )
419                         $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
420         }
421
422         function after() {
423
424                 $plugin_file = $this->upgrader->plugin_info();
425
426                 $install_actions = array();
427
428                 $from = isset($_GET['from']) ? wp_unslash( $_GET['from'] ) : 'plugins';
429
430                 if ( 'import' == $from )
431                         $install_actions['activate_plugin'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;from=import&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin &amp; Run Importer') . '</a>';
432                 else
433                         $install_actions['activate_plugin'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>';
434
435                 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
436                         $install_actions['network_activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin for all sites in this network') . '" target="_parent">' . __('Network Activate') . '</a>';
437                         unset( $install_actions['activate_plugin'] );
438                 }
439
440                 if ( 'import' == $from )
441                         $install_actions['importers_page'] = '<a href="' . admin_url('import.php') . '" title="' . esc_attr__('Return to Importers') . '" target="_parent">' . __('Return to Importers') . '</a>';
442                 else if ( $this->type == 'web' )
443                         $install_actions['plugins_page'] = '<a href="' . self_admin_url('plugin-install.php') . '" title="' . esc_attr__('Return to Plugin Installer') . '" target="_parent">' . __('Return to Plugin Installer') . '</a>';
444                 else
445                         $install_actions['plugins_page'] = '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Return to Plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>';
446
447                 if ( ! $this->result || is_wp_error($this->result) ) {
448                         unset( $install_actions['activate_plugin'], $install_actions['network_activate'] );
449                 } elseif ( ! current_user_can( 'activate_plugins' ) ) {
450                         unset( $install_actions['activate_plugin'] );
451                 }
452
453                 /**
454                  * Filter the list of action links available following a single plugin installation.
455                  *
456                  * @since 2.7.0
457                  *
458                  * @param array  $install_actions Array of plugin action links.
459                  * @param object $api             Object containing WordPress.org API plugin data. Empty
460                  *                                for non-API installs, such as when a plugin is installed
461                  *                                via upload.
462                  * @param string $plugin_file     Path to the plugin file.
463                  */
464                 $install_actions = apply_filters( 'install_plugin_complete_actions', $install_actions, $this->api, $plugin_file );
465
466                 if ( ! empty($install_actions) )
467                         $this->feedback(implode(' | ', (array)$install_actions));
468         }
469 }
470
471 /**
472  * Theme Installer Skin for the WordPress Theme Installer.
473  *
474  * @package WordPress
475  * @subpackage Upgrader
476  * @since 2.8.0
477  */
478 class Theme_Installer_Skin extends WP_Upgrader_Skin {
479         var $api;
480         var $type;
481
482         function __construct($args = array()) {
483                 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
484                 $args = wp_parse_args($args, $defaults);
485
486                 $this->type = $args['type'];
487                 $this->api = isset($args['api']) ? $args['api'] : array();
488
489                 parent::__construct($args);
490         }
491
492         function before() {
493                 if ( !empty($this->api) )
494                         $this->upgrader->strings['process_success'] = sprintf( $this->upgrader->strings['process_success_specific'], $this->api->name, $this->api->version);
495         }
496
497         function after() {
498                 if ( empty($this->upgrader->result['destination_name']) )
499                         return;
500
501                 $theme_info = $this->upgrader->theme_info();
502                 if ( empty( $theme_info ) )
503                         return;
504
505                 $name       = $theme_info->display('Name');
506                 $stylesheet = $this->upgrader->result['destination_name'];
507                 $template   = $theme_info->get_template();
508
509                 $preview_link = add_query_arg( array(
510                         'preview'    => 1,
511                         'template'   => urlencode( $template ),
512                         'stylesheet' => urlencode( $stylesheet ),
513                 ), trailingslashit( home_url() ) );
514
515                 $activate_link = add_query_arg( array(
516                         'action'     => 'activate',
517                         'template'   => urlencode( $template ),
518                         'stylesheet' => urlencode( $stylesheet ),
519                 ), admin_url('themes.php') );
520                 $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
521
522                 $install_actions = array();
523                 $install_actions['preview']  = '<a href="' . esc_url( $preview_link ) . '" class="hide-if-customize" title="' . esc_attr( sprintf( __('Preview &#8220;%s&#8221;'), $name ) ) . '">' . __('Preview') . '</a>';
524                 $install_actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="hide-if-no-customize load-customize" title="' . esc_attr( sprintf( __('Preview &#8220;%s&#8221;'), $name ) ) . '">' . __('Live Preview') . '</a>';
525                 $install_actions['activate'] = '<a href="' . esc_url( $activate_link ) . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>';
526
527                 if ( is_network_admin() && current_user_can( 'manage_network_themes' ) )
528                         $install_actions['network_enable'] = '<a href="' . esc_url( wp_nonce_url( 'themes.php?action=enable&amp;theme=' . urlencode( $stylesheet ), 'enable-theme_' . $stylesheet ) ) . '" title="' . esc_attr__( 'Enable this theme for all sites in this network' ) . '" target="_parent">' . __( 'Network Enable' ) . '</a>';
529
530                 if ( $this->type == 'web' )
531                         $install_actions['themes_page'] = '<a href="' . self_admin_url('theme-install.php') . '" title="' . esc_attr__('Return to Theme Installer') . '" target="_parent">' . __('Return to Theme Installer') . '</a>';
532                 elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
533                         $install_actions['themes_page'] = '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
534
535                 if ( ! $this->result || is_wp_error($this->result) || is_network_admin() || ! current_user_can( 'switch_themes' ) )
536                         unset( $install_actions['activate'], $install_actions['preview'] );
537
538                 /**
539                  * Filter the list of action links available following a single theme installation.
540                  *
541                  * @since 2.8.0
542                  *
543                  * @param array    $install_actions Array of theme action links.
544                  * @param object   $api             Object containing WordPress.org API theme data.
545                  * @param string   $stylesheet      Theme directory name.
546                  * @param WP_Theme $theme_info      Theme object.
547                  */
548                 $install_actions = apply_filters( 'install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info );
549                 if ( ! empty($install_actions) )
550                         $this->feedback(implode(' | ', (array)$install_actions));
551         }
552 }
553
554 /**
555  * Theme Upgrader Skin for WordPress Theme Upgrades.
556  *
557  * @package WordPress
558  * @subpackage Upgrader
559  * @since 2.8.0
560  */
561 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
562         var $theme = '';
563
564         function __construct($args = array()) {
565                 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Update Theme') );
566                 $args = wp_parse_args($args, $defaults);
567
568                 $this->theme = $args['theme'];
569
570                 parent::__construct($args);
571         }
572
573         function after() {
574                 $this->decrement_update_count( 'theme' );
575
576                 $update_actions = array();
577                 if ( ! empty( $this->upgrader->result['destination_name'] ) && $theme_info = $this->upgrader->theme_info() ) {
578                         $name       = $theme_info->display('Name');
579                         $stylesheet = $this->upgrader->result['destination_name'];
580                         $template   = $theme_info->get_template();
581
582                         $preview_link = add_query_arg( array(
583                                 'preview'    => 1,
584                                 'template'   => urlencode( $template ),
585                                 'stylesheet' => urlencode( $stylesheet ),
586                         ), trailingslashit( home_url() ) );
587
588                         $activate_link = add_query_arg( array(
589                                 'action'     => 'activate',
590                                 'template'   => urlencode( $template ),
591                                 'stylesheet' => urlencode( $stylesheet ),
592                         ), admin_url('themes.php') );
593                         $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
594
595                         if ( get_stylesheet() == $stylesheet ) {
596                                 if ( current_user_can( 'edit_theme_options' ) )
597                                         $update_actions['preview']  = '<a href="' . wp_customize_url( $stylesheet ) . '" class="hide-if-no-customize load-customize" title="' . esc_attr( sprintf( __('Customize &#8220;%s&#8221;'), $name ) ) . '">' . __('Customize') . '</a>';
598                         } elseif ( current_user_can( 'switch_themes' ) ) {
599                                 $update_actions['preview']  = '<a href="' . esc_url( $preview_link ) . '" class="hide-if-customize" title="' . esc_attr( sprintf( __('Preview &#8220;%s&#8221;'), $name ) ) . '">' . __('Preview') . '</a>';
600                                 $update_actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="hide-if-no-customize load-customize" title="' . esc_attr( sprintf( __('Preview &#8220;%s&#8221;'), $name ) ) . '">' . __('Live Preview') . '</a>';
601                                 $update_actions['activate'] = '<a href="' . esc_url( $activate_link ) . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>';
602                         }
603
604                         if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() )
605                                 unset( $update_actions['preview'], $update_actions['activate'] );
606                 }
607
608                 $update_actions['themes_page'] = '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Return to Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
609
610                 /**
611                  * Filter the list of action links available following a single theme update.
612                  *
613                  * @since 2.8.0
614                  *
615                  * @param array  $update_actions Array of theme action links.
616                  * @param string $theme          Theme directory name.
617                  */
618                 $update_actions = apply_filters( 'update_theme_complete_actions', $update_actions, $this->theme );
619
620                 if ( ! empty($update_actions) )
621                         $this->feedback(implode(' | ', (array)$update_actions));
622         }
623 }
624
625 /**
626  * Translation Upgrader Skin for WordPress Translation Upgrades.
627  *
628  * @package WordPress
629  * @subpackage Upgrader
630  * @since 3.7.0
631  */
632 class Language_Pack_Upgrader_Skin extends WP_Upgrader_Skin {
633         var $language_update = null;
634         var $done_header = false;
635         var $display_footer_actions = true;
636
637         function __construct( $args = array() ) {
638                 $defaults = array( 'url' => '', 'nonce' => '', 'title' => __( 'Update Translations' ), 'skip_header_footer' => false );
639                 $args = wp_parse_args( $args, $defaults );
640                 if ( $args['skip_header_footer'] ) {
641                         $this->done_header = true;
642                         $this->display_footer_actions = false;
643                 }
644                 parent::__construct( $args );
645         }
646
647         function before() {
648                 $name = $this->upgrader->get_name_for_update( $this->language_update );
649
650                 echo '<div class="update-messages lp-show-latest">';
651
652                 printf( '<h4>' . __( 'Updating translations for %1$s (%2$s)&#8230;' ) . '</h4>', $name, $this->language_update->language );
653         }
654
655         function error( $error ) {
656                 echo '<div class="lp-error">';
657                 parent::error( $error );
658                 echo '</div>';
659         }
660
661         function after() {
662                 echo '</div>';
663         }
664
665         function bulk_footer() {
666                 $this->decrement_update_count( 'translation' );
667                 $update_actions = array();
668                 $update_actions['updates_page'] = '<a href="' . self_admin_url( 'update-core.php' ) . '" title="' . esc_attr__( 'Go to WordPress Updates page' ) . '" target="_parent">' . __( 'Return to WordPress Updates' ) . '</a>';
669
670                 /**
671                  * Filter the list of action links available following a translations update.
672                  *
673                  * @since 3.7.0
674                  *
675                  * @param array $update_actions Array of translations update links.
676                  */
677                 $update_actions = apply_filters( 'update_translations_complete_actions', $update_actions );
678
679                 if ( $update_actions && $this->display_footer_actions )
680                         $this->feedback( implode( ' | ', $update_actions ) );
681
682                 parent::footer();
683         }
684 }
685
686 /**
687  * Upgrader Skin for Automatic WordPress Upgrades
688  *
689  * This skin is designed to be used when no output is intended, all output
690  * is captured and stored for the caller to process and log/email/discard.
691  *
692  * @package WordPress
693  * @subpackage Upgrader
694  * @since 3.7.0
695  */
696 class Automatic_Upgrader_Skin extends WP_Upgrader_Skin {
697         protected $messages = array();
698
699         function request_filesystem_credentials( $error = false, $context = '' ) {
700                 if ( $context )
701                         $this->options['context'] = $context;
702                 // TODO: fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version
703                 // This will output a credentials form in event of failure, We don't want that, so just hide with a buffer
704                 ob_start();
705                 $result = parent::request_filesystem_credentials( $error );
706                 ob_end_clean();
707                 return $result;
708         }
709
710         function get_upgrade_messages() {
711                 return $this->messages;
712         }
713
714         function feedback( $data ) {
715                 if ( is_wp_error( $data ) )
716                         $string = $data->get_error_message();
717                 else if ( is_array( $data ) )
718                         return;
719                 else
720                         $string = $data;
721
722                 if ( ! empty( $this->upgrader->strings[ $string ] ) )
723                         $string = $this->upgrader->strings[ $string ];
724
725                 if ( strpos( $string, '%' ) !== false ) {
726                         $args = func_get_args();
727                         $args = array_splice( $args, 1 );
728                         if ( ! empty( $args ) )
729                                 $string = vsprintf( $string, $args );
730                 }
731
732                 $string = trim( $string );
733
734                 // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
735                 $string = wp_kses( $string, array(
736                         'a' => array(
737                                 'href' => true
738                         ),
739                         'br' => true,
740                         'em' => true,
741                         'strong' => true,
742                 ) );
743
744                 if ( empty( $string ) )
745                         return;
746
747                 $this->messages[] = $string;
748         }
749
750         function header() {
751                 ob_start();
752         }
753
754         function footer() {
755                 $output = ob_get_contents();
756                 if ( ! empty( $output ) )
757                         $this->feedback( $output );
758                 ob_end_clean();
759         }
760
761         function bulk_header() {}
762         function bulk_footer() {}
763         function before() {}
764         function after() {}
765 }