class-automatic-upgrader-skin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 $error Optional. Whether the current request has failed to connect.
  32. * 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. // TODO: fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version
  43. // This will output a credentials form in event of failure, We don't want that, so just hide with a buffer
  44. ob_start();
  45. $result = parent::request_filesystem_credentials( $error, $context, $allow_relaxed_file_ownership );
  46. ob_end_clean();
  47. return $result;
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function get_upgrade_messages() {
  53. return $this->messages;
  54. }
  55. /**
  56. * @param string|array|WP_Error $data
  57. * @param mixed ...$args Optional text replacements.
  58. */
  59. public function feedback( $data, ...$args ) {
  60. if ( is_wp_error( $data ) ) {
  61. $string = $data->get_error_message();
  62. } elseif ( is_array( $data ) ) {
  63. return;
  64. } else {
  65. $string = $data;
  66. }
  67. if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  68. $string = $this->upgrader->strings[ $string ];
  69. }
  70. if ( strpos( $string, '%' ) !== false ) {
  71. if ( ! empty( $args ) ) {
  72. $string = vsprintf( $string, $args );
  73. }
  74. }
  75. $string = trim( $string );
  76. // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
  77. $string = wp_kses(
  78. $string,
  79. array(
  80. 'a' => array(
  81. 'href' => true,
  82. ),
  83. 'br' => true,
  84. 'em' => true,
  85. 'strong' => true,
  86. )
  87. );
  88. if ( empty( $string ) ) {
  89. return;
  90. }
  91. $this->messages[] = $string;
  92. }
  93. /**
  94. */
  95. public function header() {
  96. ob_start();
  97. }
  98. /**
  99. */
  100. public function footer() {
  101. $output = ob_get_clean();
  102. if ( ! empty( $output ) ) {
  103. $this->feedback( $output );
  104. }
  105. }
  106. }