|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin update checker for GitHub releases. |
| 4 | + */ |
| 5 | +namespace Inc\Base; |
| 6 | + |
| 7 | +class CookieUpdater |
| 8 | +{ |
| 9 | + public function registerFunction() |
| 10 | + { |
| 11 | + add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update')); |
| 12 | + } |
| 13 | + |
| 14 | + public function check_update($transient) |
| 15 | + { |
| 16 | + if (empty($transient->checked)) { |
| 17 | + return $transient; |
| 18 | + } |
| 19 | + |
| 20 | + $plugin_slug = $this->pluginName; |
| 21 | + $current_version = defined('COOKIE_VERSION') ? COOKIE_VERSION : null; |
| 22 | + |
| 23 | + if (!$current_version) { |
| 24 | + return $transient; |
| 25 | + } |
| 26 | + |
| 27 | + https://github.com/lokutus24/lion-cookie |
| 28 | + |
| 29 | + $response = wp_remote_get('https://api.github.com/repos/lokutus24/lion-cookie/releases/latest'); |
| 30 | + |
| 31 | + if (!is_wp_error($response) && isset($response['response']['code']) && $response['response']['code'] == 200) { |
| 32 | + $data = json_decode(wp_remote_retrieve_body($response)); |
| 33 | + |
| 34 | + if (isset($data->tag_name) && version_compare($current_version, $data->tag_name, '<')) { |
| 35 | + |
| 36 | + $package_url = $data->zipball_url; |
| 37 | + if (!empty($data->assets)) { |
| 38 | + foreach ($data->assets as $asset) { |
| 39 | + if ( |
| 40 | + isset($asset->name) |
| 41 | + && strpos($asset->name, 'mib-connector.zip') !== false |
| 42 | + && isset($asset->browser_download_url) |
| 43 | + ) { |
| 44 | + $package_url = $asset->browser_download_url; |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $transient->response[$plugin_slug] = (object) array( |
| 51 | + 'slug' => dirname($plugin_slug), |
| 52 | + 'plugin' => $plugin_slug, |
| 53 | + 'new_version' => $data->tag_name, |
| 54 | + 'url' => $data->html_url, |
| 55 | + 'package' => $package_url, |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return $transient; |
| 61 | + } |
| 62 | +} |
0 commit comments