3 * The User Interface "Skins" for the WordPress File Upgrader
11 * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
14 * @subpackage Upgrader
17 class WP_Upgrader_Skin {
20 var $done_header = false;
23 function __construct($args = array()) {
24 $defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
25 $this->options = wp_parse_args($args, $defaults);
28 function set_upgrader(&$upgrader) {
29 if ( is_object($upgrader) )
30 $this->upgrader =& $upgrader;
34 function add_strings() {
37 function set_result($result) {
38 $this->result = $result;
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.
50 if ( $this->done_header )
52 $this->done_header = true;
53 echo '<div class="wrap">';
55 echo '<h2>' . $this->options['title'] . '</h2>';
61 function error($errors) {
62 if ( ! $this->done_header )
64 if ( is_string($errors) ) {
65 $this->feedback($errors);
66 } elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
67 foreach ( $errors->get_error_messages() as $message ) {
68 if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) )
69 $this->feedback($message . ' ' . esc_html( $errors->get_error_data() ) );
71 $this->feedback($message);
76 function feedback($string) {
77 if ( isset( $this->upgrader->strings[$string] ) )
78 $string = $this->upgrader->strings[$string];
80 if ( strpos($string, '%') !== false ) {
81 $args = func_get_args();
82 $args = array_splice($args, 1);
84 $args = array_map( 'strip_tags', $args );
85 $args = array_map( 'esc_html', $args );
86 $string = vsprintf($string, $args);
91 show_message($string);
99 * Plugin Upgrader Skin for WordPress Plugin Upgrades.
102 * @subpackage Upgrader
105 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
107 var $plugin_active = false;
108 var $plugin_network_active = false;
110 function __construct($args = array()) {
111 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Update Plugin') );
112 $args = wp_parse_args($args, $defaults);
114 $this->plugin = $args['plugin'];
116 $this->plugin_active = is_plugin_active( $this->plugin );
117 $this->plugin_network_active = is_plugin_active_for_network( $this->plugin );
119 parent::__construct($args);
123 $this->plugin = $this->upgrader->plugin_info();
124 if ( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
125 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>';
128 $update_actions = array(
129 'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
130 'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
132 if ( $this->plugin_active || ! $this->result || is_wp_error( $this->result ) || ! current_user_can( 'activate_plugins' ) )
133 unset( $update_actions['activate_plugin'] );
135 $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
136 if ( ! empty($update_actions) )
137 $this->feedback(implode(' | ', (array)$update_actions));
141 if ( $this->upgrader->show_before ) {
142 echo $this->upgrader->show_before;
143 $this->upgrader->show_before = '';
149 * Plugin Upgrader Skin for WordPress Plugin Upgrades.
152 * @subpackage Upgrader
155 class Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
156 var $in_loop = false;
159 function __construct($args = array()) {
160 $defaults = array( 'url' => '', 'nonce' => '' );
161 $args = wp_parse_args($args, $defaults);
163 parent::__construct($args);
166 function add_strings() {
167 $this->upgrader->strings['skin_upgrade_start'] = __('The update process is starting. This process may take a while on some hosts, so please be patient.');
168 $this->upgrader->strings['skin_update_failed_error'] = __('An error occurred while updating %1$s: <strong>%2$s</strong>');
169 $this->upgrader->strings['skin_update_failed'] = __('The update of %1$s failed.');
170 $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>';
171 $this->upgrader->strings['skin_upgrade_end'] = __('All updates have been completed.');
174 function feedback($string) {
175 if ( isset( $this->upgrader->strings[$string] ) )
176 $string = $this->upgrader->strings[$string];
178 if ( strpos($string, '%') !== false ) {
179 $args = func_get_args();
180 $args = array_splice($args, 1);
182 $args = array_map( 'strip_tags', $args );
183 $args = array_map( 'esc_html', $args );
184 $string = vsprintf($string, $args);
187 if ( empty($string) )
189 if ( $this->in_loop )
190 echo "$string<br />\n";
192 echo "<p>$string</p>\n";
196 // Nothing, This will be displayed within a iframe.
200 // Nothing, This will be displayed within a iframe.
202 function error($error) {
203 if ( is_string($error) && isset( $this->upgrader->strings[$error] ) )
204 $this->error = $this->upgrader->strings[$error];
206 if ( is_wp_error($error) ) {
207 foreach ( $error->get_error_messages() as $emessage ) {
208 if ( $error->get_error_data() && is_string( $error->get_error_data() ) )
209 $messages[] = $emessage . ' ' . esc_html( $error->get_error_data() );
211 $messages[] = $emessage;
213 $this->error = implode(', ', $messages);
215 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
218 function bulk_header() {
219 $this->feedback('skin_upgrade_start');
222 function bulk_footer() {
223 $this->feedback('skin_upgrade_end');
226 function before($title = '') {
227 $this->in_loop = true;
228 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);
229 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').css("display", "inline-block");</script>';
230 echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr($this->upgrader->update_current) . '"><p>';
231 $this->flush_output();
234 function after($title = '') {
236 if ( $this->error || ! $this->result ) {
238 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed_error'], $title, $this->error) . '</p></div>';
240 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed'], $title) . '</p></div>';
242 echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
244 if ( $this->result && ! is_wp_error( $this->result ) ) {
245 if ( ! $this->error )
246 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>';
247 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
251 $this->flush_output();
255 $this->in_loop = false;
256 $this->error = false;
259 function flush_output() {
260 wp_ob_end_flush_all();
265 class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin {
266 var $plugin_info = array(); // Plugin_Upgrader::bulk() will fill this in.
268 function __construct($args = array()) {
269 parent::__construct($args);
272 function add_strings() {
273 parent::add_strings();
274 $this->upgrader->strings['skin_before_update_header'] = __('Updating Plugin %1$s (%2$d/%3$d)');
277 function before($title = '') {
278 parent::before($this->plugin_info['Title']);
281 function after($title = '') {
282 parent::after($this->plugin_info['Title']);
284 function bulk_footer() {
285 parent::bulk_footer();
286 $update_actions = array(
287 'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>',
288 '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>'
290 if ( ! current_user_can( 'activate_plugins' ) )
291 unset( $update_actions['plugins_page'] );
293 $update_actions = apply_filters('update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info);
294 if ( ! empty($update_actions) )
295 $this->feedback(implode(' | ', (array)$update_actions));
299 class Bulk_Theme_Upgrader_Skin extends Bulk_Upgrader_Skin {
300 var $theme_info = array(); // Theme_Upgrader::bulk() will fill this in.
302 function __construct($args = array()) {
303 parent::__construct($args);
306 function add_strings() {
307 parent::add_strings();
308 $this->upgrader->strings['skin_before_update_header'] = __('Updating Theme %1$s (%2$d/%3$d)');
311 function before($title = '') {
312 parent::before( $this->theme_info->display('Name') );
315 function after($title = '') {
316 parent::after( $this->theme_info->display('Name') );
319 function bulk_footer() {
320 parent::bulk_footer();
321 $update_actions = array(
322 'themes_page' => '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Go to themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>',
323 '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>'
325 if ( ! current_user_can( 'switch_themes' ) && ! current_user_can( 'edit_theme_options' ) )
326 unset( $update_actions['themes_page'] );
328 $update_actions = apply_filters('update_bulk_theme_complete_actions', $update_actions, $this->theme_info );
329 if ( ! empty($update_actions) )
330 $this->feedback(implode(' | ', (array)$update_actions));
335 * Plugin Installer Skin for WordPress Plugin Installer.
338 * @subpackage Upgrader
341 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
345 function __construct($args = array()) {
346 $defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
347 $args = wp_parse_args($args, $defaults);
349 $this->type = $args['type'];
350 $this->api = isset($args['api']) ? $args['api'] : array();
352 parent::__construct($args);
356 if ( !empty($this->api) )
357 $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
362 $plugin_file = $this->upgrader->plugin_info();
364 $install_actions = array();
366 $from = isset($_GET['from']) ? wp_unslash( $_GET['from'] ) : 'plugins';
368 if ( 'import' == $from )
369 $install_actions['activate_plugin'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&from=import&plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin & Run Importer') . '</a>';
371 $install_actions['activate_plugin'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>';
373 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
374 $install_actions['network_activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&networkwide=1&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>';
375 unset( $install_actions['activate_plugin'] );
378 if ( 'import' == $from )
379 $install_actions['importers_page'] = '<a href="' . admin_url('import.php') . '" title="' . esc_attr__('Return to Importers') . '" target="_parent">' . __('Return to Importers') . '</a>';
380 else if ( $this->type == 'web' )
381 $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>';
383 $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>';
385 if ( ! $this->result || is_wp_error($this->result) ) {
386 unset( $install_actions['activate_plugin'], $install_actions['network_activate'] );
387 } elseif ( ! current_user_can( 'activate_plugins' ) ) {
388 unset( $install_actions['activate_plugin'] );
391 $install_actions = apply_filters('install_plugin_complete_actions', $install_actions, $this->api, $plugin_file);
392 if ( ! empty($install_actions) )
393 $this->feedback(implode(' | ', (array)$install_actions));
398 * Theme Installer Skin for the WordPress Theme Installer.
401 * @subpackage Upgrader
404 class Theme_Installer_Skin extends WP_Upgrader_Skin {
408 function __construct($args = array()) {
409 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
410 $args = wp_parse_args($args, $defaults);
412 $this->type = $args['type'];
413 $this->api = isset($args['api']) ? $args['api'] : array();
415 parent::__construct($args);
419 if ( !empty($this->api) )
420 $this->upgrader->strings['process_success'] = sprintf( $this->upgrader->strings['process_success_specific'], $this->api->name, $this->api->version);
424 if ( empty($this->upgrader->result['destination_name']) )
427 $theme_info = $this->upgrader->theme_info();
428 if ( empty( $theme_info ) )
431 $name = $theme_info->display('Name');
432 $stylesheet = $this->upgrader->result['destination_name'];
433 $template = $theme_info->get_template();
435 $preview_link = add_query_arg( array(
437 'template' => urlencode( $template ),
438 'stylesheet' => urlencode( $stylesheet ),
439 ), trailingslashit( home_url() ) );
441 $activate_link = add_query_arg( array(
442 'action' => 'activate',
443 'template' => urlencode( $template ),
444 'stylesheet' => urlencode( $stylesheet ),
445 ), admin_url('themes.php') );
446 $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
448 $install_actions = array();
449 $install_actions['preview'] = '<a href="' . esc_url( $preview_link ) . '" class="hide-if-customize" title="' . esc_attr( sprintf( __('Preview “%s”'), $name ) ) . '">' . __('Preview') . '</a>';
450 $install_actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="hide-if-no-customize load-customize" title="' . esc_attr( sprintf( __('Preview “%s”'), $name ) ) . '">' . __('Live Preview') . '</a>';
451 $install_actions['activate'] = '<a href="' . esc_url( $activate_link ) . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate “%s”'), $name ) ) . '">' . __('Activate') . '</a>';
453 if ( is_network_admin() && current_user_can( 'manage_network_themes' ) )
454 $install_actions['network_enable'] = '<a href="' . esc_url( wp_nonce_url( 'themes.php?action=enable&theme=' . urlencode( $stylesheet ), 'enable-theme_' . $stylesheet ) ) . '" title="' . esc_attr__( 'Enable this theme for all sites in this network' ) . '" target="_parent">' . __( 'Network Enable' ) . '</a>';
456 if ( $this->type == 'web' )
457 $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>';
458 elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
459 $install_actions['themes_page'] = '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
461 if ( ! $this->result || is_wp_error($this->result) || is_network_admin() || ! current_user_can( 'switch_themes' ) )
462 unset( $install_actions['activate'], $install_actions['preview'] );
464 $install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
465 if ( ! empty($install_actions) )
466 $this->feedback(implode(' | ', (array)$install_actions));
471 * Theme Upgrader Skin for WordPress Theme Upgrades.
474 * @subpackage Upgrader
477 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
480 function __construct($args = array()) {
481 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Update Theme') );
482 $args = wp_parse_args($args, $defaults);
484 $this->theme = $args['theme'];
486 parent::__construct($args);
491 $update_actions = array();
492 if ( ! empty( $this->upgrader->result['destination_name'] ) && $theme_info = $this->upgrader->theme_info() ) {
493 $name = $theme_info->display('Name');
494 $stylesheet = $this->upgrader->result['destination_name'];
495 $template = $theme_info->get_template();
497 $preview_link = add_query_arg( array(
499 'template' => urlencode( $template ),
500 'stylesheet' => urlencode( $stylesheet ),
501 ), trailingslashit( home_url() ) );
503 $activate_link = add_query_arg( array(
504 'action' => 'activate',
505 'template' => urlencode( $template ),
506 'stylesheet' => urlencode( $stylesheet ),
507 ), admin_url('themes.php') );
508 $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
510 if ( get_stylesheet() == $stylesheet ) {
511 if ( current_user_can( 'edit_theme_options' ) )
512 $update_actions['preview'] = '<a href="' . wp_customize_url( $stylesheet ) . '" class="hide-if-no-customize load-customize" title="' . esc_attr( sprintf( __('Customize “%s”'), $name ) ) . '">' . __('Customize') . '</a>';
513 } elseif ( current_user_can( 'switch_themes' ) ) {
514 $update_actions['preview'] = '<a href="' . esc_url( $preview_link ) . '" class="hide-if-customize" title="' . esc_attr( sprintf( __('Preview “%s”'), $name ) ) . '">' . __('Preview') . '</a>';
515 $update_actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="hide-if-no-customize load-customize" title="' . esc_attr( sprintf( __('Preview “%s”'), $name ) ) . '">' . __('Live Preview') . '</a>';
516 $update_actions['activate'] = '<a href="' . esc_url( $activate_link ) . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate “%s”'), $name ) ) . '">' . __('Activate') . '</a>';
519 if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() )
520 unset( $update_actions['preview'], $update_actions['activate'] );
523 $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>';
525 $update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
526 if ( ! empty($update_actions) )
527 $this->feedback(implode(' | ', (array)$update_actions));
532 * Translation Upgrader Skin for WordPress Translation Upgrades.
535 * @subpackage Upgrader
538 class Language_Pack_Upgrader_Skin extends WP_Upgrader_Skin {
539 var $language_update = null;
540 var $done_header = false;
541 var $display_footer_actions = true;
543 function __construct( $args = array() ) {
544 $defaults = array( 'url' => '', 'nonce' => '', 'title' => __( 'Update Translations' ), 'skip_header_footer' => false );
545 $args = wp_parse_args( $args, $defaults );
546 if ( $args['skip_header_footer'] ) {
547 $this->done_header = true;
548 $this->display_footer_actions = false;
550 parent::__construct( $args );
554 $name = $this->upgrader->get_name_for_update( $this->language_update );
556 echo '<div class="update-messages lp-show-latest">';
558 printf( '<h4>' . __( 'Updating translations for %1$s (%2$s)…' ) . '</h4>', $name, $this->language_update->language );
561 function error( $error ) {
562 echo '<div class="lp-error">';
563 parent::error( $error );
571 function bulk_footer() {
572 $update_actions = array();
573 $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>';
574 $update_actions = apply_filters( 'update_translations_complete_actions', $update_actions );
576 if ( $update_actions && $this->display_footer_actions )
577 $this->feedback( implode( ' | ', $update_actions ) );
584 * Upgrader Skin for Automatic WordPress Upgrades
586 * This skin is designed to be used when no output is intended, all output
587 * is captured and stored for the caller to process and log/email/discard.
590 * @subpackage Upgrader
593 class Automatic_Upgrader_Skin extends WP_Upgrader_Skin {
594 protected $messages = array();
596 function request_filesystem_credentials( $error = false, $context = '' ) {
598 $this->options['context'] = $context;
599 // TODO: fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version
600 // This will output a credentials form in event of failure, We don't want that, so just hide with a buffer
602 $result = parent::request_filesystem_credentials( $error );
607 function get_upgrade_messages() {
608 return $this->messages;
611 function feedback( $data ) {
612 if ( is_wp_error( $data ) )
613 $string = $data->get_error_message();
614 else if ( is_array( $data ) )
619 if ( ! empty( $this->upgrader->strings[ $string ] ) )
620 $string = $this->upgrader->strings[ $string ];
622 if ( strpos( $string, '%' ) !== false ) {
623 $args = func_get_args();
624 $args = array_splice( $args, 1 );
625 if ( ! empty( $args ) )
626 $string = vsprintf( $string, $args );
629 $string = trim( $string );
631 // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
632 $string = wp_kses( $string, array(
641 if ( empty( $string ) )
644 $this->messages[] = $string;
652 $output = ob_get_contents();
653 if ( ! empty( $output ) )
654 $this->feedback( $output );
658 function bulk_header() {}
659 function bulk_footer() {}