class-wp-ajax-upgrader-skin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Upgrader API: WP_Ajax_Upgrader_Skin class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Upgrader Skin for Ajax WordPress upgrades.
  11. *
  12. * This skin is designed to be used for Ajax updates.
  13. *
  14. * @since 4.6.0
  15. *
  16. * @see Automatic_Upgrader_Skin
  17. */
  18. class WP_Ajax_Upgrader_Skin extends Automatic_Upgrader_Skin {
  19. /**
  20. * Plugin info.
  21. *
  22. * The Plugin_Upgrader::bulk_upgrade() method will fill this in
  23. * with info retrieved from the get_plugin_data() function.
  24. *
  25. * @var array Plugin data. Values will be empty if not supplied by the plugin.
  26. */
  27. public $plugin_info = array();
  28. /**
  29. * Theme info.
  30. *
  31. * The Theme_Upgrader::bulk_upgrade() method will fill this in
  32. * with info retrieved from the Theme_Upgrader::theme_info() method,
  33. * which in turn calls the wp_get_theme() function.
  34. *
  35. * @var WP_Theme|false The theme's info object, or false.
  36. */
  37. public $theme_info = false;
  38. /**
  39. * Holds the WP_Error object.
  40. *
  41. * @since 4.6.0
  42. *
  43. * @var null|WP_Error
  44. */
  45. protected $errors = null;
  46. /**
  47. * Constructor.
  48. *
  49. * Sets up the WordPress Ajax upgrader skin.
  50. *
  51. * @since 4.6.0
  52. *
  53. * @see WP_Upgrader_Skin::__construct()
  54. *
  55. * @param array $args Optional. The WordPress Ajax upgrader skin arguments to
  56. * override default options. See WP_Upgrader_Skin::__construct().
  57. * Default empty array.
  58. */
  59. public function __construct( $args = array() ) {
  60. parent::__construct( $args );
  61. $this->errors = new WP_Error();
  62. }
  63. /**
  64. * Retrieves the list of errors.
  65. *
  66. * @since 4.6.0
  67. *
  68. * @return WP_Error Errors during an upgrade.
  69. */
  70. public function get_errors() {
  71. return $this->errors;
  72. }
  73. /**
  74. * Retrieves a string for error messages.
  75. *
  76. * @since 4.6.0
  77. *
  78. * @return string Error messages during an upgrade.
  79. */
  80. public function get_error_messages() {
  81. $messages = array();
  82. foreach ( $this->errors->get_error_codes() as $error_code ) {
  83. $error_data = $this->errors->get_error_data( $error_code );
  84. if ( $error_data && is_string( $error_data ) ) {
  85. $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $error_data ) );
  86. } else {
  87. $messages[] = $this->errors->get_error_message( $error_code );
  88. }
  89. }
  90. return implode( ', ', $messages );
  91. }
  92. /**
  93. * Stores an error message about the upgrade.
  94. *
  95. * @since 4.6.0
  96. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  97. * to the function signature.
  98. *
  99. * @param string|WP_Error $errors Errors.
  100. * @param mixed ...$args Optional text replacements.
  101. */
  102. public function error( $errors, ...$args ) {
  103. if ( is_string( $errors ) ) {
  104. $string = $errors;
  105. if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  106. $string = $this->upgrader->strings[ $string ];
  107. }
  108. if ( false !== strpos( $string, '%' ) ) {
  109. if ( ! empty( $args ) ) {
  110. $string = vsprintf( $string, $args );
  111. }
  112. }
  113. // Count existing errors to generate a unique error code.
  114. $errors_count = count( $this->errors->get_error_codes() );
  115. $this->errors->add( 'unknown_upgrade_error_' . ( $errors_count + 1 ), $string );
  116. } elseif ( is_wp_error( $errors ) ) {
  117. foreach ( $errors->get_error_codes() as $error_code ) {
  118. $this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
  119. }
  120. }
  121. parent::error( $errors, ...$args );
  122. }
  123. /**
  124. * Stores a message about the upgrade.
  125. *
  126. * @since 4.6.0
  127. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  128. * to the function signature.
  129. * @since 5.9.0 Renamed `$data` to `$feedback` for PHP 8 named parameter support.
  130. *
  131. * @param string|array|WP_Error $feedback Message data.
  132. * @param mixed ...$args Optional text replacements.
  133. */
  134. public function feedback( $feedback, ...$args ) {
  135. if ( is_wp_error( $feedback ) ) {
  136. foreach ( $feedback->get_error_codes() as $error_code ) {
  137. $this->errors->add( $error_code, $feedback->get_error_message( $error_code ), $feedback->get_error_data( $error_code ) );
  138. }
  139. }
  140. parent::feedback( $feedback, ...$args );
  141. }
  142. }