PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.18
Boxzilla – WordPress Popup Builder v3.2.18
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 +104 -68 3.0.3 → 3.2.18 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,20 +55,37 @@
56 55 *
57 56 * @return mixed
58 57 */
59 58 public function add_auth_headers( $args, $url ) {
60 - 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 ) {
61 61 return $args;
62 62 }
63 63
64 - if( empty( $args['headers'] ) ) {
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'] ) ) {
65 70 $args['headers'] = array();
66 71 }
67 72
68 - $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->key );
73 + $args['headers']['Authorization'] = sprintf( 'Bearer %s', $this->license->activation_key );
69 74 return $args;
70 75 }
71 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 +
72 88 /**
73 89 * @param $result
74 90 * @param string $action
75 91 * @param null $args
@@ -76,25 +92,19 @@
76 92 *
77 93 * @return object
78 94 */
79 95 public function get_plugin_info( $result, $action = '', $args = null ) {
80 -
81 96 // do nothing for unrelated requests
82 - if( $action !== 'plugin_information' || ! isset( $args->slug ) ) {
97 + if ( $action !== 'plugin_information' || ! isset( $args->slug ) ) {
83 98 return $result;
84 99 }
85 100
86 101 // only act on our plugins
87 - $plugin = $this->extensions->find( function( $p ) use($args) {
88 - return dirname( $p->slug() ) == $args->slug;
89 - });
90 -
91 - // was it a plugin of ours?
92 - if( $plugin ) {
93 - return $this->get_update_info( $plugin );
102 + if ( strpos( $args->slug, 'boxzilla-' ) !== 0 ) {
103 + return $result;
94 104 }
95 105
96 - return $result;
106 + return $this->get_update_info( $args->slug );
97 107 }
98 108
99 109 /**
100 110 * @param object $updates
@@ -102,16 +112,14 @@
102 112 */
103 113 public function add_updates( $updates ) {
104 114
105 115 // do nothing if no plugins registered
106 - if( count( $this->extensions ) === 0 ) {
116 + if ( empty( $this->extensions ) ) {
107 117 return $updates;
108 118 }
109 119
110 120 // failsafe WP bug
111 - if( empty( $updates )
112 - || empty( $updates->response )
113 - || ! is_array( $updates->response ) ) {
121 + if ( empty( $updates ) || ! isset( $updates->response ) || ! is_array( $updates->response ) ) {
114 122 return $updates;
115 123 }
116 124
117 125 // fetch available updates
@@ -128,25 +136,31 @@
128 136 *
129 137 * @return array
130 138 */
131 139 protected function fetch_updates() {
140 + if ( is_array( $this->available_updates ) ) {
141 + return $this->available_updates;
142 + }
132 143
133 - if( is_array( $this->available_updates ) ) {
134 - return $this->available_updates;
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();
135 148 }
136 149
137 150 // fetch remote info
138 151 try {
139 - $remote_plugins = $this->api->get_plugins( $this->extensions );
140 - } catch( API_Exception $e ) {
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 );
141 156 return array();
142 157 }
143 -
158 +
144 159 // filter remote plugins, we only want the ones with an update available
145 160 $this->available_updates = $this->filter_remote_plugins( $remote_plugins );
146 161
147 162 return $this->available_updates;
148 -
149 163 }
150 164
151 165 /**
152 166 * @param $remote_plugins
@@ -152,30 +166,48 @@
152 166 * @param $remote_plugins
153 167 * @return array
154 168 */
155 169 protected function filter_remote_plugins( $remote_plugins ) {
156 -
157 170 $available_updates = array();
158 171
159 172 // find new versions
160 - foreach( $remote_plugins as $remote_plugin ) {
173 + foreach ( $remote_plugins as $remote_plugin ) {
161 174
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;
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;
168 189 }
169 - );
170 190
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;
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 );
174 209 }
175 -
176 - // add some dynamic data
177 - $available_updates[ $local_plugin->slug() ] = $this->format_response( $local_plugin, $remote_plugin );
178 210 }
179 211
180 212 return $available_updates;
181 213 }
@@ -180,17 +212,19 @@
180 212 return $available_updates;
181 213 }
182 214
183 215 /**
184 - * @param Plugin $plugin
216 + * @param string $slug
185 217 *
186 218 * @return null
187 219 */
188 - public function get_update_info( Plugin $plugin ) {
220 + public function get_update_info( $slug ) {
189 221 $available_updates = $this->fetch_updates();
190 222
191 - if( isset( $available_updates[ $plugin->slug() ] ) ) {
192 - 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 + }
193 227 }
194 228
195 229 return null;
196 230 }
@@ -195,32 +229,34 @@
195 229 return null;
196 230 }
197 231
198 232 /**
199 - * @param Plugin $plugin
200 - * @param $response
233 + * @param $slug
234 + * @param $response
201 235 *
202 236 * @return mixed
203 237 */
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();
238 + protected function format_response( $slug, $response ) {
239 + $response->slug = dirname( $slug );
240 + $response->plugin = $slug;
208 241
209 242 // 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' ) );
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' ) );
212 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;
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 );
246 + $response->package = null;
217 247 }
218 248
219 249 // 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 );
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 + );
222 259
223 260 return $response;
224 261 }
225 -
226 -}
262 +}