class-wp-upgrader-skin.php 4.9 KB

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