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