class-wp-upgrader-skin.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Upgrader API: WP_Upgrader_Skin class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
  11. *
  12. * @since 2.8.0
  13. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
  14. */
  15. #[AllowDynamicProperties]
  16. class WP_Upgrader_Skin {
  17. /**
  18. * Holds the upgrader data.
  19. *
  20. * @since 2.8.0
  21. *
  22. * @var WP_Upgrader
  23. */
  24. public $upgrader;
  25. /**
  26. * Whether header is done.
  27. *
  28. * @since 2.8.0
  29. *
  30. * @var bool
  31. */
  32. public $done_header = false;
  33. /**
  34. * Whether footer is done.
  35. *
  36. * @since 2.8.0
  37. *
  38. * @var bool
  39. */
  40. public $done_footer = false;
  41. /**
  42. * Holds the result of an upgrade.
  43. *
  44. * @since 2.8.0
  45. *
  46. * @var string|bool|WP_Error
  47. */
  48. public $result = false;
  49. /**
  50. * Holds the options of an upgrade.
  51. *
  52. * @since 2.8.0
  53. *
  54. * @var array
  55. */
  56. public $options = array();
  57. /**
  58. * Constructor.
  59. *
  60. * Sets up the generic skin for the WordPress Upgrader classes.
  61. *
  62. * @since 2.8.0
  63. *
  64. * @param array $args Optional. The WordPress upgrader skin arguments to
  65. * override default options. Default empty array.
  66. */
  67. public function __construct( $args = array() ) {
  68. $defaults = array(
  69. 'url' => '',
  70. 'nonce' => '',
  71. 'title' => '',
  72. 'context' => false,
  73. );
  74. $this->options = wp_parse_args( $args, $defaults );
  75. }
  76. /**
  77. * @since 2.8.0
  78. *
  79. * @param WP_Upgrader $upgrader
  80. */
  81. public function set_upgrader( &$upgrader ) {
  82. if ( is_object( $upgrader ) ) {
  83. $this->upgrader =& $upgrader;
  84. }
  85. $this->add_strings();
  86. }
  87. /**
  88. * @since 3.0.0
  89. */
  90. public function add_strings() {
  91. }
  92. /**
  93. * Sets the result of an upgrade.
  94. *
  95. * @since 2.8.0
  96. *
  97. * @param string|bool|WP_Error $result The result of an upgrade.
  98. */
  99. public function set_result( $result ) {
  100. $this->result = $result;
  101. }
  102. /**
  103. * Displays a form to the user to request for their FTP/SSH details in order
  104. * to connect to the filesystem.
  105. *
  106. * @since 2.8.0
  107. * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
  108. *
  109. * @see request_filesystem_credentials()
  110. *
  111. * @param bool|WP_Error $error Optional. Whether the current request has failed to connect,
  112. * or an error object. Default false.
  113. * @param string $context Optional. Full path to the directory that is tested
  114. * for being writable. Default empty.
  115. * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
  116. * @return bool True on success, false on failure.
  117. */
  118. public function request_filesystem_credentials( $error = false, $context = '', $allow_relaxed_file_ownership = false ) {
  119. $url = $this->options['url'];
  120. if ( ! $context ) {
  121. $context = $this->options['context'];
  122. }
  123. if ( ! empty( $this->options['nonce'] ) ) {
  124. $url = wp_nonce_url( $url, $this->options['nonce'] );
  125. }
  126. $extra_fields = array();
  127. return request_filesystem_credentials( $url, '', $error, $context, $extra_fields, $allow_relaxed_file_ownership );
  128. }
  129. /**
  130. * @since 2.8.0
  131. */
  132. public function header() {
  133. if ( $this->done_header ) {
  134. return;
  135. }
  136. $this->done_header = true;
  137. echo '<div class="wrap">';
  138. echo '<h1>' . $this->options['title'] . '</h1>';
  139. }
  140. /**
  141. * @since 2.8.0
  142. */
  143. public function footer() {
  144. if ( $this->done_footer ) {
  145. return;
  146. }
  147. $this->done_footer = true;
  148. echo '</div>';
  149. }
  150. /**
  151. * @since 2.8.0
  152. *
  153. * @param string|WP_Error $errors Errors.
  154. */
  155. public function error( $errors ) {
  156. if ( ! $this->done_header ) {
  157. $this->header();
  158. }
  159. if ( is_string( $errors ) ) {
  160. $this->feedback( $errors );
  161. } elseif ( is_wp_error( $errors ) && $errors->has_errors() ) {
  162. foreach ( $errors->get_error_messages() as $message ) {
  163. if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
  164. $this->feedback( $message . ' ' . esc_html( strip_tags( $errors->get_error_data() ) ) );
  165. } else {
  166. $this->feedback( $message );
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * @since 2.8.0
  173. * @since 5.9.0 Renamed `$string` (a PHP reserved keyword) to `$feedback` for PHP 8 named parameter support.
  174. *
  175. * @param string $feedback Message data.
  176. * @param mixed ...$args Optional text replacements.
  177. */
  178. public function feedback( $feedback, ...$args ) {
  179. if ( isset( $this->upgrader->strings[ $feedback ] ) ) {
  180. $feedback = $this->upgrader->strings[ $feedback ];
  181. }
  182. if ( strpos( $feedback, '%' ) !== false ) {
  183. if ( $args ) {
  184. $args = array_map( 'strip_tags', $args );
  185. $args = array_map( 'esc_html', $args );
  186. $feedback = vsprintf( $feedback, $args );
  187. }
  188. }
  189. if ( empty( $feedback ) ) {
  190. return;
  191. }
  192. show_message( $feedback );
  193. }
  194. /**
  195. * Action to perform before an update.
  196. *
  197. * @since 2.8.0
  198. */
  199. public function before() {}
  200. /**
  201. * Action to perform following an update.
  202. *
  203. * @since 2.8.0
  204. */
  205. public function after() {}
  206. /**
  207. * Output JavaScript that calls function to decrement the update counts.
  208. *
  209. * @since 3.9.0
  210. *
  211. * @param string $type Type of update count to decrement. Likely values include 'plugin',
  212. * 'theme', 'translation', etc.
  213. */
  214. protected function decrement_update_count( $type ) {
  215. if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) {
  216. return;
  217. }
  218. if ( defined( 'IFRAME_REQUEST' ) ) {
  219. echo '<script type="text/javascript">
  220. if ( window.postMessage && JSON ) {
  221. window.parent.postMessage( JSON.stringify( { action: "decrementUpdateCount", upgradeType: "' . $type . '" } ), window.location.protocol + "//" + window.location.hostname );
  222. }
  223. </script>';
  224. } else {
  225. echo '<script type="text/javascript">
  226. (function( wp ) {
  227. if ( wp && wp.updates && wp.updates.decrementCount ) {
  228. wp.updates.decrementCount( "' . $type . '" );
  229. }
  230. })( window.wp );
  231. </script>';
  232. }
  233. }
  234. /**
  235. * @since 3.0.0
  236. */
  237. public function bulk_header() {}
  238. /**
  239. * @since 3.0.0
  240. */
  241. public function bulk_footer() {}
  242. /**
  243. * Hides the `process_failed` error message when updating by uploading a zip file.
  244. *
  245. * @since 5.5.0
  246. *
  247. * @param WP_Error $wp_error WP_Error object.
  248. * @return bool
  249. */
  250. public function hide_process_failed( $wp_error ) {
  251. return false;
  252. }
  253. }