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

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