PluginProbe
Boxzilla – WordPress Popup Builder / 3.2
Boxzilla – WordPress Popup Builder v3.2
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
boxzilla / src / licensing / class-update-manager.php

class-update-manager.php in Boxzilla – WordPress Popup Builder 3.2, at src/licensing/class-update-manager.php

237 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla\Licensing;
4
5 use Boxzilla\Collection,
6 Boxzilla\Plugin,
7 Boxzilla\Admin\Notices;
8
9 class UpdateManager {
10
11 /**
12 * @var Collection
13 */
14 protected $extensions;
15
16 /**
17 * @var API
18 */
19 protected $api;
20
21 /**
22 * @var License
23 */
24 protected $license;
25
26 /**
27 * @var
28 */
29 protected $available_updates;
30
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 }
41
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 }
50
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 }
64
65 // only add if activation key not empty
66 if( empty( $this->license->activation_key ) ) {
67 return $args;
68 }
69
70 if( empty( $args['headers'] ) ) {
71 $args['headers'] = array();
72 }
73
74 $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->activation_key );
75 return $args;
76 }
77
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 ) {
86
87 // do nothing for unrelated requests
88 if( $action !== 'plugin_information' || ! isset( $args->slug ) ) {
89 return $result;
90 }
91
92 // 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 );
100 }
101
102 return $result;
103 }
104
105 /**
106 * @param object $updates
107 * @return object
108 */
109 public function add_updates( $updates ) {
110
111 // do nothing if no plugins registered
112 if( count( $this->extensions ) === 0 ) {
113 return $updates;
114 }
115
116 // failsafe WP bug
117 if( empty( $updates )
118 || empty( $updates->response )
119 || ! is_array( $updates->response ) ) {
120 return $updates;
121 }
122
123 // fetch available updates
124 $available_updates = $this->fetch_updates();
125
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;
141 }
142
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 }
148
149 // fetch remote info
150 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();
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 );
160
161 return $this->available_updates;
162
163 }
164
165 /**
166 * @param $remote_plugins
167 * @return array
168 */
169 protected function filter_remote_plugins( $remote_plugins ) {
170
171 $available_updates = array();
172
173 // find new versions
174 foreach( $remote_plugins as $remote_plugin ) {
175
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 );
184
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 }
189
190 // add some dynamic data
191 $available_updates[ $local_plugin->slug() ] = $this->format_response( $local_plugin, $remote_plugin );
192 }
193
194 return $available_updates;
195 }
196
197 /**
198 * @param Plugin $plugin
199 *
200 * @return null
201 */
202 public function get_update_info( Plugin $plugin ) {
203 $available_updates = $this->fetch_updates();
204
205 if( isset( $available_updates[ $plugin->slug() ] ) ) {
206 return $available_updates[ $plugin->slug() ];
207 }
208
209 return null;
210 }
211
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
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 }
228
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 );
232
233 return $response;
234 }
235
236 }
237