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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if ( $this->errors->get_error_data( $error_code ) && is_string( $this->errors->get_error_data( $error_code ) ) ) {
  58. $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $this->errors->get_error_data( $error_code ) ) );
  59. } else {
  60. $messages[] = $this->errors->get_error_message( $error_code );
  61. }
  62. }
  63. return implode( ', ', $messages );
  64. }
  65. /**
  66. * Stores a log entry for an error.
  67. *
  68. * @since 4.6.0
  69. *
  70. * @param string|WP_Error $errors Errors.
  71. */
  72. public function error( $errors ) {
  73. if ( is_string( $errors ) ) {
  74. $string = $errors;
  75. if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  76. $string = $this->upgrader->strings[ $string ];
  77. }
  78. if ( false !== strpos( $string, '%' ) ) {
  79. $args = func_get_args();
  80. $args = array_splice( $args, 1 );
  81. if ( ! empty( $args ) ) {
  82. $string = vsprintf( $string, $args );
  83. }
  84. }
  85. // Count existing errors to generate an unique error code.
  86. $errors_count = count( $this->errors->get_error_codes() );
  87. $this->errors->add( 'unknown_upgrade_error_' . $errors_count + 1 , $string );
  88. } elseif ( is_wp_error( $errors ) ) {
  89. foreach ( $errors->get_error_codes() as $error_code ) {
  90. $this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
  91. }
  92. }
  93. $args = func_get_args();
  94. call_user_func_array( array( $this, 'parent::error' ), $args );
  95. }
  96. /**
  97. * Stores a log entry.
  98. *
  99. * @since 4.6.0
  100. *
  101. * @param string|array|WP_Error $data Log entry data.
  102. */
  103. public function feedback( $data ) {
  104. if ( is_wp_error( $data ) ) {
  105. foreach ( $data->get_error_codes() as $error_code ) {
  106. $this->errors->add( $error_code, $data->get_error_message( $error_code ), $data->get_error_data( $error_code ) );
  107. }
  108. }
  109. $args = func_get_args();
  110. call_user_func_array( array( $this, 'parent::feedback' ), $args );
  111. }
  112. }