Skip to content

Commit 48f93a0

Browse files
committed
👌 IMPROVE: Updater
1 parent a608f6f commit 48f93a0

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

‎app/Updater.php‎

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,54 +29,55 @@ public function __construct() {
2929
}
3030

3131
public function request(){
32+
// Get the local manifest as a fallback.
33+
$manifest_file = dirname(__DIR__) . "/manifest.json";
34+
$local_manifest = null;
35+
if ( file_exists($manifest_file) ) {
36+
$local_manifest = json_decode( file_get_contents( $manifest_file ) );
37+
}
38+
39+
// If local manifest fails to load, create a default object to prevent errors.
40+
if ( ! is_object( $local_manifest ) ) {
41+
$local_manifest = new \stdClass();
42+
}
3243

44+
// Attempt to get the remote manifest from the transient cache.
3345
$remote = get_transient( $this->cache_key );
3446

3547
if( false === $remote || ! $this->cache_allowed ) {
36-
37-
$remote = wp_remote_get( 'https://wpfreighter.com/plugin-wpfreighter.json', [
38-
'timeout' => 10,
39-
'headers' => [
40-
'Accept' => 'application/json'
41-
]
48+
$remote_response = wp_remote_get( 'https://raw.githubusercontent.com/WPFreighter/wp-freighter/master/manifest.json', [
49+
'timeout' => 30,
50+
'headers' => [ 'Accept' => 'application/json' ]
4251
]
4352
);
4453

45-
if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) || empty( wp_remote_retrieve_body( $remote ) ) ) {
46-
return false;
54+
// If the remote request fails, return the modified local manifest.
55+
if ( is_wp_error( $remote_response ) || 200 !== wp_remote_retrieve_response_code( $remote_response ) || empty( wp_remote_retrieve_body( $remote_response ) ) ) {
56+
return $local_manifest;
4757
}
4858

59+
$remote = json_decode( wp_remote_retrieve_body( $remote_response ) );
4960
set_transient( $this->cache_key, $remote, DAY_IN_SECONDS );
50-
5161
}
5262

53-
$remote = json_decode( wp_remote_retrieve_body( $remote ) );
54-
55-
return $remote;
63+
// If a valid remote object is retrieved, return it.
64+
if ( is_object( $remote ) ) {
65+
return $remote;
66+
}
5667

68+
// Fallback to the local manifest if remote data is invalid.
69+
return $local_manifest;
5770
}
5871

5972
function info( $response, $action, $args ) {
60-
61-
// do nothing if you're not getting plugin information right now
62-
if ( 'plugin_information' !== $action ) {
63-
return $response;
64-
}
65-
66-
// do nothing if it is not our plugin
67-
if ( empty( $args->slug ) || $this->plugin_slug !== $args->slug ) {
73+
if ( 'plugin_information' !== $action || empty( $args->slug ) || $this->plugin_slug !== $args->slug ) {
6874
return $response;
6975
}
7076

71-
// get updates
7277
$remote = $this->request();
73-
74-
if ( ! $remote ) {
75-
return $response;
76-
}
78+
if ( ! $remote ) { return $response; }
7779

7880
$response = new \stdClass();
79-
8081
$response->name = $remote->name;
8182
$response->slug = $remote->slug;
8283
$response->version = $remote->version;
@@ -103,33 +104,24 @@ function info( $response, $action, $args ) {
103104
'high' => $remote->banners->high
104105
];
105106
}
106-
107107
return $response;
108-
109108
}
110109

111110
public function update( $transient ) {
112-
113-
if ( empty($transient->checked ) ) {
114-
return $transient;
115-
}
111+
if ( empty($transient->checked ) ) { return $transient; }
116112

117113
$remote = $this->request();
118-
119-
if ( $remote && version_compare( $this->version, $remote->version, '<' ) && version_compare( $remote->requires, get_bloginfo( 'version' ), '<=' ) && version_compare( $remote->requires_php, PHP_VERSION, '<' ) ) {
120-
$response = new \stdClass();
121-
$response->slug = $this->plugin_slug;
122-
$response->plugin = "{$this->plugin_slug}/{$this->plugin_slug}.php";
114+
if ( $remote && isset($remote->version) && version_compare( $this->version, $remote->version, '<' ) ) {
115+
$response = new \stdClass();
116+
$response->slug = $this->plugin_slug;
117+
$response->plugin = "{$this->plugin_slug}/{$this->plugin_slug}.php";
123118
$response->new_version = $remote->version;
124-
$response->tested = $remote->tested;
125-
$response->package = $remote->download_url;
126-
119+
$response->package = $remote->download_url;
120+
$response->tested = $remote->tested;
121+
$response->requires_php = $remote->requires_php;
127122
$transient->response[ $response->plugin ] = $response;
128-
129123
}
130-
131124
return $transient;
132-
133125
}
134126

135127
public function purge( $upgrader, $options ) {
@@ -140,10 +132,8 @@ public function purge( $upgrader, $options ) {
140132
}
141133

142134
if ( $this->cache_allowed && 'update' === $options['action'] && 'plugin' === $options[ 'type' ] ) {
143-
// just clean the cache when new plugin version is installed
144135
delete_transient( $this->cache_key );
145136
}
146-
147137
}
148138

149139
}

0 commit comments

Comments
 (0)