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 | |||
30 | return self::$instance; |
||
31 | } |
||
32 | |||
33 | private function __construct() { |
||
34 | if ( Jetpack::is_module_active( 'manage' ) ) { |
||
35 | add_filter( 'auto_update_plugin', array( $this, 'autoupdate_plugin' ), 10, 2 ); |
||
36 | add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 ); |
||
37 | add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 ); |
||
38 | add_filter( 'auto_update_translation', array( $this, 'autoupdate_translation' ), 10, 2 ); |
||
39 | add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 ); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | View Code Duplication | public function autoupdate_plugin( $update, $item ) { |
|
44 | $autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
||
45 | if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) { |
||
46 | $this->expect( $item->plugin, 'plugin' ); |
||
47 | |||
48 | return true; |
||
49 | } |
||
50 | |||
51 | return $update; |
||
52 | } |
||
53 | |||
54 | public function autoupdate_translation( $update, $item ) { |
||
55 | // Themes |
||
56 | $autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ); |
||
57 | $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
||
58 | |||
59 | /* |
||
60 | $item = { |
||
61 | "type":"theme", |
||
62 | "slug":"twentyfourteen", |
||
63 | "language":"en_CA", |
||
64 | "version":"1.8", |
||
65 | "updated":"2015-07-18 11:27:20", |
||
66 | "package":"https:\/\/downloads.wordpress.org\/translation\/theme\/twentyfourteen\/1.8\/en_CA.zip", |
||
67 | "autoupdate":true |
||
68 | } |
||
69 | */ |
||
70 | View Code Duplication | if ( ( in_array( $item->slug, $autoupdate_themes_translations ) |
|
71 | || in_array( $item->slug, $autoupdate_theme_list ) ) |
||
72 | && 'theme' === $item->type |
||
73 | ) { |
||
74 | $this->expect( $item->type + ':' + $item->slug, 'translation' ); |
||
75 | |||
76 | return true; |
||
77 | } |
||
78 | |||
79 | // Plugins |
||
80 | $autoupdate_plugin_translations = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ); |
||
81 | $autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
||
82 | $plugin_files = array_unique( array_merge( $autoupdate_plugin_list, $autoupdate_plugin_translations ) ); |
||
83 | $plugin_slugs = array_map( array( __CLASS__, 'get_plugin_slug' ), $plugin_files ); |
||
84 | |||
85 | View Code Duplication | if ( in_array( $item->slug, $plugin_slugs ) |
|
86 | && 'plugin' === $item->type |
||
87 | ) { |
||
88 | $this->expect( $item->type + ':' + $item->slug, 'translation' ); |
||
89 | return true; |
||
90 | } |
||
91 | |||
92 | return $update; |
||
93 | } |
||
94 | |||
95 | View Code Duplication | public function autoupdate_theme( $update, $item ) { |
|
96 | $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
||
97 | if ( in_array( $item->theme, $autoupdate_theme_list ) ) { |
||
98 | $this->expect( $item->theme, 'theme' ); |
||
99 | return true; |
||
100 | } |
||
101 | |||
102 | return $update; |
||
103 | } |
||
104 | |||
105 | public function autoupdate_core( $update, $item ) { |
||
0 ignored issues
–
show
|
|||
106 | $autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false ); |
||
107 | if ( $autoupdate_core ) { |
||
108 | return $autoupdate_core; |
||
109 | } |
||
110 | |||
111 | return $update; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Stores the an item identifier to the expected array. |
||
116 | * |
||
117 | * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
||
118 | * @param string $type 'plugin' or 'theme' |
||
119 | */ |
||
120 | private function expect( $item, $type ) { |
||
121 | if ( ! isset( $this->expected[ $type ] ) ) { |
||
122 | $this->expected[ $type ] = array(); |
||
123 | } |
||
124 | $this->expected[ $type ][] = $item; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * On completion of an automatic update, let's store the results. |
||
129 | * |
||
130 | * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. |
||
131 | */ |
||
132 | public function automatic_updates_complete( $results ) { |
||
133 | if ( empty( $this->expected ) ) { |
||
134 | return; |
||
135 | } |
||
136 | $this->results = empty( $results ) ? self::get_possible_failures() : $results; |
||
137 | |||
138 | add_action( 'shutdown', array( $this, 'bump_stats' ) ); |
||
139 | |||
140 | Jetpack::init(); |
||
141 | |||
142 | $items_to_log = array( 'plugin', 'theme', 'translation' ); |
||
143 | foreach ( $items_to_log as $items ) { |
||
144 | $this->log_items( $items ); |
||
145 | } |
||
146 | |||
147 | Jetpack::log( 'autoupdates', $this->get_log() ); |
||
148 | } |
||
149 | |||
150 | public function get_log() { |
||
151 | return array( |
||
152 | 'results' => $this->results, |
||
153 | 'failed' => $this->failed, |
||
154 | 'success' => $this->success |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Iterates through expected items ( plugins or themes ) and compares them to actual results. |
||
160 | * |
||
161 | * @param $items 'plugin' or 'theme' |
||
162 | */ |
||
163 | private function log_items( $items ) { |
||
164 | if ( ! isset( $this->expected[ $items ] ) ) { |
||
165 | return; |
||
166 | } |
||
167 | |||
168 | $item_results = $this->get_successful_updates( $items ); |
||
169 | |||
170 | if ( is_array( $this->expected[ $items ] ) ) { |
||
171 | foreach ( $this->expected[ $items ] as $item ) { |
||
172 | if ( in_array( $item, $item_results ) ) { |
||
173 | $this->success[ $items ][] = $item; |
||
174 | } else { |
||
175 | $this->failed[ $items ][] = $item; |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | public function bump_stats() { |
||
182 | $instance = Jetpack::init(); |
||
183 | $log = array(); |
||
184 | // Bump numbers |
||
185 | View Code Duplication | if ( ! empty( $this->success['plugin'] ) ) { |
|
186 | $instance->stat( 'autoupdates/plugin-success', count( $this->success['plugin'] ) ); |
||
187 | $log['plugins_success'] = $this->success['plugin']; |
||
188 | } |
||
189 | |||
190 | View Code Duplication | if ( ! empty( $this->failed['plugin'] ) ) { |
|
191 | $instance->stat( 'autoupdates/plugin-fail', count( $this->failed['plugin'] ) ); |
||
192 | $log['plugins_failed'] = $this->failed['plugin']; |
||
193 | } |
||
194 | |||
195 | View Code Duplication | if ( ! empty( $this->success['theme'] ) ) { |
|
196 | $instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) ); |
||
197 | $log['themes_success'] = $this->success['theme']; |
||
198 | } |
||
199 | |||
200 | View Code Duplication | if ( ! empty( $this->failed['theme'] ) ) { |
|
201 | $instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) ); |
||
202 | $log['themes_failed'] = $this->failed['theme']; |
||
203 | } |
||
204 | |||
205 | $instance->do_stats( 'server_side' ); |
||
206 | |||
207 | // Send a more detailed log to logstash |
||
208 | if ( ! empty( $log ) ) { |
||
209 | Jetpack::load_xml_rpc_client(); |
||
210 | $xml = new Jetpack_IXR_Client( array( |
||
211 | 'user_id' => get_current_user_id() |
||
212 | ) ); |
||
213 | $log['blog_id'] = Jetpack_Options::get_option( 'id' ); |
||
214 | $xml->query( 'jetpack.debug_autoupdate', $log ); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items |
||
220 | * |
||
221 | * @param string $type 'plugin' or 'theme' |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | private function get_successful_updates( $type ) { |
||
226 | $successful_updates = array(); |
||
227 | |||
228 | if ( ! isset( $this->results[ $type ] ) ) { |
||
229 | return $successful_updates; |
||
230 | } |
||
231 | |||
232 | foreach ( $this->results[ $type ] as $result ) { |
||
233 | if ( $result->result ) { |
||
234 | switch ( $type ) { |
||
235 | case 'theme': |
||
236 | $successful_updates[] = $result->item->theme; |
||
237 | break; |
||
238 | case 'plugin': |
||
239 | $successful_updates[] = $result->item->plugin; |
||
240 | break; |
||
241 | case 'translation': |
||
242 | $successful_updates[] = $result->item->type + ':' + $result->item->slug; |
||
243 | break; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | return $successful_updates; |
||
249 | } |
||
250 | |||
251 | static function get_possible_failures() { |
||
252 | $result = array(); |
||
253 | // Lets check some reasons why it might not be working as expected |
||
254 | include_once( ABSPATH . '/wp-admin/includes/admin.php' ); |
||
255 | include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' ); |
||
256 | $upgrader = new WP_Automatic_Updater; |
||
257 | |||
258 | if ( $upgrader->is_disabled() ) { |
||
259 | $result[] = 'autoupdates-disabled'; |
||
260 | } |
||
261 | if ( ! is_main_site() ) { |
||
262 | $result[] = 'is-not-main-site'; |
||
263 | } |
||
264 | if ( ! is_main_network() ) { |
||
265 | $result[] = 'is-not-main-network'; |
||
266 | } |
||
267 | if ( $upgrader->is_vcs_checkout( ABSPATH ) ) { |
||
268 | $result[] = 'site-on-vcs'; |
||
269 | } |
||
270 | if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) { |
||
271 | $result[] = 'plugin-directory-on-vcs'; |
||
272 | } |
||
273 | if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) { |
||
274 | $result[] = 'content-directory-on-vcs'; |
||
275 | } |
||
276 | $lock = get_option( 'auto_updater.lock' ); |
||
277 | if ( $lock > ( time() - HOUR_IN_SECONDS ) ) { |
||
278 | $result[] = 'lock-is-set'; |
||
279 | } |
||
280 | $skin = new Automatic_Upgrader_Skin; |
||
281 | include_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
282 | include_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
283 | if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) { |
||
284 | $result[] = 'no-system-write-access'; |
||
285 | } |
||
286 | if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false ) ) { |
||
287 | $result[] = 'no-plugin-directory-write-access'; |
||
288 | } |
||
289 | if ( ! $skin->request_filesystem_credentials( false, WP_CONTENT_DIR, false ) ) { |
||
290 | $result[] = 'no-wp-content-directory-write-access'; |
||
291 | } |
||
292 | |||
293 | return $result; |
||
294 | } |
||
295 | |||
296 | static function get_plugin_slug( $plugin_file ) { |
||
297 | $update_plugins = get_site_transient( 'update_plugins' ); |
||
0 ignored issues
–
show
Equals sign not aligned correctly; expected 1 space but found 3 spaces
This check looks for improperly formatted assignments. Every assignment must have exactly one space before and one space after the equals operator. To illustrate: $a = "a";
$ab = "ab";
$abc = "abc";
will have no issues, while $a = "a";
$ab = "ab";
$abc = "abc";
will report issues in lines 1 and 2. ![]() |
|||
298 | if ( isset( $update_plugins->no_update ) ) { |
||
299 | if ( isset( $update_plugins->no_update[ $plugin_file ] ) ) { |
||
300 | $slug = $update_plugins->no_update[ $plugin_file ]->slug; |
||
301 | } |
||
302 | } |
||
303 | if ( empty( $slug ) && isset( $update_plugins->response ) ) { |
||
304 | if ( isset( $update_plugins->response[ $plugin_file ] ) ) { |
||
305 | $slug = $update_plugins->response[ $plugin_file ]->slug; |
||
306 | } |
||
307 | } |
||
308 | |||
309 | // Try to infer from the plugin file if not cached |
||
310 | if ( empty( $slug) ) { |
||
311 | $slug = dirname( $plugin_file ); |
||
312 | if ( '.' === $slug ) { |
||
313 | $slug = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); |
||
314 | } |
||
315 | } |
||
316 | return $slug; |
||
317 | } |
||
318 | |||
319 | } |
||
320 | |||
321 | Jetpack_Autoupdate::init(); |
||
322 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.