Няма описание

class-automatic-upgrader-skin.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. *
  66. * @param string|array|WP_Error $data Message data.
  67. * @param mixed ...$args Optional text replacements.
  68. */
  69. public function feedback( $data, ...$args ) {
  70. if ( is_wp_error( $data ) ) {
  71. $string = $data->get_error_message();
  72. } elseif ( is_array( $data ) ) {
  73. return;
  74. } else {
  75. $string = $data;
  76. }
  77. if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  78. $string = $this->upgrader->strings[ $string ];
  79. }
  80. if ( strpos( $string, '%' ) !== false ) {
  81. if ( ! empty( $args ) ) {
  82. $string = vsprintf( $string, $args );
  83. }
  84. }
  85. $string = trim( $string );
  86. // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
  87. $string = wp_kses(
  88. $string,
  89. array(
  90. 'a' => array(
  91. 'href' => true,
  92. ),
  93. 'br' => true,
  94. 'em' => true,
  95. 'strong' => true,
  96. )
  97. );
  98. if ( empty( $string ) ) {
  99. return;
  100. }
  101. $this->messages[] = $string;
  102. }
  103. /**
  104. * Creates a new output buffer.
  105. *
  106. * @since 3.7.0
  107. */
  108. public function header() {
  109. ob_start();
  110. }
  111. /**
  112. * Retrieves the buffered content, deletes the buffer, and processes the output.
  113. *
  114. * @since 3.7.0
  115. */
  116. public function footer() {
  117. $output = ob_get_clean();
  118. if ( ! empty( $output ) ) {
  119. $this->feedback( $output );
  120. }
  121. }
  122. }