| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Licensing; |
| 4 |
|
| 5 |
use Boxzilla\Plugin; |
| 6 |
|
| 7 |
class UpdateManager |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
protected $extensions = array(); |
| 14 |
|
| 15 |
/** |
| 16 |
* @var API |
| 17 |
*/ |
| 18 |
protected $api; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var License |
| 22 |
*/ |
| 23 |
protected $license; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var |
| 27 |
*/ |
| 28 |
protected $available_updates; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param array $extensions |
| 32 |
* @param API $api |
| 33 |
* @param License $license |
| 34 |
*/ |
| 35 |
public function __construct(array $extensions, API $api, License $license) |
| 36 |
{ |
| 37 |
$this->extensions = $extensions; |
| 38 |
$this->license = $license; |
| 39 |
$this->api = $api; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Add hooks |
| 44 |
*/ |
| 45 |
public function hook() |
| 46 |
{ |
| 47 |
add_filter('pre_set_site_transient_update_plugins', array( $this, 'add_updates' )); |
| 48 |
add_filter('plugins_api', array( $this, 'get_plugin_info' ), 20, 3); |
| 49 |
add_filter('http_request_args', array( $this, 'add_auth_headers' ), 10, 2); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* This adds the license key header to download package requests. |
| 54 |
* |
| 55 |
* @param array $args |
| 56 |
* @param string $url |
| 57 |
* |
| 58 |
* @return mixed |
| 59 |
*/ |
| 60 |
public function add_auth_headers($args, $url) |
| 61 |
{ |
| 62 |
// only act on download request's to the Boxzilla update API |
| 63 |
if (strpos($url, $this->api->url) !== 0 || strpos($url, '/download') === false) { |
| 64 |
return $args; |
| 65 |
} |
| 66 |
|
| 67 |
// only add if activation key not empty |
| 68 |
if (empty($this->license->activation_key)) { |
| 69 |
return $args; |
| 70 |
} |
| 71 |
|
| 72 |
if (! isset($args['headers'])) { |
| 73 |
$args['headers'] = array(); |
| 74 |
} |
| 75 |
|
| 76 |
$args['headers']['Authorization'] = sprintf('Bearer %s', $this->license->activation_key); |
| 77 |
return $args; |
| 78 |
} |
| 79 |
|
| 80 |
private function get_plugin_by_slug($slug) |
| 81 |
{ |
| 82 |
foreach ($this->extensions as $p) { |
| 83 |
// find plugin by slug |
| 84 |
if (dirname($p->slug()) === $slug) { |
| 85 |
return $p; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param $result |
| 94 |
* @param string $action |
| 95 |
* @param null $args |
| 96 |
* |
| 97 |
* @return object |
| 98 |
*/ |
| 99 |
public function get_plugin_info($result, $action = '', $args = null) |
| 100 |
{ |
| 101 |
// do nothing for unrelated requests |
| 102 |
if ($action !== 'plugin_information' || ! isset($args->slug)) { |
| 103 |
return $result; |
| 104 |
} |
| 105 |
|
| 106 |
// only act on our own plugins |
| 107 |
$plugin = $this->get_plugin_by_slug($args->slug); |
| 108 |
if ($plugin === null) { |
| 109 |
return $result; |
| 110 |
} |
| 111 |
|
| 112 |
return $this->get_update_info($plugin); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @param object $updates |
| 117 |
* @return object |
| 118 |
*/ |
| 119 |
public function add_updates($updates) |
| 120 |
{ |
| 121 |
|
| 122 |
// do nothing if no plugins registered |
| 123 |
if (empty($this->extensions)) { |
| 124 |
return $updates; |
| 125 |
} |
| 126 |
|
| 127 |
// failsafe WP bug |
| 128 |
if (empty($updates) || ! isset($updates->response) || ! is_array($updates->response)) { |
| 129 |
return $updates; |
| 130 |
} |
| 131 |
|
| 132 |
// fetch available updates |
| 133 |
$available_updates = $this->fetch_updates(); |
| 134 |
|
| 135 |
// merge with other updates |
| 136 |
$updates->response = array_merge($updates->response, $available_updates); |
| 137 |
|
| 138 |
return $updates; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Fetch array of available updates from remote server |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
protected function fetch_updates() |
| 147 |
{ |
| 148 |
if (is_array($this->available_updates)) { |
| 149 |
return $this->available_updates; |
| 150 |
} |
| 151 |
|
| 152 |
// don't try if we failed a request recently. |
| 153 |
$failed_at = get_transient('boxzilla_request_failed'); |
| 154 |
if (! empty($failed_at) && ((strtotime('now') - 300) < $failed_at)) { |
| 155 |
return array(); |
| 156 |
} |
| 157 |
|
| 158 |
// fetch remote info |
| 159 |
try { |
| 160 |
$remote_plugins = $this->api->get_plugins(); |
| 161 |
} catch (API_Exception $e) { |
| 162 |
// set flag for 5 minutes |
| 163 |
set_transient('boxzilla_request_failed', strtotime('now'), 300); |
| 164 |
return array(); |
| 165 |
} |
| 166 |
|
| 167 |
// filter remote plugins, we only want the ones with an update available |
| 168 |
$this->available_updates = $this->filter_remote_plugins($remote_plugins); |
| 169 |
|
| 170 |
return $this->available_updates; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param $remote_plugins |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
protected function filter_remote_plugins($remote_plugins) |
| 178 |
{ |
| 179 |
$available_updates = array(); |
| 180 |
|
| 181 |
// find new versions |
| 182 |
foreach ($remote_plugins as $remote_plugin) { |
| 183 |
|
| 184 |
// only act on update info of our own plugins |
| 185 |
if (! isset($this->extensions[ $remote_plugin->sid ])) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
// sanity check |
| 190 |
if (! isset($remote_plugin->new_version)) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
// find corresponding local plugin |
| 195 |
/** @var Plugin $local_plugin */ |
| 196 |
$local_plugin = $this->extensions[ $remote_plugin->sid ]; |
| 197 |
|
| 198 |
// plugin found and local plugin version not same as remote version? |
| 199 |
if (! $local_plugin || version_compare($local_plugin->version(), $remote_plugin->new_version, '>=')) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
// add some dynamic data |
| 204 |
$available_updates[ $local_plugin->slug() ] = $this->format_response($local_plugin, $remote_plugin); |
| 205 |
} |
| 206 |
|
| 207 |
return $available_updates; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @param Plugin $plugin |
| 212 |
* |
| 213 |
* @return null |
| 214 |
*/ |
| 215 |
public function get_update_info(Plugin $plugin) |
| 216 |
{ |
| 217 |
$available_updates = $this->fetch_updates(); |
| 218 |
|
| 219 |
if (isset($available_updates[ $plugin->slug() ])) { |
| 220 |
return $available_updates[ $plugin->slug() ]; |
| 221 |
} |
| 222 |
|
| 223 |
return null; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @param Plugin $plugin |
| 228 |
* @param $response |
| 229 |
* |
| 230 |
* @return mixed |
| 231 |
*/ |
| 232 |
protected function format_response(Plugin $plugin, $response) |
| 233 |
{ |
| 234 |
$response->slug = dirname($plugin->slug()); |
| 235 |
$response->plugin = $plugin->slug(); |
| 236 |
|
| 237 |
// add some notices if license is inactive |
| 238 |
if (! $this->license->activated) { |
| 239 |
$response->upgrade_notice = sprintf('You will need to <a href="%s">activate your license</a> to install this plugin update.', admin_url('edit.php?post_type=boxzilla-box&page=boxzilla-settings')); |
| 240 |
$response->sections->changelog = '<p>' . sprintf('You will need to <a href="%s" target="_top">activate your license</a> to install this plugin update.', admin_url('edit.php?post_type=boxzilla-box&page=boxzilla-settings')) . '</p>' . $response->sections->changelog; |
| 241 |
$response->package = null; |
| 242 |
} |
| 243 |
|
| 244 |
// cast subkey objects to array as that is what WP expects |
| 245 |
$response->sections = get_object_vars($response->sections); |
| 246 |
$response->banners = get_object_vars($response->banners); |
| 247 |
$response->contributors = get_object_vars($response->contributors); |
| 248 |
$response->contributors = array_map(function ($v) { |
| 249 |
return get_object_vars($v); |
| 250 |
}, $response->contributors); |
| 251 |
|
| 252 |
return $response; |
| 253 |
} |
| 254 |
} |
| 255 |
|