forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.jetpack-autoupdate.php
214 lines (184 loc) · 5.83 KB
/
class.jetpack-autoupdate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Handles items that have been selected for automatic updates.
* Hooks into WP_Automatic_Updater
*/
class Jetpack_Autoupdate {
public $updates_allowed;
public $jetpack;
public $autoupdate_results;
public $is_updating = false;
public $autoupdate_expected = array(
'plugin'=> array(),
'theme' => array(),
);
public $log = array(
'plugin' => array(),
'theme' => array(),
);
private static $instance = null;
static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Jetpack_Autoupdate;
}
return self::$instance;
}
private function __construct() {
$this->updates_allowed = Jetpack::is_module_active( 'manage' );
// Only run automatic updates if a user as opted in by activating the manage module.
if ( $this->updates_allowed ) {
add_filter( 'auto_update_plugin', array( $this, 'autoupdate_plugin' ), 10, 2 );
add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 );
add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 );
add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 10, 1 );
add_action( 'shutdown', array( $this, 'log_results' ) );
}
}
function autoupdate_plugin( $update, $item ) {
$autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) {
$this->expect( $item->plugin );
return true;
}
return $update;
}
function autoupdate_theme( $update, $item ) {
$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
if ( in_array( $item->theme , $autoupdate_theme_list) ) {
$this->expect( $item->theme, $type = 'theme' );
return true;
}
return $update;
}
function autoupdate_core( $update, $item ) {
$autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false );
if ( $autoupdate_core ) {
return $autoupdate_core;
}
return $update;
}
/**
* Stores the an item identifier to the autoupdate_expected array.
*
* @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme'
* @param string $type 'plugin' or 'theme'
*/
function expect( $item, $type='plugin' ) {
$this->is_updating = true;
$this->autoupdate_expected[ $type ][] = $item;
}
/**
* On completion of an automatic update, let's store the results.
*
* @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty.
*/
function automatic_updates_complete( $results ) {
$this->autoupdate_results = $results;
}
/**
* On shutdown, let's check to see if we've preformed an automatic update.
* If so, let's compare the expected results to the actual results, and log our findings.
*
* Results are logged locally via Jetpack::log(), and globally via Jetpack::do_stats()
*/
function log_results() {
if ( $this->is_updating ) {
$this->jetpack = Jetpack::init();
$items_to_log = array( 'plugin', 'theme' );
foreach( $items_to_log as $items ) {
$this->log_items( $items );
}
$this->jetpack->do_stats( 'server_side' );
$this->jetpack->log( 'autoupdates', $this->log );
}
}
/**
* Iterates through expected items ( plugins or themes ) and compares them to actual results.
*
* @param $items 'plugin' or 'theme'
*/
function log_items( $items ) {
$num_items_updated = 0;
$num_items_failed = 0;
$item_results = $this->get_successful_updates( $items );
$items_failed = array();
foreach( $this->autoupdate_expected[ $items ] as $item ) {
if ( in_array( $item, $item_results ) ) {
$num_items_updated++;
$this->log[ $items ][ $item ] = true;
} else {
$num_items_failed++;
$this->log[ $items ][ $item ] = new WP_Error( "$items-fail", $this->get_error_message( $item, $type = $items ) );
$items_failed[] = $item;
}
}
if ( $num_items_updated ) {
$this->jetpack->stat( "autoupdates/$items-success", $num_items_updated );
}
if ( $num_items_failed ) {
// bump stats
$this->jetpack->stat( "autoupdates/$items-fail", $num_items_failed );
Jetpack::load_xml_rpc_client();
$xml = new Jetpack_IXR_Client( array(
'user_id' => get_current_user_id()
) );
$request = array(
'plugins' => $items_failed,
'blog_id' => Jetpack_Options::get_option( 'id' ),
);
$xml->query( 'jetpack.debug_autoupdate', $request );
}
}
/**
* Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items
*
* @param string $type 'plugin' or 'theme'
*
* @return array
*/
private function get_successful_updates( $type = 'plugin' ) {
$successful_updates = array();
if ( ! isset( $this->autoupdate_results[ $type ] ) ) {
return $successful_updates;
}
foreach( $this->autoupdate_results[ $type ] as $result ) {
if ( $result->result ) {
switch( $type ) {
case 'theme':
$successful_updates[] = $result->item->theme;
break;
default:
$successful_updates[] = $result->item->plugin;
}
}
}
return $successful_updates;
}
/**
* Cycles through results generated by WP_Automatic_Updater to find the messages for the given item and item type.
*
* @param $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme'
* @param string $type 'plugin' or 'theme'
*
* @return bool|string
*/
private function get_error_message( $item, $type = 'plugin' ) {
if ( ! isset( $this->autoupdate_results[ $type ] ) ) {
return false;
}
foreach( $this->autoupdate_results[ $type ] as $result ) {
switch( $type ) {
case 'theme':
$id = $result->item->theme;
break;
default:
$id = $result->item->plugin;
}
if ( $id == $item && isset( $result->messages ) ) {
return implode( ', ', $result->messages );
}
}
return false;
}
}
Jetpack_Autoupdate::init();