wp/wp-admin/includes/class-wp-upgrader.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * A File upgrader class for WordPress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * This set of classes are designed to be used to upgrade/install a local set of files on the filesystem via the Filesystem Abstraction classes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
 * @link https://core.trac.wordpress.org/ticket/7875 consolidate plugin/theme/core upgrade/install functions
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
require ABSPATH . 'wp-admin/includes/class-wp-upgrader-skins.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * WordPress Upgrader class for Upgrading/Installing a local set of files via the Filesystem Abstraction classes from a Zip file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
class WP_Upgrader {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
	 * The error/notification strings used to update the user on the progress.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
	 * @var string $strings
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
	public $strings = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
	 * The upgrader skin being used.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
	 * @var WP_Upgrader_Skin $skin
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
	public $skin = null;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	 * The result of the installation.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	 * This is set by {@see WP_Upgrader::install_package()}, only when the package is installed
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	 * successfully. It will then be an array, unless a {@see WP_Error} is returned by the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	 * {@see 'upgrader_post_install'} filter. In that case, the `WP_Error` will be assigned to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
	 * it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
	 * @var WP_Error|array $result {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
	 *      @type string $source             The full path to the source the files were installed from.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
	 *      @type string $source_files       List of all the files in the source directory.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
	 *      @type string $destination        The full path to the install destination folder.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
	 *      @type string $destination_name   The name of the destination folder, or empty if `$destination`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
	 *                                       and `$local_destination` are the same.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
	 *      @type string $local_destination  The full local path to the destination folder. This is usually
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
	 *                                       the same as `$destination`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
	 *      @type string $remote_destination The full remote path to the destination folder
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
	 *                                       (i.e., from `$wp_filesystem`).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
	 *      @type bool   $clear_destination  Whether the destination folder was cleared.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
	public $result = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
	 * The total number of updates being performed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
	 * Set by the bulk update methods.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
	 * @var int $update_count
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
	public $update_count = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    74
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
	 * The current update if multiple updates are being performed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
	 * Used by the bulk update methods, and incremented for each update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
	 * @var int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
	public $update_current = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
	 * Construct the upgrader with a skin.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
	 * @param WP_Upgrader_Skin $skin The upgrader skin to use. Default is a {@see WP_Upgrader_Skin}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
	 *                               instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
	public function __construct( $skin = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		if ( null == $skin )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			$this->skin = new WP_Upgrader_Skin();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			$this->skin = $skin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
	 * Initialize the upgrader.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
	 * This will set the relationship between the skin being used and this upgrader,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
	 * and also add the generic strings to `WP_Upgrader::$strings`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
	public function init() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		$this->skin->set_upgrader($this);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		$this->generic_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
	 * Add the generic strings to WP_Upgrader::$strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
	public function generic_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		$this->strings['bad_request'] = __('Invalid Data provided.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		$this->strings['fs_unavailable'] = __('Could not access filesystem.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		$this->strings['fs_error'] = __('Filesystem error.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		$this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		$this->strings['fs_no_content_dir'] = __('Unable to locate WordPress Content directory (wp-content).');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		$this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		$this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		/* translators: %s: directory name */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		$this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		$this->strings['download_failed'] = __('Download failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
		$this->strings['installing_package'] = __('Installing the latest version&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		$this->strings['no_files'] = __('The package contains no files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
		$this->strings['folder_exists'] = __('Destination folder already exists.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		$this->strings['mkdir_failed'] = __('Could not create directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		$this->strings['incompatible_archive'] = __('The package could not be installed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		$this->strings['maintenance_start'] = __('Enabling Maintenance mode&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		$this->strings['maintenance_end'] = __('Disabling Maintenance mode&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
	 * Connect to the filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
	 * @param array $directories                  Optional. A list of directories. If any of these do
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
	 *                                            not exist, a {@see WP_Error} object will be returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
	 *                                            Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
	 * @param bool  $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
	 *                                            Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
	 * @return bool|WP_Error True if able to connect, false or a {@see WP_Error} otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
	public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
		if ( false === ( $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
		if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
			$error = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
			if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
				$error = $wp_filesystem->errors;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
			// Failed to connect, Error and request again
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
			$this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		if ( ! is_object($wp_filesystem) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
			return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
			return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		foreach ( (array)$directories as $dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
			switch ( $dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
				case ABSPATH:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
					if ( ! $wp_filesystem->abspath() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
						return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
				case WP_CONTENT_DIR:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
					if ( ! $wp_filesystem->wp_content_dir() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
						return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
				case WP_PLUGIN_DIR:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
					if ( ! $wp_filesystem->wp_plugins_dir() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
						return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
				case get_theme_root():
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
					if ( ! $wp_filesystem->wp_themes_dir() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
						return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
				default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
					if ( ! $wp_filesystem->find_folder($dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
						return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	} //end fs_connect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
	 * Download a package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
	 * @param string $package The URI of the package. If this is the full path to an
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   207
	 *                        existing local file, it will be returned untouched.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   208
	 * @return string|WP_Error The full path to the downloaded package file, or a {@see WP_Error} object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   209
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
	public function download_package( $package ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		 * Filter whether to return the package.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
		 * @param bool        $reply   Whether to bail without returning the package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
		 *                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
		 * @param string      $package The package file name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
		 * @param WP_Upgrader $this    The WP_Upgrader instance.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
		$reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		if ( false !== $reply )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
			return $reply;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			return $package; //must be a local file..
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
		if ( empty($package) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
			return new WP_Error('no_package', $this->strings['no_package']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
		$this->skin->feedback('downloading_package', $package);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		$download_file = download_url($package);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
		if ( is_wp_error($download_file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
		return $download_file;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
	 * Unpack a compressed package file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   247
	 * @param string $package        Full path to the package file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
	 * @param bool   $delete_package Optional. Whether to delete the package file after attempting
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
	 *                               to unpack it. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
	 * @return string|WP_Error The path to the unpacked contents, or a {@see WP_Error} on failure.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
	public function unpack_package( $package, $delete_package = true ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		$this->skin->feedback('unpack_package');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		$upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		//Clean up contents of upgrade directory beforehand.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
		$upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
		if ( !empty($upgrade_files) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
			foreach ( $upgrade_files as $file )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
				$wp_filesystem->delete($upgrade_folder . $file['name'], true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
		// We need a working directory - Strip off any .tmp or .zip suffixes
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   267
		$working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
		// Clean up working directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
		if ( $wp_filesystem->is_dir($working_dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
			$wp_filesystem->delete($working_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
		// Unzip package to working directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
		$result = unzip_file( $package, $working_dir );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
		// Once extracted, delete the package if required.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
		if ( $delete_package )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
			unlink($package);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
		if ( is_wp_error($result) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
			$wp_filesystem->delete($working_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
			if ( 'incompatible_archive' == $result->get_error_code() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
				return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
		return $working_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	 * Install a package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
	 * Copies the contents of a package form a source directory, and installs them in
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
	 * a destination directory. Optionally removes the source. It can also optionally
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
	 * clear out the destination folder if it already exists.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
	 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   301
	 *     Optional. Array or string of arguments for installing a package. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   302
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
	 *     @type string $source                      Required path to the package source. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
	 *     @type string $destination                 Required path to a folder to install the package in.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   305
	 *                                               Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
	 *     @type bool   $clear_destination           Whether to delete any files already in the destination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
	 *                                               folder. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
	 *     @type bool   $clear_working               Whether to delete the files form the working directory
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
	 *                                               after copying to the destination. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
	 *     @type bool   $abort_if_destination_exists Whether to abort the installation if
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
	 *                                               the destination folder already exists. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
	 *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
	 *                                               {@see WP_Upgrader::install_package()}. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
	 * @return array|WP_Error The result (also stored in `WP_Upgrader:$result`), or a {@see WP_Error} on failure.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
	public function install_package( $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		global $wp_filesystem, $wp_theme_directories;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
			'source' => '', // Please always pass this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			'destination' => '', // and this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
			'clear_destination' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			'clear_working' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
			'abort_if_destination_exists' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
			'hook_extra' => array()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
		$args = wp_parse_args($args, $defaults);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
		// These were previously extract()'d.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
		$source = $args['source'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
		$destination = $args['destination'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   335
		$clear_destination = $args['clear_destination'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		@set_time_limit( 300 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
		if ( empty( $source ) || empty( $destination ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
			return new WP_Error( 'bad_request', $this->strings['bad_request'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
		$this->skin->feedback( 'installing_package' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
		 * Filter the install response before the installation has started.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
		 * Returning a truthy value, or one that could be evaluated as a WP_Error
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
		 * will effectively short-circuit the installation, returning that value
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
		 * instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
		 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
		 * @param bool|WP_Error $response   Response.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
		 * @param array         $hook_extra Extra arguments passed to hooked filters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
		$res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
		if ( is_wp_error( $res ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
			return $res;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
		//Retain the Original source and destinations
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
		$remote_source = $args['source'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
		$local_destination = $destination;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
		$source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
		$remote_destination = $wp_filesystem->find_folder( $local_destination );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
		//Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
		if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { //Only one folder? Then we want its contents.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
			$source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
		} elseif ( count( $source_files ) == 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
			return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
		} else { //It's only a single file, the upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
			$source = trailingslashit( $args['source'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
		 * Filter the source file location for the upgrade package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
		 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
		 * @param string      $source        File source location.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   383
		 * @param string      $remote_source Remove file source location.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
		 * @param WP_Upgrader $this          WP_Upgrader instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
		$source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
		if ( is_wp_error( $source ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
			return $source;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   391
		// Has the source location changed? If so, we need a new source_files list.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   392
		if ( $source !== $remote_source ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   393
			$source_files = array_keys( $wp_filesystem->dirlist( $source ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   394
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   395
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
		 * Protection against deleting files in any important base directories.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
		 * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
		 * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
		 * to copy the directory into the directory, whilst they pass the source
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
		 * as the actual files to copy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
		$protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
		if ( is_array( $wp_theme_directories ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
			$protected_directories = array_merge( $protected_directories, $wp_theme_directories );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   406
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
		if ( in_array( $destination, $protected_directories ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   408
			$remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   409
			$destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		if ( $clear_destination ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
			//We're going to clear the destination if there's something there
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
			$this->skin->feedback('remove_old');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
			$removed = true;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
			if ( $wp_filesystem->exists( $remote_destination ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
				$removed = $wp_filesystem->delete( $remote_destination, true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   418
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   420
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   421
			 * Filter whether the upgrader cleared the destination.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
			 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   424
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
			 * @param bool   $removed            Whether the destination was cleared.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   426
			 * @param string $local_destination  The local package destination.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
			 * @param string $remote_destination The remote package destination.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
			 * @param array  $hook_extra         Extra arguments passed to hooked filters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   429
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
			$removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
			if ( is_wp_error($removed) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
				return $removed;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
			} elseif ( ! $removed ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
				return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
		} elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists($remote_destination) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
			//If we're not clearing the destination folder and something exists there already, Bail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
			//But first check to see if there are actually any files in the folder.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
			$_files = $wp_filesystem->dirlist($remote_destination);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
			if ( ! empty($_files) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
				$wp_filesystem->delete($remote_source, true); //Clear out the source files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
				return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
		//Create destination if needed
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   448
		if ( ! $wp_filesystem->exists( $remote_destination ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   449
			if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
				return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   452
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
		// Copy new version of item into place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
		$result = copy_dir($source, $remote_destination);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
		if ( is_wp_error($result) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
			if ( $args['clear_working'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
				$wp_filesystem->delete( $remote_source, true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		//Clear the Working folder?
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   463
		if ( $args['clear_working'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   464
			$wp_filesystem->delete( $remote_source, true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   465
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
		$destination_name = basename( str_replace($local_destination, '', $destination) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   468
		if ( '.' == $destination_name ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
			$destination_name = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   470
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   471
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
		$this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   473
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
		 * Filter the install response after the installation has finished.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   476
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
		 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
		 * @param bool  $response   Install response.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   480
		 * @param array $hook_extra Extra arguments passed to hooked filters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
		 * @param array $result     Installation result data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   482
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   483
		$res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   484
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
		if ( is_wp_error($res) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
			$this->result = $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
			return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
		//Bombard the calling function will all the info which we've just used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
		return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   494
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   495
	 * Run an upgrade/install.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   496
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   497
	 * Attempts to download the package (if it is not a local file), unpack it, and
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   498
	 * install it in the destination folder.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   499
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   500
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   501
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   502
	 * @param array $options {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   503
	 *     Array or string of arguments for upgrading/installing a package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   504
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   505
	 *     @type string $package                     The full path or URI of the package to install.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   506
	 *                                               Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   507
	 *     @type string $destination                 The full path to the destination folder.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   508
	 *                                               Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   509
	 *     @type bool   $clear_destination           Whether to delete any files already in the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   510
	 *                                               destination folder. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   511
	 *     @type bool   $clear_working               Whether to delete the files form the working
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   512
	 *                                               directory after copying to the destination.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   513
	 *                                               Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   514
	 *     @type bool   $abort_if_destination_exists Whether to abort the installation if the destination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
	 *                                               folder already exists. When true, `$clear_destination`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   516
	 *                                               should be false. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   517
	 *     @type bool   $is_multi                    Whether this run is one of multiple upgrade/install
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   518
	 *                                               actions being performed in bulk. When true, the skin
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   519
	 *                                               {@see WP_Upgrader::header()} and {@see WP_Upgrader::footer()}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   520
	 *                                               aren't called. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   521
	 *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
	 *                                               {@see WP_Upgrader::run()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   523
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   524
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   525
	 * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   526
	 *                              or false if unable to connect to the filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   527
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
	public function run( $options ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
			'package' => '', // Please always pass this.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
			'destination' => '', // And this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
			'clear_destination' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
			'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
			'is_multi' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
			'hook_extra' => array() // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   540
		$options = wp_parse_args( $options, $defaults );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   541
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   542
		if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
			$this->skin->header();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   544
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		// Connect to the Filesystem first.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   547
		$res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		// Mainly for non-connected filesystem.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
		if ( ! $res ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   550
			if ( ! $options['is_multi'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
				$this->skin->footer();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   552
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
		$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
		if ( is_wp_error($res) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
			$this->skin->error($res);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
			$this->skin->after();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   561
			if ( ! $options['is_multi'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
				$this->skin->footer();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   563
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
			return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
		//Download the package (Note, This just returns the filename of the file if the package is a local file)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   568
		$download = $this->download_package( $options['package'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
		if ( is_wp_error($download) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
			$this->skin->error($download);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
			$this->skin->after();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   572
			if ( ! $options['is_multi'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
				$this->skin->footer();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
			return $download;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   578
		$delete_package = ( $download != $options['package'] ); // Do not delete a "local" file
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
		//Unzips the file into a temporary directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
		$working_dir = $this->unpack_package( $download, $delete_package );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
		if ( is_wp_error($working_dir) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
			$this->skin->error($working_dir);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
			$this->skin->after();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   585
			if ( ! $options['is_multi'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
				$this->skin->footer();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
			return $working_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
		//With the given options, this installs it to the destination directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
		$result = $this->install_package( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
			'source' => $working_dir,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   594
			'destination' => $options['destination'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   595
			'clear_destination' => $options['clear_destination'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   596
			'abort_if_destination_exists' => $options['abort_if_destination_exists'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   597
			'clear_working' => $options['clear_working'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   598
			'hook_extra' => $options['hook_extra']
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
		$this->skin->set_result($result);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
		if ( is_wp_error($result) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
			$this->skin->error($result);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
			$this->skin->feedback('process_failed');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
			//Install Succeeded
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
			$this->skin->feedback('process_success');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
		$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   612
		if ( ! $options['is_multi'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   613
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   614
			/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   615
			do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   622
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   623
	 * Toggle maintenance mode for the site.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   624
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   625
	 * Creates/deletes the maintenance file to enable/disable maintenance mode.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   626
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   627
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   628
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   629
	 * @param bool $enable True to enable maintenance mode, false to disable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   631
	public function maintenance_mode( $enable = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
		$file = $wp_filesystem->abspath() . '.maintenance';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
		if ( $enable ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
			$this->skin->feedback('maintenance_start');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
			// Create maintenance file to signal that we are upgrading
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
			$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
			$wp_filesystem->delete($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
			$wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
		} elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
			$this->skin->feedback('maintenance_end');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
			$wp_filesystem->delete($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
 * Plugin Upgrader class for WordPress Plugins, It is designed to upgrade/install plugins from a local zip, remote zip URL, or uploaded zip file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
class Plugin_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   657
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
	 * Plugin upgrade result.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   659
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   660
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   661
	 * @var array|WP_Error $result
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   662
	 * @see WP_Upgrader::$result
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   663
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   664
	public $result;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   665
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   666
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   667
	 * Whether a bulk upgrade/install is being performed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   668
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   669
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   670
	 * @var bool $bulk
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   671
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   672
	public $bulk = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   673
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   674
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   675
	 * Initialize the upgrade strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   676
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   678
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   679
	public function upgrade_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
		$this->strings['up_to_date'] = __('The plugin is at the latest version.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
		$this->strings['no_package'] = __('Update package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
		$this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
		$this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
		$this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
		$this->strings['process_failed'] = __('Plugin update failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
		$this->strings['process_success'] = __('Plugin updated successfully.');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
		$this->strings['process_bulk_success'] = __('Plugins updated successfully.');
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   691
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   692
	 * Initialize the install strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   696
	public function install_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		$this->strings['no_package'] = __('Install package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
		$this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
		$this->strings['unpack_package'] = __('Unpacking the package&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
		$this->strings['installing_package'] = __('Installing the plugin&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
		$this->strings['no_files'] = __('The plugin contains no files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
		$this->strings['process_failed'] = __('Plugin install failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
		$this->strings['process_success'] = __('Plugin installed successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
	 * Install a plugin package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   711
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   712
	 * @param string $package The full local path or URI of the package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   713
	 * @param array  $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   714
	 *     Optional. Other arguments for installing a plugin package. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   715
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   716
	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   717
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   718
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
	 * @return bool|WP_Error True if the install was successful, false or a WP_Error otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
	public function install( $package, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
		$this->install_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
		add_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
			'package' => $package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
			'destination' => WP_PLUGIN_DIR,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
			'clear_destination' => false, // Do not overwrite files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
				'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
				'action' => 'install',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
			)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
		remove_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
		// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   756
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   757
	 * Upgrade a plugin.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   758
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   759
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   760
	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   761
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   762
	 * @param string $plugin The basename path to the main plugin file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   763
	 * @param array  $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   764
	 *     Optional. Other arguments for upgrading a plugin package. Defualt empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   765
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   766
	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   767
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   768
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
	 * @return bool|WP_Error True if the upgrade was successful, false or a {@see WP_Error} object otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   770
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   771
	public function upgrade( $plugin, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
		$current = get_site_transient( 'update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
		if ( !isset( $current->response[ $plugin ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
			$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
			$this->skin->set_result(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
			$this->skin->error('up_to_date');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
		// Get the URL to the zip file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
		$r = $current->response[ $plugin ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
		add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
		add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
		//'source_selection' => array($this, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
			'package' => $r->package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
			'destination' => WP_PLUGIN_DIR,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
			'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
				'plugin' => $plugin,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
				'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
				'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
		// Cleanup our hooks, in case something else does a upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
		remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
		// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   822
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   823
	 * Bulk upgrade several plugins at once.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   825
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   826
	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   827
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   828
	 * @param array $plugins Array of the basename paths of the plugins' main files.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   829
	 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   830
	 *     Optional. Other arguments for upgrading several plugins at once. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   831
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   832
	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   833
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   834
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   835
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   836
	 * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   837
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   838
	public function bulk_upgrade( $plugins, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
		$this->bulk = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
		$current = get_site_transient( 'update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
		add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
		$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
		$res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
		$this->skin->bulk_header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
		// Only start maintenance mode if:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
		// - running Multisite and there are one or more plugins specified, OR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
		// - a plugin with an update available is currently active.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
		// @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
		$maintenance = ( is_multisite() && ! empty( $plugins ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
		foreach ( $plugins as $plugin )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
			$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
		if ( $maintenance )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
			$this->maintenance_mode(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
		$results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
		$this->update_count = count($plugins);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
		$this->update_current = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
		foreach ( $plugins as $plugin ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
			$this->update_current++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
			$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
			if ( !isset( $current->response[ $plugin ] ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   883
				$this->skin->set_result('up_to_date');
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
				$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
				$this->skin->feedback('up_to_date');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
				$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
				$results[$plugin] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
			// Get the URL to the zip file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
			$r = $current->response[ $plugin ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
			$this->skin->plugin_active = is_plugin_active($plugin);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
			$result = $this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
				'package' => $r->package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
				'destination' => WP_PLUGIN_DIR,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
				'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
				'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
				'is_multi' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
				'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
					'plugin' => $plugin
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
				)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
			) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
			$results[$plugin] = $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
			// Prevent credentials auth screen from displaying multiple times
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
			if ( false === $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
		} //end foreach $plugins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
		$this->maintenance_mode(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   916
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
		 * Fires when the bulk upgrader process is complete.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   918
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   919
		 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   920
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   921
		 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   922
		 *                              be a Theme_Upgrader or Core_Upgrade instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   923
		 * @param array           $data {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
		 *     Array of bulk item update data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   925
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
		 *     @type string $action   Type of action. Default 'update'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
		 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   928
		 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   929
		 *     @type array  $packages Array of plugin, theme, or core packages to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   930
		 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
		do_action( 'upgrader_process_complete', $this, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
			'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
			'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
			'bulk' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
			'plugins' => $plugins,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
		$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
		// Cleanup our hooks, in case something else does a upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
		// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
		return $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   952
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   953
	 * Check a source package to be sure it contains a plugin.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
	 * This function is added to the {@see 'upgrader_source_selection'} filter by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
	 * {@see Plugin_Upgrader::install()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
	 * @param string $source The path to the downloaded package source.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
	 * @return string|WP_Error The source as passed, or a {@see WP_Error} object if no plugins were found.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
	public function check_package($source) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
		if ( is_wp_error($source) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
		if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
		// Check the folder contains at least 1 valid plugin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
		$plugins_found = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
		foreach ( glob( $working_directory . '*.php' ) as $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
			$info = get_plugin_data($file, false, false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
			if ( !empty( $info['Name'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
				$plugins_found = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
		if ( ! $plugins_found )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
			return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
		return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   990
	 * Retrieve the path to the file that contains the plugin info.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
	 * This isn't used internally in the class, but is called by the skins.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   996
	 * @return string|false The full path to the main plugin file, or false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
	public function plugin_info() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
		if ( ! is_array($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
		if ( empty($this->result['destination_name']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
		$plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
		if ( empty($plugin) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
		$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
		return $this->result['destination_name'] . '/' . $pluginfiles[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1014
	 * Deactivates a plugin before it is upgraded.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1016
	 * Hooked to the {@see 'upgrader_pre_install'} filter by {@see Plugin_Upgrader::upgrade()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1018
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1019
	 * @since 4.1.0 Added a return value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1020
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1021
	 * @param bool|WP_Error  $return Upgrade offer return.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1022
	 * @param array          $plugin Plugin package arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
	 * @return bool|WP_Error The passed in $return param or {@see WP_Error}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1024
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1025
	public function deactivate_plugin_before_upgrade($return, $plugin) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		if ( is_wp_error($return) ) //Bypass.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
		// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
		if ( defined( 'DOING_CRON' ) && DOING_CRON )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
		$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
		if ( empty($plugin) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
			return new WP_Error('bad_request', $this->strings['bad_request']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
		if ( is_plugin_active($plugin) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
			//Deactivate the plugin silently, Prevent deactivation hooks from running.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
			deactivate_plugins($plugin, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1042
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1043
		return $return;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1046
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1047
	 * Delete the old plugin during an upgrade.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1048
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1049
	 * Hooked to the {@see 'upgrader_clear_destination'} filter by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1050
	 * {@see Plugin_Upgrader::upgrade()} and {@see Plugin_Upgrader::bulk_upgrade()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1051
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1053
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1054
	public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
		if ( is_wp_error($removed) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
			return $removed; //Pass errors through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
		$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
		if ( empty($plugin) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
			return new WP_Error('bad_request', $this->strings['bad_request']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
		$plugins_dir = $wp_filesystem->wp_plugins_dir();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
		$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
		if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
			return $removed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
		// If plugin is in its own directory, recursively delete the directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
		if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
			$deleted = $wp_filesystem->delete($this_plugin_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
			$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
		if ( ! $deleted )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
			return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
 * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
class Theme_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1092
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1093
	 * Result of the theme upgrade offer.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1094
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1095
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1096
	 * @var array|WP_Erorr $result
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1097
	 * @see WP_Upgrader::$result
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1098
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1099
	public $result;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1100
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1101
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1102
	 * Whether multiple plugins are being upgraded/installed in bulk.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1103
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1105
	 * @var bool $bulk
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1106
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
	public $bulk = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1108
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1109
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1110
	 * Initialize the upgrade strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1111
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1112
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1113
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1114
	public function upgrade_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
		$this->strings['up_to_date'] = __('The theme is at the latest version.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
		$this->strings['no_package'] = __('Update package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
		$this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
		$this->strings['remove_old'] = __('Removing the old version of the theme&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
		$this->strings['remove_old_failed'] = __('Could not remove the old theme.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
		$this->strings['process_failed'] = __('Theme update failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
		$this->strings['process_success'] = __('Theme updated successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1125
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1126
	 * Initialize the install strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1127
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1128
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1129
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1130
	public function install_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
		$this->strings['no_package'] = __('Install package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
		$this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
		$this->strings['unpack_package'] = __('Unpacking the package&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
		$this->strings['installing_package'] = __('Installing the theme&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
		$this->strings['no_files'] = __('The theme contains no files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
		$this->strings['process_failed'] = __('Theme install failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
		$this->strings['process_success'] = __('Theme installed successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
		$this->strings['process_success_specific'] = __('Successfully installed the theme <strong>%1$s %2$s</strong>.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
		$this->strings['parent_theme_search'] = __('This theme requires a parent theme. Checking if it is installed&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
		$this->strings['parent_theme_prepare_install'] = __('Preparing to install <strong>%1$s %2$s</strong>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
		$this->strings['parent_theme_currently_installed'] = __('The parent theme, <strong>%1$s %2$s</strong>, is currently installed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
		$this->strings['parent_theme_install_success'] = __('Successfully installed the parent theme, <strong>%1$s %2$s</strong>.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
		$this->strings['parent_theme_not_found'] = __('<strong>The parent theme could not be found.</strong> You will need to install the parent theme, <strong>%s</strong>, before you can use this child theme.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1150
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1151
	 * Check if a child theme is being installed and we need to install its parent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1152
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1153
	 * Hooked to the {@see 'upgrader_post_install'} filter by {@see Theme_Upgrader::install()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1155
	 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1156
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1157
	public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
		// Check to see if we need to install a parent theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
		$theme_info = $this->theme_info();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
		if ( ! $theme_info->parent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
			return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
		$this->skin->feedback( 'parent_theme_search' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
		if ( ! $theme_info->parent()->errors() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
			$this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display('Name'), $theme_info->parent()->display('Version') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
			// We already have the theme, fall through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
			return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1172
		// We don't have the parent theme, let's install it.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
		$api = themes_api('theme_information', array('slug' => $theme_info->get('Template'), 'fields' => array('sections' => false, 'tags' => false) ) ); //Save on a bit of bandwidth.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
		if ( ! $api || is_wp_error($api) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
			$this->skin->feedback( 'parent_theme_not_found', $theme_info->get('Template') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
			// Don't show activate or preview actions after install
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
			add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
			return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
		// Backup required data we're going to override:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
		$child_api = $this->skin->api;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
		$child_success_message = $this->strings['process_success'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
		// Override them
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
		$this->skin->api = $api;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
		$this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];//, $api->name, $api->version);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
		$this->skin->feedback('parent_theme_prepare_install', $api->name, $api->version);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
		add_filter('install_theme_complete_actions', '__return_false', 999); // Don't show any actions after installing the theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
		// Install the parent theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
		$parent_result = $this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
			'package' => $api->download_link,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
			'destination' => get_theme_root(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
			'clear_destination' => false, //Do not overwrite files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
			'clear_working' => true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
		if ( is_wp_error($parent_result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
			add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
		// Start cleaning up after the parents installation
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
		remove_filter('install_theme_complete_actions', '__return_false', 999);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
		// Reset child's result and data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
		$this->result = $child_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
		$this->skin->api = $child_api;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
		$this->strings['process_success'] = $child_success_message;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
		return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
	 * Don't display the activate and preview actions to the user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1218
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
	 * Hooked to the {@see 'install_theme_complete_actions'} filter by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1220
	 * {@see Theme_Upgrader::check_parent_theme_filter()} when installing
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1221
	 * a child theme and installing the parent theme fails.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1222
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1223
	 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1224
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1225
	 * @param array $actions Preview actions.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1226
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1227
	public function hide_activate_preview_actions( $actions ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
		unset($actions['activate'], $actions['preview']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
		return $actions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1232
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1233
	 * Install a theme package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1234
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1235
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1236
	 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1237
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
	 * @param string $package The full local path or URI of the package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1239
	 * @param array  $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1240
	 *     Optional. Other arguments for installing a theme package. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1241
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1242
	 *     @type bool $clear_update_cache Whether to clear the updates cache if successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1243
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1244
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1245
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1246
	 * @return bool|WP_Error True if the install was successful, false or a {@see WP_Error} object otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1247
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1248
	public function install( $package, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
		$this->install_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
		add_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
		add_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'), 10, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
			'package' => $package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
			'destination' => get_theme_root(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
			'clear_destination' => false, //Do not overwrite files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
				'type' => 'theme',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
				'action' => 'install',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
		remove_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
		remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
		// Refresh the Theme Update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1284
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1285
	 * Upgrade a theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1286
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1287
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1288
	 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1289
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1290
	 * @param string $theme The theme slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1291
	 * @param array  $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1292
	 *     Optional. Other arguments for upgrading a theme. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1293
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1294
	 *     @type bool $clear_update_cache Whether to clear the update cache if successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1295
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1296
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1297
	 * @return bool|WP_Error True if the upgrade was successful, false or a {@see WP_Error} object otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1298
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1299
	public function upgrade( $theme, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
		// Is an update available?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
		$current = get_site_transient( 'update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
		if ( !isset( $current->response[ $theme ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
			$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
			$this->skin->set_result(false);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1314
			$this->skin->error( 'up_to_date' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
		$r = $current->response[ $theme ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
		add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
		add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
		add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
			'package' => $r['package'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
			'destination' => get_theme_root( $theme ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
			'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
				'theme' => $theme,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
				'type' => 'theme',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
				'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
		remove_filter('upgrader_pre_install', array($this, 'current_before'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
		remove_filter('upgrader_post_install', array($this, 'current_after'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
	 * Upgrade several themes at once.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1352
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1353
	 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1354
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1355
	 * @param array $themes The theme slugs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1356
	 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1357
	 *     Optional. Other arguments for upgrading several themes at once. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1358
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1359
	 *     @type bool $clear_update_cache Whether to clear the update cache if successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1360
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1361
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1362
	 * @return array[]|false An array of results, or false if unable to connect to the filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1363
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1364
	public function bulk_upgrade( $themes, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		$this->bulk = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
		$current = get_site_transient( 'update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
		add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
		add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
		add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
		$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
		$res = $this->fs_connect( array(WP_CONTENT_DIR) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
		$this->skin->bulk_header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
		// Only start maintenance mode if:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
		// - running Multisite and there are one or more themes specified, OR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
		// - a theme with an update available is currently in use.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
		// @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
		$maintenance = ( is_multisite() && ! empty( $themes ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
		foreach ( $themes as $theme )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
			$maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
		if ( $maintenance )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
			$this->maintenance_mode(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
		$results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
		$this->update_count = count($themes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
		$this->update_current = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
		foreach ( $themes as $theme ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
			$this->update_current++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
			$this->skin->theme_info = $this->theme_info($theme);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
			if ( !isset( $current->response[ $theme ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
				$this->skin->set_result(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
				$this->skin->before();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1414
				$this->skin->feedback( 'up_to_date' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
				$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
				$results[$theme] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
			// Get the URL to the zip file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
			$r = $current->response[ $theme ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
			$result = $this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
				'package' => $r['package'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
				'destination' => get_theme_root( $theme ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
				'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
				'clear_working' => true,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1428
				'is_multi' => true,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
				'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
					'theme' => $theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
				),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
			) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
			$results[$theme] = $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
			// Prevent credentials auth screen from displaying multiple times
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
			if ( false === $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
		} //end foreach $plugins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
		$this->maintenance_mode(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1443
		/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
		do_action( 'upgrader_process_complete', $this, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
			'action' => 'update',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1446
			'type' => 'theme',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
			'bulk' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
			'themes' => $themes,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
		$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
		$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
		// Cleanup our hooks, in case something else does a upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
		remove_filter('upgrader_pre_install', array($this, 'current_before'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
		remove_filter('upgrader_post_install', array($this, 'current_after'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
		// Refresh the Theme Update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
		return $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1466
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1467
	 * Check that the package source contains a valid theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1468
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1469
	 * Hooked to the {@see 'upgrader_source_selection'} filter by {@see Theme_Upgrader::install()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1470
	 * It will return an error if the theme doesn't have style.css or index.php
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1471
	 * files.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1472
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1473
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1474
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1475
	 * @param string $source The full path to the package source.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1476
	 * @return string|WP_Error The source or a WP_Error.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1477
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1478
	public function check_package( $source ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
		if ( is_wp_error($source) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
		// Check the folder contains a valid theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1486
		if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
		// A proper archive should have a style.css file in the single subdirectory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
		if ( ! file_exists( $working_directory . 'style.css' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
			return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'], __( 'The theme is missing the <code>style.css</code> stylesheet.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
		$info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
		if ( empty( $info['Name'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
			return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'], __( "The <code>style.css</code> stylesheet doesn't contain a valid theme header." ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
		// If it's not a child theme, it must have at least an index.php to be legit.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
		if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
			return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'], __( 'The theme is missing the <code>index.php</code> file.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
		return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1505
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1506
	 * Turn on maintenance mode before attempting to upgrade the current theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1507
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1508
	 * Hooked to the {@see 'upgrader_pre_install'} filter by {@see Theme_Upgrader::upgrade()} and
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1509
	 * {@see Theme_Upgrader::bulk_upgrade()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1510
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1511
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1512
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1513
	public function current_before($return, $theme) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
		if ( is_wp_error($return) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
		$theme = isset($theme['theme']) ? $theme['theme'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
		if ( $theme != get_stylesheet() ) //If not current
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
		//Change to maintenance mode now.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
		if ( ! $this->bulk )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
			$this->maintenance_mode(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1529
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1530
	 * Turn off maintenance mode after upgrading the current theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1531
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1532
	 * Hooked to the {@see 'upgrader_post_install'} filter by {@see Theme_Upgrader::upgrade()}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1533
	 * and {@see Theme_Upgrader::bulk_upgrade()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1534
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1535
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1536
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1537
	public function current_after($return, $theme) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
		if ( is_wp_error($return) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
		$theme = isset($theme['theme']) ? $theme['theme'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
		if ( $theme != get_stylesheet() ) // If not current
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
		// Ensure stylesheet name hasn't changed after the upgrade:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
		if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
			wp_clean_themes_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
			$stylesheet = $this->result['destination_name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
			switch_theme( $stylesheet );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
		//Time to remove maintenance mode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
		if ( ! $this->bulk )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
			$this->maintenance_mode(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1559
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1560
	 * Delete the old theme during an upgrade.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1561
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1562
	 * Hooked to the {@see 'upgrader_clear_destination'} filter by {@see Theme_Upgrader::upgrade()}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1563
	 * and {@see Theme_Upgrader::bulk_upgrade()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1564
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1565
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1566
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1567
	public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
		if ( is_wp_error( $removed ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
			return $removed; // Pass errors through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
		if ( ! isset( $theme['theme'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
			return $removed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
		$theme = $theme['theme'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
		$themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
		if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
			if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1586
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1587
	 * Get the WP_Theme object for a theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1588
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1589
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1590
	 * @since 3.0.0 The `$theme` argument was added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1591
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1592
	 * @param string $theme The directory name of the theme. This is optional, and if not supplied,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1593
	 *                      the directory name from the last result will be used.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1594
	 * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1595
	 *                        and the last result isn't set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1596
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1597
	public function theme_info($theme = null) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
		if ( empty($theme) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
			if ( !empty($this->result['destination_name']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
				$theme = $this->result['destination_name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1605
		return wp_get_theme( $theme );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1606
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1607
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1613
 * Language pack upgrader, for updating translations of plugins, themes, and core.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1614
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1616
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
class Language_Pack_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1621
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1622
	 * Result of the language pack upgrade.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1623
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1624
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1625
	 * @var array|WP_Error $result
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1626
	 * @see WP_Upgrader::$result
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1627
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1628
	public $result;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1629
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1630
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1631
	 * Whether a bulk upgrade/install is being performed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1632
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1633
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1634
	 * @var bool $bulk
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1635
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1636
	public $bulk = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1637
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1638
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1639
	 * Asynchronously upgrade language packs after other upgrades have been made.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1640
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1641
	 * Hooked to the {@see 'upgrader_process_complete'} action by default.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1642
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1643
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1644
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1645
	public static function async_upgrade( $upgrader = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
		// Avoid recursion.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1647
		if ( $upgrader && $upgrader instanceof Language_Pack_Upgrader ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1649
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
		// Nothing to do?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
		$language_updates = wp_get_translation_updates();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1653
		if ( ! $language_updates ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1654
			return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1655
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1656
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1657
		// Avoid messing with VCS installs, at least for now.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1658
		// Noted: this is not the ideal way to accomplish this.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1659
		$check_vcs = new WP_Automatic_Updater;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1660
		if ( $check_vcs->is_vcs_checkout( WP_CONTENT_DIR ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1662
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1663
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1664
		foreach ( $language_updates as $key => $language_update ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1665
			$update = ! empty( $language_update->autoupdate );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1666
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1667
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1668
			 * Filter whether to asynchronously update translation for core, a plugin, or a theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1669
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1670
			 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1671
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1672
			 * @param bool   $update          Whether to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1673
			 * @param object $language_update The update offer.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1674
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1675
			$update = apply_filters( 'async_update_translation', $update, $language_update );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1676
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1677
			if ( ! $update ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1678
				unset( $language_updates[ $key ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1679
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1680
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1681
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1682
		if ( empty( $language_updates ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1683
			return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1684
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
		$skin = new Language_Pack_Upgrader_Skin( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
			'skip_header_footer' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
		$lp_upgrader = new Language_Pack_Upgrader( $skin );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1691
		$lp_upgrader->bulk_upgrade( $language_updates );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1694
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1695
	 * Initialize the upgrade strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1696
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1697
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1698
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1699
	public function upgrade_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
		$this->strings['starting_upgrade'] = __( 'Some of your translations need updating. Sit tight for a few more seconds while we update them as well.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
		$this->strings['up_to_date'] = __( 'The translation is up to date.' ); // We need to silently skip this case
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
		$this->strings['no_package'] = __( 'Update package not available.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
		$this->strings['downloading_package'] = __( 'Downloading translation from <span class="code">%s</span>&#8230;' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
		$this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
		$this->strings['process_failed'] = __( 'Translation update failed.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
		$this->strings['process_success'] = __( 'Translation updated successfully.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1709
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1710
	 * Upgrade a language pack.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1711
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1712
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1713
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1714
	 * @param string|false $update Optional. Whether an update offer is available. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1715
	 * @param array        $args   Optional. Other optional arguments, see
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1716
	 *                             {@see Language_Pack_Upgrader::bulk_upgrade()}. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1717
	 * @return array|WP_Error The result of the upgrade, or a {@see wP_Error} object instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1718
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1719
	public function upgrade( $update = false, $args = array() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1720
		if ( $update ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
			$update = array( $update );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1722
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1723
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
		$results = $this->bulk_upgrade( $update, $args );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1725
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1726
		if ( ! is_array( $results ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1727
			return $results;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1728
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1729
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1730
		return $results[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1731
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1732
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1733
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1734
	 * Bulk upgrade language packs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1735
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1736
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1737
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1738
	 * @param array $language_updates Optional. Language pack updates. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1739
	 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1740
	 *     Optional. Other arguments for upgrading multiple language packs. Default empty array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1741
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1742
	 *     @type bool $clear_update_cache Whether to clear the update cache when done.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1743
	 *                                    Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1744
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1745
	 * @return array|true|false|WP_Error Will return an array of results, or true if there are no updates,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1746
	 *                                   false or WP_Error for initial errors.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1747
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1748
	public function bulk_upgrade( $language_updates = array(), $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1757
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1759
		if ( ! $language_updates )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1760
			$language_updates = wp_get_translation_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1761
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
		if ( empty( $language_updates ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
			$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
			$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
			$this->skin->set_result( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
			$this->skin->feedback( 'up_to_date' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
			$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1769
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1771
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
		if ( 'upgrader_process_complete' == current_filter() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
			$this->skin->feedback( 'starting_upgrade' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1776
		// Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1777
		remove_all_filters( 'upgrader_pre_install' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1778
		remove_all_filters( 'upgrader_clear_destination' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1779
		remove_all_filterS( 'upgrader_post_install' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1780
		remove_all_filters( 'upgrader_source_selection' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1781
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1782
		add_filter( 'upgrader_source_selection', array( $this, 'check_package' ), 10, 2 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1784
		$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1786
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
		$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
		$results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1795
		$this->update_count = count( $language_updates );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
		$this->update_current = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1797
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1798
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1799
		 * The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1800
		 * as we then may need to create a /plugins or /themes directory inside of it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1801
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1802
		$remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1803
		if ( ! $wp_filesystem->exists( $remote_destination ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1804
			if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1805
				return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1806
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1807
		foreach ( $language_updates as $language_update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
			$this->skin->language_update = $language_update;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
			$destination = WP_LANG_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1812
			if ( 'plugin' == $language_update->type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
				$destination .= '/plugins';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
			elseif ( 'theme' == $language_update->type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
				$destination .= '/themes';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
			$this->update_current++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1818
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1819
			$options = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
				'package' => $language_update->package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
				'destination' => $destination,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
				'clear_destination' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1823
				'abort_if_destination_exists' => false, // We expect the destination to exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
				'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1825
				'is_multi' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1826
				'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1827
					'language_update_type' => $language_update->type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
					'language_update' => $language_update,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
				)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1830
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1831
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1832
			$result = $this->run( $options );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1833
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1834
			$results[] = $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1835
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1836
			// Prevent credentials auth screen from displaying multiple times.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1837
			if ( false === $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1838
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1839
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1840
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
		$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
		$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1844
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
		// Clean up our hooks, in case something else does an upgrade on this connection.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1846
		remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1848
		if ( $parsed_args['clear_update_cache'] ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1849
			wp_clean_update_cache();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1850
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1851
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
		return $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1854
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1855
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1856
	 * Check the package source to make sure there are .mo and .po files.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1857
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1858
	 * Hooked to the {@see 'upgrader_source_selection'} filter by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1859
	 * {@see Language_Pack_Upgrader::bulk_upgrade()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1860
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1861
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1862
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1863
	public function check_package( $source, $remote_source ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
		if ( is_wp_error( $source ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
		// Check that the folder contains a valid language.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
		$files = $wp_filesystem->dirlist( $remote_source );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
		// Check to see if a .po and .mo exist in the folder.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
		$po = $mo = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
		foreach ( (array) $files as $file => $filedata ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
			if ( '.po' == substr( $file, -3 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
				$po = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1877
			elseif ( '.mo' == substr( $file, -3 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1878
				$mo = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1880
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1881
		if ( ! $mo || ! $po )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
			return new WP_Error( 'incompatible_archive_pomo', $this->strings['incompatible_archive'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
				__( 'The language pack is missing either the <code>.po</code> or <code>.mo</code> files.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
		return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1886
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1888
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1889
	 * Get the name of an item being updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1890
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1891
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1892
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1893
	 * @param object The data for an update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1894
	 * @return string The name of the item being updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1895
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1896
	public function get_name_for_update( $update ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
		switch ( $update->type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
			case 'core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1899
				return 'WordPress'; // Not translated
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1900
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
			case 'theme':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
				$theme = wp_get_theme( $update->slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
				if ( $theme->exists() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
					return $theme->Get( 'Name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
			case 'plugin':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
				$plugin_data = get_plugins( '/' . $update->slug );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1908
				$plugin_data = reset( $plugin_data );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
				if ( $plugin_data )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
					return $plugin_data['Name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1911
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1914
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1915
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1916
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1917
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1918
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1919
 * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combination with the wp-admin/includes/update-core.php file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1921
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1922
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1925
class Core_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1926
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1927
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1928
	 * Initialize the upgrade strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1929
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1930
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1931
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1932
	public function upgrade_strings() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1933
		$this->strings['up_to_date'] = __('WordPress is at the latest version.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1934
		$this->strings['no_package'] = __('Update package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1935
		$this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1936
		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1937
		$this->strings['copy_failed'] = __('Could not copy files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1938
		$this->strings['copy_failed_space'] = __('Could not copy files. You may have run out of disk space.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1939
		$this->strings['start_rollback'] = __( 'Attempting to roll back to previous version.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
		$this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has rolled back to your previous version.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1941
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1942
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1943
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1944
	 * Upgrade WordPress core.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1945
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1946
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1947
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1948
	 * @param object $current Response object for whether WordPress is current.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1949
	 * @param array  $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1950
	 *        Optional. Arguments for upgrading WordPress core. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1951
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1952
	 *        @type bool $pre_check_md5    Whether to check the file checksums before
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1953
	 *                                     attempting the upgrade. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1954
	 *        @type bool $attempt_rollback Whether to attempt to rollback the chances if
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1955
	 *                                     there is a problem. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1956
	 *        @type bool $do_rollback      Whether to perform this "upgrade" as a rollback.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1957
	 *                                     Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1958
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1959
	 * @return null|false|WP_Error False or WP_Error on failure, null on success.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1960
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1961
	public function upgrade( $current, $args = array() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1962
		global $wp_filesystem;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1963
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1964
		include( ABSPATH . WPINC . '/version.php' ); // $wp_version;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
		$start_time = time();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
			'pre_check_md5'    => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
			'attempt_rollback' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
			'do_rollback'      => false,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1972
			'allow_relaxed_file_ownership' => false,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1979
		// Is an update available?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
		if ( !isset( $current->response ) || $current->response == 'latest' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
			return new WP_Error('up_to_date', $this->strings['up_to_date']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1983
		$res = $this->fs_connect( array( ABSPATH, WP_CONTENT_DIR ), $parsed_args['allow_relaxed_file_ownership'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1984
		if ( ! $res || is_wp_error( $res ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
			return $res;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1986
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
		$wp_dir = trailingslashit($wp_filesystem->abspath());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
		$partial = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1991
		if ( $parsed_args['do_rollback'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1992
			$partial = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
		elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
			$partial = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1996
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1997
		 * If partial update is returned from the API, use that, unless we're doing
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1998
		 * a reinstall. If we cross the new_bundled version number, then use
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1999
		 * the new_bundled zip. Don't though if the constant is set to skip bundled items.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2000
		 * If the API returns a no_content zip, go with it. Finally, default to the full zip.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2001
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
		if ( $parsed_args['do_rollback'] && $current->packages->rollback )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
			$to_download = 'rollback';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
		elseif ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && $partial )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
			$to_download = 'partial';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
		elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2007
			&& ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2008
			$to_download = 'new_bundled';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2009
		elseif ( $current->packages->no_content )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2010
			$to_download = 'no_content';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2011
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
			$to_download = 'full';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
		$download = $this->download_package( $current->packages->$to_download );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
		if ( is_wp_error($download) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
			return $download;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2018
		$working_dir = $this->unpack_package( $download );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
		if ( is_wp_error($working_dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
			return $working_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
		// Copy update-core.php from the new version into place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2023
		if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
			$wp_filesystem->delete($working_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
			return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2027
		$wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
		require_once( ABSPATH . 'wp-admin/includes/update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2031
		if ( ! function_exists( 'update_core' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2032
			return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2034
		$result = update_core( $working_dir, $wp_dir );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2035
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2036
		// In the event of an issue, we may be able to roll back.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
		if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2038
			$try_rollback = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2039
			if ( is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2040
				$error_code = $result->get_error_code();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2041
				/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2042
				 * Not all errors are equal. These codes are critical: copy_failed__copy_dir,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2043
				 * mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2044
				 * do_rollback allows for update_core() to trigger a rollback if needed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2045
				 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2046
				if ( false !== strpos( $error_code, 'do_rollback' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
					$try_rollback = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
				elseif ( false !== strpos( $error_code, '__copy_dir' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2049
					$try_rollback = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
				elseif ( 'disk_full' === $error_code )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2051
					$try_rollback = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2052
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
			if ( $try_rollback ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2055
				/** This filter is documented in wp-admin/includes/update-core.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
				apply_filters( 'update_feedback', $result );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2057
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2058
				/** This filter is documented in wp-admin/includes/update-core.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
				apply_filters( 'update_feedback', $this->strings['start_rollback'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
				$rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
				$original_result = $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
				$result = new WP_Error( 'rollback_was_required', $this->strings['rollback_was_required'], (object) array( 'update' => $original_result, 'rollback' => $rollback_result ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2068
		/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
		do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
		// Clear the current updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
		delete_site_transient( 'update_core' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
		if ( ! $parsed_args['do_rollback'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2075
			$stats = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2076
				'update_type'      => $current->response,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
				'success'          => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
				'fs_method'        => $wp_filesystem->method,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2079
				'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2080
				'fs_method_direct' => !empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOBALS['_wp_filesystem_direct_method'] : '',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2081
				'time_taken'       => time() - $start_time,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2082
				'reported'         => $wp_version,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2083
				'attempted'        => $current->version,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2086
			if ( is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
				$stats['success'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
				// Did a rollback occur?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
				if ( ! empty( $try_rollback ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
					$stats['error_code'] = $original_result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2091
					$stats['error_data'] = $original_result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2092
					// Was the rollback successful? If not, collect its error too.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
					$stats['rollback'] = ! is_wp_error( $rollback_result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
					if ( is_wp_error( $rollback_result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2095
						$stats['rollback_code'] = $rollback_result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
						$stats['rollback_data'] = $rollback_result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
					$stats['error_code'] = $result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
					$stats['error_data'] = $result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
			wp_version_check( $stats );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2105
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2110
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2111
	 * Determines if this WordPress Core version should update to an offered version or not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2112
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2113
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2114
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2115
	 * @param string $offered_ver The offered version, of the format x.y.z.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2116
	 * @return bool True if we should update to the offered version, otherwise false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2117
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2118
	public static function should_update_to_version( $offered_ver ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2119
		include( ABSPATH . WPINC . '/version.php' ); // $wp_version; // x.y.z
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2121
		$current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version  ), 0, 2 ) ); // x.y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
		$new_branch     = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2123
		$current_is_development_version = (bool) strpos( $wp_version, '-' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2125
		// Defaults:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
		$upgrade_dev   = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2127
		$upgrade_minor = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
		$upgrade_major = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2130
		// WP_AUTO_UPDATE_CORE = true (all), 'minor', false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2131
		if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
			if ( false === WP_AUTO_UPDATE_CORE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2133
				// Defaults to turned off, unless a filter allows it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
				$upgrade_dev = $upgrade_minor = $upgrade_major = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
			} elseif ( true === WP_AUTO_UPDATE_CORE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
				// ALL updates for core
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
				$upgrade_dev = $upgrade_minor = $upgrade_major = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2138
			} elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2139
				// Only minor updates for core
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
				$upgrade_dev = $upgrade_major = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
				$upgrade_minor = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
		// 1: If we're already on that version, not much point in updating?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
		if ( $offered_ver == $wp_version )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
		// 2: If we're running a newer version, that's a nope
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
		if ( version_compare( $wp_version, $offered_ver, '>' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
		$failure_data = get_site_option( 'auto_core_update_failed' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
		if ( $failure_data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
			// If this was a critical update failure, cannot update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
			if ( ! empty( $failure_data['critical'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
			// Don't claim we can update on update-core.php if we have a non-critical failure logged.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
			if ( $wp_version == $failure_data['current'] && false !== strpos( $offered_ver, '.1.next.minor' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
			// Cannot update if we're retrying the same A to B update that caused a non-critical failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
			// Some non-critical failures do allow retries, like download_failed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2165
			// 3.7.1 => 3.7.2 resulted in files_not_writable, if we are still on 3.7.1 and still trying to update to 3.7.2.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2166
			if ( empty( $failure_data['retry'] ) && $wp_version == $failure_data['current'] && $offered_ver == $failure_data['attempted'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2167
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2168
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
		// 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
		if ( $current_is_development_version ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2172
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2173
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2174
			 * Filter whether to enable automatic core updates for development versions.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2175
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2176
			 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2177
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2178
			 * @param bool $upgrade_dev Whether to enable automatic updates for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2179
			 *                          development versions.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2180
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
			if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
				return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2183
			// Else fall through to minor + major branches below.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
		// 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2187
		if ( $current_branch == $new_branch ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2188
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2189
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2190
			 * Filter whether to enable minor automatic core updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2191
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2192
			 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2193
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2194
			 * @param bool $upgrade_minor Whether to enable minor automatic core updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2195
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2196
			return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2197
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2199
		// 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2200
		if ( version_compare( $new_branch, $current_branch, '>' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2201
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2202
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2203
			 * Filter whether to enable major automatic core updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2204
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2205
			 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2206
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2207
			 * @param bool $upgrade_major Whether to enable major automatic core updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2208
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
			return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2210
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
		// If we're not sure, we don't want it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2216
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2217
	 * Compare the disk file checksums agains the expected checksums.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2218
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2219
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2220
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2221
	 * @return bool True if the checksums match, otherwise false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2222
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2223
	public function check_files() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2224
		global $wp_version, $wp_local_package;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2226
		$checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2227
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2228
		if ( ! is_array( $checksums ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2229
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
		foreach ( $checksums as $file => $checksum ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
			// Skip files which get updated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
			if ( 'wp-content' == substr( $file, 0, 10 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2235
			if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2236
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2237
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2238
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2239
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2240
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2241
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2243
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2244
 * Upgrade Skin helper for File uploads. This class handles the upload process and passes it as if it's a local file to the Upgrade/Installer functions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2245
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2246
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2247
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2248
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2249
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2250
class File_Upload_Upgrader {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2251
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2252
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2253
	 * The full path to the file package.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2254
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2255
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2256
	 * @var string $package
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2257
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2258
	public $package;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2259
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2260
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2261
	 * The name of the file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2262
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2263
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2264
	 * @var string $filename
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2265
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2266
	public $filename;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2267
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2268
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2269
	 * The ID of the attachment post for this file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2270
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2271
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2272
	 * @var int $id
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2273
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2274
	public $id = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2275
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2276
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2277
	 * Construct the upgrader for a form.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2278
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2279
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2280
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2281
	 * @param string $form      The name of the form the file was uploaded from.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2282
	 * @param string $urlholder The name of the `GET` parameter that holds the filename.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2283
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2284
	public function __construct( $form, $urlholder ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2286
		if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2287
			wp_die(__('Please select a file'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
		//Handle a newly uploaded file, Else assume it's already been uploaded
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2290
		if ( ! empty($_FILES) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2291
			$overrides = array( 'test_form' => false, 'test_type' => false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2292
			$file = wp_handle_upload( $_FILES[$form], $overrides );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2293
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2294
			if ( isset( $file['error'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2295
				wp_die( $file['error'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2296
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2297
			$this->filename = $_FILES[$form]['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2298
			$this->package = $file['file'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2299
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2300
			// Construct the object array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2301
			$object = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2302
				'post_title' => $this->filename,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2303
				'post_content' => $file['url'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2304
				'post_mime_type' => $file['type'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2305
				'guid' => $file['url'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2306
				'context' => 'upgrader',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2307
				'post_status' => 'private'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2308
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2309
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2310
			// Save the data.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2311
			$this->id = wp_insert_attachment( $object, $file['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2312
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2313
			// Schedule a cleanup for 2 hours from now in case of failed install.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2314
			wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2315
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2316
		} elseif ( is_numeric( $_GET[$urlholder] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2317
			// Numeric Package = previously uploaded file, see above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2318
			$this->id = (int) $_GET[$urlholder];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2319
			$attachment = get_post( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2320
			if ( empty($attachment) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2321
				wp_die(__('Please select a file'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2322
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2323
			$this->filename = $attachment->post_title;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2324
			$this->package = get_attached_file( $attachment->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2325
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2326
			// Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2327
			if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2328
				wp_die( $uploads['error'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2329
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2330
			$this->filename = $_GET[$urlholder];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2331
			$this->package = $uploads['basedir'] . '/' . $this->filename;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2332
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2333
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2334
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2335
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2336
	 * Delete the attachment/uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2337
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2338
	 * @since 3.2.2
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2339
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2340
	 * @return bool Whether the cleanup was successful.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2341
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2342
	public function cleanup() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2343
		if ( $this->id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2344
			wp_delete_attachment( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2346
		elseif ( file_exists( $this->package ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2347
			return @unlink( $this->package );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2349
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2350
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2351
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2352
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2353
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
 * The WordPress automatic background updater.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2355
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2356
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2357
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2358
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2359
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2360
class WP_Automatic_Updater {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2362
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
	 * Tracks update results during processing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2364
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2367
	protected $update_results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2369
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2370
	 * Whether the entire automatic updater is disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2371
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2372
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2373
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2374
	public function is_disabled() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2375
		// Background updates are disabled if you don't want file changes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2376
		if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2377
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2379
		if ( defined( 'WP_INSTALLING' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2380
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2381
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2382
		// More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2383
		$disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2385
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2386
		 * Filter whether to entirely disable background updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
		 * There are more fine-grained filters and controls for selective disabling.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
		 * This filter parallels the AUTOMATIC_UPDATER_DISABLED constant in name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2390
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2391
		 * This also disables update notification emails. That may change in the future.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2392
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2393
		 * @since 3.7.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2394
		 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
		 * @param bool $disabled Whether the updater should be disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
		return apply_filters( 'automatic_updater_disabled', $disabled );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
	 * Check for version control checkouts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2403
	 * Checks for Subversion, Git, Mercurial, and Bazaar. It recursively looks up the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2404
	 * filesystem to the top of the drive, erring on the side of detecting a VCS
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2405
	 * checkout somewhere.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2406
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2407
	 * ABSPATH is always checked in addition to whatever $context is (which may be the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2408
	 * wp-content directory, for example). The underlying assumption is that if you are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2409
	 * using version control *anywhere*, then you should be making decisions for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2410
	 * how things get updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2411
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2412
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2413
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2414
	 * @param string $context The filesystem path to check, in addition to ABSPATH.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2415
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2416
	public function is_vcs_checkout( $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2417
		$context_dirs = array( untrailingslashit( $context ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2418
		if ( $context !== ABSPATH )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2419
			$context_dirs[] = untrailingslashit( ABSPATH );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2420
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2421
		$vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2422
		$check_dirs = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2423
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2424
		foreach ( $context_dirs as $context_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2425
			// Walk up from $context_dir to the root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2426
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2427
				$check_dirs[] = $context_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2428
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2429
				// Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2430
				if ( $context_dir == dirname( $context_dir ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2431
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2432
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2433
			// Continue one level at a time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2434
			} while ( $context_dir = dirname( $context_dir ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2435
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2436
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2437
		$check_dirs = array_unique( $check_dirs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2438
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2439
		// Search all directories we've found for evidence of version control.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2440
		foreach ( $vcs_dirs as $vcs_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2441
			foreach ( $check_dirs as $check_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2442
				if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2443
					break 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2444
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2445
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2447
		/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2448
		 * Filter whether the automatic updater should consider a filesystem
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2449
		 * location to be potentially managed by a version control system.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2450
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2451
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2452
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2453
		 * @param bool $checkout  Whether a VCS checkout was discovered at $context
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2454
		 *                        or ABSPATH, or anywhere higher.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2455
		 * @param string $context The filesystem context (a path) against which
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2456
		 *                        filesystem status should be checked.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2457
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2458
		return apply_filters( 'automatic_updates_is_vcs_checkout', $checkout, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2459
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2460
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2461
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2462
	 * Tests to see if we can and should update a specific item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2463
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2464
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2465
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2466
	 * @param string $type    The type of update being checked: 'core', 'theme',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2467
	 *                        'plugin', 'translation'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2468
	 * @param object $item    The update offer.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2469
	 * @param string $context The filesystem context (a path) against which filesystem
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2470
	 *                        access and status should be checked.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2471
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2472
	public function should_update( $type, $item, $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2473
		// Used to see if WP_Filesystem is set up to allow unattended updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2474
		$skin = new Automatic_Upgrader_Skin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2476
		if ( $this->is_disabled() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2477
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2478
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2479
		// Only relax the filesystem checks when the update doesn't include new files
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2480
		$allow_relaxed_file_ownership = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2481
		if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2482
			$allow_relaxed_file_ownership = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2483
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2484
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2485
		// If we can't do an auto core update, we may still be able to email the user.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2486
		if ( ! $skin->request_filesystem_credentials( false, $context, $allow_relaxed_file_ownership ) || $this->is_vcs_checkout( $context ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2487
			if ( 'core' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2488
				$this->send_core_update_notification_email( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2489
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2490
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2491
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2492
		// Next up, is this an item we can update?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2493
		if ( 'core' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2494
			$update = Core_Upgrader::should_update_to_version( $item->current );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2495
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2496
			$update = ! empty( $item->autoupdate );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2498
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2499
		 * Filter whether to automatically update core, a plugin, a theme, or a language.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2500
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2501
		 * The dynamic portion of the hook name, `$type`, refers to the type of update
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2502
		 * being checked. Can be 'core', 'theme', 'plugin', or 'translation'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2503
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2504
		 * Generally speaking, plugins, themes, and major core versions are not updated
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2505
		 * by default, while translations and minor and development versions for core
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2506
		 * are updated by default.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2507
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2508
		 * See the {@see 'allow_dev_auto_core_updates', {@see 'allow_minor_auto_core_updates'},
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2509
		 * and {@see 'allow_major_auto_core_updates'} filters for a more straightforward way to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2510
		 * adjust core updates.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2511
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2512
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2513
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2514
		 * @param bool   $update Whether to update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2515
		 * @param object $item   The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2516
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2517
		$update = apply_filters( 'auto_update_' . $type, $update, $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2519
		if ( ! $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2520
			if ( 'core' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2521
				$this->send_core_update_notification_email( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2522
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2523
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2524
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2525
		// If it's a core update, are we actually compatible with its requirements?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2526
		if ( 'core' == $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2527
			global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2528
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2529
			$php_compat = version_compare( phpversion(), $item->php_version, '>=' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2530
			if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2531
				$mysql_compat = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2532
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2533
				$mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2534
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2535
			if ( ! $php_compat || ! $mysql_compat )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2536
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2537
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2539
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2540
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2541
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2542
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2543
	 * Notifies an administrator of a core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2544
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2545
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2546
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2547
	 * @param object $item The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2548
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2549
	protected function send_core_update_notification_email( $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2550
		$notified = get_site_option( 'auto_core_update_notified' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2551
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2552
		// Don't notify if we've already notified the same email address of the same version.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2553
		if ( $notified && $notified['email'] == get_site_option( 'admin_email' ) && $notified['version'] == $item->current )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2554
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2555
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2556
		// See if we need to notify users of a core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2557
		$notify = ! empty( $item->notify_email );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2558
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2559
		/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2560
		 * Filter whether to notify the site administrator of a new core update.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2561
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2562
		 * By default, administrators are notified when the update offer received
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2563
		 * from WordPress.org sets a particular flag. This allows some discretion
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2564
		 * in if and when to notify.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2565
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2566
		 * This filter is only evaluated once per release. If the same email address
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2567
		 * was already notified of the same new version, WordPress won't repeatedly
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2568
		 * email the administrator.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2569
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2570
		 * This filter is also used on about.php to check if a plugin has disabled
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2571
		 * these notifications.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2572
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2573
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2574
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2575
		 * @param bool   $notify Whether the site administrator is notified.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2576
		 * @param object $item   The update offer.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2577
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2578
		if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2579
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2580
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2581
		$this->send_email( 'manual', $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2582
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2583
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2584
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2585
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2586
	 * Update an item, if appropriate.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2587
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2588
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2589
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2590
	 * @param string $type The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2591
	 * @param object $item The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2592
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2593
	public function update( $type, $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2594
		$skin = new Automatic_Upgrader_Skin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2595
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2596
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2597
			case 'core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2598
				// The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2599
				add_filter( 'update_feedback', array( $skin, 'feedback' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2600
				$upgrader = new Core_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2601
				$context  = ABSPATH;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2602
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2603
			case 'plugin':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2604
				$upgrader = new Plugin_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2605
				$context  = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2606
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2607
			case 'theme':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2608
				$upgrader = new Theme_Upgrader( $skin );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2609
				$context  = get_theme_root( $item->theme );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2610
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2611
			case 'translation':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2612
				$upgrader = new Language_Pack_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2613
				$context  = WP_CONTENT_DIR; // WP_LANG_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2614
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2615
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2616
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2617
		// Determine whether we can and should perform this update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2618
		if ( ! $this->should_update( $type, $item, $context ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2619
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2620
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2621
		$upgrader_item = $item;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2622
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2623
			case 'core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2624
				$skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2625
				$item_name = sprintf( __( 'WordPress %s' ), $item->version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2626
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2627
			case 'theme':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2628
				$upgrader_item = $item->theme;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2629
				$theme = wp_get_theme( $upgrader_item );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2630
				$item_name = $theme->Get( 'Name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2631
				$skin->feedback( __( 'Updating theme: %s' ), $item_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2632
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2633
			case 'plugin':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2634
				$upgrader_item = $item->plugin;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2635
				$plugin_data = get_plugin_data( $context . '/' . $upgrader_item );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2636
				$item_name = $plugin_data['Name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2637
				$skin->feedback( __( 'Updating plugin: %s' ), $item_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2638
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2639
			case 'translation':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2640
				$language_item_name = $upgrader->get_name_for_update( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2641
				$item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2642
				$skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)&#8230;' ), $language_item_name, $item->language ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2643
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2644
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2645
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2646
		$allow_relaxed_file_ownership = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2647
		if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2648
			$allow_relaxed_file_ownership = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2649
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2650
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2651
		// Boom, This sites about to get a whole new splash of paint!
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2652
		$upgrade_result = $upgrader->upgrade( $upgrader_item, array(
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2653
			'clear_update_cache' => false,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2654
			// Always use partial builds if possible for core updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2655
			'pre_check_md5'      => false,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2656
			// Only available for core updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2657
			'attempt_rollback'   => true,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2658
			// Allow relaxed file ownership in some scenarios
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2659
			'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2660
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2661
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2662
		// If the filesystem is unavailable, false is returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2663
		if ( false === $upgrade_result ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2664
			$upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2665
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2666
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2667
		// Core doesn't output this, so let's append it so we don't get confused.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2668
		if ( 'core' == $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2669
			if ( is_wp_error( $upgrade_result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2670
				$skin->error( __( 'Installation Failed' ), $upgrade_result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2671
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2672
				$skin->feedback( __( 'WordPress updated successfully' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2673
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2674
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2675
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2676
		$this->update_results[ $type ][] = (object) array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2677
			'item'     => $item,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2678
			'result'   => $upgrade_result,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2679
			'name'     => $item_name,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2680
			'messages' => $skin->get_upgrade_messages()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2681
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2682
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2683
		return $upgrade_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2684
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2685
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2686
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2687
	 * Kicks off the background update process, looping through all pending updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2688
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2689
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2690
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2691
	public function run() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2692
		global $wpdb, $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2693
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2694
		if ( $this->is_disabled() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2695
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2696
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2697
		if ( ! is_main_network() || ! is_main_site() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2698
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2699
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2700
		$lock_name = 'auto_updater.lock';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2702
		// Try to lock
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2703
		$lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2704
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2705
		if ( ! $lock_result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2706
			$lock_result = get_option( $lock_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2708
			// If we couldn't create a lock, and there isn't a lock, bail
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2709
			if ( ! $lock_result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2710
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2711
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2712
			// Check to see if the lock is still valid
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2713
			if ( $lock_result > ( time() - HOUR_IN_SECONDS ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2714
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2715
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2716
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2717
		// Update the lock, as by this point we've definitely got a lock, just need to fire the actions
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2718
		update_option( $lock_name, time() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2719
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2720
		// Don't automatically run these thins, as we'll handle it ourselves
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2721
		remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2722
		remove_action( 'upgrader_process_complete', 'wp_version_check' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2723
		remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2724
		remove_action( 'upgrader_process_complete', 'wp_update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2725
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2726
		// Next, Plugins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2727
		wp_update_plugins(); // Check for Plugin updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2728
		$plugin_updates = get_site_transient( 'update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2729
		if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2730
			foreach ( $plugin_updates->response as $plugin ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2731
				$this->update( 'plugin', $plugin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2732
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2733
			// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2734
			wp_clean_plugins_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2735
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2736
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2737
		// Next, those themes we all love
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2738
		wp_update_themes();  // Check for Theme updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2739
		$theme_updates = get_site_transient( 'update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2740
		if ( $theme_updates && !empty( $theme_updates->response ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2741
			foreach ( $theme_updates->response as $theme ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2742
				$this->update( 'theme', (object) $theme );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2743
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2744
			// Force refresh of theme update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2745
			wp_clean_themes_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2746
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2747
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2748
		// Next, Process any core update
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2749
		wp_version_check(); // Check for Core updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2750
		$core_update = find_core_auto_update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2752
		if ( $core_update )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2753
			$this->update( 'core', $core_update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2754
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2755
		// Clean up, and check for any pending translations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2756
		// (Core_Upgrader checks for core updates)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2757
		$theme_stats = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2758
		if ( isset( $this->update_results['theme'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2759
			foreach ( $this->update_results['theme'] as $upgrade ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2760
				$theme_stats[ $upgrade->item->theme ] = ( true === $upgrade->result );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2761
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2762
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2763
		wp_update_themes( $theme_stats );  // Check for Theme updates
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2764
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2765
		$plugin_stats = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2766
		if ( isset( $this->update_results['plugin'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2767
			foreach ( $this->update_results['plugin'] as $upgrade ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2768
				$plugin_stats[ $upgrade->item->plugin ] = ( true === $upgrade->result );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2769
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2770
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2771
		wp_update_plugins( $plugin_stats ); // Check for Plugin updates
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2773
		// Finally, Process any new translations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2774
		$language_updates = wp_get_translation_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2775
		if ( $language_updates ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2776
			foreach ( $language_updates as $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2777
				$this->update( 'translation', $update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2778
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2779
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2780
			// Clear existing caches
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2781
			wp_clean_update_cache();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2782
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2783
			wp_version_check();  // check for Core updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2784
			wp_update_themes();  // Check for Theme updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2785
			wp_update_plugins(); // Check for Plugin updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2786
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2787
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2788
		// Send debugging email to all development installs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2789
		if ( ! empty( $this->update_results ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2790
			$development_version = false !== strpos( $wp_version, '-' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2791
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2792
			/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2793
			 * Filter whether to send a debugging email for each automatic background update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2794
			 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2795
			 * @since 3.7.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2796
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2797
			 * @param bool $development_version By default, emails are sent if the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2798
			 *                                  install is a development version.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2799
			 *                                  Return false to avoid the email.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2800
			 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2801
			if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2802
				$this->send_debug_email();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2803
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2804
			if ( ! empty( $this->update_results['core'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2805
				$this->after_core_update( $this->update_results['core'][0] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2806
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2807
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2808
			 * Fires after all automatic updates have run.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2809
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2810
			 * @since 3.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2811
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2812
			 * @param array $update_results The results of all attempted updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2813
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2814
			do_action( 'automatic_updates_complete', $this->update_results );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2815
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2817
		// Clear the lock
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2818
		delete_option( $lock_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2819
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2820
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2821
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2822
	 * If we tried to perform a core update, check if we should send an email,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2823
	 * and if we need to avoid processing future updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2824
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2825
	 * @param object $update_result The result of the core update. Includes the update offer and result.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2826
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2827
	protected function after_core_update( $update_result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2828
		global $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2829
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2830
		$core_update = $update_result->item;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2831
		$result      = $update_result->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2833
		if ( ! is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2834
			$this->send_email( 'success', $core_update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2835
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2836
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2837
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2838
		$error_code = $result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2839
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2840
		// Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2841
		// We should not try to perform a background update again until there is a successful one-click update performed by the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2842
		$critical = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2843
		if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2844
			$critical = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2845
		} elseif ( $error_code === 'rollback_was_required' && is_wp_error( $result->get_error_data()->rollback ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2846
			// A rollback is only critical if it failed too.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2847
			$critical = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2848
			$rollback_result = $result->get_error_data()->rollback;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2849
		} elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2850
			$critical = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2851
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2852
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2853
		if ( $critical ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2854
			$critical_data = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2855
				'attempted'  => $core_update->current,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2856
				'current'    => $wp_version,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2857
				'error_code' => $error_code,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2858
				'error_data' => $result->get_error_data(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2859
				'timestamp'  => time(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2860
				'critical'   => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2861
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2862
			if ( isset( $rollback_result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2863
				$critical_data['rollback_code'] = $rollback_result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2864
				$critical_data['rollback_data'] = $rollback_result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2865
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2866
			update_site_option( 'auto_core_update_failed', $critical_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2867
			$this->send_email( 'critical', $core_update, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2868
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2869
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2870
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2871
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2872
		 * Any other WP_Error code (like download_failed or files_not_writable) occurs before
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2873
		 * we tried to copy over core files. Thus, the failures are early and graceful.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2874
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2875
		 * We should avoid trying to perform a background update again for the same version.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2876
		 * But we can try again if another version is released.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2877
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2878
		 * For certain 'transient' failures, like download_failed, we should allow retries.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2879
		 * In fact, let's schedule a special update for an hour from now. (It's possible
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2880
		 * the issue could actually be on WordPress.org's side.) If that one fails, then email.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2881
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2882
		$send = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2883
  		$transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2884
  		if ( in_array( $error_code, $transient_failures ) && ! get_site_option( 'auto_core_update_failed' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2885
  			wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2886
  			$send = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2887
  		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2888
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2889
  		$n = get_site_option( 'auto_core_update_notified' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2890
		// Don't notify if we've already notified the same email address of the same version of the same notification type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2891
		if ( $n && 'fail' == $n['type'] && $n['email'] == get_site_option( 'admin_email' ) && $n['version'] == $core_update->current )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2892
			$send = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2893
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2894
		update_site_option( 'auto_core_update_failed', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2895
			'attempted'  => $core_update->current,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2896
			'current'    => $wp_version,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2897
			'error_code' => $error_code,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2898
			'error_data' => $result->get_error_data(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2899
			'timestamp'  => time(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2900
			'retry'      => in_array( $error_code, $transient_failures ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2901
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2902
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2903
		if ( $send )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2904
			$this->send_email( 'fail', $core_update, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2905
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2906
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2907
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2908
	 * Sends an email upon the completion or failure of a background core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2909
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2910
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2911
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2912
	 * @param string $type        The type of email to send. Can be one of 'success', 'fail', 'manual', 'critical'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2913
	 * @param object $core_update The update offer that was attempted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2914
	 * @param mixed  $result      Optional. The result for the core update. Can be WP_Error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2915
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2916
	protected function send_email( $type, $core_update, $result = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2917
		update_site_option( 'auto_core_update_notified', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2918
			'type'      => $type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2919
			'email'     => get_site_option( 'admin_email' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2920
			'version'   => $core_update->current,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2921
			'timestamp' => time(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2922
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2923
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2924
		$next_user_core_update = get_preferred_from_update_core();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2925
		// If the update transient is empty, use the update we just performed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2926
		if ( ! $next_user_core_update )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2927
			$next_user_core_update = $core_update;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2928
		$newer_version_available = ( 'upgrade' == $next_user_core_update->response && version_compare( $next_user_core_update->version, $core_update->version, '>' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2929
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2930
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2931
		 * Filter whether to send an email following an automatic background core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2932
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2933
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2934
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2935
		 * @param bool   $send        Whether to send the email. Default true.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2936
		 * @param string $type        The type of email to send. Can be one of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2937
		 *                            'success', 'fail', 'critical'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2938
		 * @param object $core_update The update offer that was attempted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2939
		 * @param mixed  $result      The result for the core update. Can be WP_Error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2940
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2941
		if ( 'manual' !== $type && ! apply_filters( 'auto_core_update_send_email', true, $type, $core_update, $result ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2942
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2943
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2944
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2945
			case 'success' : // We updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2946
				/* translators: 1: Site name, 2: WordPress version number. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2947
				$subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2948
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2949
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2950
			case 'fail' :   // We tried to update but couldn't.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2951
			case 'manual' : // We can't update (and made no attempt).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2952
				/* translators: 1: Site name, 2: WordPress version number. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2953
				$subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2954
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2955
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2956
			case 'critical' : // We tried to update, started to copy files, then things went wrong.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2957
				/* translators: 1: Site name. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2958
				$subject = __( '[%1$s] URGENT: Your site may be down due to a failed update' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2959
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2960
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2961
			default :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2962
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2963
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2964
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2965
		// If the auto update is not to the latest version, say that the current version of WP is available instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2966
		$version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2967
		$subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2969
		$body = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2970
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2971
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2972
			case 'success' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2973
				$body .= sprintf( __( 'Howdy! Your site at %1$s has been updated automatically to WordPress %2$s.' ), home_url(), $core_update->current );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2974
				$body .= "\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2975
				if ( ! $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2976
					$body .= __( 'No further action is needed on your part.' ) . ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2978
				// Can only reference the About screen if their update was successful.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2979
				list( $about_version ) = explode( '-', $core_update->current, 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2980
				$body .= sprintf( __( "For more on version %s, see the About WordPress screen:" ), $about_version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2981
				$body .= "\n" . admin_url( 'about.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2982
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2983
				if ( $newer_version_available ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2984
					$body .= "\n\n" . sprintf( __( 'WordPress %s is also now available.' ), $next_user_core_update->current ) . ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2985
					$body .= __( 'Updating is easy and only takes a few moments:' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2986
					$body .= "\n" . network_admin_url( 'update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2987
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2988
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2989
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2990
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2991
			case 'fail' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2992
			case 'manual' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2993
				$body .= sprintf( __( 'Please update your site at %1$s to WordPress %2$s.' ), home_url(), $next_user_core_update->current );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2994
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2995
				$body .= "\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2996
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2997
				// Don't show this message if there is a newer version available.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2998
				// Potential for confusion, and also not useful for them to know at this point.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2999
				if ( 'fail' == $type && ! $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3000
					$body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3001
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3002
				$body .= __( 'Updating is easy and only takes a few moments:' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3003
				$body .= "\n" . network_admin_url( 'update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3004
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3005
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3006
			case 'critical' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3007
				if ( $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3008
					$body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update WordPress to version %2$s.' ), home_url(), $core_update->current );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3009
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3010
					$body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update to the latest version of WordPress, %2$s.' ), home_url(), $core_update->current );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3011
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3012
				$body .= "\n\n" . __( "This means your site may be offline or broken. Don't panic; this can be fixed." );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3013
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3014
				$body .= "\n\n" . __( "Please check out your site now. It's possible that everything is working. If it says you need to update, you should do so:" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3015
				$body .= "\n" . network_admin_url( 'update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3016
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3017
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3018
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3019
		$critical_support = 'critical' === $type && ! empty( $core_update->support_email );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3020
		if ( $critical_support ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3021
			// Support offer if available.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3022
			$body .= "\n\n" . sprintf( __( "The WordPress team is willing to help you. Forward this email to %s and the team will work with you to make sure your site is working." ), $core_update->support_email );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3023
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3024
			// Add a note about the support forums.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3025
			$body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3026
			$body .= "\n" . __( 'https://wordpress.org/support/' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3027
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3028
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3029
		// Updates are important!
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3030
		if ( $type != 'success' || $newer_version_available ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3031
			$body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3032
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3033
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3034
		if ( $critical_support ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3035
			$body .= " " . __( "If you reach out to us, we'll also ensure you'll never have this problem again." );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3036
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3037
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3038
		// If things are successful and we're now on the latest, mention plugins and themes if any are out of date.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3039
		if ( $type == 'success' && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3040
			$body .= "\n\n" . __( 'You also have some plugins or themes with updates available. Update them now:' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3041
			$body .= "\n" . network_admin_url();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3042
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3043
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3044
		$body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3045
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3046
		if ( 'critical' == $type && is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3047
			$body .= "\n***\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3048
			$body .= sprintf( __( 'Your site was running version %s.' ), $GLOBALS['wp_version'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3049
			$body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3050
			$body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3051
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3052
			// If we had a rollback and we're still critical, then the rollback failed too.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3053
			// Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3054
			if ( 'rollback_was_required' == $result->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3055
				$errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3056
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3057
				$errors = array( $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3058
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3059
			foreach ( $errors as $error ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3060
				if ( ! is_wp_error( $error ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3061
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3062
				$error_code = $error->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3063
				$body .= "\n\n" . sprintf( __( "Error code: %s" ), $error_code );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3064
				if ( 'rollback_was_required' == $error_code )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3065
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3066
				if ( $error->get_error_message() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3067
					$body .= "\n" . $error->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3068
				$error_data = $error->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3069
				if ( $error_data )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3070
					$body .= "\n" . implode( ', ', (array) $error_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3071
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3072
			$body .= "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3073
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3074
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3075
		$to  = get_site_option( 'admin_email' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3076
		$headers = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3077
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3078
		$email = compact( 'to', 'subject', 'body', 'headers' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3079
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3080
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3081
		 * Filter the email sent following an automatic background core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3082
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3083
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3084
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3085
		 * @param array $email {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3086
		 *     Array of email arguments that will be passed to wp_mail().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3087
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3088
		 *     @type string $to      The email recipient. An array of emails
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3089
		 *                            can be returned, as handled by wp_mail().
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3090
		 *     @type string $subject The email's subject.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3091
		 *     @type string $body    The email message body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3092
		 *     @type string $headers Any email headers, defaults to no headers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3093
		 * }
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3094
		 * @param string $type        The type of email being sent. Can be one of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3095
		 *                            'success', 'fail', 'manual', 'critical'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3096
		 * @param object $core_update The update offer that was attempted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3097
		 * @param mixed  $result      The result for the core update. Can be WP_Error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3098
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3099
		$email = apply_filters( 'auto_core_update_email', $email, $type, $core_update, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3100
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3101
		wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3102
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3104
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3105
	 * Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3106
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3107
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3108
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3109
	protected function send_debug_email() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3110
		$update_count = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3111
		foreach ( $this->update_results as $type => $updates )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3112
			$update_count += count( $updates );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3113
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3114
		$body = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3115
		$failures = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3116
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3117
		$body[] = sprintf( __( 'WordPress site: %s' ), network_home_url( '/' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3119
		// Core
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3120
		if ( isset( $this->update_results['core'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3121
			$result = $this->update_results['core'][0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3122
			if ( $result->result && ! is_wp_error( $result->result ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3123
				$body[] = sprintf( __( 'SUCCESS: WordPress was successfully updated to %s' ), $result->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3124
			} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3125
				$body[] = sprintf( __( 'FAILED: WordPress failed to update to %s' ), $result->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3126
				$failures++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3127
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3128
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3129
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3131
		// Plugins, Themes, Translations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3132
		foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3133
			if ( ! isset( $this->update_results[ $type ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3134
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3135
			$success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3136
			if ( $success_items ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3137
				$messages = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3138
					'plugin'      => __( 'The following plugins were successfully updated:' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3139
					'theme'       => __( 'The following themes were successfully updated:' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3140
					'translation' => __( 'The following translations were successfully updated:' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3141
				);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3142
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3143
				$body[] = $messages[ $type ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3144
				foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3145
					$body[] = ' * ' . sprintf( __( 'SUCCESS: %s' ), $name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3146
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3147
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3148
			if ( $success_items != $this->update_results[ $type ] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3149
				// Failed updates
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3150
				$messages = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3151
					'plugin'      => __( 'The following plugins failed to update:' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3152
					'theme'       => __( 'The following themes failed to update:' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3153
					'translation' => __( 'The following translations failed to update:' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3154
				);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3155
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3156
				$body[] = $messages[ $type ];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3157
				foreach ( $this->update_results[ $type ] as $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3158
					if ( ! $item->result || is_wp_error( $item->result ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3159
						$body[] = ' * ' . sprintf( __( 'FAILED: %s' ), $item->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3160
						$failures++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3161
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3162
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3163
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3164
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3165
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3166
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3167
		$site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3168
		if ( $failures ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3169
			$body[] = trim( __(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3170
"BETA TESTING?
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3171
=============
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3172
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3173
This debugging email is sent when you are using a development version of WordPress.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3174
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3175
If you think these failures might be due to a bug in WordPress, could you report it?
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3176
 * Open a thread in the support forums: https://wordpress.org/support/forum/alphabeta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3177
 * Or, if you're comfortable writing a bug report: https://core.trac.wordpress.org/
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3178
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3179
Thanks! -- The WordPress Team" ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3180
			$body[] = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3181
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3182
			$subject = sprintf( __( '[%s] There were failures during background updates' ), $site_title );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3183
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3184
			$subject = sprintf( __( '[%s] Background updates have finished' ), $site_title );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3185
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3186
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3187
		$body[] = trim( __(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3188
'UPDATE LOG
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3189
==========' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3190
		$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3192
		foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3193
			if ( ! isset( $this->update_results[ $type ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3194
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3195
			foreach ( $this->update_results[ $type ] as $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3196
				$body[] = $update->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3197
				$body[] = str_repeat( '-', strlen( $update->name ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3198
				foreach ( $update->messages as $message )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3199
					$body[] = "  " . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3200
				if ( is_wp_error( $update->result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3201
					$results = array( 'update' => $update->result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3202
					// If we rolled back, we want to know an error that occurred then too.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3203
					if ( 'rollback_was_required' === $update->result->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3204
						$results = (array) $update->result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3205
					foreach ( $results as $result_type => $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3206
						if ( ! is_wp_error( $result ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3207
							continue;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3208
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3209
						if ( 'rollback' === $result_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3210
							/* translators: 1: Error code, 2: Error message. */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3211
							$body[] = '  ' . sprintf( __( 'Rollback Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3212
						} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3213
							/* translators: 1: Error code, 2: Error message. */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3214
							$body[] = '  ' . sprintf( __( 'Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3215
						}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3216
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3217
						if ( $result->get_error_data() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3218
							$body[] = '         ' . implode( ', ', (array) $result->get_error_data() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3219
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3220
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3221
				$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3222
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3223
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3224
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3225
		$email = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3226
			'to'      => get_site_option( 'admin_email' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3227
			'subject' => $subject,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3228
			'body'    => implode( "\n", $body ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3229
			'headers' => ''
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3230
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3231
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3232
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3233
		 * Filter the debug email that can be sent following an automatic
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3234
		 * background core update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3235
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3236
		 * @since 3.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3237
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3238
		 * @param array $email {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3239
		 *     Array of email arguments that will be passed to wp_mail().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3240
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3241
		 *     @type string $to      The email recipient. An array of emails
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3242
		 *                           can be returned, as handled by wp_mail().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3243
		 *     @type string $subject Email subject.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3244
		 *     @type string $body    Email message body.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3245
		 *     @type string $headers Any email headers. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3246
		 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3247
		 * @param int   $failures The number of failures encountered while upgrading.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3248
		 * @param mixed $results  The results of all attempted updates.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3249
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3250
		$email = apply_filters( 'automatic_updates_debug_email', $email, $failures, $this->update_results );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3251
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3252
		wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3253
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3254
}