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