class-automatic-upgrader-skin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Upgrader API: Automatic_Upgrader_Skin class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Upgrader Skin for Automatic WordPress Upgrades.
  11. *
  12. * This skin is designed to be used when no output is intended, all output
  13. * is captured and stored for the caller to process and log/email/discard.
  14. *
  15. * @since 3.7.0
  16. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
  17. *
  18. * @see Bulk_Upgrader_Skin
  19. */
  20. class Automatic_Upgrader_Skin extends WP_Upgrader_Skin {
  21. protected $messages = array();
  22. /**
  23. * Determines whether the upgrader needs FTP/SSH details in order to connect
  24. * to the filesystem.
  25. *
  26. * @since 3.7.0
  27. * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
  28. *
  29. * @see request_filesystem_credentials()
  30. *
  31. * @param bool|WP_Error $error Optional. Whether the current request has failed to connect,
  32. * or an error object. Default false.
  33. * @param string $context Optional. Full path to the directory that is tested
  34. * for being writable. Default empty.
  35. * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
  36. * @return bool True on success, false on failure.
  37. */
  38. public function request_filesystem_credentials( $error = false, $context = '', $allow_relaxed_file_ownership = false ) {
  39. if ( $context ) {
  40. $this->options['context'] = $context;
  41. }
  42. /*
  43. * TODO: Fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version.
  44. * This will output a credentials form in event of failure. We don't want that, so just hide with a buffer.
  45. */
  46. ob_start();
  47. $result = parent::request_filesystem_credentials( $error, $context, $allow_relaxed_file_ownership );
  48. ob_end_clean();
  49. return $result;
  50. }
  51. /**
  52. * Retrieves the upgrade messages.
  53. *
  54. * @since 3.7.0
  55. *
  56. * @return string[] Messages during an upgrade.
  57. */
  58. public function get_upgrade_messages() {
  59. return $this->messages;
  60. }
  61. /**
  62. * Stores a message about the upgrade.
  63. *
  64. * @since 3.7.0
  65. * @since 5.9.0 Renamed `$data` to `$feedback` for PHP 8 named parameter support.
  66. *
  67. * @param string|array|WP_Error $feedback Message data.
  68. * @param mixed ...$args Optional text replacements.
  69. */
  70. public function feedback( $feedback, ...$args ) {
  71. if ( is_wp_error( $feedback ) ) {
  72. $string = $feedback->get_error_message();
  73. } elseif ( is_array( $feedback ) ) {
  74. return;
  75. } else {
  76. $string = $feedback;
  77. }
  78. if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  79. $string = $this->upgrader->strings[ $string ];
  80. }
  81. if ( strpos( $string, '%' ) !== false ) {
  82. if ( ! empty( $args ) ) {
  83. $string = vsprintf( $string, $args );
  84. }
  85. }
  86. $string = trim( $string );
  87. // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
  88. $string = wp_kses(
  89. $string,
  90. array(
  91. 'a' => array(
  92. 'href' => true,
  93. ),
  94. 'br' => true,
  95. 'em' => true,
  96. 'strong' => true,
  97. )
  98. );
  99. if ( empty( $string ) ) {
  100. return;
  101. }
  102. $this->messages[] = $string;
  103. }
  104. /**
  105. * Creates a new output buffer.
  106. *
  107. * @since 3.7.0
  108. */
  109. public function header() {
  110. ob_start();
  111. }
  112. /**
  113. * Retrieves the buffered content, deletes the buffer, and processes the output.
  114. *
  115. * @since 3.7.0
  116. */
  117. public function footer() {
  118. $output = ob_get_clean();
  119. if ( ! empty( $output ) ) {
  120. $this->feedback( $output );
  121. }
  122. }
  123. }