.htaccess
1 year ago
Abilities.php
1 day ago
Blog.php
1 year ago
Helper.php
5 months ago
Init.php
5 months ago
Installer.php
7 months ago
MySQL.php
1 year ago
UI.php
4 months ago
Update.php
1 year ago
Wordpress.php
1 day ago
index.html
1 year ago
web.config
1 year ago
Update.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Wordpress; |
| 4 | |
| 5 | use JetBackup\Exception\HttpRequestException; |
| 6 | use JetBackup\Factory; |
| 7 | use JetBackup\JetBackup; |
| 8 | use JetBackup\Settings\Updates; |
| 9 | use JetBackup\Web\JetHttp; |
| 10 | use stdClass; |
| 11 | |
| 12 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 13 | |
| 14 | class Update { |
| 15 | const REPO_URL = 'https://repo.jetlicense.com'; |
| 16 | const REPO_EXTENSIONS_URL = self::REPO_URL . '/extensions/%s'; |
| 17 | const REPO_EXTENSIONS_DATA_URL = self::REPO_EXTENSIONS_URL . '/repodata_v2'; |
| 18 | const TRANSIENT = 'jetbackup_plugin_updates'; |
| 19 | |
| 20 | const WP_ASSETS_URL = 'https://ps.w.org/' . JetBackup::PLUGIN_NAME . '/assets'; |
| 21 | |
| 22 | private function __construct() {} |
| 23 | |
| 24 | /** |
| 25 | * @return array|null |
| 26 | */ |
| 27 | private static function _fetchRepoData():?array { |
| 28 | |
| 29 | $tier = Factory::getSettingsUpdates()->getUpdateTier(); |
| 30 | |
| 31 | try { |
| 32 | $response = JetHttp::request() |
| 33 | ->setReturnTransfer() |
| 34 | ->setSSLVerify(0, 0) |
| 35 | ->exec(sprintf(self::REPO_EXTENSIONS_DATA_URL, $tier)); |
| 36 | |
| 37 | if($response->getHeaders()->getCode() != 200) return null; |
| 38 | $data = json_decode($response->getBody()); |
| 39 | return (array) ($data !== false ? $data : []); |
| 40 | } catch(HttpRequestException $e) { |
| 41 | return null; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * NOTE: WordPress may pass a boolean (false) if the update_plugins transient is not set or has expired. |
| 47 | * |
| 48 | * @param object|null $transient |
| 49 | * |
| 50 | * @return object |
| 51 | */ |
| 52 | public static function check($transient):object { |
| 53 | |
| 54 | if (!$transient || !is_object($transient)) { |
| 55 | $transient = new stdClass(); |
| 56 | $transient->response = []; |
| 57 | $transient->no_update = []; |
| 58 | } |
| 59 | |
| 60 | $tier = Factory::getSettingsUpdates()->getUpdateTier(); |
| 61 | if($tier == Updates::TIER_RELEASE) return $transient; |
| 62 | |
| 63 | $repo_data = Wordpress::getTransient(self::TRANSIENT); |
| 64 | |
| 65 | if(!$repo_data || !isset($repo_data->tier) || $repo_data->tier != $tier || !isset($repo_data->last_check) || $repo_data->last_check < (time() - 86400)) { |
| 66 | if(!($repo_data = self::_fetchRepoData())) return $transient; |
| 67 | |
| 68 | if(is_array($repo_data)) { |
| 69 | |
| 70 | $newest_package = new stdClass(); |
| 71 | foreach($repo_data as $package) { |
| 72 | if(!isset($newest_package->version) || (isset($package->version) && version_compare($newest_package->version, $package->version, '<'))) $newest_package = $package; |
| 73 | } |
| 74 | |
| 75 | $repo_data = $newest_package; |
| 76 | } |
| 77 | |
| 78 | // Check if we found any version in our repo |
| 79 | if(!isset($repo_data->version)) return $transient; |
| 80 | |
| 81 | $repo_data->tier = $tier; |
| 82 | $repo_data->last_check = time(); |
| 83 | Wordpress::setTransient(self::TRANSIENT, $repo_data); |
| 84 | } |
| 85 | |
| 86 | $current = new stdClass(); |
| 87 | if(isset($transient->response[JetBackup::PLUGIN_SLUG])) $current = $transient->response[JetBackup::PLUGIN_SLUG]; |
| 88 | if(isset($transient->no_update[JetBackup::PLUGIN_SLUG])) $current = $transient->no_update[JetBackup::PLUGIN_SLUG]; |
| 89 | |
| 90 | $object = (object) [ |
| 91 | 'id' => 'w.org/plugins/' . JetBackup::PLUGIN_NAME, |
| 92 | 'slug' => JetBackup::PLUGIN_NAME, |
| 93 | 'plugin' => JetBackup::PLUGIN_SLUG, |
| 94 | 'new_version' => $repo_data->version, |
| 95 | 'url' => $current->url ?? 'https://wordpress.org/plugins/' . JetBackup::PLUGIN_NAME . '/', |
| 96 | 'icons' => $current->icons ?? [ |
| 97 | '1x' => self::WP_ASSETS_URL . '/icon-128x128.png?rev=3113473', |
| 98 | ], |
| 99 | 'banners' => $current->banners ?? [ |
| 100 | '2x' => self::WP_ASSETS_URL . '/banner-1544x500.png?rev=2858586', |
| 101 | '1x' => self::WP_ASSETS_URL . '/banner-772x250.png?rev=2858590', |
| 102 | ], |
| 103 | 'banners_rtl' => $current->banners_rtl ?? [], |
| 104 | 'package' => sprintf(self::REPO_EXTENSIONS_URL, $tier) . '/' . $repo_data->package, |
| 105 | 'requires' => $repo_data->min_version, |
| 106 | 'tested' => $repo_data->tested_on ?? JetBackup::TESTED_ON_WP_VERSION, |
| 107 | 'requires_php' => $repo_data->requires_php ?? JetBackup::MINIMUM_PHP_VERSION |
| 108 | ]; |
| 109 | |
| 110 | if(version_compare($repo_data->version, JetBackup::VERSION, '>')) { |
| 111 | $transient->response[JetBackup::PLUGIN_SLUG] = $object; |
| 112 | if(isset($transient->no_update[JetBackup::PLUGIN_SLUG])) unset($transient->no_update[JetBackup::PLUGIN_SLUG]); |
| 113 | } else { |
| 114 | $transient->no_update[JetBackup::PLUGIN_SLUG] = $object; |
| 115 | if(isset($transient->response[JetBackup::PLUGIN_SLUG])) unset($transient->response[JetBackup::PLUGIN_SLUG]); |
| 116 | } |
| 117 | |
| 118 | return $transient; |
| 119 | } |
| 120 | } |