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