class-bulk-upgrader-skin.php 5.1 KB

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