class-bulk-upgrader-skin.php 5.4 KB

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