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">';
54 echo '<h2>' . $this->options['title'] . '</h2>';
60 function error($errors) {
61 if ( ! $this->done_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( $errors->get_error_data() ) );
70 $this->feedback($message);
75 function feedback($string) {
76 if ( isset( $this->upgrader->strings[$string] ) )
77 $string = $this->upgrader->strings[$string];
79 if ( strpos($string, '%') !== false ) {
80 $args = func_get_args();
81 $args = array_splice($args, 1);
83 $args = array_map( 'strip_tags', $args );
84 $args = array_map( 'esc_html', $args );
85 $string = vsprintf($string, $args);
90 show_message($string);
98 * Plugin Upgrader Skin for WordPress Plugin Upgrades.
101 * @subpackage Upgrader
104 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
106 var $plugin_active = false;
107 var $plugin_network_active = false;
109 function __construct($args = array()) {
110 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Update Plugin') );
111 $args = wp_parse_args($args, $defaults);
113 $this->plugin = $args['plugin'];
115 $this->plugin_active = is_plugin_active( $this->plugin );
116 $this->plugin_network_active = is_plugin_active_for_network( $this->plugin );
118 parent::__construct($args);
122 $this->plugin = $this->upgrader->plugin_info();
123 if ( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
124 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>';
127 $update_actions = array(
128 '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>',
129 'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
131 if ( $this->plugin_active || ! $this->result || is_wp_error( $this->result ) || ! current_user_can( 'activate_plugins' ) )
132 unset( $update_actions['activate_plugin'] );
134 $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
135 if ( ! empty($update_actions) )
136 $this->feedback(implode(' | ', (array)$update_actions));
141 * Plugin Upgrader Skin for WordPress Plugin Upgrades.
144 * @subpackage Upgrader
147 class Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
148 var $in_loop = false;
151 function __construct($args = array()) {
152 $defaults = array( 'url' => '', 'nonce' => '' );
153 $args = wp_parse_args($args, $defaults);
155 parent::__construct($args);
158 function add_strings() {
159 $this->upgrader->strings['skin_upgrade_start'] = __('The update process is starting. This process may take a while on some hosts, so please be patient.');
160 $this->upgrader->strings['skin_update_failed_error'] = __('An error occurred while updating %1$s: <strong>%2$s</strong>');
161 $this->upgrader->strings['skin_update_failed'] = __('The update of %1$s failed.');
162 $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>';
163 $this->upgrader->strings['skin_upgrade_end'] = __('All updates have been completed.');
166 function feedback($string) {
167 if ( isset( $this->upgrader->strings[$string] ) )
168 $string = $this->upgrader->strings[$string];
170 if ( strpos($string, '%') !== false ) {
171 $args = func_get_args();
172 $args = array_splice($args, 1);
174 $args = array_map( 'strip_tags', $args );
175 $args = array_map( 'esc_html', $args );
176 $string = vsprintf($string, $args);
179 if ( empty($string) )
181 if ( $this->in_loop )
182 echo "$string<br />\n";
184 echo "<p>$string</p>\n";
188 // Nothing, This will be displayed within a iframe.
192 // Nothing, This will be displayed within a iframe.
194 function error($error) {
195 if ( is_string($error) && isset( $this->upgrader->strings[$error] ) )
196 $this->error = $this->upgrader->strings[$error];
198 if ( is_wp_error($error) ) {
199 foreach ( $error->get_error_messages() as $emessage ) {
200 if ( $error->get_error_data() && is_string( $error->get_error_data() ) )
201 $messages[] = $emessage . ' ' . esc_html( $error->get_error_data() );
203 $messages[] = $emessage;
205 $this->error = implode(', ', $messages);
207 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
210 function bulk_header() {
211 $this->feedback('skin_upgrade_start');
214 function bulk_footer() {
215 $this->feedback('skin_upgrade_end');
218 function before($title = '') {
219 $this->in_loop = true;
220 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);
221 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').css("display", "inline-block");</script>';
222 echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr($this->upgrader->update_current) . '"><p>';
223 $this->flush_output();
226 function after($title = '') {
228 if ( $this->error || ! $this->result ) {
230 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed_error'], $title, $this->error) . '</p></div>';
232 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed'], $title) . '</p></div>';
234 echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
236 if ( $this->result && ! is_wp_error( $this->result ) ) {
237 if ( ! $this->error )
238 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>';
239 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
243 $this->flush_output();
247 $this->in_loop = false;
248 $this->error = false;
251 function flush_output() {
252 wp_ob_end_flush_all();
257 class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin {
258 var $plugin_info = array(); // Plugin_Upgrader::bulk() will fill this in.
260 function __construct($args = array()) {
261 parent::__construct($args);
264 function add_strings() {
265 parent::add_strings();
266 $this->upgrader->strings['skin_before_update_header'] = __('Updating Plugin %1$s (%2$d/%3$d)');
269 function before($title = '') {
270 parent::before($this->plugin_info['Title']);
273 function after($title = '') {
274 parent::after($this->plugin_info['Title']);
276 function bulk_footer() {
277 parent::bulk_footer();
278 $update_actions = array(
279 'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>',
280 '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>'
282 if ( ! current_user_can( 'activate_plugins' ) )
283 unset( $update_actions['plugins_page'] );
285 $update_actions = apply_filters('update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info);
286 if ( ! empty($update_actions) )
287 $this->feedback(implode(' | ', (array)$update_actions));
291 class Bulk_Theme_Upgrader_Skin extends Bulk_Upgrader_Skin {
292 var $theme_info = array(); // Theme_Upgrader::bulk() will fill this in.
294 function __construct($args = array()) {
295 parent::__construct($args);
298 function add_strings() {
299 parent::add_strings();
300 $this->upgrader->strings['skin_before_update_header'] = __('Updating Theme %1$s (%2$d/%3$d)');
303 function before($title = '') {
304 parent::before( $this->theme_info->display('Name') );
307 function after($title = '') {
308 parent::after( $this->theme_info->display('Name') );
311 function bulk_footer() {
312 parent::bulk_footer();
313 $update_actions = array(
314 'themes_page' => '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Go to themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>',
315 '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>'
317 if ( ! current_user_can( 'switch_themes' ) && ! current_user_can( 'edit_theme_options' ) )
318 unset( $update_actions['themes_page'] );
320 $update_actions = apply_filters('update_bulk_theme_complete_actions', $update_actions, $this->theme_info );
321 if ( ! empty($update_actions) )
322 $this->feedback(implode(' | ', (array)$update_actions));
327 * Plugin Installer Skin for WordPress Plugin Installer.
330 * @subpackage Upgrader
333 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
337 function __construct($args = array()) {
338 $defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
339 $args = wp_parse_args($args, $defaults);
341 $this->type = $args['type'];
342 $this->api = isset($args['api']) ? $args['api'] : array();
344 parent::__construct($args);
348 if ( !empty($this->api) )
349 $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
354 $plugin_file = $this->upgrader->plugin_info();
356 $install_actions = array();
358 $from = isset($_GET['from']) ? wp_unslash( $_GET['from'] ) : 'plugins';
360 if ( 'import' == $from )
361 $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>';
363 $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>';
365 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
366 $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>';
367 unset( $install_actions['activate_plugin'] );
370 if ( 'import' == $from )
371 $install_actions['importers_page'] = '<a href="' . admin_url('import.php') . '" title="' . esc_attr__('Return to Importers') . '" target="_parent">' . __('Return to Importers') . '</a>';
372 else if ( $this->type == 'web' )
373 $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>';
375 $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>';
377 if ( ! $this->result || is_wp_error($this->result) ) {
378 unset( $install_actions['activate_plugin'], $install_actions['network_activate'] );
379 } elseif ( ! current_user_can( 'activate_plugins' ) ) {
380 unset( $install_actions['activate_plugin'] );
383 $install_actions = apply_filters('install_plugin_complete_actions', $install_actions, $this->api, $plugin_file);
384 if ( ! empty($install_actions) )
385 $this->feedback(implode(' | ', (array)$install_actions));
390 * Theme Installer Skin for the WordPress Theme Installer.
393 * @subpackage Upgrader
396 class Theme_Installer_Skin extends WP_Upgrader_Skin {
400 function __construct($args = array()) {
401 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
402 $args = wp_parse_args($args, $defaults);
404 $this->type = $args['type'];
405 $this->api = isset($args['api']) ? $args['api'] : array();
407 parent::__construct($args);
411 if ( !empty($this->api) )
412 $this->upgrader->strings['process_success'] = sprintf( $this->upgrader->strings['process_success_specific'], $this->api->name, $this->api->version);
416 if ( empty($this->upgrader->result['destination_name']) )
419 $theme_info = $this->upgrader->theme_info();
420 if ( empty( $theme_info ) )
423 $name = $theme_info->display('Name');
424 $stylesheet = $this->upgrader->result['destination_name'];
425 $template = $theme_info->get_template();
427 $preview_link = add_query_arg( array(
429 'template' => urlencode( $template ),
430 'stylesheet' => urlencode( $stylesheet ),
431 ), trailingslashit( home_url() ) );
433 $activate_link = add_query_arg( array(
434 'action' => 'activate',
435 'template' => urlencode( $template ),
436 'stylesheet' => urlencode( $stylesheet ),
437 ), admin_url('themes.php') );
438 $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
440 $install_actions = array();
441 $install_actions['preview'] = '<a href="' . esc_url( $preview_link ) . '" class="hide-if-customize" title="' . esc_attr( sprintf( __('Preview “%s”'), $name ) ) . '">' . __('Preview') . '</a>';
442 $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>';
443 $install_actions['activate'] = '<a href="' . esc_url( $activate_link ) . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate “%s”'), $name ) ) . '">' . __('Activate') . '</a>';
445 if ( is_network_admin() && current_user_can( 'manage_network_themes' ) )
446 $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>';
448 if ( $this->type == 'web' )
449 $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>';
450 elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
451 $install_actions['themes_page'] = '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
453 if ( ! $this->result || is_wp_error($this->result) || is_network_admin() || ! current_user_can( 'switch_themes' ) )
454 unset( $install_actions['activate'], $install_actions['preview'] );
456 $install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
457 if ( ! empty($install_actions) )
458 $this->feedback(implode(' | ', (array)$install_actions));
463 * Theme Upgrader Skin for WordPress Theme Upgrades.
466 * @subpackage Upgrader
469 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
472 function __construct($args = array()) {
473 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Update Theme') );
474 $args = wp_parse_args($args, $defaults);
476 $this->theme = $args['theme'];
478 parent::__construct($args);
483 $update_actions = array();
484 if ( ! empty( $this->upgrader->result['destination_name'] ) && $theme_info = $this->upgrader->theme_info() ) {
485 $name = $theme_info->display('Name');
486 $stylesheet = $this->upgrader->result['destination_name'];
487 $template = $theme_info->get_template();
489 $preview_link = add_query_arg( array(
491 'template' => urlencode( $template ),
492 'stylesheet' => urlencode( $stylesheet ),
493 ), trailingslashit( home_url() ) );
495 $activate_link = add_query_arg( array(
496 'action' => 'activate',
497 'template' => urlencode( $template ),
498 'stylesheet' => urlencode( $stylesheet ),
499 ), admin_url('themes.php') );
500 $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
502 if ( get_stylesheet() == $stylesheet ) {
503 if ( current_user_can( 'edit_theme_options' ) )
504 $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>';
505 } elseif ( current_user_can( 'switch_themes' ) ) {
506 $update_actions['preview'] = '<a href="' . esc_url( $preview_link ) . '" class="hide-if-customize" title="' . esc_attr( sprintf( __('Preview “%s”'), $name ) ) . '">' . __('Preview') . '</a>';
507 $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>';
508 $update_actions['activate'] = '<a href="' . esc_url( $activate_link ) . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate “%s”'), $name ) ) . '">' . __('Activate') . '</a>';
511 if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() )
512 unset( $update_actions['preview'], $update_actions['activate'] );
515 $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>';
517 $update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
518 if ( ! empty($update_actions) )
519 $this->feedback(implode(' | ', (array)$update_actions));
524 * Translation Upgrader Skin for WordPress Translation Upgrades.
527 * @subpackage Upgrader
530 class Language_Pack_Upgrader_Skin extends WP_Upgrader_Skin {
531 var $language_update = null;
532 var $done_header = false;
533 var $display_footer_actions = true;
535 function __construct( $args = array() ) {
536 $defaults = array( 'url' => '', 'nonce' => '', 'title' => __( 'Update Translations' ), 'skip_header_footer' => false );
537 $args = wp_parse_args( $args, $defaults );
538 if ( $args['skip_header_footer'] ) {
539 $this->done_header = true;
540 $this->display_footer_actions = false;
542 parent::__construct( $args );
546 $name = $this->upgrader->get_name_for_update( $this->language_update );
548 echo '<div class="update-messages lp-show-latest">';
550 printf( '<h4>' . __( 'Updating translations for %1$s (%2$s)…' ) . '</h4>', $name, $this->language_update->language );
553 function error( $error ) {
554 echo '<div class="lp-error">';
555 parent::error( $error );
563 function bulk_footer() {
564 $update_actions = array();
565 $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>';
566 $update_actions = apply_filters( 'update_translations_complete_actions', $update_actions );
568 if ( $update_actions && $this->display_footer_actions )
569 $this->feedback( implode( ' | ', $update_actions ) );
576 * Upgrader Skin for Automatic WordPress Upgrades
578 * This skin is designed to be used when no output is intended, all output
579 * is captured and stored for the caller to process and log/email/discard.
582 * @subpackage Upgrader
585 class Automatic_Upgrader_Skin extends WP_Upgrader_Skin {
586 protected $messages = array();
588 function request_filesystem_credentials( $error = false, $context = '' ) {
590 $this->options['context'] = $context;
591 // TODO: fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version
592 // This will output a credentials form in event of failure, We don't want that, so just hide with a buffer
594 $result = parent::request_filesystem_credentials( $error );
599 function get_upgrade_messages() {
600 return $this->messages;
603 function feedback( $data ) {
604 if ( is_wp_error( $data ) )
605 $string = $data->get_error_message();
606 else if ( is_array( $data ) )
611 if ( ! empty( $this->upgrader->strings[ $string ] ) )
612 $string = $this->upgrader->strings[ $string ];
614 if ( strpos( $string, '%' ) !== false ) {
615 $args = func_get_args();
616 $args = array_splice( $args, 1 );
617 if ( ! empty( $args ) )
618 $string = vsprintf( $string, $args );
621 $string = trim( $string );
623 // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
624 $string = wp_kses( $string, array(
633 if ( empty( $string ) )
636 $this->messages[] = $string;
644 $output = ob_get_contents();
645 if ( ! empty( $output ) )
646 $this->feedback( $output );
650 function bulk_header() {}
651 function bulk_footer() {}