class-bulk-upgrader-skin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * @param string $string
  48. * @param mixed ...$args Optional text replacements.
  49. */
  50. public function feedback( $string, ...$args ) {
  51. if ( isset( $this->upgrader->strings[ $string ] ) ) {
  52. $string = $this->upgrader->strings[ $string ];
  53. }
  54. if ( strpos( $string, '%' ) !== false ) {
  55. if ( $args ) {
  56. $args = array_map( 'strip_tags', $args );
  57. $args = array_map( 'esc_html', $args );
  58. $string = vsprintf( $string, $args );
  59. }
  60. }
  61. if ( empty( $string ) ) {
  62. return;
  63. }
  64. if ( $this->in_loop ) {
  65. echo "$string<br />\n";
  66. } else {
  67. echo "<p>$string</p>\n";
  68. }
  69. }
  70. /**
  71. */
  72. public function header() {
  73. // Nothing, This will be displayed within a iframe.
  74. }
  75. /**
  76. */
  77. public function footer() {
  78. // Nothing, This will be displayed within a iframe.
  79. }
  80. /**
  81. * @param string|WP_Error $error
  82. */
  83. public function error( $error ) {
  84. if ( is_string( $error ) && isset( $this->upgrader->strings[ $error ] ) ) {
  85. $this->error = $this->upgrader->strings[ $error ];
  86. }
  87. if ( is_wp_error( $error ) ) {
  88. $messages = array();
  89. foreach ( $error->get_error_messages() as $emessage ) {
  90. if ( $error->get_error_data() && is_string( $error->get_error_data() ) ) {
  91. $messages[] = $emessage . ' ' . esc_html( strip_tags( $error->get_error_data() ) );
  92. } else {
  93. $messages[] = $emessage;
  94. }
  95. }
  96. $this->error = implode( ', ', $messages );
  97. }
  98. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').hide();</script>';
  99. }
  100. /**
  101. */
  102. public function bulk_header() {
  103. $this->feedback( 'skin_upgrade_start' );
  104. }
  105. /**
  106. */
  107. public function bulk_footer() {
  108. $this->feedback( 'skin_upgrade_end' );
  109. }
  110. /**
  111. * @param string $title
  112. */
  113. public function before( $title = '' ) {
  114. $this->in_loop = true;
  115. 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 );
  116. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').css("display", "inline-block");</script>';
  117. // This progress messages div gets moved via JavaScript when clicking on "Show details.".
  118. echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr( $this->upgrader->update_current ) . '"><p>';
  119. $this->flush_output();
  120. }
  121. /**
  122. * @param string $title
  123. */
  124. public function after( $title = '' ) {
  125. echo '</p></div>';
  126. if ( $this->error || ! $this->result ) {
  127. if ( $this->error ) {
  128. echo '<div class="error"><p>' . sprintf( $this->upgrader->strings['skin_update_failed_error'], $title, '<strong>' . $this->error . '</strong>' ) . '</p></div>';
  129. } else {
  130. echo '<div class="error"><p>' . sprintf( $this->upgrader->strings['skin_update_failed'], $title ) . '</p></div>';
  131. }
  132. echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js( $this->upgrader->update_current ) . '\').show();</script>';
  133. }
  134. if ( $this->result && ! is_wp_error( $this->result ) ) {
  135. if ( ! $this->error ) {
  136. echo '<div class="updated js-update-details" data-update-details="progress-' . esc_attr( $this->upgrader->update_current ) . '">' .
  137. '<p>' . sprintf( $this->upgrader->strings['skin_update_successful'], $title ) .
  138. ' <button type="button" class="hide-if-no-js button-link js-update-details-toggle" aria-expanded="false">' . __( 'Show details.' ) . '</button>' .
  139. '</p></div>';
  140. }
  141. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').hide();</script>';
  142. }
  143. $this->reset();
  144. $this->flush_output();
  145. }
  146. /**
  147. */
  148. public function reset() {
  149. $this->in_loop = false;
  150. $this->error = false;
  151. }
  152. /**
  153. */
  154. public function flush_output() {
  155. wp_ob_end_flush_all();
  156. flush();
  157. }
  158. }