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