Completed
Push — add/is_version_check_skip ( a5514c...856772 )
by
unknown
11:58
created

Jetpack_Autoupdate::autoupdate_theme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %
Metric Value
dl 8
loc 8
rs 9.4286
nc 2
cc 2
eloc 6
nop 2
1
<?php
2
3
/**
4
 * Handles items that have been selected for automatic updates.
5
 * Hooks into WP_Automatic_Updater
6
 */
7
class Jetpack_Autoupdate {
1 ignored issue
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
9
	private $results = array();
10
11
	private $expected = array();
12
13
	private $success = array(
14
		'plugin' => array(),
15
		'theme' => array(),
16
	);
17
18
	private $failed = array(
19
		'plugin' => array(),
20
		'theme' => array(),
21
	);
22
23
	private static $instance = null;
24
25
	static function init() {
26
		if ( is_null( self::$instance ) ) {
27
			self::$instance = new Jetpack_Autoupdate;
28
		}
29
		return self::$instance;
30
	}
31
32
	private function __construct() {
33
		if ( Jetpack::is_module_active( 'manage' ) ) {
34
			add_filter( 'auto_update_plugin',  array( $this, 'autoupdate_plugin' ), 10, 2 );
35
			add_filter( 'auto_update_theme',   array( $this, 'autoupdate_theme' ), 10, 2 );
36
			add_filter( 'auto_update_core',    array( $this, 'autoupdate_core' ), 10, 2 );
37
			add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 );
38
39
			if ( Jetpack_Options::get_option( 'skip_version_control_check', false ) ) {
40
				add_filter( 'automatic_updates_is_vcs_checkout', array( $this, '__return_false'), 99 );
41
			}
42
		}
43
	}
44
45
	public function __return_false( $result ) {
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
		return false;
47
	}
48
49 View Code Duplication
	public function autoupdate_plugin( $update, $item ) {
50
		$autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
51
		if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) {
52
			$this->expect( $item->plugin, 'plugin' );
53
 			return true;
54
		}
55
		return $update;
56
	}
57
58 View Code Duplication
	public function autoupdate_theme( $update, $item ) {
59
		$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
60
		if ( in_array( $item->theme , $autoupdate_theme_list) ) {
61
			$this->expect( $item->theme, 'theme' );
62
			return true;
63
		}
64
		return $update;
65
	}
66
67
	public function autoupdate_core( $update, $item ) {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
		$autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false );
69
		if ( $autoupdate_core ) {
70
			return $autoupdate_core;
71
		}
72
		return $update;
73
	}
74
75
	/**
76
	 * Stores the an item identifier to the expected array.
77
	 *
78
	 * @param string $item  Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme'
79
	 * @param string $type 'plugin' or 'theme'
80
	 */
81
	private function expect( $item, $type ) {
82
		if ( ! isset( $this->expected[ $type ] ) ) {
83
			$this->expected[ $type ] = array();
84
		}
85
		$this->expected[ $type ][] = $item;
86
	}
87
88
	/**
89
	 * On completion of an automatic update, let's store the results.
90
	 *
91
	 * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty.
92
	 */
93
	public function automatic_updates_complete( $results ) {
94
		if ( empty( $this->expected ) ) {
95
			return;
96
		}
97
		$this->results = empty( $results ) ? self::get_possible_failures() : $results;
98
99
		add_action( 'shutdown', array( $this, 'bump_stats' ) );
100
101
		Jetpack::init();
102
103
		$items_to_log = array( 'plugin', 'theme' );
104
		foreach( $items_to_log as $items ) {
105
			$this->log_items( $items );
106
		}
107
108
		Jetpack::log( 'autoupdates', $this->get_log() );
109
	}
110
111
	public function get_log() {
112
		return array(
113
			'results'	=> $this->results,
114
			'failed'	=> $this->failed,
115
			'success'	=> $this->success
116
		);
117
	}
118
119
	/**
120
	 * Iterates through expected items ( plugins or themes ) and compares them to actual results.
121
	 *
122
	 * @param $items 'plugin' or 'theme'
123
	 */
124
	private function log_items( $items ) {
125
126
		if ( ! isset( $this->expected[ $items ] ) ) {
127
			return;
128
		}
129
130
		$item_results = $this->get_successful_updates( $items );
131
132
		if ( is_array( $this->expected[ $items ] ) ) {
133
			foreach( $this->expected[ $items ] as $item ) {
134
				if ( in_array( $item, $item_results ) ) {
135
						$this->success[ $items ][] = $item;
136
				} else {
137
						$this->failed[ $items ][] = $item;
138
				}
139
			}
140
		}
141
	}
142
143
	public function bump_stats() {
144
		$instance = Jetpack::init();
145
		$log = array();
146
		// Bump numbers
147 View Code Duplication
		if ( ! empty( $this->success['plugin'] ) ) {
148
			$instance->stat( 'autoupdates/plugin-success', count( $this->success['plugin'] ) );
149
			$log['plugins_success'] = $this->success['plugin'];
150
		}
151
152 View Code Duplication
		if ( ! empty( $this->failed['plugin'] ) ) {
153
			$instance->stat( 'autoupdates/plugin-fail', count( $this->failed['plugin'] ) );
154
			$log['plugins_failed'] = $this->failed['plugin'];
155
		}
156
157 View Code Duplication
		if ( ! empty( $this->success['theme'] ) ) {
158
			$instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) );
159
			$log['themes_success'] = $this->success['theme'];
160
		}
161
162 View Code Duplication
		if ( ! empty( $this->failed['theme'] ) ) {
163
			$instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) );
164
			$log['themes_failed'] = $this->failed['theme'];
165
		}
166
167
		$instance->do_stats( 'server_side' );
168
169
		// Send a more detailed log to logstash
170
		if ( ! empty( $log ) ) {
171
			Jetpack::load_xml_rpc_client();
172
			$xml = new Jetpack_IXR_Client( array(
173
				'user_id' => get_current_user_id()
174
			) );
175
			$log['blog_id'] = Jetpack_Options::get_option( 'id' );
176
			$xml->query( 'jetpack.debug_autoupdate', $log );
177
		}
178
	}
179
180
	/**
181
	 * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items
182
	 *
183
	 * @param string $type 'plugin' or 'theme'
184
	 *
185
	 * @return array
186
	 */
187
	private function get_successful_updates( $type ) {
188
		$successful_updates = array();
189
190
		if ( ! isset( $this->results[ $type ] ) ) {
191
			return $successful_updates;
192
		}
193
194
		foreach( $this->results[ $type ] as $result ) {
195
			if ( $result->result ) {
196
				switch( $type ) {
197
					case 'theme':
198
						$successful_updates[] = $result->item->theme;
199
						break;
200
					case 'plugin':
201
						$successful_updates[] = $result->item->plugin;
202
				}
203
			}
204
		}
205
206
		return $successful_updates;
207
	}
208
209
	static function get_possible_failures() {
210
		$result = array();
211
		// Lets check some reasons why it might not be working as expected
212
		include_once( ABSPATH . '/wp-admin/includes/admin.php' );
213
		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
214
		$upgrader = new WP_Automatic_Updater;
215
216
		if ( $upgrader->is_disabled() ) {
217
			$result[] = 'autoupdates-disabled';
218
		}
219
		if ( ! is_main_site() ) {
220
			$result[] = 'is-not-main-site';
221
		}
222
		if ( ! is_main_network() ) {
223
			$result[] = 'is-not-main-network';
224
		}
225
		if ( $upgrader->is_vcs_checkout( ABSPATH ) ) {
226
			$result[] = 'site-on-vcs';
227
		}
228
		if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) {
229
			$result[] = 'plugin-directory-on-vcs';
230
		}
231
		if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) {
232
			$result[] = 'content-directory-on-vcs';
233
		}
234
		$lock = get_option( 'auto_updater.lock' );
235
		if ( $lock > ( time() - HOUR_IN_SECONDS ) ) {
236
			$result[] = 'lock-is-set';
237
		}
238
		$skin = new Automatic_Upgrader_Skin;
239
		include_once( ABSPATH . 'wp-admin/includes/file.php' );
240
		include_once( ABSPATH . 'wp-admin/includes/template.php' );
241
		if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) {
242
			$result[] = 'no-system-write-access';
243
		}
244
		if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false )  ) {
245
			$result[] = 'no-plugin-directory-write-access';
246
		}
247
		if ( ! $skin->request_filesystem_credentials( false,  WP_CONTENT_DIR, false ) ) {
248
			$result[] = 'no-wp-content-directory-write-access';
249
		}
250
		return $result;
251
	}
252
253
}
254
Jetpack_Autoupdate::init();
255