wp/wp-admin/includes/class-bulk-upgrader-skin.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Upgrader API: Bulk_Upgrader_Skin class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Upgrader
       
     7  * @since 4.6.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Generic Bulk Upgrader Skin for WordPress Upgrades.
       
    12  *
       
    13  * @since 3.0.0
       
    14  * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
       
    15  *
       
    16  * @see WP_Upgrader_Skin
       
    17  */
       
    18 class Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
       
    19 	public $in_loop = false;
       
    20 	/**
       
    21 	 * @var string|false
       
    22 	 */
       
    23 	public $error = false;
       
    24 
       
    25 	/**
       
    26 	 *
       
    27 	 * @param array $args
       
    28 	 */
       
    29 	public function __construct($args = array()) {
       
    30 		$defaults = array( 'url' => '', 'nonce' => '' );
       
    31 		$args = wp_parse_args($args, $defaults);
       
    32 
       
    33 		parent::__construct($args);
       
    34 	}
       
    35 
       
    36 	/**
       
    37 	 */
       
    38 	public function add_strings() {
       
    39 		$this->upgrader->strings['skin_upgrade_start'] = __('The update process is starting. This process may take a while on some hosts, so please be patient.');
       
    40 		/* translators: 1: Title of an update, 2: Error message */
       
    41 		$this->upgrader->strings['skin_update_failed_error'] = __('An error occurred while updating %1$s: %2$s');
       
    42 		/* translators: 1: Title of an update */
       
    43 		$this->upgrader->strings['skin_update_failed'] = __('The update of %1$s failed.');
       
    44 		/* translators: 1: Title of an update */
       
    45 		$this->upgrader->strings['skin_update_successful'] = __( '%1$s updated successfully.' );
       
    46 		$this->upgrader->strings['skin_upgrade_end'] = __('All updates have been completed.');
       
    47 	}
       
    48 
       
    49 	/**
       
    50 	 *
       
    51 	 * @param string $string
       
    52 	 */
       
    53 	public function feedback($string) {
       
    54 		if ( isset( $this->upgrader->strings[$string] ) )
       
    55 			$string = $this->upgrader->strings[$string];
       
    56 
       
    57 		if ( strpos($string, '%') !== false ) {
       
    58 			$args = func_get_args();
       
    59 			$args = array_splice($args, 1);
       
    60 			if ( $args ) {
       
    61 				$args = array_map( 'strip_tags', $args );
       
    62 				$args = array_map( 'esc_html', $args );
       
    63 				$string = vsprintf($string, $args);
       
    64 			}
       
    65 		}
       
    66 		if ( empty($string) )
       
    67 			return;
       
    68 		if ( $this->in_loop )
       
    69 			echo "$string<br />\n";
       
    70 		else
       
    71 			echo "<p>$string</p>\n";
       
    72 	}
       
    73 
       
    74 	/**
       
    75 	 */
       
    76 	public function header() {
       
    77 		// Nothing, This will be displayed within a iframe.
       
    78 	}
       
    79 
       
    80 	/**
       
    81 	 */
       
    82 	public function footer() {
       
    83 		// Nothing, This will be displayed within a iframe.
       
    84 	}
       
    85 
       
    86 	/**
       
    87 	 *
       
    88 	 * @param string|WP_Error $error
       
    89 	 */
       
    90 	public function error($error) {
       
    91 		if ( is_string($error) && isset( $this->upgrader->strings[$error] ) )
       
    92 			$this->error = $this->upgrader->strings[$error];
       
    93 
       
    94 		if ( is_wp_error($error) ) {
       
    95 			$messages = array();
       
    96 			foreach ( $error->get_error_messages() as $emessage ) {
       
    97 				if ( $error->get_error_data() && is_string( $error->get_error_data() ) )
       
    98 					$messages[] = $emessage . ' ' . esc_html( strip_tags( $error->get_error_data() ) );
       
    99 				else
       
   100 					$messages[] = $emessage;
       
   101 			}
       
   102 			$this->error = implode(', ', $messages);
       
   103 		}
       
   104 		echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
       
   105 	}
       
   106 
       
   107 	/**
       
   108 	 */
       
   109 	public function bulk_header() {
       
   110 		$this->feedback('skin_upgrade_start');
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 */
       
   115 	public function bulk_footer() {
       
   116 		$this->feedback('skin_upgrade_end');
       
   117 	}
       
   118 
       
   119 	/**
       
   120 	 *
       
   121 	 * @param string $title
       
   122 	 */
       
   123 	public function before($title = '') {
       
   124 		$this->in_loop = true;
       
   125 		printf( '<h2>' . $this->upgrader->strings['skin_before_update_header'] . ' <span class="spinner waiting-' . $this->upgrader->update_current . '"></span></h2>', $title, $this->upgrader->update_current, $this->upgrader->update_count );
       
   126 		echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').css("display", "inline-block");</script>';
       
   127 		// This progress messages div gets moved via JavaScript when clicking on "Show details.".
       
   128 		echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr($this->upgrader->update_current) . '"><p>';
       
   129 		$this->flush_output();
       
   130 	}
       
   131 
       
   132 	/**
       
   133 	 *
       
   134 	 * @param string $title
       
   135 	 */
       
   136 	public function after($title = '') {
       
   137 		echo '</p></div>';
       
   138 		if ( $this->error || ! $this->result ) {
       
   139 			if ( $this->error ) {
       
   140 				echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed_error'], $title, '<strong>' . $this->error . '</strong>' ) . '</p></div>';
       
   141 			} else {
       
   142 				echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed'], $title) . '</p></div>';
       
   143 			}
       
   144 
       
   145 			echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
       
   146 		}
       
   147 		if ( $this->result && ! is_wp_error( $this->result ) ) {
       
   148 			if ( ! $this->error ) {
       
   149 				echo '<div class="updated js-update-details" data-update-details="progress-' . esc_attr( $this->upgrader->update_current ) . '">' .
       
   150 					'<p>' . sprintf( $this->upgrader->strings['skin_update_successful'], $title ) .
       
   151 					' <button type="button" class="hide-if-no-js button-link js-update-details-toggle" aria-expanded="false">' . __( 'Show details.' ) . '</button>' .
       
   152 					'</p></div>';
       
   153 			}
       
   154 
       
   155 			echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
       
   156 		}
       
   157 
       
   158 		$this->reset();
       
   159 		$this->flush_output();
       
   160 	}
       
   161 
       
   162 	/**
       
   163 	 */
       
   164 	public function reset() {
       
   165 		$this->in_loop = false;
       
   166 		$this->error = false;
       
   167 	}
       
   168 
       
   169 	/**
       
   170 	 */
       
   171 	public function flush_output() {
       
   172 		wp_ob_end_flush_all();
       
   173 		flush();
       
   174 	}
       
   175 }