wp/wp-admin/includes/class-wp-upgrader.php
author ymh <ymh.work@gmail.com>
Wed, 06 Nov 2013 03:21:17 +0000
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
permissions -rw-r--r--
first import
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
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @link http://trac.wordpress.org/ticket/7875 consolidate plugin/theme/core upgrade/install functions
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 {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	var $strings = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	var $skin = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	var $result = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	function __construct($skin = null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
		if ( null == $skin )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
			$this->skin = new WP_Upgrader_Skin();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
			$this->skin = $skin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	function init() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		$this->skin->set_upgrader($this);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		$this->generic_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	function generic_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		$this->strings['bad_request'] = __('Invalid Data provided.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		$this->strings['fs_unavailable'] = __('Could not access filesystem.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
		$this->strings['fs_error'] = __('Filesystem error.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		$this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		$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
    46
		$this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		$this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		/* translators: %s: directory name */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		$this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		$this->strings['download_failed'] = __('Download failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
		$this->strings['installing_package'] = __('Installing the latest version&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		$this->strings['no_files'] = __('The package contains no files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		$this->strings['folder_exists'] = __('Destination folder already exists.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		$this->strings['mkdir_failed'] = __('Could not create directory.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		$this->strings['incompatible_archive'] = __('The package could not be installed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		$this->strings['maintenance_start'] = __('Enabling Maintenance mode&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		$this->strings['maintenance_end'] = __('Disabling Maintenance mode&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	function fs_connect( $directories = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		if ( false === ($credentials = $this->skin->request_filesystem_credentials()) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		if ( ! WP_Filesystem($credentials) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
			$error = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
			if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
				$error = $wp_filesystem->errors;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			$this->skin->request_filesystem_credentials($error); //Failed to connect, Error and request again
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		if ( ! is_object($wp_filesystem) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
			return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
			return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		foreach ( (array)$directories as $dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			switch ( $dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
				case ABSPATH:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
					if ( ! $wp_filesystem->abspath() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
						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
    87
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
				case WP_CONTENT_DIR:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
					if ( ! $wp_filesystem->wp_content_dir() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
						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
    91
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
				case WP_PLUGIN_DIR:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
					if ( ! $wp_filesystem->wp_plugins_dir() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
						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
    95
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
				case get_theme_root():
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
					if ( ! $wp_filesystem->wp_themes_dir() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
						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
    99
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
				default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
					if ( ! $wp_filesystem->find_folder($dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
						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
   103
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	} //end fs_connect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	function download_package($package) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		 * Filter whether to return the package.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		 * @param bool    $reply   Whether to bail without returning the package. Default is false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		 * @param string  $package The package file name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		 * @param object  $this    The WP_Upgrader instance.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		$reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		if ( false !== $reply )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
			return $reply;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		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
   125
			return $package; //must be a local file..
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		if ( empty($package) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			return new WP_Error('no_package', $this->strings['no_package']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
		$this->skin->feedback('downloading_package', $package);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
		$download_file = download_url($package);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		if ( is_wp_error($download_file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			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
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		return $download_file;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
	function unpack_package($package, $delete_package = true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		$this->skin->feedback('unpack_package');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		$upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		//Clean up contents of upgrade directory beforehand.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		$upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		if ( !empty($upgrade_files) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
			foreach ( $upgrade_files as $file )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
				$wp_filesystem->delete($upgrade_folder . $file['name'], true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		//We need a working directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		$working_dir = $upgrade_folder . basename($package, '.zip');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
		// Clean up working directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		if ( $wp_filesystem->is_dir($working_dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
			$wp_filesystem->delete($working_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		// Unzip package to working directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
		$result = unzip_file( $package, $working_dir );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		// Once extracted, delete the package if required.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
		if ( $delete_package )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
			unlink($package);
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_wp_error($result) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
			$wp_filesystem->delete($working_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
			if ( 'incompatible_archive' == $result->get_error_code() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
				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
   172
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		return $working_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	function install_package( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		global $wp_filesystem, $wp_theme_directories;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
			'source' => '', // Please always pass this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
			'destination' => '', // and this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
			'clear_destination' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
			'clear_working' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
			'abort_if_destination_exists' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
			'hook_extra' => array()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
		extract($args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
		@set_time_limit( 300 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		if ( empty($source) || empty($destination) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
			return new WP_Error('bad_request', $this->strings['bad_request']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
		$this->skin->feedback('installing_package');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
		$res = apply_filters('upgrader_pre_install', true, $hook_extra);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		if ( is_wp_error($res) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
			return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
		//Retain the Original source and destinations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		$remote_source = $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		$local_destination = $destination;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
		$source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		$remote_destination = $wp_filesystem->find_folder($local_destination);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		//Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
			$source = trailingslashit($source) . trailingslashit($source_files[0]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		elseif ( count($source_files) == 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
			return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		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.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
			$source = trailingslashit($source);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
		//Hook ability to change the source file location..
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		$source = apply_filters('upgrader_source_selection', $source, $remote_source, $this);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
		if ( is_wp_error($source) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		//Has the source location changed? If so, we need a new source_files list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		if ( $source !== $remote_source )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			$source_files = array_keys( $wp_filesystem->dirlist($source) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
		// Protection against deleting files in any important base directories.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
		// Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the destination directory (WP_PLUGIN_DIR / wp-content/themes)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
		// intending to copy the directory into the directory, whilst they pass the source as the actual files to copy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
		$protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		if ( is_array( $wp_theme_directories ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
			$protected_directories = array_merge( $protected_directories, $wp_theme_directories );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
		if ( in_array( $destination, $protected_directories ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
			$remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			$destination = trailingslashit($destination) . trailingslashit(basename($source));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
		if ( $clear_destination ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
			//We're going to clear the destination if there's something there
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
			$this->skin->feedback('remove_old');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
			$removed = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
			if ( $wp_filesystem->exists($remote_destination) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
				$removed = $wp_filesystem->delete($remote_destination, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
			$removed = apply_filters('upgrader_clear_destination', $removed, $local_destination, $remote_destination, $hook_extra);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
			if ( is_wp_error($removed) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
				return $removed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
			else if ( ! $removed )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
				return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		} elseif ( $abort_if_destination_exists && $wp_filesystem->exists($remote_destination) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
			//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
   254
			//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
   255
			$_files = $wp_filesystem->dirlist($remote_destination);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
			if ( ! empty($_files) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
				$wp_filesystem->delete($remote_source, true); //Clear out the source files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
				return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
		//Create destination if needed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
		if ( !$wp_filesystem->exists($remote_destination) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
			if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
				return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		// Copy new version of item into place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
		$result = copy_dir($source, $remote_destination);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
		if ( is_wp_error($result) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
			if ( $clear_working )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
				$wp_filesystem->delete($remote_source, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
		//Clear the Working folder?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
		if ( $clear_working )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
			$wp_filesystem->delete($remote_source, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
		$destination_name = basename( str_replace($local_destination, '', $destination) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
		if ( '.' == $destination_name )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
			$destination_name = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
		$this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
		$res = apply_filters('upgrader_post_install', true, $hook_extra, $this->result);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
		if ( is_wp_error($res) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
			$this->result = $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
			return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
		//Bombard the calling function will all the info which we've just used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
		return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	function run($options) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
			'package' => '', // Please always pass this.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
			'destination' => '', // And this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
			'clear_destination' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
			'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
   302
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
			'is_multi' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
			'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
   305
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		$options = wp_parse_args($options, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		extract($options);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
		if ( ! $is_multi ) // call $this->header separately if running multiple times
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
			$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		$res = $this->fs_connect( array(WP_CONTENT_DIR, $destination) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		// Mainly for non-connected filesystem.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
			if ( ! $is_multi )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
				$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
		if ( is_wp_error($res) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			$this->skin->error($res);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
			if ( ! $is_multi )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
				$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
			return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
		//Download the package (Note, This just returns the filename of the file if the package is a local file)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
		$download = $this->download_package( $package );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		if ( is_wp_error($download) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
			$this->skin->error($download);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
			if ( ! $is_multi )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
				$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
			return $download;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		$delete_package = ($download != $package); // Do not delete a "local" file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
		//Unzips the file into a temporary directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
		$working_dir = $this->unpack_package( $download, $delete_package );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
		if ( is_wp_error($working_dir) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
			$this->skin->error($working_dir);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
			if ( ! $is_multi )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
				$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
			return $working_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
		//With the given options, this installs it to the destination directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		$result = $this->install_package( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
			'source' => $working_dir,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
			'destination' => $destination,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
			'clear_destination' => $clear_destination,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
			'abort_if_destination_exists' => $abort_if_destination_exists,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
			'clear_working' => $clear_working,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
			'hook_extra' => $hook_extra
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		$this->skin->set_result($result);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
		if ( is_wp_error($result) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
			$this->skin->error($result);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
			$this->skin->feedback('process_failed');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
			//Install Succeeded
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
			$this->skin->feedback('process_success');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
		$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
		if ( ! $is_multi ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
			do_action( 'upgrader_process_complete', $this, $hook_extra );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	function maintenance_mode($enable = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
		$file = $wp_filesystem->abspath() . '.maintenance';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		if ( $enable ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
			$this->skin->feedback('maintenance_start');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
			// Create maintenance file to signal that we are upgrading
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
			$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
			$wp_filesystem->delete($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
			$wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		} else if ( !$enable && $wp_filesystem->exists($file) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
			$this->skin->feedback('maintenance_end');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
			$wp_filesystem->delete($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
 * 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
   402
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
class Plugin_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	var $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
	var $bulk = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	var $show_before = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	function upgrade_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
		$this->strings['up_to_date'] = __('The plugin is at the latest version.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
		$this->strings['no_package'] = __('Update package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
		$this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
		$this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
		$this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
		$this->strings['process_failed'] = __('Plugin update failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
		$this->strings['process_success'] = __('Plugin updated successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
	function install_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
		$this->strings['no_package'] = __('Install package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		$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
   427
		$this->strings['unpack_package'] = __('Unpacking the package&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
		$this->strings['installing_package'] = __('Installing the plugin&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
		$this->strings['no_files'] = __('The plugin contains no files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
		$this->strings['process_failed'] = __('Plugin install failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		$this->strings['process_success'] = __('Plugin installed successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
	function install( $package, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
		$this->install_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
		add_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
			'package' => $package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
			'destination' => WP_PLUGIN_DIR,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
			'clear_destination' => false, // Do not overwrite files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
				'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
				'action' => 'install',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
			)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
		remove_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	function upgrade( $plugin, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		$current = get_site_transient( 'update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
		if ( !isset( $current->response[ $plugin ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
			$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
			$this->skin->set_result(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
			$this->skin->error('up_to_date');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
		// Get the URL to the zip file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
		$r = $current->response[ $plugin ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
		add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
		add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		//'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
   493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
			'package' => $r->package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
			'destination' => WP_PLUGIN_DIR,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
			'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
				'plugin' => $plugin,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
				'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
				'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
		// Cleanup our hooks, in case something else does a upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
		remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
		// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
	function bulk_upgrade( $plugins, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
		$this->bulk = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
		$current = get_site_transient( 'update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
		add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
		$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
		$res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
		$this->skin->bulk_header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
		// Only start maintenance mode if:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		// - running Multisite and there are one or more plugins specified, OR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
		// - a plugin with an update available is currently active.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		// @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
   549
		$maintenance = ( is_multisite() && ! empty( $plugins ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
		foreach ( $plugins as $plugin )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
			$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		if ( $maintenance )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
			$this->maintenance_mode(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
		$results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
		$this->update_count = count($plugins);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
		$this->update_current = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		foreach ( $plugins as $plugin ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
			$this->update_current++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
			$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
			if ( !isset( $current->response[ $plugin ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
				$this->skin->set_result(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
				$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
				$this->skin->feedback('up_to_date');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
				$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
				$results[$plugin] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
			// Get the URL to the zip file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
			$r = $current->response[ $plugin ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
			$this->skin->plugin_active = is_plugin_active($plugin);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
			$result = $this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
				'package' => $r->package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
				'destination' => WP_PLUGIN_DIR,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
				'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
				'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
				'is_multi' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
				'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
					'plugin' => $plugin
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
				)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
			) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
			$results[$plugin] = $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
			// Prevent credentials auth screen from displaying multiple times
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
			if ( false === $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
		} //end foreach $plugins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
		$this->maintenance_mode(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
		do_action( 'upgrader_process_complete', $this, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
			'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
			'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
			'bulk' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
			'plugins' => $plugins,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
		$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
		$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
		// Cleanup our hooks, in case something else does a upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
		// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
		return $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
	function check_package($source) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
		if ( is_wp_error($source) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
		if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, lets not prevent installation.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
		// Check the folder contains at least 1 valid plugin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
		$plugins_found = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
		foreach ( glob( $working_directory . '*.php' ) as $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
			$info = get_plugin_data($file, false, false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
			if ( !empty( $info['Name'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
				$plugins_found = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
		if ( ! $plugins_found )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
			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
   639
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
		return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
	//return plugin info.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
	function plugin_info() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
		if ( ! is_array($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
		if ( empty($this->result['destination_name']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
		$plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
		if ( empty($plugin) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
		$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
   655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
		return $this->result['destination_name'] . '/' . $pluginfiles[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
	//Hooked to pre_install
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	function deactivate_plugin_before_upgrade($return, $plugin) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
		if ( is_wp_error($return) ) //Bypass.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
		// 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
   666
		if ( defined( 'DOING_CRON' ) && DOING_CRON )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
		$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
		if ( empty($plugin) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
			return new WP_Error('bad_request', $this->strings['bad_request']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
		if ( is_plugin_active($plugin) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
			//Deactivate the plugin silently, Prevent deactivation hooks from running.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
			deactivate_plugins($plugin, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	//Hooked to upgrade_clear_destination
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
	function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
		if ( is_wp_error($removed) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
			return $removed; //Pass errors through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
		$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
		if ( empty($plugin) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
			return new WP_Error('bad_request', $this->strings['bad_request']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
		$plugins_dir = $wp_filesystem->wp_plugins_dir();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
		$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
		if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
			return $removed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
		// If plugin is in its own directory, recursively delete the directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		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
   698
			$deleted = $wp_filesystem->delete($this_plugin_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
			$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
		if ( ! $deleted )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
			return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
 * 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
   711
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
class Theme_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
	var $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
	var $bulk = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
	function upgrade_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
		$this->strings['up_to_date'] = __('The theme is at the latest version.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
		$this->strings['no_package'] = __('Update package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
		$this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
		$this->strings['remove_old'] = __('Removing the old version of the theme&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
		$this->strings['remove_old_failed'] = __('Could not remove the old theme.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
		$this->strings['process_failed'] = __('Theme update failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
		$this->strings['process_success'] = __('Theme updated successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
	function install_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		$this->strings['no_package'] = __('Install package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		$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
   735
		$this->strings['unpack_package'] = __('Unpacking the package&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
		$this->strings['installing_package'] = __('Installing the theme&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
		$this->strings['no_files'] = __('The theme contains no files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
		$this->strings['process_failed'] = __('Theme install failed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
		$this->strings['process_success'] = __('Theme installed successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
		$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
   742
		$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
   743
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
		$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
   745
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
		$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
   747
		/* translators: 1: theme name, 2: version */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
		$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
   749
		$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
   750
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	function check_parent_theme_filter($install_result, $hook_extra, $child_result) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
		// Check to see if we need to install a parent theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
		$theme_info = $this->theme_info();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
		if ( ! $theme_info->parent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
			return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
		$this->skin->feedback( 'parent_theme_search' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
		if ( ! $theme_info->parent()->errors() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
			$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
   763
			// We already have the theme, fall through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
			return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
		// We don't have the parent theme, lets install it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
		$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
   769
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
		if ( ! $api || is_wp_error($api) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
			$this->skin->feedback( 'parent_theme_not_found', $theme_info->get('Template') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
			// Don't show activate or preview actions after install
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
			add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
			return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
		// Backup required data we're going to override:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
		$child_api = $this->skin->api;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
		$child_success_message = $this->strings['process_success'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
		// Override them
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
		$this->skin->api = $api;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
		$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
   784
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
		$this->skin->feedback('parent_theme_prepare_install', $api->name, $api->version);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
		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
   788
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		// Install the parent theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
		$parent_result = $this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
			'package' => $api->download_link,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
			'destination' => get_theme_root(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
			'clear_destination' => false, //Do not overwrite files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
			'clear_working' => true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
		if ( is_wp_error($parent_result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
			add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
		// Start cleaning up after the parents installation
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		remove_filter('install_theme_complete_actions', '__return_false', 999);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
		// Reset child's result and data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		$this->result = $child_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
		$this->skin->api = $child_api;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
		$this->strings['process_success'] = $child_success_message;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
		return $install_result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
	function hide_activate_preview_actions($actions) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
		unset($actions['activate'], $actions['preview']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
		return $actions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
	function install( $package, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
			'clear_update_cache' => 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
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
		$this->install_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
		add_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
		add_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'), 10, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
			'package' => $package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
			'destination' => get_theme_root(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
			'clear_destination' => false, //Do not overwrite files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
				'type' => 'theme',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
				'action' => 'install',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
		remove_filter('upgrader_source_selection', array($this, 'check_package') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
		remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
		// Refresh the Theme Update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
	function upgrade( $theme, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
		// Is an update available?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
		$current = get_site_transient( 'update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
		if ( !isset( $current->response[ $theme ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
			$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
			$this->skin->set_result(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
			$this->skin->error('up_to_date');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
		$r = $current->response[ $theme ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
		add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
		add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
		add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
		$this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
			'package' => $r['package'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
			'destination' => get_theme_root( $theme ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
			'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
			'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
			'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
				'theme' => $theme,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
				'type' => 'theme',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
				'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
		remove_filter('upgrader_pre_install', array($this, 'current_before'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
		remove_filter('upgrader_post_install', array($this, 'current_after'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
		if ( ! $this->result || is_wp_error($this->result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
			return $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
	function bulk_upgrade( $themes, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
		$this->bulk = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
		$current = get_site_transient( 'update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
		add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
		add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
		add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
		$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
		$res = $this->fs_connect( array(WP_CONTENT_DIR) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
		$this->skin->bulk_header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
		// Only start maintenance mode if:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
		// - running Multisite and there are one or more themes specified, OR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
		// - a theme with an update available is currently in use.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
		// @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
   934
		$maintenance = ( is_multisite() && ! empty( $themes ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
		foreach ( $themes as $theme )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
			$maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
		if ( $maintenance )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
			$this->maintenance_mode(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
		$results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
		$this->update_count = count($themes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
		$this->update_current = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
		foreach ( $themes as $theme ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
			$this->update_current++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
			$this->skin->theme_info = $this->theme_info($theme);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
			if ( !isset( $current->response[ $theme ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
				$this->skin->set_result(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
				$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
				$this->skin->feedback('up_to_date');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
				$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
				$results[$theme] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
			// Get the URL to the zip file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
			$r = $current->response[ $theme ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
			$result = $this->run( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
				'package' => $r['package'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
				'destination' => get_theme_root( $theme ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
				'clear_destination' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
				'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
				'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
					'theme' => $theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
				),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
			) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
			$results[$theme] = $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
			// Prevent credentials auth screen from displaying multiple times
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
			if ( false === $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
		} //end foreach $plugins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
		$this->maintenance_mode(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
		do_action( 'upgrader_process_complete', $this, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
			'action' => 'update',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
			'type' => 'plugin',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
			'bulk' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
			'themes' => $themes,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
		$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
		$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
		// Cleanup our hooks, in case something else does a upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
		remove_filter('upgrader_pre_install', array($this, 'current_before'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
		remove_filter('upgrader_post_install', array($this, 'current_after'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
		remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
		// Refresh the Theme Update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
		return $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
	function check_package($source) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
		if ( is_wp_error($source) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
		// Check the folder contains a valid theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
		if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, lets not prevent installation.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
		// A proper archive should have a style.css file in the single subdirectory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
		if ( ! file_exists( $working_directory . 'style.css' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
			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
  1016
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
		$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
  1018
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		if ( empty( $info['Name'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
			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
  1021
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		// 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
  1023
		if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
			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
  1025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
		return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
	function current_before($return, $theme) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
		if ( is_wp_error($return) )
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
		$theme = isset($theme['theme']) ? $theme['theme'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
		if ( $theme != get_stylesheet() ) //If not current
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
		//Change to maintenance mode now.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
		if ( ! $this->bulk )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
			$this->maintenance_mode(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	function current_after($return, $theme) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		if ( is_wp_error($return) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
		$theme = isset($theme['theme']) ? $theme['theme'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
		if ( $theme != get_stylesheet() ) // If not current
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
		// Ensure stylesheet name hasn't changed after the upgrade:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
		if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
			wp_clean_themes_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
			$stylesheet = $this->result['destination_name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
			switch_theme( $stylesheet );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
		//Time to remove maintenance mode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
		if ( ! $this->bulk )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
			$this->maintenance_mode(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
	function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
		global $wp_filesystem;
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 ( is_wp_error( $removed ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
			return $removed; // Pass errors through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
		if ( ! isset( $theme['theme'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
			return $removed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
		$theme = $theme['theme'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
		$themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
		if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
			if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
				return false;
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
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
	function theme_info($theme = null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
		if ( empty($theme) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
			if ( !empty($this->result['destination_name']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
				$theme = $this->result['destination_name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
		return wp_get_theme( $theme );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
 * Language pack upgrader, for updating translations of plugins, themes, and core.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
class Language_Pack_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
	var $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
	var $bulk = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
	static function async_upgrade( $upgrader = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
		// Avoid recursion.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
		if ( $upgrader && $upgrader instanceof Language_Pack_Upgrader )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
		// Nothing to do?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
		$language_updates = wp_get_translation_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
		if ( ! $language_updates )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
		$skin = new Language_Pack_Upgrader_Skin( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
			'skip_header_footer' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
		$lp_upgrader = new Language_Pack_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
		$lp_upgrader->upgrade();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
	function upgrade_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
		$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
  1133
		$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
  1134
		$this->strings['no_package'] = __( 'Update package not available.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
		$this->strings['downloading_package'] = __( 'Downloading translation from <span class="code">%s</span>&#8230;' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
		$this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
		$this->strings['process_failed'] = __( 'Translation update failed.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
		$this->strings['process_success'] = __( 'Translation updated successfully.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
	function upgrade( $update = false, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
		if ( $update )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
			$update = array( $update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
		$results = $this->bulk_upgrade( $update, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		return $results[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
	function bulk_upgrade( $language_updates = array(), $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
			'clear_update_cache' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
		if ( ! $language_updates )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
			$language_updates = wp_get_translation_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
		if ( empty( $language_updates ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
			$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
			$this->skin->before();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
			$this->skin->set_result( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
			$this->skin->feedback( 'up_to_date' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
			$this->skin->after();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
			$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
		if ( 'upgrader_process_complete' == current_filter() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
			$this->skin->feedback( 'starting_upgrade' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
		add_filter( 'upgrader_source_selection', array( &$this, 'check_package' ), 10, 3 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
		$this->skin->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
		// Connect to the Filesystem first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
		$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
		if ( ! $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
			$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
		$results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
		$this->update_count = count( $language_updates );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
		$this->update_current = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
		// The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
		// as we then may need to create a /plugins or /themes directory inside of it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
		$remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
		if ( ! $wp_filesystem->exists( $remote_destination ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
			if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
				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
  1198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
		foreach ( $language_updates as $language_update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
			$this->skin->language_update = $language_update;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
			$destination = WP_LANG_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
			if ( 'plugin' == $language_update->type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
				$destination .= '/plugins';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
			elseif ( 'theme' == $language_update->type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
				$destination .= '/themes';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
			$this->update_current++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
			$options = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
				'package' => $language_update->package,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
				'destination' => $destination,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
				'clear_destination' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
				'abort_if_destination_exists' => false, // We expect the destination to exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
				'clear_working' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
				'is_multi' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
				'hook_extra' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
					'language_update_type' => $language_update->type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
					'language_update' => $language_update,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
				)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
			$result = $this->run( $options );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
			$results[] = $this->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
			// Prevent credentials auth screen from displaying multiple times.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
			if ( false === $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
		$this->skin->bulk_footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
		$this->skin->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
		// Clean up our hooks, in case something else does an upgrade on this connection.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
		remove_filter( 'upgrader_source_selection', array( &$this, 'check_package' ), 10, 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
		if ( $parsed_args['clear_update_cache'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
			wp_clean_themes_cache( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
			wp_clean_plugins_cache( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
			delete_site_transient( 'update_core' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
		return $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
	function check_package( $source, $remote_source ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
		global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
		if ( is_wp_error( $source ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
			return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
		// Check that the folder contains a valid language.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
		$files = $wp_filesystem->dirlist( $remote_source );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
		// Check to see if a .po and .mo exist in the folder.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
		$po = $mo = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
		foreach ( (array) $files as $file => $filedata ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
			if ( '.po' == substr( $file, -3 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
				$po = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
			elseif ( '.mo' == substr( $file, -3 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
				$mo = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
		if ( ! $mo || ! $po )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
			return new WP_Error( 'incompatible_archive_pomo', $this->strings['incompatible_archive'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
				__( '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
  1270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
		return $source;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
	function get_name_for_update( $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
		switch ( $update->type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
			case 'core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
				return 'WordPress'; // Not translated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
			case 'theme':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
				$theme = wp_get_theme( $update->slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
				if ( $theme->exists() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
					return $theme->Get( 'Name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
			case 'plugin':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
				$plugin_data = get_plugins( '/' . $update->slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
				$plugin_data = array_shift( $plugin_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
				if ( $plugin_data )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
					return $plugin_data['Name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
 * 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
  1298
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
class Core_Upgrader extends WP_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
	function upgrade_strings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
		$this->strings['up_to_date'] = __('WordPress is at the latest version.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
		$this->strings['no_package'] = __('Update package not available.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
		$this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
		$this->strings['copy_failed'] = __('Could not copy files.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
		$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
  1312
		$this->strings['start_rollback'] = __( 'Attempting to roll back to previous version.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
		$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
  1314
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
	function upgrade( $current, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
		global $wp_filesystem, $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
		$start_time = time();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
			'pre_check_md5'    => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
			'attempt_rollback' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
			'do_rollback'      => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
		$parsed_args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
		$this->init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
		$this->upgrade_strings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
		// Is an update available?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
		if ( !isset( $current->response ) || $current->response == 'latest' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
			return new WP_Error('up_to_date', $this->strings['up_to_date']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
		$res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
		if ( is_wp_error($res) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
			return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
		$wp_dir = trailingslashit($wp_filesystem->abspath());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
		$partial = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
		if ( $parsed_args['do_rollback'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
			$partial = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
		elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
			$partial = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
		// If partial update is returned from the API, use that, unless we're doing a reinstall.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
		// If we cross the new_bundled version number, then use the new_bundled zip.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1349
		// Don't though if the constant is set to skip bundled items.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1350
		// If the API returns a no_content zip, go with it. Finally, default to the full zip.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
		if ( $parsed_args['do_rollback'] && $current->packages->rollback )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
			$to_download = 'rollback';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
		elseif ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && $partial )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
			$to_download = 'partial';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
		elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
			&& ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
			$to_download = 'new_bundled';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
		elseif ( $current->packages->no_content )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
			$to_download = 'no_content';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
			$to_download = 'full';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
		$download = $this->download_package( $current->packages->$to_download );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
		if ( is_wp_error($download) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
			return $download;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
		$working_dir = $this->unpack_package( $download );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
		if ( is_wp_error($working_dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
			return $working_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
		// Copy update-core.php from the new version into place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		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
  1373
			$wp_filesystem->delete($working_dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
			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
  1375
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
		$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
  1377
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
		require_once( ABSPATH . 'wp-admin/includes/update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
		if ( ! function_exists( 'update_core' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
			return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
		$result = update_core( $working_dir, $wp_dir );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
		// In the event of an issue, we may be able to roll back.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
		if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
			$try_rollback = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
			if ( is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
				$error_code = $result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
				// Not all errors are equal. These codes are critical: copy_failed__copy_dir,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
				// mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
				// do_rollback allows for update_core() to trigger a rollback if needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
				if ( false !== strpos( $error_code, 'do_rollback' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
					$try_rollback = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
				elseif ( false !== strpos( $error_code, '__copy_dir' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
					$try_rollback = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
				elseif ( 'disk_full' === $error_code )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
					$try_rollback = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
			if ( $try_rollback ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
				apply_filters( 'update_feedback', $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
				apply_filters( 'update_feedback', $this->strings['start_rollback'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
				$rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
				$original_result = $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
				$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
  1409
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
		do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
		// Clear the current updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
		delete_site_transient( 'update_core' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
		if ( ! $parsed_args['do_rollback'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
			$stats = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
				'update_type'      => $current->response,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
				'success'          => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
				'fs_method'        => $wp_filesystem->method,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
				'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
				'time_taken'       => time() - $start_time,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
				'attempted'        => $current->version,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
			if ( is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
				$stats['success'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
				// Did a rollback occur?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
				if ( ! empty( $try_rollback ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
					$stats['error_code'] = $original_result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
					$stats['error_data'] = $original_result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
					// Was the rollback successful? If not, collect its error too.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
					$stats['rollback'] = ! is_wp_error( $rollback_result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
					if ( is_wp_error( $rollback_result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
						$stats['rollback_code'] = $rollback_result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
						$stats['rollback_data'] = $rollback_result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
					$stats['error_code'] = $result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
					$stats['error_data'] = $result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
			wp_version_check( $stats );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
		return $result;
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
	// Determines if this WordPress Core version should update to $offered_ver or not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
	static function should_update_to_version( $offered_ver /* x.y.z */ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
		include ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
		$current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version  ), 0, 2 ) ); // x.y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
		$new_branch     = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
		$current_is_development_version = (bool) strpos( $wp_version, '-' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
		// Defaults:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
		$upgrade_dev   = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
		$upgrade_minor = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
		$upgrade_major = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
		// WP_AUTO_UPDATE_CORE = true (all), 'minor', false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
		if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
			if ( false === WP_AUTO_UPDATE_CORE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
				// Defaults to turned off, unless a filter allows it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1468
				$upgrade_dev = $upgrade_minor = $upgrade_major = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
			} elseif ( true === WP_AUTO_UPDATE_CORE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
				// ALL updates for core
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
				$upgrade_dev = $upgrade_minor = $upgrade_major = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
			} elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
				// Only minor updates for core
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
				$upgrade_dev = $upgrade_major = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
				$upgrade_minor = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
		// 1: If we're already on that version, not much point in updating?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
		if ( $offered_ver == $wp_version )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
		// 2: If we're running a newer version, that's a nope
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
		if ( version_compare( $wp_version, $offered_ver, '>' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
		$failure_data = get_site_option( 'auto_core_update_failed' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
		if ( $failure_data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
			// If this was a critical update failure, cannot update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
			if ( ! empty( $failure_data['critical'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
			// 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
  1494
			if ( $wp_version == $failure_data['current'] && false !== strpos( $offered_ver, '.1.next.minor' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
			// 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
  1498
			// Some non-critical failures do allow retries, like download_failed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
			// 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
  1500
			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
  1501
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
		// 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
  1505
		if ( $current_is_development_version ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
			if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
			// else fall through to minor + major branches below
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
		// 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
		if ( $current_branch == $new_branch )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
			return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
		// 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
		if ( version_compare( $new_branch, $current_branch, '>' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
			return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
		// If we're not sure, we don't want it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
	function check_files() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
		global $wp_version, $wp_local_package;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
		$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
  1527
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
		if ( ! is_array( $checksums ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
		foreach ( $checksums as $file => $checksum ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
			// Skip files which get updated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
			if ( 'wp-content' == substr( $file, 0, 10 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
			if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
 * 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
  1545
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
class File_Upload_Upgrader {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
	var $package;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
	var $filename;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
	var $id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
	function __construct($form, $urlholder) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
		if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
			wp_die(__('Please select a file'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
		//Handle a newly uploaded file, Else assume it's already been uploaded
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
		if ( ! empty($_FILES) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
			$overrides = array( 'test_form' => false, 'test_type' => false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
			$file = wp_handle_upload( $_FILES[$form], $overrides );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
			if ( isset( $file['error'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
				wp_die( $file['error'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
			$this->filename = $_FILES[$form]['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
			$this->package = $file['file'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
			// Construct the object array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
			$object = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
				'post_title' => $this->filename,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
				'post_content' => $file['url'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
				'post_mime_type' => $file['type'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
				'guid' => $file['url'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
				'context' => 'upgrader',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
				'post_status' => 'private'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
			// Save the data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
			$this->id = wp_insert_attachment( $object, $file['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
			// schedule a cleanup for 2 hours from now in case of failed install
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
			wp_schedule_single_event( time() + 7200, 'upgrader_scheduled_cleanup', array( $this->id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
		} elseif ( is_numeric( $_GET[$urlholder] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
			// Numeric Package = previously uploaded file, see above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
			$this->id = (int) $_GET[$urlholder];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
			$attachment = get_post( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
			if ( empty($attachment) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
				wp_die(__('Please select a file'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
			$this->filename = $attachment->post_title;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
			$this->package = get_attached_file( $attachment->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1597
			// 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
  1598
			if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
				wp_die( $uploads['error'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
			$this->filename = $_GET[$urlholder];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
			$this->package = $uploads['basedir'] . '/' . $this->filename;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1605
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1606
	function cleanup() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1607
		if ( $this->id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
			wp_delete_attachment( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
		elseif ( file_exists( $this->package ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
			return @unlink( $this->package );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1613
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1614
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1616
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
 * The WordPress automatic background updater.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
 * @subpackage Upgrader
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
class WP_Automatic_Updater {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
	 * Tracks update results during processing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
	protected $update_results = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
	 * Whether the entire automatic updater is disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
	public function is_disabled() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
		// Background updates are disabled if you don't want file changes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
		if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
		if ( defined( 'WP_INSTALLING' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
		// 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
  1647
		$disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
		 * Filter whether to entirely disable background updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
		 * There are more fine-grained filters and controls for selective disabling.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
		 * This filter parallels the AUTOMATIC_UPDATER_DISABLED constant in name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
		 * This also disables update notification emails. That may change in the future.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
		 * @param bool $disabled Whether the updater should be disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
		return apply_filters( 'automatic_updater_disabled', $disabled );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
	 * Check for version control checkouts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
	 * Checks for Subversion, Git, Mercurial, and Bazaar. It recursively looks up the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1667
	 * 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
  1668
	 * checkout somewhere.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
	 * 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
  1671
	 * wp-content directory, for example). The underlying assumption is that if you are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
	 * using version control *anywhere*, then you should be making decisions for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
	 * how things get updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1674
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1675
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1676
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1677
	 * @param string $context The filesystem path to check, in addition to ABSPATH.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
	public function is_vcs_checkout( $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
		$context_dirs = array( untrailingslashit( $context ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
		if ( $context !== ABSPATH )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
			$context_dirs[] = untrailingslashit( ABSPATH );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
		$vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
		$check_dirs = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
		foreach ( $context_dirs as $context_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
			// Walk up from $context_dir to the root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
				$check_dirs[] = $context_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
				// 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
  1693
				if ( $context_dir == dirname( $context_dir ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
			// Continue one level at a time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
			} while ( $context_dir = dirname( $context_dir ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
		$check_dirs = array_unique( $check_dirs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
		// Search all directories we've found for evidence of version control.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
		foreach ( $vcs_dirs as $vcs_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
			foreach ( $check_dirs as $check_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
				if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
					break 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
		 * Filter whether the automatic updater should consider a filesystem location to be potentially
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
		 * managed by a version control system.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
		 * @param bool $checkout  Whether a VCS checkout was discovered at $context or ABSPATH, or anywhere higher.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
		 * @param string $context The filesystem context (a path) against which filesystem status should be checked.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
		return apply_filters( 'automatic_updates_is_vcs_checkout', $checkout, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1722
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1723
	 * Tests to see if we can and should update a specific item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1725
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1726
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1727
	 * @param string $type    The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
	 * @param object $item    The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1729
	 * @param string $context The filesystem context (a path) against which filesystem access and status
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1730
	 *                        should be checked.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1731
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1732
	public function should_update( $type, $item, $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1733
		// Used to see if WP_Filesystem is set up to allow unattended updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
		$skin = new Automatic_Upgrader_Skin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1736
		if ( $this->is_disabled() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1737
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
		// If we can't do an auto core update, we may still be able to email the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
		if ( ! $skin->request_filesystem_credentials( false, $context ) || $this->is_vcs_checkout( $context ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
			if ( 'core' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
				$this->send_core_update_notification_email( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1743
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
		// Next up, is this an item we can update?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1747
		if ( 'core' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
			$update = Core_Upgrader::should_update_to_version( $item->current );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
			$update = ! empty( $item->autoupdate );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
		 * Filter whether to automatically update core, a plugin, a theme, or a language.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
		 * The dynamic portion of the hook name, $type, refers to the type of update
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
		 * being checked. Can be 'core', 'theme', 'plugin', or 'translation'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1757
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1758
		 * Generally speaking, plugins, themes, and major core versions are not updated by default,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1759
		 * while translations and minor and development versions for core are updated by default.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1760
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1761
		 * See the filters allow_dev_auto_core_updates, allow_minor_auto_core_updates, and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
		 * allow_major_auto_core_updates more straightforward filters to adjust core updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
		 * @param bool   $update Whether to update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
		 * @param object $item   The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1769
		$update = apply_filters( 'auto_update_' . $type, $update, $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1771
		if ( ! $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
			if ( 'core' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
				$this->send_core_update_notification_email( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1777
		// If it's a core update, are we actually compatible with its requirements?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
		if ( 'core' == $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
			global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1781
			$php_compat = version_compare( phpversion(), $item->php_version, '>=' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1782
			if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
				$mysql_compat = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1784
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
				$mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1786
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
			if ( ! $php_compat || ! $mysql_compat )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1795
	 * Notifies an administrator of a core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1797
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1798
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1799
	 * @param object $item The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1800
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1801
	protected function send_core_update_notification_email( $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1802
		$notify   = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1803
		$notified = get_site_option( 'auto_core_update_notified' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1804
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1805
		// 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
  1806
		if ( $notified && $notified['email'] == get_site_option( 'admin_email' ) && $notified['version'] == $item->current )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1807
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
		// See if we need to notify users of a core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
		$notify = ! empty( $item->notify_email );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1812
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
		 * Whether to notify the site administrator of a new core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
		 * By default, administrators are notified when the update offer received from WordPress.org
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1816
		 * sets a particular flag. This allows for discretion in if and when to notify.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1818
		 * This filter only fires once per release -- if the same email address was already
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1819
		 * notified of the same new version, we won't repeatedly email the administrator.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
		 * This filter is also used on about.php to check if a plugin has disabled these notifications.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1823
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1825
		 * @param bool $notify Whether the site administrator is notified.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1826
		 * @param object $item The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1827
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
		if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1830
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1831
		$this->send_email( 'manual', $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1832
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1833
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1834
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1835
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1836
	 * Update an item, if appropriate.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1837
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1838
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1839
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1840
	 * @param string $type The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
	 * @param object $item The update offer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
	public function update( $type, $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1844
		$skin = new Automatic_Upgrader_Skin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1846
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
			case 'core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1848
				// 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
  1849
				add_filter( 'update_feedback', array( $skin, 'feedback' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1850
				$upgrader = new Core_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1851
				$context  = ABSPATH;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
			case 'plugin':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1854
				$upgrader = new Plugin_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1855
				$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
  1856
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1857
			case 'theme':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1858
				$upgrader = new Theme_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1859
				$context  = get_theme_root( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1860
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1861
			case 'translation':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
				$upgrader = new Language_Pack_Upgrader( $skin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
				$context  = WP_CONTENT_DIR; // WP_LANG_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
		// Determine whether we can and should perform this update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
		if ( ! $this->should_update( $type, $item, $context ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
			case 'core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
				$skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
				$item_name = sprintf( __( 'WordPress %s' ), $item->version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
			case 'theme':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1877
				$theme = wp_get_theme( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1878
				$item_name = $theme->Get( 'Name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
				$skin->feedback( __( 'Updating theme: %s' ), $item_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1880
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1881
			case 'plugin':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
				$plugin_data = get_plugin_data( $context . '/' . $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
				$item_name = $plugin_data['Name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
				$skin->feedback( __( 'Updating plugin: %s' ), $item_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1886
			case 'translation':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
				$language_item_name = $upgrader->get_name_for_update( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1888
				$item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1889
				$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
  1890
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1891
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1892
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1893
		// Boom, This sites about to get a whole new splash of paint!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1894
		$upgrade_result = $upgrader->upgrade( $item, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1895
			'clear_update_cache' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
			'pre_check_md5'      => false, /* always use partial builds if possible for core updates */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
			'attempt_rollback'   => true, /* only available for core updates */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1899
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1900
		// Core doesn't output this, so lets append it so we don't get confused
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
		if ( 'core' == $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
			if ( is_wp_error( $upgrade_result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
				$skin->error( __( 'Installation Failed' ), $upgrade_result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
				$skin->feedback( __( 'WordPress updated successfully' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
		$this->update_results[ $type ][] = (object) array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
			'item'     => $item,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1911
			'result'   => $upgrade_result,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
			'name'     => $item_name,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
			'messages' => $skin->get_upgrade_messages()
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
		return $upgrade_result;
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
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
	 * Kicks off the background update process, looping through all pending updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1921
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1922
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
	public function run() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1925
		global $wpdb, $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1926
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1927
		if ( $this->is_disabled() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1928
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1929
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1930
		if ( ! is_main_network() || ! is_main_site() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1931
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1932
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1933
		$lock_name = 'auto_updater.lock';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1934
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1935
		// Try to lock
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1936
		$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
  1937
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1938
		if ( ! $lock_result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1939
			$lock_result = get_option( $lock_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1941
			// 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
  1942
			if ( ! $lock_result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1944
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
			// Check to see if the lock is still valid
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1946
			if ( $lock_result > ( time() - HOUR_IN_SECONDS ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1947
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1950
		// Update the lock, as by this point we've definately got a lock, just need to fire the actions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
		update_option( $lock_name, time() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1953
		// Don't automatically run these thins, as we'll handle it ourselves
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1954
		remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1955
		remove_action( 'upgrader_process_complete', 'wp_version_check' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1956
		remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1957
		remove_action( 'upgrader_process_complete', 'wp_update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1958
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1959
		// Next, Plugins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1960
		wp_update_plugins(); // Check for Plugin updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1961
		$plugin_updates = get_site_transient( 'update_plugins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
		if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1963
			foreach ( array_keys( $plugin_updates->response ) as $plugin ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1964
				$this->update( 'plugin', $plugin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
			// Force refresh of plugin update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
			wp_clean_plugins_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
		// Next, those themes we all love
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
		wp_update_themes();  // Check for Theme updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1972
		$theme_updates = get_site_transient( 'update_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
		if ( $theme_updates && !empty( $theme_updates->response ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
			foreach ( array_keys( $theme_updates->response ) as $theme ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
				$this->update( 'theme', $theme );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
			// Force refresh of theme update information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
			wp_clean_themes_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1979
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
		// Next, Process any core update
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
		wp_version_check(); // Check for Core updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
		$core_update = find_core_auto_update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
		if ( $core_update )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
			$this->update( 'core', $core_update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
		// Clean up, and check for any pending translations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
		// (Core_Upgrader checks for core updates)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
		wp_update_themes();  // Check for Theme updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1991
		wp_update_plugins(); // Check for Plugin updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1992
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
		// Finally, Process any new translations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
		$language_updates = wp_get_translation_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
		if ( $language_updates ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
			foreach ( $language_updates as $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
				$this->update( 'translation', $update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
			// Clear existing caches
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
			wp_clean_plugins_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
			wp_clean_themes_cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
			delete_site_transient( 'update_core' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
			wp_version_check();  // check for Core updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
			wp_update_themes();  // Check for Theme updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2007
			wp_update_plugins(); // Check for Plugin updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2008
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2009
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2010
		// Send debugging email to all development installs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2011
		if ( ! empty( $this->update_results ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
			$development_version = false !== strpos( $wp_version, '-' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
			/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
			 * Filter whether to send a debugging email for each automatic background update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
			 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
			 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
			 * @param bool $development_version By default, emails are sent if the install is a development version.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2018
			 *                                  Return false to avoid the email.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
			 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
			if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
				$this->send_debug_email();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2023
			if ( ! empty( $this->update_results['core'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
				$this->after_core_update( $this->update_results['core'][0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2027
		// Clear the lock
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
		delete_option( $lock_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2031
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2032
	 * 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
  2033
	 * and if we need to avoid processing future updates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2034
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2035
	 * @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
  2036
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
	protected function after_core_update( $update_result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2038
		global $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2039
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2040
		$core_update = $update_result->item;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2041
		$result      = $update_result->result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2042
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2043
		if ( ! is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2044
			$this->send_email( 'success', $core_update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2045
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2046
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
		$error_code = $result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2049
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
		// 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
  2051
		// 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
  2052
		$critical = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
		if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
			$critical = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2055
		} 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
  2056
			// A rollback is only critical if it failed too.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
			$critical = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
			$rollback_result = $result->get_error_data()->rollback;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
		} elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
			$critical = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
		if ( $critical ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
			$critical_data = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
				'attempted'  => $core_update->current,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
				'current'    => $wp_version,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
				'error_code' => $error_code,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
				'error_data' => $result->get_error_data(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
				'timestamp'  => time(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
				'critical'   => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
			if ( isset( $rollback_result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
				$critical_data['rollback_code'] = $rollback_result->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
				$critical_data['rollback_data'] = $rollback_result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2075
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2076
			update_site_option( 'auto_core_update_failed', $critical_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
			$this->send_email( 'critical', $core_update, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2079
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2080
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2081
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
		 * 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
  2083
		 * 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
  2084
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
		 * 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
  2086
		 * But we can try again if another version is released.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
		 * For certain 'transient' failures, like download_failed, we should allow retries.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
		 * 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
  2090
		 * 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
  2091
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2092
		$send = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
  		$transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
  		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
  2095
  			wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
  			$send = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
  		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
  		$n = get_site_option( 'auto_core_update_notified' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
		// 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
  2101
		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
  2102
			$send = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
		update_site_option( 'auto_core_update_failed', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2105
			'attempted'  => $core_update->current,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
			'current'    => $wp_version,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
			'error_code' => $error_code,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
			'error_data' => $result->get_error_data(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
			'timestamp'  => time(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2110
			'retry'      => in_array( $error_code, $transient_failures ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2111
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
		if ( $send )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
			$this->send_email( 'fail', $core_update, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2115
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2117
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2118
	 * Sends an email upon the completion or failure of a background core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2119
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2121
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
	 * @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
  2123
	 * @param object $core_update The update offer that was attempted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2124
	 * @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
  2125
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
	protected function send_email( $type, $core_update, $result = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2127
		update_site_option( 'auto_core_update_notified', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
			'type'      => $type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2129
			'email'     => get_site_option( 'admin_email' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2130
			'version'   => $core_update->current,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2131
			'timestamp' => time(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
		$next_user_core_update = get_preferred_from_update_core();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
		// If the update transient is empty, use the update we just performed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
		if ( ! $next_user_core_update )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
			$next_user_core_update = $core_update;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2138
		$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
  2139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
		 * Filter whether to send an email following an automatic background core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
		 * @param bool   $send        Whether to send the email. Default true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
		 * @param string $type        The type of email to send. Can be one of 'success', 'fail', 'critical'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
		 * @param object $core_update The update offer that was attempted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
		 * @param mixed  $result      The result for the core update. Can be WP_Error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
		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
  2151
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
			case 'success' : // We updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
				/* translators: 1: Site name, 2: WordPress version number. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
				$subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
			case 'fail' :   // We tried to update but couldn't.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
			case 'manual' : // We can't update (and made no attempt).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
				/* translators: 1: Site name, 2: WordPress version number. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
				$subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2165
			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
  2166
				/* translators: 1: Site name. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2167
				$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
  2168
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
			default :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2172
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2174
		// 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
  2175
		$version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2176
		$subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
		$body = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
		switch ( $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
			case 'success' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
				$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
  2183
				$body .= "\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
				if ( ! $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
					$body .= __( 'No further action is needed on your part.' ) . ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
				// Can only reference the About screen if their update was successful.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2188
				list( $about_version ) = explode( '-', $core_update->current, 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2189
				$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
  2190
				$body .= "\n" . admin_url( 'about.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2192
				if ( $newer_version_available ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2193
					$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
  2194
					$body .= __( 'Updating is easy and only takes a few moments:' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2195
					$body .= "\n" . network_admin_url( 'update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2196
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2197
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2200
			case 'fail' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2201
			case 'manual' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2202
				$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
  2203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2204
				$body .= "\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2206
				// Don't show this message if there is a newer version available.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2207
				// 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
  2208
				if ( 'fail' == $type && ! $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
					$body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
				$body .= __( 'Updating is easy and only takes a few moments:' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
				$body .= "\n" . network_admin_url( 'update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
			case 'critical' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
				if ( $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2217
					$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
  2218
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2219
					$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
  2220
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2221
				$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
  2222
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2223
				$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
  2224
				$body .= "\n" . network_admin_url( 'update-core.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2225
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2226
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2227
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2228
		// Updates are important!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2229
		if ( $type != 'success' || $newer_version_available )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2230
			$body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
		// Add a note about the support forums to all emails.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
		$body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
		$body .= "\n" . __( 'http://wordpress.org/support/' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2235
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2236
		// 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
  2237
		if ( $type == 'success' && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2238
			$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
  2239
			$body .= "\n" . network_admin_url();
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
		$body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2243
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2244
		if ( 'critical' == $type && is_wp_error( $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2245
			$body .= "\n***\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2246
			$body .= sprintf( __( 'Your site was running version %s.' ), $GLOBALS['wp_version'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2247
			$body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2248
			$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
  2249
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2250
			// 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
  2251
			// 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
  2252
			if ( 'rollback_was_required' == $result->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2253
				$errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2254
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2255
				$errors = array( $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2256
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2257
			foreach ( $errors as $error ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2258
				if ( ! is_wp_error( $error ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2259
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
				$error_code = $error->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
				$body .= "\n\n" . sprintf( __( "Error code: %s" ), $error_code );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
				if ( 'rollback_was_required' == $error_code )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
				if ( $error->get_error_message() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2265
					$body .= "\n" . $error->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
				$error_data = $error->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2267
				if ( $error_data )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2268
					$body .= "\n" . implode( ', ', (array) $error_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2269
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2270
			$body .= "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2271
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2272
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2273
		$to  = get_site_option( 'admin_email' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2274
		$headers = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2275
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2276
		$email = compact( 'to', 'subject', 'body', 'headers' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2277
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2278
		 * Filter the email sent following an automatic background core update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2279
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2280
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2281
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2282
		 * @param array $email {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2283
		 *     Array of email arguments that will be passed to wp_mail().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2284
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
		 *     @type string $to      The email recipient. An array of emails can be returned, as handled by wp_mail().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2286
		 *     @type string $subject The email's subject.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2287
		 *     @type string $body    The email message body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
		 *     @type string $headers Any email headers, defaults to no headers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
		 * }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2290
		 * @param string $type        The type of email being sent. Can be one of 'success', 'fail', 'manual', 'critical'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2291
		 * @param object $core_update The update offer that was attempted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2292
		 * @param mixed  $result      The result for the core update. Can be WP_Error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2293
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2294
		$email = apply_filters( 'auto_core_update_email', $email, $type, $core_update, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2296
		wp_mail( $email['to'], $email['subject'], $email['body'], $email['headers'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2297
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2298
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2299
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2300
	 * 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
  2301
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2302
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2303
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2304
	protected function send_debug_email() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2305
		$update_count = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2306
		foreach ( $this->update_results as $type => $updates )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2307
			$update_count += count( $updates );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2308
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2309
		$body = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2310
		$failures = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2311
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2312
		$body[] = 'WordPress site: ' . network_home_url( '/' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2313
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2314
		// Core
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2315
		if ( isset( $this->update_results['core'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2316
			$result = $this->update_results['core'][0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2317
			if ( $result->result && ! is_wp_error( $result->result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2318
				$body[] = sprintf( 'SUCCESS: WordPress was successfully updated to %s', $result->name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2319
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2320
				$body[] = sprintf( 'FAILED: WordPress failed to update to %s', $result->name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2321
				$failures++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2322
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2323
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2324
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2325
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2326
		// Plugins, Themes, Translations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2327
		foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2328
			if ( ! isset( $this->update_results[ $type ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2329
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2330
			$success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2331
			if ( $success_items ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2332
				$body[] = "The following {$type}s were successfully updated:";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2333
				foreach ( wp_list_pluck( $success_items, 'name' ) as $name )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2334
					$body[] = ' * SUCCESS: ' . $name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2335
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2336
			if ( $success_items != $this->update_results[ $type ] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2337
				// Failed updates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2338
				$body[] = "The following {$type}s failed to update:";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2339
				foreach ( $this->update_results[ $type ] as $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2340
					if ( ! $item->result || is_wp_error( $item->result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2341
						$body[] = ' * FAILED: ' . $item->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2342
						$failures++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2343
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2344
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2345
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2346
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2347
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2349
		if ( $failures ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2350
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2351
			$body[] = 'BETA TESTING?';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2352
			$body[] = '=============';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2353
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
			$body[] = 'This debugging email is sent when you are using a development version of WordPress.';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2355
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2356
			$body[] = 'If you think these failures might be due to a bug in WordPress, could you report it?';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2357
			$body[] = ' * Open a thread in the support forums: http://wordpress.org/support/forum/alphabeta';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2358
			$body[] = " * Or, if you're comfortable writing a bug report: http://core.trac.wordpress.org/";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2359
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2360
			$body[] = 'Thanks! -- The WordPress Team';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2361
			$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2362
			$subject = sprintf( '[%s] There were failures during background updates', get_bloginfo( 'name' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2364
			$subject = sprintf( '[%s] Background updates have finished', get_bloginfo( 'name' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2367
		$body[] = 'UPDATE LOG';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2368
		$body[] = '==========';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2369
		$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2371
		foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2372
			if ( ! isset( $this->update_results[ $type ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2373
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2374
			foreach ( $this->update_results[ $type ] as $update ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2375
				$body[] = $update->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2376
				$body[] = str_repeat( '-', strlen( $update->name ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2377
				foreach ( $update->messages as $message )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2378
					$body[] = "  " . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2379
				if ( is_wp_error( $update->result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2380
					$results = array( 'update' => $update->result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2381
					// 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
  2382
					if ( 'rollback_was_required' === $update->result->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2383
						$results = (array) $update->result->get_error_data();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2384
					foreach ( $results as $result_type => $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2385
						if ( ! is_wp_error( $result ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2386
							continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
						$body[] = '  ' . ( 'rollback' === $result_type ? 'Rollback ' : '' ) . 'Error: [' . $result->get_error_code() . '] ' . $result->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
						if ( $result->get_error_data() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
							$body[] = '         ' . implode( ', ', (array) $result->get_error_data() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2390
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2391
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2392
				$body[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2393
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2394
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
		//echo "<h1>\n$subject\n</h1>\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
		//echo "<pre>\n" . implode( "\n", $body ) . "\n</pre>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
		wp_mail( get_site_option( 'admin_email' ), $subject, implode( "\n", $body ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
}