| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Updater_UpdateManager |
| 12 |
{ |
| 13 |
|
| 14 |
private $context; |
| 15 |
|
| 16 |
private $coreInfo; |
| 17 |
|
| 18 |
private $pluginInfo; |
| 19 |
|
| 20 |
private $themeInfo; |
| 21 |
|
| 22 |
public function __construct(MWP_WordPress_Context $context) |
| 23 |
{ |
| 24 |
$this->context = $context; |
| 25 |
$this->reload(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @return int|null UNIX timestamp of last update check. |
| 30 |
*/ |
| 31 |
public function getLastChecked() |
| 32 |
{ |
| 33 |
if (empty($this->pluginInfo->last_checked)) { |
| 34 |
return null; |
| 35 |
} |
| 36 |
|
| 37 |
return $this->pluginInfo->last_checked; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param string $pluginBasename Ie. 'worker/init.php' |
| 42 |
* |
| 43 |
* @return MWP_Updater_PluginUpdate|null |
| 44 |
*/ |
| 45 |
public function getPluginUpdate($pluginBasename) |
| 46 |
{ |
| 47 |
if (empty($this->pluginInfo->response[$pluginBasename])) { |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
$info = $this->pluginInfo->response[$pluginBasename]; |
| 52 |
|
| 53 |
$update = new MWP_Updater_PluginUpdate(); |
| 54 |
$update->id = $info->id; |
| 55 |
$update->version = $info->new_version; |
| 56 |
$update->basename = $pluginBasename; |
| 57 |
$update->url = $info->url; |
| 58 |
$update->package = $info->package; |
| 59 |
$update->slug = $info->plugin; |
| 60 |
|
| 61 |
return $update; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param string $themeSlug Ie. 'twentyeleven'. |
| 66 |
* |
| 67 |
* @return MWP_Updater_ThemeUpdate|null |
| 68 |
*/ |
| 69 |
public function getThemeUpdate($themeSlug) |
| 70 |
{ |
| 71 |
if (empty($this->themeInfo->response[$themeSlug])) { |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
$info = $this->themeInfo->response[$themeSlug]; |
| 76 |
|
| 77 |
$update = new MWP_Updater_ThemeUpdate(); |
| 78 |
$update->slug = $info['theme']; // === $themeSlug |
| 79 |
$update->version = $info['new_version']; |
| 80 |
$update->url = $info['url']; |
| 81 |
$update->package = $info['package']; |
| 82 |
|
| 83 |
return $update; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return MWP_Updater_PluginUpdate[] |
| 88 |
*/ |
| 89 |
public function getPluginUpdates() |
| 90 |
{ |
| 91 |
if (empty($this->pluginInfo->response)) { |
| 92 |
return array(); |
| 93 |
} |
| 94 |
|
| 95 |
$updates = array(); |
| 96 |
foreach (array_keys($this->pluginInfo->response) as $basename) { |
| 97 |
$updates[] = $this->getPluginUpdate($basename); |
| 98 |
} |
| 99 |
|
| 100 |
return $updates; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return MWP_Updater_ThemeUpdate[] |
| 105 |
*/ |
| 106 |
public function getThemeUpdates() |
| 107 |
{ |
| 108 |
if (empty($this->themeInfo->response)) { |
| 109 |
return array(); |
| 110 |
} |
| 111 |
|
| 112 |
$updates = array(); |
| 113 |
foreach (array_keys($this->themeInfo->response) as $slug) { |
| 114 |
$updates[] = $this->getThemeUpdate($slug); |
| 115 |
} |
| 116 |
|
| 117 |
return $updates; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @return MWP_Updater_CoreUpdate[] |
| 122 |
* |
| 123 |
* @see core_upgrade_preamble |
| 124 |
*/ |
| 125 |
public function getCoreUpdates() |
| 126 |
{ |
| 127 |
if (empty($this->coreInfo->updates)) { |
| 128 |
return array(); |
| 129 |
} |
| 130 |
|
| 131 |
$firstUpdate = $this->coreInfo->updates[0]; |
| 132 |
|
| 133 |
if (!isset($firstUpdate->response) || 'latest' === $firstUpdate->response) { |
| 134 |
return array(); |
| 135 |
} |
| 136 |
|
| 137 |
$locale = $this->context->getLocale(); |
| 138 |
$latestUpdate = null; |
| 139 |
|
| 140 |
foreach ($this->coreInfo->updates as $update) { |
| 141 |
if ($update->response === 'upgrade' && $update->locale === $locale) { |
| 142 |
$latestUpdate = $update; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
$updates = array(); |
| 147 |
|
| 148 |
if ($latestUpdate !== null) { |
| 149 |
$showUpdate = new MWP_Updater_CoreUpdate(); |
| 150 |
$showUpdate->locale = $latestUpdate->locale; |
| 151 |
$showUpdate->type = $latestUpdate->response; |
| 152 |
$showUpdate->version = $latestUpdate->version; |
| 153 |
$showUpdate->phpVersion = $latestUpdate->php_version; |
| 154 |
$showUpdate->mysqlVersion = $latestUpdate->mysql_version; |
| 155 |
|
| 156 |
$updates[] = $showUpdate; |
| 157 |
} |
| 158 |
|
| 159 |
return $updates; |
| 160 |
} |
| 161 |
|
| 162 |
private function reload() |
| 163 |
{ |
| 164 |
$this->coreInfo = $this->context->transientGet('update_core'); |
| 165 |
$this->pluginInfo = $this->context->transientGet('update_plugins'); |
| 166 |
$this->themeInfo = $this->context->transientGet('update_themes'); |
| 167 |
} |
| 168 |
} |
| 169 |
|