PageRenderTime 136ms CodeModel.GetById 29ms app.highlight 89ms RepoModel.GetById 1ms app.codeStats 2ms

/wp-admin/includes/class-wp-upgrader-skins.php

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