PluginProbe
Boxzilla – WordPress Popup Builder / 3.4.11
Boxzilla – WordPress Popup Builder v3.4.11
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 +213 -187 3.1.173.4.11 View file →
@@ -1,236 +1,262 @@
1 1 <?php
2 2
3 3 namespace Boxzilla\Licensing;
4 4
5 -use Boxzilla\Collection,
6 - Boxzilla\Plugin,
7 - Boxzilla\Admin\Notices;
5 +use Boxzilla\Plugin;
8 6
9 -class UpdateManager {
7 +if (! defined('ABSPATH')) {
8 + exit;
9 +}
10 10
11 - /**
12 - * @var Collection
13 - */
14 - protected $extensions;
11 +class UpdateManager
12 +{
13 + /**
14 + * @var array
15 + */
16 + protected $extensions = [];
15 17
16 - /**
17 - * @var API
18 - */
19 - protected $api;
18 + /**
19 + * @var API
20 + */
21 + protected $api;
20 22
21 - /**
22 - * @var License
23 - */
24 - protected $license;
23 + /**
24 + * @var License
25 + */
26 + protected $license;
25 27
26 - /**
27 - * @var
28 - */
29 - protected $available_updates;
28 + /**
29 + * @var array
30 + */
31 + protected $available_updates;
30 32
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 - }
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 + }
41 44
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 - }
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 + }
50 54
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 - }
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 + }
64 69
65 - // only add if activation key not empty
66 - if( empty( $this->license->activation_key ) ) {
67 - return $args;
68 - }
70 + // only add if activation key not empty
71 + if (empty($this->license->activation_key)) {
72 + return $args;
73 + }
69 74
70 - if( empty( $args['headers'] ) ) {
71 - $args['headers'] = array();
72 - }
75 + if (! isset($args['headers'])) {
76 + $args['headers'] = [];
77 + }
73 78
74 - $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->activation_key );
75 - return $args;
76 - }
79 + $args['headers']['Authorization'] = "Bearer {$this->license->activation_key}";
80 + return $args;
81 + }
77 82
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 ) {
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 + }
86 96
87 - // do nothing for unrelated requests
88 - if( $action !== 'plugin_information' || ! isset( $args->slug ) ) {
89 - return $result;
90 - }
97 + // only act on our plugins
98 + if (strpos($args->slug, 'boxzilla-') !== 0) {
99 + return $result;
100 + }
91 101
92 - // only act on our plugins
93 - $plugin = $this->extensions->find( function( $p ) use($args) {
94 - return dirname( $p->slug() ) == $args->slug;
95 - });
102 + return $this->get_update_info($args->slug);
103 + }
96 104
97 - // was it a plugin of ours?
98 - if( $plugin ) {
99 - return $this->get_update_info( $plugin );
100 - }
105 + /**
106 + * @param null|object $updates
107 + * @return object
108 + */
109 + public function add_updates($updates)
110 + {
101 111
102 - return $result;
103 - }
112 + // do nothing if no plugins registered
113 + if (empty($this->extensions)) {
114 + return $updates;
115 + }
104 116
105 - /**
106 - * @param object $updates
107 - * @return object
108 - */
109 - public function add_updates( $updates ) {
117 + // failsafe WP bug
118 + if (empty($updates) || ! isset($updates->response) || ! is_array($updates->response)) {
119 + return $updates;
120 + }
110 121
111 - // do nothing if no plugins registered
112 - if( count( $this->extensions ) === 0 ) {
113 - return $updates;
114 - }
122 + // fetch available updates
123 + $available_updates = $this->fetch_updates();
115 124
116 - // failsafe WP bug
117 - if( empty( $updates )
118 - || empty( $updates->response )
119 - || ! is_array( $updates->response ) ) {
120 - return $updates;
121 - }
125 + // merge with other updates
126 + $updates->response = array_merge($updates->response, $available_updates);
122 127
123 - // fetch available updates
124 - $available_updates = $this->fetch_updates();
128 + return $updates;
129 + }
125 130
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;
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;
141 140 }
142 141
143 142 // don't try if we failed a request recently.
144 - $failed_at = get_transient( 'boxzilla_request_failed' );
145 - if( ! empty( $failed_at ) && ( (strtotime('now') - 300) < $failed_at ) ) {
146 - return array();
143 + $failed_at = get_transient('boxzilla_request_failed');
144 + if (! empty($failed_at) && ( ( time() - 300 ) < $failed_at )) {
145 + return [];
147 146 }
148 147
149 - // fetch remote info
150 - try {
151 - $remote_plugins = $this->api->get_plugins( $this->extensions );
152 - } catch( API_Exception $e ) {
148 + // fetch remote info
149 + try {
150 + $remote_plugins = $this->api->get_plugins();
151 + } catch (API_Exception $e) {
153 152 // set flag for 5 minutes
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 );
153 + set_transient('boxzilla_request_failed', time(), 300);
154 + return [];
155 + }
160 156
161 - return $this->available_updates;
157 + // filter remote plugins, we only want the ones with an update available
158 + $this->available_updates = $this->filter_remote_plugins($remote_plugins);
162 159
163 - }
160 + return $this->available_updates;
161 + }
164 162
165 - /**
166 - * @param $remote_plugins
167 - * @return array
168 - */
169 - protected function filter_remote_plugins( $remote_plugins ) {
163 + /**
164 + * @param $remote_plugins
165 + * @return array
166 + */
167 + protected function filter_remote_plugins($remote_plugins)
168 + {
169 + $available_updates = [];
170 170
171 - $available_updates = array();
171 + // find new versions
172 + foreach ($remote_plugins as $remote_plugin) {
173 + // sanity check
174 + if (! isset($remote_plugin->new_version)) {
175 + continue;
176 + }
172 177
173 - // find new versions
174 - foreach( $remote_plugins as $remote_plugin ) {
178 + // if plugin is activated, we can access it here
179 + if (isset($this->extensions[ $remote_plugin->sid ])) {
175 180
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 - );
181 + /** @var Plugin $local_plugin */
182 + $local_plugin = $this->extensions[ $remote_plugin->sid ];
184 183
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 - }
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 + }
189 188
190 - // add some dynamic data
191 - $available_updates[ $local_plugin->slug() ] = $this->format_response( $local_plugin, $remote_plugin );
192 - }
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 + }
193 197
194 - return $available_updates;
195 - }
198 + $plugin_data = get_plugin_data($plugin_file);
196 199
197 - /**
198 - * @param Plugin $plugin
199 - *
200 - * @return null
201 - */
202 - public function get_update_info( Plugin $plugin ) {
203 - $available_updates = $this->fetch_updates();
200 + if (! $plugin_data || version_compare($plugin_data['Version'], $remote_plugin->new_version, '>=')) {
201 + continue;
202 + }
204 203
205 - if( isset( $available_updates[ $plugin->slug() ] ) ) {
206 - return $available_updates[ $plugin->slug() ];
207 - }
204 + // add some dynamic data
205 + $slug = plugin_basename($plugin_file);
206 + $available_updates[ $slug ] = $this->format_response($slug, $remote_plugin);
207 + }
208 + }
208 209
209 - return null;
210 - }
210 + return $available_updates;
211 + }
211 212
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();
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();
221 221
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 - }
222 + foreach ($available_updates as $plugin_file => $update_info) {
223 + if ($slug === $update_info->slug) {
224 + return $update_info;
225 + }
226 + }
228 227
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 );
228 + return null;
229 + }
232 230
233 - return $response;
234 - }
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;
235 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 + }
236 262 }