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