| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.'); |
| 4 |
|
| 5 |
class UpdraftCentral_Updates_Commands extends UpdraftCentral_Commands { |
| 6 |
|
| 7 |
public function do_updates($updates) { |
| 8 |
|
| 9 |
if (!is_array($updates)) $this->_generic_error_response('invalid_data'); |
| 10 |
|
| 11 |
if (!empty($updates['plugins']) && !current_user_can('update_plugins')) return $this->_generic_error_response('updates_permission_denied', 'update_plugins'); |
| 12 |
|
| 13 |
if (!empty($updates['themes']) && !current_user_can('update_themes')) return $this->_generic_error_response('updates_permission_denied', 'update_themes'); |
| 14 |
|
| 15 |
if (!empty($updates['core']) && !current_user_can('update_core')) return $this->_generic_error_response('updates_permission_denied', 'update_core'); |
| 16 |
|
| 17 |
$this->_admin_include('plugin.php', 'update.php', 'file.php', 'template.php'); |
| 18 |
$this->_frontend_include('update.php'); |
| 19 |
|
| 20 |
if (!empty($updates['meta']) && isset($updates['meta']['filesystem_credentials'])) { |
| 21 |
parse_str($updates['meta']['filesystem_credentials'], $filesystem_credentials); |
| 22 |
if (is_array($filesystem_credentials)) { |
| 23 |
foreach ($filesystem_credentials as $key => $value) { |
| 24 |
// Put them into $_POST, which is where request_filesystem_credentials() checks for them. |
| 25 |
$_POST[$key] = $value; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
$plugins = empty($updates['plugins']) ? array() : $updates['plugins']; |
| 31 |
$plugin_updates = array(); |
| 32 |
foreach ($plugins as $plugin_info) { |
| 33 |
$plugin_file = $plugin_info['plugin']; |
| 34 |
$plugin_updates[] = $this->_update_plugin($plugin_info['plugin'], $plugin_info['slug']); |
| 35 |
} |
| 36 |
|
| 37 |
$themes = empty($updates['themes']) ? array() : $updates['themes']; |
| 38 |
$theme_updates = array(); |
| 39 |
foreach ($themes as $theme_info) { |
| 40 |
$theme = $theme_info['theme']; |
| 41 |
$theme_updates[] = $this->_update_theme($theme); |
| 42 |
} |
| 43 |
|
| 44 |
$cores = empty($updates['core']) ? array() : $updates['core']; |
| 45 |
$core_updates = array(); |
| 46 |
foreach ($cores as $core) { |
| 47 |
$core_updates[] = $this->_update_core(null); |
| 48 |
// Only one (and always we go to the latest version) - i.e. we ignore the passed parameters |
| 49 |
break; |
| 50 |
} |
| 51 |
|
| 52 |
return $this->_response(array( |
| 53 |
'plugins' => $plugin_updates, |
| 54 |
'themes' => $theme_updates, |
| 55 |
'core' => $core_updates, |
| 56 |
)); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Updates a plugin. A facade method that exposes a private updates |
| 62 |
* feature for other modules to consume. |
| 63 |
* |
| 64 |
* @param string $plugin Specific plugin to be updated |
| 65 |
* @param string $slug Unique key passed for updates |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function update_plugin($plugin, $slug) { |
| 70 |
return $this->_update_plugin($plugin, $slug); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Updates a theme. A facade method that exposes a private updates |
| 75 |
* feature for other modules to consume. |
| 76 |
* |
| 77 |
* @param string $theme Specific theme to be updated |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
public function update_theme($theme) { |
| 82 |
return $this->_update_theme($theme); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Gets available updates for a certain entity (e.g. plugin or theme). A facade method that |
| 87 |
* exposes a private updates feature for other modules to consume. |
| 88 |
* |
| 89 |
* @param string $entity The name of the entity that this request is intended for (e.g. themes or plugins) |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function get_item_updates($entity) { |
| 94 |
$updates = array(); |
| 95 |
switch ($entity) { |
| 96 |
case 'themes': |
| 97 |
wp_update_themes(); |
| 98 |
$updates = $this->maybe_add_third_party_items(get_theme_updates(), 'theme'); |
| 99 |
break; |
| 100 |
case 'plugins': |
| 101 |
wp_update_plugins(); |
| 102 |
$updates = $this->maybe_add_third_party_items(get_plugin_updates(), 'plugin'); |
| 103 |
break; |
| 104 |
} |
| 105 |
|
| 106 |
return $updates; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Mostly from wp_ajax_update_plugin() in wp-admin/includes/ajax-actions.php (WP 4.5.2) |
| 111 |
* Code-formatting style has been retained from the original, for ease of comparison/updating |
| 112 |
* |
| 113 |
* @param string $plugin Specific plugin to be updated |
| 114 |
* @param string $slug Unique key passed for updates |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
private function _update_plugin($plugin, $slug) { |
| 118 |
|
| 119 |
$status = array( |
| 120 |
'update' => 'plugin', |
| 121 |
'plugin' => $plugin, |
| 122 |
'slug' => sanitize_key($slug), |
| 123 |
'oldVersion' => '', |
| 124 |
'newVersion' => '', |
| 125 |
); |
| 126 |
|
| 127 |
if (false !== strpos($plugin, '..') || false !== strpos($plugin, ':') || !preg_match('#^[^\/]#i', $plugin)) { |
| 128 |
$status['error'] = 'not_found'; |
| 129 |
return $status; |
| 130 |
} |
| 131 |
|
| 132 |
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin); |
| 133 |
if (!isset($plugin_data['Name']) || !isset($plugin_data['Author']) || ('' == $plugin_data['Name'] && '' == $plugin_data['Author'])) { |
| 134 |
$status['error'] = 'not_found'; |
| 135 |
return $status; |
| 136 |
} |
| 137 |
|
| 138 |
if ($plugin_data['Version']) { |
| 139 |
$status['oldVersion'] = $plugin_data['Version']; |
| 140 |
} |
| 141 |
|
| 142 |
if (!current_user_can('update_plugins')) { |
| 143 |
$status['error'] = 'updates_permission_denied'; |
| 144 |
return $status; |
| 145 |
} |
| 146 |
|
| 147 |
include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 148 |
|
| 149 |
wp_update_plugins(); |
| 150 |
|
| 151 |
// WP < 3.7 |
| 152 |
if (!class_exists('Automatic_Upgrader_Skin')) include_once(UPDRAFTCENTRAL_CLIENT_DIR.'/classes/class-automatic-upgrader-skin.php'); |
| 153 |
|
| 154 |
$skin = new Automatic_Upgrader_Skin(); |
| 155 |
$upgrader = new Plugin_Upgrader($skin); |
| 156 |
$result = $upgrader->bulk_upgrade(array($plugin)); |
| 157 |
|
| 158 |
if (is_array($result) && empty($result[$plugin]) && is_wp_error($skin->result)) { |
| 159 |
$result = $skin->result; |
| 160 |
} |
| 161 |
|
| 162 |
$status['messages'] = $upgrader->skin->get_upgrade_messages(); |
| 163 |
|
| 164 |
if (is_array($result) && !empty($result[$plugin])) { |
| 165 |
$plugin_update_data = current($result); |
| 166 |
|
| 167 |
/* |
| 168 |
* If the `update_plugins` site transient is empty (e.g. when you update |
| 169 |
* two plugins in quick succession before the transient repopulates), |
| 170 |
* this may be the return. |
| 171 |
* |
| 172 |
* Preferably something can be done to ensure `update_plugins` isn't empty. |
| 173 |
* For now, surface some sort of error here. |
| 174 |
*/ |
| 175 |
if (true === $plugin_update_data) { |
| 176 |
$status['error'] = 'update_failed'; |
| 177 |
return $status; |
| 178 |
} |
| 179 |
|
| 180 |
$plugin_data = get_plugins('/' . $result[$plugin]['destination_name']); |
| 181 |
$plugin_data = reset($plugin_data); |
| 182 |
|
| 183 |
if ($plugin_data['Version']) { |
| 184 |
$status['newVersion'] = $plugin_data['Version']; |
| 185 |
} |
| 186 |
return $status; |
| 187 |
|
| 188 |
} elseif (is_wp_error($result)) { |
| 189 |
$status['error'] = $result->get_error_code(); |
| 190 |
$status['error_message'] = $result->get_error_message(); |
| 191 |
return $status; |
| 192 |
|
| 193 |
} elseif (is_bool($result) && !$result) { |
| 194 |
$status['error'] = 'unable_to_connect_to_filesystem'; |
| 195 |
|
| 196 |
global $wp_filesystem; |
| 197 |
|
| 198 |
// Pass through the error from WP_Filesystem if one was raised |
| 199 |
if (isset($wp_filesystem->errors) && is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 200 |
$status['error'] = $wp_filesystem->errors->get_error_code(); |
| 201 |
$status['error_message'] = $wp_filesystem->errors->get_error_message(); |
| 202 |
} |
| 203 |
|
| 204 |
return $status; |
| 205 |
|
| 206 |
} else { |
| 207 |
// An unhandled error occured |
| 208 |
$status['error'] = 'update_failed'; |
| 209 |
return $status; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Adapted from _update_theme (above) |
| 215 |
* |
| 216 |
* @param string $core |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
private function _update_core($core) { |
| 220 |
|
| 221 |
global $wp_filesystem; |
| 222 |
|
| 223 |
$status = array( |
| 224 |
'update' => 'core', |
| 225 |
'core' => $core, |
| 226 |
'oldVersion' => '', |
| 227 |
'newVersion' => '', |
| 228 |
); |
| 229 |
|
| 230 |
include(ABSPATH.WPINC.'/version.php'); |
| 231 |
|
| 232 |
$status['oldVersion'] = $wp_version; |
| 233 |
|
| 234 |
if (!current_user_can('update_core')) { |
| 235 |
$status['error'] = 'updates_permission_denied'; |
| 236 |
return $status; |
| 237 |
} |
| 238 |
|
| 239 |
include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 240 |
|
| 241 |
wp_version_check(); |
| 242 |
|
| 243 |
$locale = get_locale(); |
| 244 |
|
| 245 |
$core_update_key = false; |
| 246 |
$core_update_latest_version = false; |
| 247 |
|
| 248 |
$get_core_updates = get_core_updates(); |
| 249 |
|
| 250 |
@include(ABSPATH.WPINC.'/version.php'); |
| 251 |
|
| 252 |
foreach ($get_core_updates as $k => $core_update) { |
| 253 |
if (isset($core_update->version) && version_compare($core_update->version, $wp_version, '>') && version_compare($core_update->version, $core_update_latest_version, '>')) { |
| 254 |
$core_update_latest_version = $core_update->version; |
| 255 |
$core_update_key = $k; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
if (false === $core_update_key) { |
| 260 |
$status['error'] = 'no_update_found'; |
| 261 |
return $status; |
| 262 |
} |
| 263 |
|
| 264 |
$update = $get_core_updates[$core_update_key]; |
| 265 |
|
| 266 |
// WP < 3.7 |
| 267 |
if (!class_exists('Automatic_Upgrader_Skin')) include_once(UPDRAFTCENTRAL_CLIENT_DIR.'/classes/class-automatic-upgrader-skin.php'); |
| 268 |
|
| 269 |
$skin = new Automatic_Upgrader_Skin(); |
| 270 |
$upgrader = new Core_Upgrader($skin); |
| 271 |
|
| 272 |
$result = $upgrader->upgrade($update); |
| 273 |
|
| 274 |
$status['messages'] = $upgrader->skin->get_upgrade_messages(); |
| 275 |
|
| 276 |
if (is_wp_error($result)) { |
| 277 |
$status['error'] = $result->get_error_code(); |
| 278 |
$status['error_message'] = $result->get_error_message(); |
| 279 |
return $status; |
| 280 |
|
| 281 |
} elseif (is_bool($result) && !$result) { |
| 282 |
$status['error'] = 'unable_to_connect_to_filesystem'; |
| 283 |
|
| 284 |
// Pass through the error from WP_Filesystem if one was raised |
| 285 |
if (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 286 |
$status['error'] = $wp_filesystem->errors->get_error_code(); |
| 287 |
$status['error_message'] = $wp_filesystem->errors->get_error_message(); |
| 288 |
} |
| 289 |
|
| 290 |
return $status; |
| 291 |
|
| 292 |
|
| 293 |
} elseif (preg_match('/^[0-9]/', $result)) { |
| 294 |
|
| 295 |
$status['newVersion'] = $result; |
| 296 |
|
| 297 |
return $status; |
| 298 |
|
| 299 |
} else { |
| 300 |
// An unhandled error occured |
| 301 |
$status['error'] = 'update_failed'; |
| 302 |
return $status; |
| 303 |
} |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
private function _update_theme($theme) { |
| 308 |
|
| 309 |
global $wp_filesystem; |
| 310 |
|
| 311 |
$status = array( |
| 312 |
'update' => 'theme', |
| 313 |
'theme' => $theme, |
| 314 |
'oldVersion' => '', |
| 315 |
'newVersion' => '', |
| 316 |
); |
| 317 |
|
| 318 |
if (false !== strpos($theme, '/') || false !== strpos($theme, '\\')) { |
| 319 |
$status['error'] = 'not_found'; |
| 320 |
return $status; |
| 321 |
} |
| 322 |
|
| 323 |
$theme_version = $this->get_theme_version($theme); |
| 324 |
if (false === $theme_version) { |
| 325 |
$status['error'] = 'not_found'; |
| 326 |
return $status; |
| 327 |
} |
| 328 |
$status['oldVersion'] = $theme_version; |
| 329 |
|
| 330 |
if (!current_user_can('update_themes')) { |
| 331 |
$status['error'] = 'updates_permission_denied'; |
| 332 |
return $status; |
| 333 |
} |
| 334 |
|
| 335 |
include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 336 |
|
| 337 |
wp_update_themes(); |
| 338 |
|
| 339 |
// WP < 3.7 |
| 340 |
if (!class_exists('Automatic_Upgrader_Skin')) include_once(UPDRAFTCENTRAL_CLIENT_DIR.'/classes/class-automatic-upgrader-skin.php'); |
| 341 |
|
| 342 |
$skin = new Automatic_Upgrader_Skin(); |
| 343 |
$upgrader = new Theme_Upgrader($skin); |
| 344 |
$upgrader->init(); |
| 345 |
$result = $upgrader->bulk_upgrade(array($theme)); |
| 346 |
|
| 347 |
if (is_array($result) && empty($result[$theme]) && is_wp_error($skin->result)) { |
| 348 |
$result = $skin->result; |
| 349 |
} |
| 350 |
|
| 351 |
$status['messages'] = $upgrader->skin->get_upgrade_messages(); |
| 352 |
|
| 353 |
if (is_array($result) && !empty($result[$theme])) { |
| 354 |
$theme_update_data = current($result); |
| 355 |
|
| 356 |
/* |
| 357 |
* If the `update_themes` site transient is empty (e.g. when you update |
| 358 |
* two plugins in quick succession before the transient repopulates), |
| 359 |
* this may be the return. |
| 360 |
* |
| 361 |
* Preferably something can be done to ensure `update_themes` isn't empty. |
| 362 |
* For now, surface some sort of error here. |
| 363 |
*/ |
| 364 |
if (true === $theme_update_data) { |
| 365 |
$status['error'] = 'update_failed'; |
| 366 |
return $status; |
| 367 |
} |
| 368 |
|
| 369 |
$new_theme_version = $this->get_theme_version($theme); |
| 370 |
if (false === $new_theme_version) { |
| 371 |
$status['error'] = 'update_failed'; |
| 372 |
return $status; |
| 373 |
} |
| 374 |
|
| 375 |
$status['newVersion'] = $new_theme_version; |
| 376 |
|
| 377 |
return $status; |
| 378 |
|
| 379 |
} elseif (is_wp_error($result)) { |
| 380 |
$status['error'] = $result->get_error_code(); |
| 381 |
$status['error_message'] = $result->get_error_message(); |
| 382 |
return $status; |
| 383 |
|
| 384 |
} elseif (is_bool($result) && !$result) { |
| 385 |
$status['error'] = 'unable_to_connect_to_filesystem'; |
| 386 |
|
| 387 |
// Pass through the error from WP_Filesystem if one was raised |
| 388 |
if (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 389 |
$status['error'] = $wp_filesystem->errors->get_error_code(); |
| 390 |
$status['error_message'] = $wp_filesystem->errors->get_error_message(); |
| 391 |
} |
| 392 |
|
| 393 |
return $status; |
| 394 |
|
| 395 |
} else { |
| 396 |
// An unhandled error occured |
| 397 |
$status['error'] = 'update_failed'; |
| 398 |
return $status; |
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
private function get_theme_version($theme) { |
| 404 |
|
| 405 |
if (function_exists('wp_get_theme')) { |
| 406 |
// Since WP 3.4.0 |
| 407 |
$theme = wp_get_theme($theme); |
| 408 |
|
| 409 |
if (is_a($theme, 'WP_Theme')) { |
| 410 |
return $theme->Version; |
| 411 |
} else { |
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
} else { |
| 416 |
$theme_data = get_theme_data(WP_CONTENT_DIR . '/themes/'.$theme.'/style.css'); |
| 417 |
|
| 418 |
if (isset($theme_data['Version'])) { |
| 419 |
return $theme_data['Version']; |
| 420 |
} else { |
| 421 |
return false; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Adding third-party plugins/theme for UDC automatic updates, for some updaters which store their information when the transient is set, instead of (like most) when it is fetched |
| 428 |
* |
| 429 |
* @param Array $items A collection of plugins or themes for updates |
| 430 |
* @param String $type A string indicating which type of collection to process (e.g. 'plugin' or 'theme') |
| 431 |
* @return Array An updated collection of plugins or themes for updates |
| 432 |
*/ |
| 433 |
private function maybe_add_third_party_items($items, $type) { |
| 434 |
|
| 435 |
// Here we're preparing a dummy transient object that will be pass to the filter |
| 436 |
// and gets populated by those plugins or themes that hooked into the "pre_set_site_transient_*" filter. |
| 437 |
// |
| 438 |
// We're setting some default properties so that plugins and themes won't be able to bypass populating them, |
| 439 |
// because most of the plugins and themes updater scripts checks whether or not these properties are set and |
| 440 |
// non-empty or passed the 12 hour period (where WordPress re-starts the process of checking updates for |
| 441 |
// these plugins and themes), otherwise, they bypass populating the update/upgrade info for these items. |
| 442 |
$transient = (object) array( |
| 443 |
'last_checked' => time() - (13 * 3600), /* Making sure that we passed the 12 hour period check */ |
| 444 |
'checked' => array('default' => 'none'), |
| 445 |
'response' => array('default' => 'none') |
| 446 |
); |
| 447 |
|
| 448 |
// Most of the premium plugin developers are hooking into the "pre_set_site_transient_update_plugins" and |
| 449 |
// "pre_set_site_transient_update_themes" filters if they want their plugins or themes to support automatic |
| 450 |
// updates. Thus, we're making sure here that if for some reason, those plugins or themes didn't get through |
| 451 |
// and added to the "update_plugins" or "update_themes" transients when calling the get_site_transient('update_plugins') |
| 452 |
// or get_site_transient('update_themes') we add them here manually. |
| 453 |
$filters = apply_filters("pre_set_site_transient_update_{$type}s", $transient, "update_{$type}s"); |
| 454 |
|
| 455 |
|
| 456 |
$all_items = array(); |
| 457 |
switch ($type) { |
| 458 |
case 'plugin': |
| 459 |
$all_items = get_plugins(); |
| 460 |
break; |
| 461 |
case 'theme': |
| 462 |
$all_items = get_themes(); |
| 463 |
break; |
| 464 |
default: |
| 465 |
break; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
if (!empty($all_items)) { |
| 470 |
$all_items = (array) $all_items; |
| 471 |
foreach ($all_items as $key => $data) { |
| 472 |
if (!isset($items[$key]) && isset($filters->response[$key])) { |
| 473 |
|
| 474 |
$update_info = ('plugin' === $type) ? $filters->response[$key] : $data; |
| 475 |
|
| 476 |
// If "package" is empty, it means that this plugin or theme does not support automatic updates |
| 477 |
// currently, since the "package" field is the one holding the download link of these plugins/themes |
| 478 |
// and WordPress is using this field to download the latest version of these items. |
| 479 |
// |
| 480 |
// Most of the time, this "package" field is not empty, but for premium plugins/themes this can be |
| 481 |
// conditional, only then if the user provides a legit access or api key can this field be populated or available. |
| 482 |
// |
| 483 |
// We set this variable to "false" by default, as plugins/themes hosted in wordpress.org always sets this |
| 484 |
// to the downloadable zip file of the plugin/theme. |
| 485 |
// |
| 486 |
// N.B. We only add premium plugins/themes that has this "package" field set and non-empty, otherwise, it |
| 487 |
// does not support automatic updates as explained above. |
| 488 |
$is_package_empty = false; |
| 489 |
|
| 490 |
if (is_object($update_info)) { |
| 491 |
if (!isset($update_info->package) || empty($update_info->package)) { |
| 492 |
$is_package_empty = true; |
| 493 |
} |
| 494 |
|
| 495 |
} elseif (is_array($update_info)) { |
| 496 |
if (!isset($update_info['package']) || empty($update_info['package'])) { |
| 497 |
$is_package_empty = true; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
// Add this plugin/theme to the current updates collection |
| 502 |
if (!$is_package_empty) { |
| 503 |
$items[$key] = ('plugin' === $type) ? (object) $data : $this->get_theme_info($key); |
| 504 |
$items[$key]->update = $update_info; |
| 505 |
} |
| 506 |
|
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return $this->prep_items_for_updates($items, $type); |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* Extracts theme's data or information |
| 516 |
* |
| 517 |
* @param string $theme A string representing a theme's name or slug. |
| 518 |
* @return object|boolean If successful, an object containing the theme data or information, "false" otherwise. |
| 519 |
*/ |
| 520 |
private function get_theme_info($theme) { |
| 521 |
|
| 522 |
if (function_exists('wp_get_theme')) { |
| 523 |
$theme = wp_get_theme($theme); |
| 524 |
if (is_a($theme, 'WP_Theme')) { |
| 525 |
return $theme; |
| 526 |
} |
| 527 |
} else { |
| 528 |
$theme_data = get_theme_data(WP_CONTENT_DIR.'/themes/'.$theme.'/style.css'); |
| 529 |
if (isset($theme_data['Version'])) { |
| 530 |
if (!isset($theme_data['ThemeURI'])) $theme_data['ThemeURI'] = $theme_data['URI']; |
| 531 |
return (object) $theme_data; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Fix items for update with missing "plugin" or "theme" field if applicable |
| 540 |
* |
| 541 |
* @param Array $items A collection of plugins or themes for updates |
| 542 |
* @param String $type A string indicating which type of collection to process (e.g. 'plugin' or 'theme') |
| 543 |
* @return Array An updated collection of plugins or themes for updates |
| 544 |
*/ |
| 545 |
private function prep_items_for_updates($items, $type) { |
| 546 |
|
| 547 |
foreach ($items as $key => $data) { |
| 548 |
$update_info = $data->update; |
| 549 |
|
| 550 |
// Some plugins and/or themes does not adhere to the standard WordPress updates meta |
| 551 |
// properties/fields. Thus, missing some fields such as "plugin" or "theme" |
| 552 |
// in their update information results in "Automatic updates is unavailable for this item" |
| 553 |
// in UDC since we're using these fields to process the updates. |
| 554 |
// |
| 555 |
// As a workaround, we're filling these missing fields in order to solve the above issue |
| 556 |
// in case the developer of these plugins/themes forgot to include them. |
| 557 |
if (is_object($update_info)) { |
| 558 |
$update_info = (array) $update_info; |
| 559 |
if (!isset($update_info[$type])) { |
| 560 |
$update_info[$type] = $key; |
| 561 |
} |
| 562 |
|
| 563 |
$update_info = (object) $update_info; |
| 564 |
|
| 565 |
} elseif (is_array($update_info)) { |
| 566 |
if (!isset($update_info[$type])) { |
| 567 |
$update_info[$type] = $key; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
// Re-assign the updated info to the original "update" property |
| 572 |
$items[$key]->update = $update_info; |
| 573 |
} |
| 574 |
|
| 575 |
return $items; |
| 576 |
} |
| 577 |
|
| 578 |
public function get_updates($options) { |
| 579 |
|
| 580 |
// Forcing Elegant Themes (Divi) updates component to load if it exist. |
| 581 |
if (function_exists('et_register_updates_component')) { |
| 582 |
et_register_updates_component(); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
if (!current_user_can('update_plugins') && !current_user_can('update_themes') && !current_user_can('update_core')) return $this->_generic_error_response('updates_permission_denied'); |
| 587 |
|
| 588 |
$this->_admin_include('plugin.php', 'update.php', 'file.php', 'template.php'); |
| 589 |
$this->_frontend_include('update.php'); |
| 590 |
|
| 591 |
if (!is_array($options)) $options = array(); |
| 592 |
|
| 593 |
// Normalise it |
| 594 |
$plugin_updates = array(); |
| 595 |
if (current_user_can('update_plugins')) { |
| 596 |
|
| 597 |
// Detect if refresh needed |
| 598 |
$transient = get_site_transient('update_plugins'); |
| 599 |
if (!empty($options['force_refresh']) || false === $transient) { |
| 600 |
delete_site_transient('update_plugins'); |
| 601 |
wp_update_plugins(); |
| 602 |
} |
| 603 |
|
| 604 |
$get_plugin_updates = $this->maybe_add_third_party_items(get_plugin_updates(), 'plugin'); |
| 605 |
if (is_array($get_plugin_updates)) { |
| 606 |
foreach ($get_plugin_updates as $update) { |
| 607 |
|
| 608 |
// For some reason, some 3rd-party (premium) plugins are returning the same version |
| 609 |
// with that of the currently installed version in WordPress. Thus, we're making sure here to |
| 610 |
// only return those items for update that has new versions greater than the currently installed version. |
| 611 |
if (version_compare($update->Version, $update->update->new_version, '>=')) continue; |
| 612 |
|
| 613 |
$plugin_updates[] = array( |
| 614 |
'name' => $update->Name, |
| 615 |
'plugin_uri' => $update->PluginURI, |
| 616 |
'version' => $update->Version, |
| 617 |
'description' => $update->Description, |
| 618 |
'author' => $update->Author, |
| 619 |
'author_uri' => $update->AuthorURI, |
| 620 |
'title' => $update->Title, |
| 621 |
'author_name' => $update->AuthorName, |
| 622 |
'update' => array( |
| 623 |
// With Affiliates-WP, if you have not connected, this is null. |
| 624 |
'plugin' => isset($update->update->plugin) ? $update->update->plugin : null, |
| 625 |
'slug' => $update->update->slug, |
| 626 |
'new_version' => $update->update->new_version, |
| 627 |
'package' => $update->update->package, |
| 628 |
'tested' => isset($update->update->tested) ? $update->update->tested : null, |
| 629 |
'compatibility' => isset($update->update->compatibility) ? (array) $update->update->compatibility : null, |
| 630 |
'sections' => isset($update->update->sections) ? (array) $update->update->sections : null, |
| 631 |
), |
| 632 |
); |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
$theme_updates = array(); |
| 638 |
if (current_user_can('update_themes')) { |
| 639 |
|
| 640 |
// Detect if refresh needed |
| 641 |
$transient = get_site_transient('update_themes'); |
| 642 |
if (!empty($options['force_refresh']) || false === $transient) { |
| 643 |
delete_site_transient('update_themes'); |
| 644 |
wp_update_themes(); |
| 645 |
} |
| 646 |
$get_theme_updates = $this->maybe_add_third_party_items(get_theme_updates(), 'theme'); |
| 647 |
if (is_array($get_theme_updates)) { |
| 648 |
foreach ($get_theme_updates as $update) { |
| 649 |
|
| 650 |
// We're making sure here to only return those items for update that has new |
| 651 |
// versions greater than the currently installed version. |
| 652 |
if (version_compare($update->Version, $update->update['new_version'], '>=')) continue; |
| 653 |
|
| 654 |
$theme_updates[] = array( |
| 655 |
'name' => $update->Name, |
| 656 |
'theme_uri' => $update->ThemeURI, |
| 657 |
'version' => $update->Version, |
| 658 |
'description' => $update->Description, |
| 659 |
'author' => $update->Author, |
| 660 |
'author_uri' => $update->AuthorURI, |
| 661 |
'update' => array( |
| 662 |
'theme' => $update->update['theme'], |
| 663 |
'new_version' => $update->update['new_version'], |
| 664 |
'package' => $update->update['package'], |
| 665 |
'url' => $update->update['url'], |
| 666 |
), |
| 667 |
); |
| 668 |
|
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
$core_updates = array(); |
| 674 |
if (current_user_can('update_core')) { |
| 675 |
|
| 676 |
// Detect if refresh needed |
| 677 |
$transient = get_site_transient('update_core'); |
| 678 |
if (!empty($options['force_refresh']) || false === $transient) { |
| 679 |
// The next line is only needed for older WP versions - otherwise, the parameter to wp_version_check forces a check. |
| 680 |
delete_site_transient('update_core'); |
| 681 |
wp_version_check(array(), true); |
| 682 |
} |
| 683 |
|
| 684 |
$get_core_updates = get_core_updates(); |
| 685 |
|
| 686 |
if (is_array($get_core_updates)) { |
| 687 |
|
| 688 |
$core_update_key = false; |
| 689 |
$core_update_latest_version = false; |
| 690 |
|
| 691 |
@include(ABSPATH.WPINC.'/version.php'); |
| 692 |
|
| 693 |
foreach ($get_core_updates as $k => $core_update) { |
| 694 |
if (isset($core_update->version) && version_compare($core_update->version, $wp_version, '>') && version_compare($core_update->version, $core_update_latest_version, '>')) { |
| 695 |
$core_update_latest_version = $core_update->version; |
| 696 |
$core_update_key = $k; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
if (false !== $core_update_key) { |
| 701 |
|
| 702 |
$update = $get_core_updates[$core_update_key]; |
| 703 |
|
| 704 |
global $wpdb; |
| 705 |
|
| 706 |
$mysql_version = $wpdb->db_version(); |
| 707 |
|
| 708 |
$is_mysql = (file_exists(WP_CONTENT_DIR . '/db.php') && empty($wpdb->is_mysql)) ? false : true; |
| 709 |
|
| 710 |
// We're making sure here to only return those items for update that has new |
| 711 |
// versions greater than the currently installed version. |
| 712 |
if (version_compare($wp_version, $update->version, '<')) { |
| 713 |
$core_updates[] = array( |
| 714 |
'download' => $update->download, |
| 715 |
'version' => $update->version, |
| 716 |
'php_version' => $update->php_version, |
| 717 |
'mysql_version' => $update->mysql_version, |
| 718 |
'installed' => array( |
| 719 |
'version' => $wp_version, |
| 720 |
'mysql' => $mysql_version, |
| 721 |
'php' => PHP_VERSION, |
| 722 |
'is_mysql' => $is_mysql, |
| 723 |
), |
| 724 |
'sufficient' => array( |
| 725 |
'mysql' => version_compare($mysql_version, $update->mysql_version, '>='), |
| 726 |
'php' => version_compare(PHP_VERSION, $update->php_version, '>='), |
| 727 |
), |
| 728 |
); |
| 729 |
} |
| 730 |
|
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
// Do we need to ask the user for filesystem credentials? |
| 738 |
$request_filesystem_credentials = array(); |
| 739 |
$check_fs = array( |
| 740 |
'plugins' => WP_PLUGIN_DIR, |
| 741 |
'themes' => WP_CONTENT_DIR.'/themes', |
| 742 |
'core' => untrailingslashit(ABSPATH) |
| 743 |
); |
| 744 |
|
| 745 |
foreach ($check_fs as $entity => $dir) { |
| 746 |
$filesystem_method = get_filesystem_method(array(), $dir); |
| 747 |
ob_start(); |
| 748 |
$filesystem_credentials_are_stored = request_filesystem_credentials(site_url()); |
| 749 |
$filesystem_form = strip_tags(ob_get_contents(), '<div><h2><p><input><label><fieldset><legend><span><em>'); |
| 750 |
ob_end_clean(); |
| 751 |
$request_filesystem_credentials[$entity] = ('direct' != $filesystem_method && !$filesystem_credentials_are_stored); |
| 752 |
} |
| 753 |
|
| 754 |
$automatic_backups = (class_exists('UpdraftPlus_Options') && class_exists('UpdraftPlus_Addon_Autobackup') && UpdraftPlus_Options::get_updraft_option('updraft_autobackup_default', true)) ? true : false; |
| 755 |
|
| 756 |
return $this->_response(array( |
| 757 |
'plugins' => $plugin_updates, |
| 758 |
'themes' => $theme_updates, |
| 759 |
'core' => $core_updates, |
| 760 |
'meta' => array( |
| 761 |
'request_filesystem_credentials' => $request_filesystem_credentials, |
| 762 |
'filesystem_form' => $filesystem_form, |
| 763 |
'automatic_backups' => $automatic_backups |
| 764 |
), |
| 765 |
)); |
| 766 |
} |
| 767 |
} |
| 768 |
|