class-wp-upgrader-skin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. public $upgrader;
  17. public $done_header = false;
  18. public $done_footer = false;
  19. /**
  20. * Holds the result of an upgrade.
  21. *
  22. * @since 2.8.0
  23. * @var string|bool|WP_Error
  24. */
  25. public $result = false;
  26. public $options = array();
  27. /**
  28. * @param array $args
  29. */
  30. public function __construct( $args = array() ) {
  31. $defaults = array(
  32. 'url' => '',
  33. 'nonce' => '',
  34. 'title' => '',
  35. 'context' => false,
  36. );
  37. $this->options = wp_parse_args( $args, $defaults );
  38. }
  39. /**
  40. * @param WP_Upgrader $upgrader
  41. */
  42. public function set_upgrader( &$upgrader ) {
  43. if ( is_object( $upgrader ) ) {
  44. $this->upgrader =& $upgrader;
  45. }
  46. $this->add_strings();
  47. }
  48. /**
  49. */
  50. public function add_strings() {
  51. }
  52. /**
  53. * Sets the result of an upgrade.
  54. *
  55. * @since 2.8.0
  56. *
  57. * @param string|bool|WP_Error $result The result of an upgrade.
  58. */
  59. public function set_result( $result ) {
  60. $this->result = $result;
  61. }
  62. /**
  63. * Displays a form to the user to request for their FTP/SSH details in order
  64. * to connect to the filesystem.
  65. *
  66. * @since 2.8.0
  67. * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
  68. *
  69. * @see request_filesystem_credentials()
  70. *
  71. * @param bool $error Optional. Whether the current request has failed to connect.
  72. * Default false.
  73. * @param string $context Optional. Full path to the directory that is tested
  74. * for being writable. Default empty.
  75. * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
  76. * @return bool False on failure, true on success.
  77. */
  78. public function request_filesystem_credentials( $error = false, $context = '', $allow_relaxed_file_ownership = false ) {
  79. $url = $this->options['url'];
  80. if ( ! $context ) {
  81. $context = $this->options['context'];
  82. }
  83. if ( ! empty( $this->options['nonce'] ) ) {
  84. $url = wp_nonce_url( $url, $this->options['nonce'] );
  85. }
  86. $extra_fields = array();
  87. return request_filesystem_credentials( $url, '', $error, $context, $extra_fields, $allow_relaxed_file_ownership );
  88. }
  89. /**
  90. */
  91. public function header() {
  92. if ( $this->done_header ) {
  93. return;
  94. }
  95. $this->done_header = true;
  96. echo '<div class="wrap">';
  97. echo '<h1>' . $this->options['title'] . '</h1>';
  98. }
  99. /**
  100. */
  101. public function footer() {
  102. if ( $this->done_footer ) {
  103. return;
  104. }
  105. $this->done_footer = true;
  106. echo '</div>';
  107. }
  108. /**
  109. * @param string|WP_Error $errors
  110. */
  111. public function error( $errors ) {
  112. if ( ! $this->done_header ) {
  113. $this->header();
  114. }
  115. if ( is_string( $errors ) ) {
  116. $this->feedback( $errors );
  117. } elseif ( is_wp_error( $errors ) && $errors->has_errors() ) {
  118. foreach ( $errors->get_error_messages() as $message ) {
  119. if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
  120. $this->feedback( $message . ' ' . esc_html( strip_tags( $errors->get_error_data() ) ) );
  121. } else {
  122. $this->feedback( $message );
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * @param string $string
  129. * @param mixed ...$args Optional text replacements.
  130. */
  131. public function feedback( $string, ...$args ) {
  132. if ( isset( $this->upgrader->strings[ $string ] ) ) {
  133. $string = $this->upgrader->strings[ $string ];
  134. }
  135. if ( strpos( $string, '%' ) !== false ) {
  136. if ( $args ) {
  137. $args = array_map( 'strip_tags', $args );
  138. $args = array_map( 'esc_html', $args );
  139. $string = vsprintf( $string, $args );
  140. }
  141. }
  142. if ( empty( $string ) ) {
  143. return;
  144. }
  145. show_message( $string );
  146. }
  147. /**
  148. */
  149. public function before() {}
  150. /**
  151. */
  152. public function after() {}
  153. /**
  154. * Output JavaScript that calls function to decrement the update counts.
  155. *
  156. * @since 3.9.0
  157. *
  158. * @param string $type Type of update count to decrement. Likely values include 'plugin',
  159. * 'theme', 'translation', etc.
  160. */
  161. protected function decrement_update_count( $type ) {
  162. if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) {
  163. return;
  164. }
  165. if ( defined( 'IFRAME_REQUEST' ) ) {
  166. echo '<script type="text/javascript">
  167. if ( window.postMessage && JSON ) {
  168. window.parent.postMessage( JSON.stringify( { action: "decrementUpdateCount", upgradeType: "' . $type . '" } ), window.location.protocol + "//" + window.location.hostname );
  169. }
  170. </script>';
  171. } else {
  172. echo '<script type="text/javascript">
  173. (function( wp ) {
  174. if ( wp && wp.updates && wp.updates.decrementCount ) {
  175. wp.updates.decrementCount( "' . $type . '" );
  176. }
  177. })( window.wp );
  178. </script>';
  179. }
  180. }
  181. /**
  182. */
  183. public function bulk_header() {}
  184. /**
  185. */
  186. public function bulk_footer() {}
  187. }