diff --git a/camping-care-widget.php b/camping-care-widget.php index f4dfa1e..01cd30a 100644 --- a/camping-care-widget.php +++ b/camping-care-widget.php @@ -1,7 +1,7 @@ */ @@ -26,4 +26,5 @@ add_action('wp_enqueue_scripts', function(){ \CampingCareWidget\Widget::init(); \CampingCareWidget\Admin::init(); -\CampingCareWidget\Updates::init(); + +new \CampingCareWidget\Updates(basename(__DIR__)); diff --git a/src/Updates.php b/src/Updates.php index 4e14a7b..e334e6d 100644 --- a/src/Updates.php +++ b/src/Updates.php @@ -5,10 +5,14 @@ namespace CampingCareWidget; class Updates { - public static function init() + const BASES = [ + 'https://wordpress-plugin-updates.exp.sforum.nl/plugin', + ]; + + public function __construct(private string $plugin) { - add_filter('plugins_api', [static::class, 'get_plugin_info'], 20, 3); + add_filter('update_plugins_'.$this->plugin, [$this, 'get_plugin_info'], 20, 3); } /* @@ -16,74 +20,41 @@ class Updates * $action 'plugin_information' * $args stdClass Object ( [slug] => woocommerce [is_ssl] => [fields] => Array ( [banners] => 1 [reviews] => 1 [downloaded] => [active_installs] => 1 ) [per_page] => 24 [locale] => en_US ) */ - public static function get_plugin_info($res, $action, $args) + public function get_plugin_info($res, $args) { - // do nothing if this is not about getting plugin information - if ('plugin_information' !== $action) { - return $res; - } - - $pluginName = plugin_basename(__DIR__); + $pluginName = $this->plugin; // do nothing if it is not our plugin - if ($pluginName !== $args->slug) { + if ($pluginName !== $args['UpdateURI']) { return $res; } - // info.json is the file with the actual plugin information on your server - $remote = wp_remote_get( - 'https://wordpress-plugin-repository.exp.sforum.nl/plugins/' . urlencode($pluginName) . '/info.json', - array( - 'timeout' => 10, - 'headers' => array( - 'Accept' => 'application/json' + foreach(self::BASES as $base){ + $url = sprintf('%s/%s/info.json', $base, urlencode($pluginName)); + + $remote = wp_remote_get( + $url, + array( + 'timeout' => 10, + 'headers' => array( + 'Accept' => 'application/json' + ) ) - ) - ); + ); - // do nothing if we don't get the correct response from the server - if ( - is_wp_error($remote) - || 200 !== wp_remote_retrieve_response_code($remote) - || empty(wp_remote_retrieve_body($remote)) - ) { - return $res; + if( is_wp_error($remote) + || 200 !== wp_remote_retrieve_response_code($remote) + || empty(wp_remote_retrieve_body($remote))){ + continue; + } + + + $res = json_decode(wp_remote_retrieve_body($remote)); + break; } - $res = json_decode(wp_remote_retrieve_body($remote)); return $res; -// -// $res = (object)[]; -// $res->name = $remote->name; -// $res->slug = $remote->slug; -// $res->author = $remote->author; -// $res->author_profile = $remote->author_profile; -// $res->version = $remote->version; -// $res->tested = $remote->tested; -// $res->requires = $remote->requires; -// $res->requires_php = $remote->requires_php; -// $res->download_link = $remote->download_url; -// $res->trunk = $remote->download_url; -// $res->last_updated = $remote->last_updated; -// $res->sections = array( -// 'description' => $remote->sections->description, -// 'installation' => $remote->sections->installation, -// 'changelog' => $remote->sections->changelog -// // you can add your custom sections (tabs) here -// ); -// // in case you want the screenshots tab, use the following HTML format for its content: -// //
  1. CAPTION

    CAPTION

-// if (!empty($remote->sections->screenshots)) { -// $res->sections['screenshots'] = $remote->sections->screenshots; -// } -// -// $res->banners = array( -// 'low' => $remote->banners->low, -// 'high' => $remote->banners->high -// ); -// -// return $res; }