Aucune description

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Holds the WP_Error object.
  21. *
  22. * @since 4.6.0
  23. *
  24. * @var null|WP_Error
  25. */
  26. protected $errors = null;
  27. /**
  28. * Constructor.
  29. *
  30. * Sets up the WordPress Ajax upgrader skin.
  31. *
  32. * @since 4.6.0
  33. *
  34. * @see WP_Upgrader_Skin::__construct()
  35. *
  36. * @param array $args Optional. The WordPress Ajax upgrader skin arguments to
  37. * override default options. See WP_Upgrader_Skin::__construct().
  38. * Default empty array.
  39. */
  40. public function __construct( $args = array() ) {
  41. parent::__construct( $args );
  42. $this->errors = new WP_Error();
  43. }
  44. /**
  45. * Retrieves the list of errors.
  46. *
  47. * @since 4.6.0
  48. *
  49. * @return WP_Error Errors during an upgrade.
  50. */
  51. public function get_errors() {
  52. return $this->errors;
  53. }
  54. /**
  55. * Retrieves a string for error messages.
  56. *
  57. * @since 4.6.0
  58. *
  59. * @return string Error messages during an upgrade.
  60. */
  61. public function get_error_messages() {
  62. $messages = array();
  63. foreach ( $this->errors->get_error_codes() as $error_code ) {
  64. $error_data = $this->errors->get_error_data( $error_code );
  65. if ( $error_data && is_string( $error_data ) ) {
  66. $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $error_data ) );
  67. } else {
  68. $messages[] = $this->errors->get_error_message( $error_code );
  69. }
  70. }
  71. return implode( ', ', $messages );
  72. }
  73. /**
  74. * Stores an error message about the upgrade.
  75. *
  76. * @since 4.6.0
  77. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  78. * to the function signature.
  79. *
  80. * @param string|WP_Error $errors Errors.
  81. * @param mixed ...$args Optional text replacements.
  82. */
  83. public function error( $errors, ...$args ) {
  84. if ( is_string( $errors ) ) {
  85. $string = $errors;
  86. if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  87. $string = $this->upgrader->strings[ $string ];
  88. }
  89. if ( false !== strpos( $string, '%' ) ) {
  90. if ( ! empty( $args ) ) {
  91. $string = vsprintf( $string, $args );
  92. }
  93. }
  94. // Count existing errors to generate a unique error code.
  95. $errors_count = count( $this->errors->get_error_codes() );
  96. $this->errors->add( 'unknown_upgrade_error_' . ( $errors_count + 1 ), $string );
  97. } elseif ( is_wp_error( $errors ) ) {
  98. foreach ( $errors->get_error_codes() as $error_code ) {
  99. $this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
  100. }
  101. }
  102. parent::error( $errors, ...$args );
  103. }
  104. /**
  105. * Stores a message about the upgrade.
  106. *
  107. * @since 4.6.0
  108. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  109. * to the function signature.
  110. * @since 5.9.0 Renamed `$data` to `$feedback` for PHP 8 named parameter support.
  111. *
  112. * @param string|array|WP_Error $feedback Message data.
  113. * @param mixed ...$args Optional text replacements.
  114. */
  115. public function feedback( $feedback, ...$args ) {
  116. if ( is_wp_error( $feedback ) ) {
  117. foreach ( $feedback->get_error_codes() as $error_code ) {
  118. $this->errors->add( $error_code, $feedback->get_error_message( $error_code ), $feedback->get_error_data( $error_code ) );
  119. }
  120. }
  121. parent::feedback( $feedback, ...$args );
  122. }
  123. }