PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.21
Boxzilla – WordPress Popup Builder v3.2.21
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 +102 -76 3.1.14 → 3.2.21 View file →
@@ -1,18 +1,17 @@
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 7 class UpdateManager {
10 8
9 +
11 10 /**
12 - * @var Collection
11 + * @var array
13 12 */
14 - protected $extensions;
13 + protected $extensions = array();
15 14
16 15 /**
17 16 * @var API
18 17 */
@@ -28,22 +27,22 @@
28 27 */
29 28 protected $available_updates;
30 29
31 30 /**
32 - * @param Collection $extensions
31 + * @param array $extensions
33 32 * @param API $api
34 33 * @param License $license
35 34 */
36 - public function __construct( Collection $extensions, API $api, License $license ) {
35 + public function __construct( array $extensions, API $api, License $license ) {
37 36 $this->extensions = $extensions;
38 - $this->license = $license;
39 - $this->api = $api;
37 + $this->license = $license;
38 + $this->api = $api;
40 39 }
41 40
42 41 /**
43 42 * Add hooks
44 43 */
45 - public function hook() {
44 + public function init() {
46 45 add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'add_updates' ) );
47 46 add_filter( 'plugins_api', array( $this, 'get_plugin_info' ), 20, 3 );
48 47 add_filter( 'http_request_args', array( $this, 'add_auth_headers' ), 10, 2 );
49 48 }
@@ -56,26 +55,37 @@
56 55 *
57 56 * @return mixed
58 57 */
59 58 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 ) {
59 + // only act on download request's to the Boxzilla update API
60 + if ( strpos( $url, $this->api->url ) !== 0 || strpos( $url, '/download' ) === false ) {
62 61 return $args;
63 62 }
64 63
65 64 // only add if activation key not empty
66 - if( empty( $this->license->activation_key ) ) {
65 + if ( empty( $this->license->activation_key ) ) {
67 66 return $args;
68 - }
67 + }
69 68
70 - if( empty( $args['headers'] ) ) {
69 + if ( ! isset( $args['headers'] ) ) {
71 70 $args['headers'] = array();
72 71 }
73 72
74 - $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->activation_key );
73 + $args['headers']['Authorization'] = sprintf( 'Bearer %s', $this->license->activation_key );
75 74 return $args;
76 75 }
77 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 +
78 88 /**
79 89 * @param $result
80 90 * @param string $action
81 91 * @param null $args
@@ -82,25 +92,19 @@
82 92 *
83 93 * @return object
84 94 */
85 95 public function get_plugin_info( $result, $action = '', $args = null ) {
86 -
87 96 // do nothing for unrelated requests
88 - if( $action !== 'plugin_information' || ! isset( $args->slug ) ) {
97 + if ( $action !== 'plugin_information' || ! isset( $args->slug ) ) {
89 98 return $result;
90 99 }
91 100
92 101 // only act on our plugins
93 - $plugin = $this->extensions->find( function( $p ) use($args) {
94 - return dirname( $p->slug() ) == $args->slug;
95 - });
96 -
97 - // was it a plugin of ours?
98 - if( $plugin ) {
99 - return $this->get_update_info( $plugin );
102 + if ( strpos( $args->slug, 'boxzilla-' ) !== 0 ) {
103 + return $result;
100 104 }
101 105
102 - return $result;
106 + return $this->get_update_info( $args->slug );
103 107 }
104 108
105 109 /**
106 110 * @param object $updates
@@ -108,16 +112,14 @@
108 112 */
109 113 public function add_updates( $updates ) {
110 114
111 115 // do nothing if no plugins registered
112 - if( count( $this->extensions ) === 0 ) {
116 + if ( empty( $this->extensions ) ) {
113 117 return $updates;
114 118 }
115 119
116 120 // failsafe WP bug
117 - if( empty( $updates )
118 - || empty( $updates->response )
119 - || ! is_array( $updates->response ) ) {
121 + if ( empty( $updates ) || ! isset( $updates->response ) || ! is_array( $updates->response ) ) {
120 122 return $updates;
121 123 }
122 124
123 125 // fetch available updates
@@ -134,33 +136,31 @@
134 136 *
135 137 * @return array
136 138 */
137 139 protected function fetch_updates() {
138 -
139 - if( is_array( $this->available_updates ) ) {
140 + if ( is_array( $this->available_updates ) ) {
140 141 return $this->available_updates;
141 - }
142 + }
142 143
143 - // 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();
147 - }
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 + }
148 149
149 150 // fetch remote info
150 151 try {
151 - $remote_plugins = $this->api->get_plugins( $this->extensions );
152 - } catch( API_Exception $e ) {
153 - // set flag for 5 minutes
154 - set_transient( 'boxzilla_request_failed', strtotime('now'), 300 );
155 - return array();
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();
156 157 }
157 -
158 +
158 159 // filter remote plugins, we only want the ones with an update available
159 160 $this->available_updates = $this->filter_remote_plugins( $remote_plugins );
160 161
161 162 return $this->available_updates;
162 -
163 163 }
164 164
165 165 /**
166 166 * @param $remote_plugins
@@ -166,30 +166,48 @@
166 166 * @param $remote_plugins
167 167 * @return array
168 168 */
169 169 protected function filter_remote_plugins( $remote_plugins ) {
170 -
171 170 $available_updates = array();
172 171
173 172 // find new versions
174 - foreach( $remote_plugins as $remote_plugin ) {
173 + foreach ( $remote_plugins as $remote_plugin ) {
175 174
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;
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;
182 189 }
183 - );
184 190
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;
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 );
188 209 }
189 -
190 - // add some dynamic data
191 - $available_updates[ $local_plugin->slug() ] = $this->format_response( $local_plugin, $remote_plugin );
192 210 }
193 211
194 212 return $available_updates;
195 213 }
@@ -194,17 +212,19 @@
194 212 return $available_updates;
195 213 }
196 214
197 215 /**
198 - * @param Plugin $plugin
216 + * @param string $slug
199 217 *
200 218 * @return null
201 219 */
202 - public function get_update_info( Plugin $plugin ) {
220 + public function get_update_info( $slug ) {
203 221 $available_updates = $this->fetch_updates();
204 222
205 - if( isset( $available_updates[ $plugin->slug() ] ) ) {
206 - return $available_updates[ $plugin->slug() ];
223 + foreach ( $available_updates as $plugin_file => $update_info ) {
224 + if ( $slug === $update_info->slug ) {
225 + return $update_info;
226 + }
207 227 }
208 228
209 229 return null;
210 230 }
@@ -209,28 +229,34 @@
209 229 return null;
210 230 }
211 231
212 232 /**
213 - * @param Plugin $plugin
214 - * @param $response
233 + * @param $slug
234 + * @param $response
215 235 *
216 236 * @return mixed
217 237 */
218 - protected function format_response( Plugin $plugin, $response ) {
219 - $response->slug = dirname( $plugin->slug() );
220 - $response->plugin = $plugin->slug();
238 + protected function format_response( $slug, $response ) {
239 + $response->slug = dirname( $slug );
240 + $response->plugin = $slug;
221 241
222 242 // 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' ) );
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' ) );
225 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;
226 - $response->package = null;
227 - }
246 + $response->package = null;
247 + }
228 248
229 249 // 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 );
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 + );
232 259
233 260 return $response;
234 261 }
235 -
236 262 }