PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.1
YayMail – WooCommerce Email Customizer v4.4.1
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / vendor-prefixed / src / License / EDD_SL_Plugin_Updater.php

EDD_SL_Plugin_Updater.php in YayMail – WooCommerce Email Customizer 4.4.1, at vendor-prefixed/src/License/EDD_SL_Plugin_Updater.php

462 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMailScoped\YayCommerce\AdminShell\License;
4
5 // Exit if accessed directly
6 if (!\defined('ABSPATH')) {
7 exit;
8 }
9 /**
10 * EDD SL Plugin Updater
11 *
12 * @package EDD_SL_Plugin_Updater
13 */
14 class EDD_SL_Plugin_Updater
15 {
16 private $api_url = '';
17 private $api_data = [];
18 private $name = '';
19 private $slug = '';
20 private $version = '';
21 private $wp_override = \false;
22 private $beta = \false;
23 private $cache_key = '';
24 private $health_check_timeout = 5;
25 /**
26 * Class constructor.
27 *
28 * @uses plugin_basename()
29 * @uses hook()
30 *
31 * @param string $_api_url The URL pointing to the custom API endpoint.
32 * @param string $_plugin_file Path to the plugin file.
33 * @param array $_api_data Optional data to send with API calls.
34 */
35 public function __construct($_api_url, $_plugin_file, $_api_data = null)
36 {
37 global $edd_plugin_data;
38 $this->api_url = \trailingslashit($_api_url);
39 $this->api_data = $_api_data;
40 $this->name = \plugin_basename($_plugin_file);
41 $this->slug = \basename($_plugin_file, '.php');
42 $this->version = $_api_data['version'];
43 $this->wp_override = isset($_api_data['wp_override']) ? (bool) $_api_data['wp_override'] : \false;
44 $this->beta = !empty($this->api_data['beta']) ? \true : \false;
45 $this->cache_key = 'edd_sl_' . \md5(\serialize($this->slug . $this->api_data['license'] . $this->beta));
46 $edd_plugin_data[$this->slug] = $this->api_data;
47 /**
48 * Fires after the $edd_plugin_data is setup.
49 *
50 * @since 3.4.5
51 *
52 * @param array $edd_plugin_data Array of EDD SL plugin data.
53 */
54 \do_action('post_edd_sl_plugin_updater_setup', $edd_plugin_data);
55 // Set up hooks.
56 $this->init();
57 }
58 /**
59 * Set up WordPress filters to hook into WP's update process.
60 *
61 * @uses add_filter()
62 *
63 * @return void
64 */
65 public function init()
66 {
67 \add_filter('pre_set_site_transient_update_plugins', [$this, 'check_update']);
68 \add_filter('plugins_api', [$this, 'plugins_api_filter'], 10, 3);
69 // add_action( 'load-plugins.php', array( $this, 'remove_default_after_row' ), 100 );
70 \add_action('after_plugin_row_' . $this->name, [$this, 'show_update_notification'], 11, 2);
71 \add_action('admin_init', [$this, 'show_changelog']);
72 }
73 public function remove_default_after_row()
74 {
75 \remove_action('after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10, 2);
76 }
77 /**
78 * Check for Updates at the defined API endpoint and modify the update array.
79 *
80 * This function dives into the update API just when WordPress creates its update array,
81 * then adds a custom API call and injects the custom plugin data retrieved from the API.
82 * It is reassembled from parts of the native WordPress plugin update code.
83 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
84 *
85 * @uses api_request()
86 *
87 * @param array $_transient_data Update array build by WordPress.
88 * @return array Modified update array with custom plugin data.
89 */
90 public function check_update($_transient_data)
91 {
92 global $pagenow;
93 if (!\is_object($_transient_data)) {
94 $_transient_data = new \stdClass();
95 }
96 if ('plugins.php' == $pagenow) {
97 return $_transient_data;
98 }
99 if (!empty($_transient_data->response) && !empty($_transient_data->response[$this->name]) && \false === $this->wp_override) {
100 return $_transient_data;
101 }
102 $current = $this->get_repo_api_data();
103 if (\false !== $current && \is_object($current) && isset($current->new_version)) {
104 if (\version_compare($this->version, $current->new_version, '<')) {
105 $_transient_data->response[$this->name] = $current;
106 } else {
107 // Populating the no_update information is required to support auto-updates in WordPress 5.5.
108 $_transient_data->no_update[$this->name] = $current;
109 }
110 }
111 $_transient_data->last_checked = \time();
112 $_transient_data->checked[$this->name] = $this->version;
113 return $_transient_data;
114 }
115 /**
116 * Get repo API data from store.
117 * Save to cache.
118 *
119 * @return \stdClass
120 */
121 public function get_repo_api_data()
122 {
123 $version_info = $this->get_cached_version_info();
124 if (\false === $version_info) {
125 $version_info = $this->api_request('plugin_latest_version', ['slug' => $this->slug, 'beta' => $this->beta]);
126 if (!$version_info) {
127 return \false;
128 }
129 // This is required for your plugin to support auto-updates in WordPress 5.5.
130 $version_info->plugin = $this->name;
131 $version_info->id = $this->name;
132 $this->set_version_info_cache($version_info);
133 }
134 return $version_info;
135 }
136 public function yaycommerce_update_transient()
137 {
138 if (\is_network_admin()) {
139 return;
140 }
141 if (!\current_user_can('update_plugins')) {
142 return;
143 }
144 // Remove our filter on the site transient
145 \remove_filter('pre_set_site_transient_update_plugins', [$this, 'check_update'], 10);
146 $update_cache = \get_site_transient('update_plugins');
147 $update_cache = \is_object($update_cache) ? $update_cache : new \stdClass();
148 if (empty($update_cache->response) || empty($update_cache->response[$this->name])) {
149 $version_info = $this->get_repo_api_data();
150 if (\false === $version_info) {
151 $version_info = $this->api_request('plugin_latest_version', ['slug' => $this->slug, 'beta' => $this->beta]);
152 // Since we disabled our filter for the transient, we aren't running our object conversion on banners, sections, or icons. Do this now:
153 if (isset($version_info->banners) && !\is_array($version_info->banners)) {
154 $version_info->banners = $this->convert_object_to_array($version_info->banners);
155 }
156 if (isset($version_info->sections) && !\is_array($version_info->sections)) {
157 $version_info->sections = $this->convert_object_to_array($version_info->sections);
158 }
159 if (isset($version_info->icons) && !\is_array($version_info->icons)) {
160 $version_info->icons = $this->convert_object_to_array($version_info->icons);
161 }
162 if (isset($version_info->contributors) && !\is_array($version_info->contributors)) {
163 $version_info->contributors = $this->convert_object_to_array($version_info->contributors);
164 }
165 $this->set_version_info_cache($version_info);
166 }
167 //end if
168 if (!\is_object($version_info)) {
169 return;
170 }
171 $has_update = \false;
172 if (\version_compare($this->version, $version_info->new_version, '<')) {
173 $update_cache->response[$this->name] = $version_info;
174 $has_update = \true;
175 } else {
176 $update_cache->no_update[$this->name] = $version_info;
177 }
178 $update_cache->last_checked = \time();
179 $update_cache->checked[$this->name] = $this->version;
180 if ($has_update) {
181 \set_site_transient('update_plugins', $update_cache);
182 }
183 } else {
184 $version_info = $update_cache->response[$this->name];
185 }
186 //end if
187 }
188 /**
189 * Show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
190 *
191 * @param string $file
192 * @param array $plugin
193 */
194 public function show_update_notification($file, $plugin)
195 {
196 $output = '';
197 if ($this->name != $file) {
198 return $output;
199 }
200 $this->yaycommerce_update_transient();
201 $update_cache = \get_site_transient('update_plugins');
202 $update_cache = \is_object($update_cache) ? $update_cache : new \stdClass();
203 $version_info = isset($update_cache->response[$this->name]) ? $update_cache->response[$this->name] : '';
204 // Restore our filter
205 \add_filter('pre_set_site_transient_update_plugins', [$this, 'check_update']);
206 \do_action("in_plugin_update_message-{$file}", $plugin, $version_info);
207 echo \wp_kses_post($output);
208 }
209 /**
210 * Updates information on the "View version x.x details" page with custom data.
211 *
212 * @uses api_request()
213 *
214 * @param mixed $_data
215 * @param string $_action
216 * @param object $_args
217 * @return object $_data
218 */
219 public function plugins_api_filter($_data, $_action = '', $_args = null)
220 {
221 if ('plugin_information' != $_action) {
222 return $_data;
223 }
224 if (!isset($_args->slug) || $_args->slug != $this->slug) {
225 return $_data;
226 }
227 $to_send = ['slug' => $this->slug, 'is_ssl' => \is_ssl(), 'fields' => ['banners' => [], 'reviews' => \false, 'icons' => []]];
228 // Get the transient where we store the api request for this plugin for 24 hours
229 $edd_api_request_transient = $this->get_cached_version_info();
230 // If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
231 if (empty($edd_api_request_transient)) {
232 $api_response = $this->api_request('plugin_information', $to_send);
233 // Expires in 3 hours
234 $this->set_version_info_cache($api_response);
235 if (\false !== $api_response) {
236 $_data = $api_response;
237 }
238 } else {
239 $_data = $edd_api_request_transient;
240 }
241 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
242 if (isset($_data->sections) && !\is_array($_data->sections)) {
243 $_data->sections = $this->convert_object_to_array($_data->sections);
244 }
245 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
246 if (isset($_data->banners) && !\is_array($_data->banners)) {
247 $_data->banners = $this->convert_object_to_array($_data->banners);
248 }
249 // Convert icons into an associative array, since we're getting an object, but Core expects an array.
250 if (isset($_data->icons) && !\is_array($_data->icons)) {
251 $_data->icons = $this->convert_object_to_array($_data->icons);
252 }
253 // Convert contributors into an associative array, since we're getting an object, but Core expects an array.
254 if (isset($_data->contributors) && !\is_array($_data->contributors)) {
255 $_data->contributors = $this->convert_object_to_array($_data->contributors);
256 }
257 if (!isset($_data->plugin)) {
258 $_data->plugin = $this->name;
259 }
260 return $_data;
261 }
262 /**
263 * Convert some objects to arrays when injecting data into the update API
264 *
265 * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
266 * decoding, they are objects. This method allows us to pass in the object and return an associative array.
267 *
268 * @since 3.6.5
269 *
270 * @param stdClass $data
271 *
272 * @return array
273 */
274 private function convert_object_to_array($data)
275 {
276 $new_data = [];
277 foreach ($data as $key => $value) {
278 $new_data[$key] = \is_object($value) ? $this->convert_object_to_array($value) : $value;
279 }
280 return $new_data;
281 }
282 /**
283 * Disable SSL verification in order to prevent download update failures
284 *
285 * @param array $args
286 * @param string $url
287 * @return object $array
288 */
289 public function http_request_args($args, $url)
290 {
291 $verify_ssl = $this->verify_ssl();
292 if (\strpos($url, 'https://') !== \false && \strpos($url, 'edd_action=package_download')) {
293 $args['sslverify'] = $verify_ssl;
294 }
295 return $args;
296 }
297 /**
298 * Calls the API and, if successfull, returns the object delivered by the API.
299 *
300 * @uses get_bloginfo()
301 * @uses wp_remote_post()
302 * @uses is_wp_error()
303 *
304 * @param string $_action The requested action.
305 * @param array $_data Parameters for the API action.
306 * @return false|object
307 */
308 private function api_request($_action, $_data)
309 {
310 global $wp_version, $edd_plugin_url_available;
311 $verify_ssl = $this->verify_ssl();
312 // Do a quick status check on this domain if we haven't already checked it.
313 $store_hash = \md5($this->api_url);
314 if (!\is_array($edd_plugin_url_available) || !isset($edd_plugin_url_available[$store_hash])) {
315 $test_url_parts = \parse_url($this->api_url);
316 $scheme = !empty($test_url_parts['scheme']) ? $test_url_parts['scheme'] : 'http';
317 $host = !empty($test_url_parts['host']) ? $test_url_parts['host'] : '';
318 $port = !empty($test_url_parts['port']) ? ':' . $test_url_parts['port'] : '';
319 if (empty($host)) {
320 $edd_plugin_url_available[$store_hash] = \false;
321 } else {
322 $test_url = $scheme . '://' . $host . $port;
323 $response = \wp_remote_get($test_url, ['timeout' => $this->health_check_timeout, 'sslverify' => $verify_ssl]);
324 $edd_plugin_url_available[$store_hash] = \is_wp_error($response) ? \false : \true;
325 }
326 }
327 //end if
328 if (\false === $edd_plugin_url_available[$store_hash]) {
329 return \false;
330 }
331 $data = \array_merge($this->api_data, $_data);
332 if ($data['slug'] != $this->slug) {
333 return \false;
334 }
335 if (\trailingslashit(\home_url()) == $this->api_url) {
336 return \false;
337 // Don't allow a plugin to ping itself
338 }
339 $api_params = ['edd_action' => 'get_version', 'license' => !empty($data['license']) ? $data['license'] : '', 'item_name' => isset($data['item_name']) ? $data['item_name'] : \false, 'item_id' => isset($data['item_id']) ? $data['item_id'] : \false, 'version' => isset($data['version']) ? $data['version'] : \false, 'slug' => $data['slug'], 'author' => $data['author'], 'url' => \home_url(), 'beta' => !empty($data['beta'])];
340 $request = \wp_remote_post($this->api_url, ['timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params]);
341 if (!\is_wp_error($request)) {
342 $request = \json_decode(\wp_remote_retrieve_body($request));
343 }
344 if ($request && isset($request->sections)) {
345 $request->sections = \maybe_unserialize($request->sections);
346 } else {
347 $request = \false;
348 }
349 if ($request && isset($request->banners)) {
350 $request->banners = \maybe_unserialize($request->banners);
351 }
352 if ($request && isset($request->icons)) {
353 $request->icons = \maybe_unserialize($request->icons);
354 }
355 if (!empty($request->sections)) {
356 foreach ($request->sections as $key => $section) {
357 $request->{$key} = (array) $section;
358 }
359 }
360 return $request;
361 }
362 /**
363 * If available, show the changelog for sites in a multisite install.
364 */
365 public function show_changelog()
366 {
367 global $edd_plugin_data;
368 if (empty($_REQUEST['edd_sl_action']) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action']) {
369 return;
370 }
371 if (empty($_REQUEST['plugin'])) {
372 return;
373 }
374 if (empty($_REQUEST['slug'])) {
375 return;
376 }
377 if (!\current_user_can('update_plugins')) {
378 \wp_die(\esc_html__('You do not have permission to install plugin updates', 'easy-digital-downloads'), \esc_html__('Error', 'easy-digital-downloads'), ['response' => 403]);
379 }
380 $data = $edd_plugin_data[\sanitize_text_field($_REQUEST['slug'])];
381 $version_info = $this->get_cached_version_info();
382 if (\false === $version_info) {
383 $api_params = ['edd_action' => 'get_version', 'item_name' => isset($data['item_name']) ? $data['item_name'] : \false, 'item_id' => isset($data['item_id']) ? $data['item_id'] : \false, 'slug' => \sanitize_text_field($_REQUEST['slug']), 'author' => $data['author'], 'url' => \home_url(), 'beta' => !empty($data['beta'])];
384 $verify_ssl = $this->verify_ssl();
385 $request = \wp_remote_post($this->api_url, ['timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params]);
386 if (!\is_wp_error($request)) {
387 $version_info = \json_decode(\wp_remote_retrieve_body($request));
388 }
389 if (!empty($version_info) && isset($version_info->sections)) {
390 $version_info->sections = \maybe_unserialize($version_info->sections);
391 } else {
392 $version_info = \false;
393 }
394 if (!empty($version_info)) {
395 foreach ($version_info->sections as $key => $section) {
396 $version_info->{$key} = (array) $section;
397 }
398 }
399 $this->set_version_info_cache($version_info);
400 // Delete the unneeded option
401 \delete_option(\md5('edd_plugin_' . \sanitize_key($_REQUEST['plugin']) . '_' . $this->beta . '_version_info'));
402 }
403 //end if
404 if (isset($version_info->sections)) {
405 $sections = $this->convert_object_to_array($version_info->sections);
406 if (!empty($sections['changelog'])) {
407 echo '<div style="background:#fff;padding:10px;">' . \wp_kses_post($sections['changelog']) . '</div>';
408 }
409 }
410 exit;
411 }
412 /**
413 * Gets the plugin's cached version information from the database.
414 *
415 * @param string $cache_key
416 * @return boolean|string
417 */
418 public function get_cached_version_info($cache_key = '')
419 {
420 if (empty($cache_key)) {
421 $cache_key = $this->cache_key;
422 }
423 $cache = \get_option($cache_key);
424 if (empty($cache['timeout']) || \time() > $cache['timeout']) {
425 return \false;
426 // Cache is expired
427 }
428 // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
429 $cache['value'] = \json_decode($cache['value']);
430 if (!empty($cache['value']->icons)) {
431 $cache['value']->icons = (array) $cache['value']->icons;
432 }
433 return $cache['value'];
434 }
435 /**
436 * Adds the plugin version information to the database.
437 *
438 * @param string $value
439 * @param string $cache_key
440 */
441 public function set_version_info_cache($value = '', $cache_key = '')
442 {
443 if (empty($cache_key)) {
444 $cache_key = $this->cache_key;
445 }
446 $data = ['timeout' => \strtotime('+3 hours', \time()), 'value' => \json_encode($value)];
447 \update_option($cache_key, $data, 'no');
448 // Delete the duplicate option
449 \delete_option('edd_api_request_' . \md5(\serialize($this->slug . $this->api_data['license'] . $this->beta)));
450 }
451 /**
452 * Returns if the SSL of the store should be verified.
453 *
454 * @since 1.6.13
455 * @return bool
456 */
457 private function verify_ssl()
458 {
459 return (bool) \apply_filters('edd_sl_api_request_verify_ssl', \true, $this);
460 }
461 }
462