/wp-admin/includes/class-wp-upgrader-skin.php

https://github.com/livinglab/openlab · PHP · 276 lines · 113 code · 25 blank · 138 comment · 20 complexity · 4d243dac389a3a94fae6e205417ac86e MD5 · raw file

  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
  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. *
  173. * @param string $string
  174. * @param mixed ...$args Optional text replacements.
  175. */
  176. public function feedback( $string, ...$args ) {
  177. if ( isset( $this->upgrader->strings[ $string ] ) ) {
  178. $string = $this->upgrader->strings[ $string ];
  179. }
  180. if ( strpos( $string, '%' ) !== false ) {
  181. if ( $args ) {
  182. $args = array_map( 'strip_tags', $args );
  183. $args = array_map( 'esc_html', $args );
  184. $string = vsprintf( $string, $args );
  185. }
  186. }
  187. if ( empty( $string ) ) {
  188. return;
  189. }
  190. show_message( $string );
  191. }
  192. /**
  193. * Action to perform before an update.
  194. *
  195. * @since 2.8.0
  196. */
  197. public function before() {}
  198. /**
  199. * Action to perform following an update.
  200. *
  201. * @since 2.8.0
  202. */
  203. public function after() {}
  204. /**
  205. * Output JavaScript that calls function to decrement the update counts.
  206. *
  207. * @since 3.9.0
  208. *
  209. * @param string $type Type of update count to decrement. Likely values include 'plugin',
  210. * 'theme', 'translation', etc.
  211. */
  212. protected function decrement_update_count( $type ) {
  213. if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) {
  214. return;
  215. }
  216. if ( defined( 'IFRAME_REQUEST' ) ) {
  217. echo '<script type="text/javascript">
  218. if ( window.postMessage && JSON ) {
  219. window.parent.postMessage( JSON.stringify( { action: "decrementUpdateCount", upgradeType: "' . $type . '" } ), window.location.protocol + "//" + window.location.hostname );
  220. }
  221. </script>';
  222. } else {
  223. echo '<script type="text/javascript">
  224. (function( wp ) {
  225. if ( wp && wp.updates && wp.updates.decrementCount ) {
  226. wp.updates.decrementCount( "' . $type . '" );
  227. }
  228. })( window.wp );
  229. </script>';
  230. }
  231. }
  232. /**
  233. * @since 3.0.0
  234. */
  235. public function bulk_header() {}
  236. /**
  237. * @since 3.0.0
  238. */
  239. public function bulk_footer() {}
  240. /**
  241. * Hides the `process_failed` error message when updating by uploading a zip file.
  242. *
  243. * @since 5.5.0
  244. *
  245. * @param WP_Error $wp_error WP_Error object.
  246. * @return bool
  247. */
  248. public function hide_process_failed( $wp_error ) {
  249. return false;
  250. }
  251. }