Nessuna descrizione

class-wp-upgrader-skin.php 6.3KB

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