[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-bulk-upgrader-skin.php (source)

   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  
  20      /**
  21       * Whether the bulk update process has started.
  22       *
  23       * @since 3.0.0
  24       * @var bool
  25       */
  26      public $in_loop = false;
  27  
  28      /**
  29       * Stores an error message about the update.
  30       *
  31       * @since 3.0.0
  32       * @var string|false
  33       */
  34      public $error = false;
  35  
  36      /**
  37       * Constructor.
  38       *
  39       * Sets up the generic skin for the Bulk Upgrader classes.
  40       *
  41       * @since 3.0.0
  42       *
  43       * @param array $args
  44       */
  45  	public function __construct( $args = array() ) {
  46          $defaults = array(
  47              'url'   => '',
  48              'nonce' => '',
  49          );
  50          $args     = wp_parse_args( $args, $defaults );
  51  
  52          parent::__construct( $args );
  53      }
  54  
  55      /**
  56       * Sets up the strings used in the update process.
  57       *
  58       * @since 3.0.0
  59       */
  60  	public function add_strings() {
  61          $this->upgrader->strings['skin_upgrade_start'] = __( 'The update process is starting. This process may take a while on some hosts, so please be patient.' );
  62          /* translators: 1: Title of an update, 2: Error message. */
  63          $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while updating %1$s: %2$s' );
  64          /* translators: %s: Title of an update. */
  65          $this->upgrader->strings['skin_update_failed'] = __( 'The update of %s failed.' );
  66          /* translators: %s: Title of an update. */
  67          $this->upgrader->strings['skin_update_successful'] = __( '%s updated successfully.' );
  68          $this->upgrader->strings['skin_upgrade_end']       = __( 'All updates have been completed.' );
  69      }
  70  
  71      /**
  72       * Displays a message about the update.
  73       *
  74       * @since 3.0.0
  75       * @since 5.9.0 Renamed `$string` (a PHP reserved keyword) to `$feedback` for PHP 8 named parameter support.
  76       *
  77       * @param string $feedback Message data.
  78       * @param mixed  ...$args  Optional text replacements.
  79       */
  80  	public function feedback( $feedback, ...$args ) {
  81          if ( isset( $this->upgrader->strings[ $feedback ] ) ) {
  82              $feedback = $this->upgrader->strings[ $feedback ];
  83          }
  84  
  85          if ( str_contains( $feedback, '%' ) ) {
  86              if ( $args ) {
  87                  $args     = array_map( 'strip_tags', $args );
  88                  $args     = array_map( 'esc_html', $args );
  89                  $feedback = vsprintf( $feedback, $args );
  90              }
  91          }
  92          if ( empty( $feedback ) ) {
  93              return;
  94          }
  95          if ( $this->in_loop ) {
  96              echo "$feedback<br />\n";
  97          } else {
  98              echo "<p>$feedback</p>\n";
  99          }
 100      }
 101  
 102      /**
 103       * Displays the header before the update process.
 104       *
 105       * @since 3.0.0
 106       */
 107  	public function header() {
 108          // Nothing. This will be displayed within an iframe.
 109      }
 110  
 111      /**
 112       * Displays the footer following the update process.
 113       *
 114       * @since 3.0.0
 115       */
 116  	public function footer() {
 117          // Nothing. This will be displayed within an iframe.
 118      }
 119  
 120      /**
 121       * Displays an error message about the update.
 122       *
 123       * @since 3.0.0
 124       * @since 5.9.0 Renamed `$error` to `$errors` for PHP 8 named parameter support.
 125       *
 126       * @param string|WP_Error $errors Errors.
 127       */
 128  	public function error( $errors ) {
 129          if ( is_string( $errors ) && isset( $this->upgrader->strings[ $errors ] ) ) {
 130              $this->error = $this->upgrader->strings[ $errors ];
 131          }
 132  
 133          if ( is_wp_error( $errors ) ) {
 134              $messages = array();
 135              foreach ( $errors->get_error_messages() as $emessage ) {
 136                  if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
 137                      $messages[] = $emessage . ' ' . esc_html( strip_tags( $errors->get_error_data() ) );
 138                  } else {
 139                      $messages[] = $emessage;
 140                  }
 141              }
 142              $this->error = implode( ', ', $messages );
 143          }
 144          $js_function = <<<'JS'
 145              ( selector ) => {
 146                  jQuery( selector ).hide();
 147              }
 148              JS;
 149          wp_print_inline_script_tag(
 150              sprintf(
 151                  '( %s )( %s );',
 152                  $js_function,
 153                  wp_json_encode( '.waiting-' . $this->upgrader->update_current, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 154              )
 155          );
 156      }
 157  
 158      /**
 159       * Displays the header before the bulk update process.
 160       *
 161       * @since 3.0.0
 162       */
 163  	public function bulk_header() {
 164          $this->feedback( 'skin_upgrade_start' );
 165      }
 166  
 167      /**
 168       * Displays the footer following the bulk update process.
 169       *
 170       * @since 3.0.0
 171       */
 172  	public function bulk_footer() {
 173          $this->feedback( 'skin_upgrade_end' );
 174      }
 175  
 176      /**
 177       * Performs an action before a bulk update.
 178       *
 179       * @since 3.0.0
 180       *
 181       * @param string $title
 182       */
 183  	public function before( $title = '' ) {
 184          $this->in_loop = true;
 185          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 );
 186          $js_function = <<<'JS'
 187              ( selector ) => {
 188                  jQuery( selector ).css( 'display', 'inline-block' );
 189              }
 190              JS;
 191          wp_print_inline_script_tag(
 192              sprintf(
 193                  '( %s )( %s );',
 194                  $js_function,
 195                  wp_json_encode( '.waiting-' . $this->upgrader->update_current, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 196              )
 197          );
 198          // This progress messages div gets moved via JavaScript when clicking on "More details.".
 199          echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr( $this->upgrader->update_current ) . '"><p>';
 200          $this->flush_output();
 201      }
 202  
 203      /**
 204       * Performs an action following a bulk update.
 205       *
 206       * @since 3.0.0
 207       *
 208       * @param string $title
 209       */
 210  	public function after( $title = '' ) {
 211          echo '</p></div>';
 212          if ( $this->error || ! $this->result ) {
 213              if ( $this->error ) {
 214                  $after_error_message = sprintf( $this->upgrader->strings['skin_update_failed_error'], $title, '<strong>' . $this->error . '</strong>' );
 215              } else {
 216                  $after_error_message = sprintf( $this->upgrader->strings['skin_update_failed'], $title );
 217              }
 218              wp_admin_notice(
 219                  $after_error_message,
 220                  array(
 221                      'additional_classes' => array( 'error' ),
 222                  )
 223              );
 224  
 225              $js_function = <<<'JS'
 226                  ( selector ) => {
 227                      jQuery( selector ).show();
 228                  }
 229                  JS;
 230              wp_print_inline_script_tag(
 231                  sprintf(
 232                      '( %s )( %s );',
 233                      $js_function,
 234                      wp_json_encode( '#progress-' . $this->upgrader->update_current, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 235                  )
 236              );
 237          }
 238          if ( $this->result && ! is_wp_error( $this->result ) ) {
 239              if ( ! $this->error ) {
 240                  echo '<div class="updated js-update-details" data-update-details="progress-' . esc_attr( $this->upgrader->update_current ) . '">' .
 241                      '<p>' . sprintf( $this->upgrader->strings['skin_update_successful'], $title ) .
 242                      ' <button type="button" class="hide-if-no-js button-link js-update-details-toggle" aria-expanded="false">' . __( 'More details.' ) . '<span class="dashicons dashicons-arrow-down" aria-hidden="true"></span></button>' .
 243                      '</p></div>';
 244              }
 245  
 246              $js_function = <<<'JS'
 247                  ( selector ) => {
 248                      jQuery( selector ).hide();
 249                  }
 250                  JS;
 251              wp_print_inline_script_tag(
 252                  sprintf(
 253                      '( %s )( %s );',
 254                      $js_function,
 255                      wp_json_encode( '.waiting-' . $this->upgrader->update_current, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 256                  )
 257              );
 258          }
 259  
 260          $this->reset();
 261          $this->flush_output();
 262      }
 263  
 264      /**
 265       * Resets the properties used in the update process.
 266       *
 267       * @since 3.0.0
 268       */
 269  	public function reset() {
 270          $this->in_loop = false;
 271          $this->error   = false;
 272      }
 273  
 274      /**
 275       * Flushes all output buffers.
 276       *
 277       * @since 3.0.0
 278       */
 279  	public function flush_output() {
 280          wp_ob_end_flush_all();
 281          flush();
 282      }
 283  }


Generated : Tue Sep 29 08:20:35 2026 Cross-referenced by PHPXref