Completed
Push — update/themes-endpoint-add-tra... ( 066af4...d2b09c )
by
unknown
36:37 queued 30:03
created

class.jetpack-autoupdate.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 {
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_filter( 'auto_update_translation', array( $this, 'autoupdate_translation', 10, 2 ) );
38
			add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 );
39
		}
40
	}
41
42 View Code Duplication
	public function autoupdate_plugin( $update, $item ) {
43
		$autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
44
		if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) {
45
			$this->expect( $item->plugin, 'plugin' );
46
 			return true;
47
		}
48
		return $update;
49
	}
50
	
51
	public function autoupdate_translation( $update, $item ) {
52
		$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
53
		$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
54
		
55
		// $item = {
56
		//  "type":"theme",
57
		//  "slug":"twentyfourteen",
58
		//  "language":"en_CA",
59
		//  "version":"1.8",
60
		//  "updated":"2015-07-18 11:27:20",
61
		//  "package":"https:\/\/downloads.wordpress.org\/translation\/theme\/twentyfourteen\/1.8\/en_CA.zip",
62
		//  "autoupdate":true
63
		//}
64
		if ( (  in_array( $item->slug, $autoupdate_themes_translations )
65
	         || in_array( $item->slug, $autoupdate_theme_list ) )
66
             && 'theme' === $item->type ) {
67
			$this->expect( $item->slug, 'theme' );
68
			return true;
69
		}
70
71
		return $update;
72
	}
73
74 View Code Duplication
	public function autoupdate_theme( $update, $item ) {
75
		$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
76
		if ( in_array( $item->theme , $autoupdate_theme_list) ) {
77
			$this->expect( $item->theme, 'theme' );
78
			return true;
79
		}
80
		return $update;
81
	}
82
83
	public function autoupdate_core( $update, $item ) {
0 ignored issues
show
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...
84
		$autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false );
85
		if ( $autoupdate_core ) {
86
			return $autoupdate_core;
87
		}
88
		return $update;
89
	}
90
91
	/**
92
	 * Stores the an item identifier to the expected array.
93
	 *
94
	 * @param string $item  Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme'
95
	 * @param string $type 'plugin' or 'theme'
96
	 */
97
	private function expect( $item, $type ) {
98
		if ( ! isset( $this->expected[ $type ] ) ) {
99
			$this->expected[ $type ] = array();
100
		}
101
		$this->expected[ $type ][] = $item;
102
	}
103
104
	/**
105
	 * On completion of an automatic update, let's store the results.
106
	 *
107
	 * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty.
108
	 */
109
	public function automatic_updates_complete( $results ) {
110
		if ( empty( $this->expected ) ) {
111
			return;
112
		}
113
		$this->results = empty( $results ) ? self::get_possible_failures() : $results;
114
115
		add_action( 'shutdown', array( $this, 'bump_stats' ) );
116
117
		Jetpack::init();
118
119
		$items_to_log = array( 'plugin', 'theme' );
120
		foreach( $items_to_log as $items ) {
121
			$this->log_items( $items );
122
		}
123
124
		Jetpack::log( 'autoupdates', $this->get_log() );
125
	}
126
127
	public function get_log() {
128
		return array(
129
			'results'	=> $this->results,
130
			'failed'	=> $this->failed,
131
			'success'	=> $this->success
132
		);
133
	}
134
135
	/**
136
	 * Iterates through expected items ( plugins or themes ) and compares them to actual results.
137
	 *
138
	 * @param $items 'plugin' or 'theme'
139
	 */
140
	private function log_items( $items ) {
141
142
		if ( ! isset( $this->expected[ $items ] ) ) {
143
			return;
144
		}
145
146
		$item_results = $this->get_successful_updates( $items );
147
148
		if ( is_array( $this->expected[ $items ] ) ) {
149
			foreach( $this->expected[ $items ] as $item ) {
150
				if ( in_array( $item, $item_results ) ) {
151
						$this->success[ $items ][] = $item;
152
				} else {
153
						$this->failed[ $items ][] = $item;
154
				}
155
			}
156
		}
157
	}
158
159
	public function bump_stats() {
160
		$instance = Jetpack::init();
161
		$log = array();
162
		// Bump numbers
163 View Code Duplication
		if ( ! empty( $this->success['plugin'] ) ) {
164
			$instance->stat( 'autoupdates/plugin-success', count( $this->success['plugin'] ) );
165
			$log['plugins_success'] = $this->success['plugin'];
166
		}
167
168 View Code Duplication
		if ( ! empty( $this->failed['plugin'] ) ) {
169
			$instance->stat( 'autoupdates/plugin-fail', count( $this->failed['plugin'] ) );
170
			$log['plugins_failed'] = $this->failed['plugin'];
171
		}
172
173 View Code Duplication
		if ( ! empty( $this->success['theme'] ) ) {
174
			$instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) );
175
			$log['themes_success'] = $this->success['theme'];
176
		}
177
178 View Code Duplication
		if ( ! empty( $this->failed['theme'] ) ) {
179
			$instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) );
180
			$log['themes_failed'] = $this->failed['theme'];
181
		}
182
183
		$instance->do_stats( 'server_side' );
184
185
		// Send a more detailed log to logstash
186
		if ( ! empty( $log ) ) {
187
			Jetpack::load_xml_rpc_client();
188
			$xml = new Jetpack_IXR_Client( array(
189
				'user_id' => get_current_user_id()
190
			) );
191
			$log['blog_id'] = Jetpack_Options::get_option( 'id' );
192
			$xml->query( 'jetpack.debug_autoupdate', $log );
193
		}
194
	}
195
196
	/**
197
	 * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items
198
	 *
199
	 * @param string $type 'plugin' or 'theme'
200
	 *
201
	 * @return array
202
	 */
203
	private function get_successful_updates( $type ) {
204
		$successful_updates = array();
205
206
		if ( ! isset( $this->results[ $type ] ) ) {
207
			return $successful_updates;
208
		}
209
210
		foreach( $this->results[ $type ] as $result ) {
211
			if ( $result->result ) {
212
				switch( $type ) {
213
					case 'theme':
214
						$successful_updates[] = $result->item->theme;
215
						break;
216
					case 'plugin':
217
						$successful_updates[] = $result->item->plugin;
218
				}
219
			}
220
		}
221
222
		return $successful_updates;
223
	}
224
225
	static function get_possible_failures() {
226
		$result = array();
227
		// Lets check some reasons why it might not be working as expected
228
		include_once( ABSPATH . '/wp-admin/includes/admin.php' );
229
		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
230
		$upgrader = new WP_Automatic_Updater;
231
232
		if ( $upgrader->is_disabled() ) {
233
			$result[] = 'autoupdates-disabled';
234
		}
235
		if ( ! is_main_site() ) {
236
			$result[] = 'is-not-main-site';
237
		}
238
		if ( ! is_main_network() ) {
239
			$result[] = 'is-not-main-network';
240
		}
241
		if ( $upgrader->is_vcs_checkout( ABSPATH ) ) {
242
			$result[] = 'site-on-vcs';
243
		}
244
		if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) {
245
			$result[] = 'plugin-directory-on-vcs';
246
		}
247
		if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) {
248
			$result[] = 'content-directory-on-vcs';
249
		}
250
		$lock = get_option( 'auto_updater.lock' );
251
		if ( $lock > ( time() - HOUR_IN_SECONDS ) ) {
252
			$result[] = 'lock-is-set';
253
		}
254
		$skin = new Automatic_Upgrader_Skin;
255
		include_once( ABSPATH . 'wp-admin/includes/file.php' );
256
		include_once( ABSPATH . 'wp-admin/includes/template.php' );
257
		if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) {
258
			$result[] = 'no-system-write-access';
259
		}
260
		if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false )  ) {
261
			$result[] = 'no-plugin-directory-write-access';
262
		}
263
		if ( ! $skin->request_filesystem_credentials( false,  WP_CONTENT_DIR, false ) ) {
264
			$result[] = 'no-wp-content-directory-write-access';
265
		}
266
		return $result;
267
	}
268
269
}
270
Jetpack_Autoupdate::init();
271