Skip to content

Commit 55e3ab4

Browse files
committed
📦 NEW: Updater class
1 parent 94842b5 commit 55e3ab4

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

‎app/Updater.php‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace WPFreighter;
4+
5+
class Updater {
6+
7+
public $plugin_slug;
8+
public $version;
9+
public $cache_key;
10+
public $cache_allowed;
11+
12+
public function __construct() {
13+
14+
if ( defined( 'WP_FREIGHTER_DEV_MODE' ) ) {
15+
add_filter('https_ssl_verify', '__return_false');
16+
add_filter('https_local_ssl_verify', '__return_false');
17+
add_filter('http_request_host_is_external', '__return_true');
18+
}
19+
20+
$this->plugin_slug = dirname ( plugin_basename( __DIR__ ) );
21+
$this->version = '1.1';
22+
$this->cache_key = 'wpfreighter_updater';
23+
$this->cache_allowed = false;
24+
25+
add_filter( 'plugins_api', [ $this, 'info' ], 20, 3 );
26+
add_filter( 'site_transient_update_plugins', [ $this, 'update' ] );
27+
add_action( 'upgrader_process_complete', [ $this, 'purge' ], 10, 2 );
28+
29+
}
30+
31+
public function request(){
32+
33+
$remote = get_transient( $this->cache_key );
34+
35+
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+
]
42+
]
43+
);
44+
45+
if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) || empty( wp_remote_retrieve_body( $remote ) ) ) {
46+
return false;
47+
}
48+
49+
set_transient( $this->cache_key, $remote, DAY_IN_SECONDS );
50+
51+
}
52+
53+
$remote = json_decode( wp_remote_retrieve_body( $remote ) );
54+
55+
return $remote;
56+
57+
}
58+
59+
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 ) {
68+
return $response;
69+
}
70+
71+
// get updates
72+
$remote = $this->request();
73+
74+
if ( ! $remote ) {
75+
return $response;
76+
}
77+
78+
$response = new \stdClass();
79+
80+
$response->name = $remote->name;
81+
$response->slug = $remote->slug;
82+
$response->version = $remote->version;
83+
$response->tested = $remote->tested;
84+
$response->requires = $remote->requires;
85+
$response->author = $remote->author;
86+
$response->author_profile = $remote->author_profile;
87+
$response->donate_link = $remote->donate_link;
88+
$response->homepage = $remote->homepage;
89+
$response->download_link = $remote->download_url;
90+
$response->trunk = $remote->download_url;
91+
$response->requires_php = $remote->requires_php;
92+
$response->last_updated = $remote->last_updated;
93+
94+
$response->sections = [
95+
'description' => $remote->sections->description,
96+
'installation' => $remote->sections->installation,
97+
'changelog' => $remote->sections->changelog
98+
];
99+
100+
if ( ! empty( $remote->banners ) ) {
101+
$response->banners = [
102+
'low' => $remote->banners->low,
103+
'high' => $remote->banners->high
104+
];
105+
}
106+
107+
return $response;
108+
109+
}
110+
111+
public function update( $transient ) {
112+
113+
if ( empty($transient->checked ) ) {
114+
return $transient;
115+
}
116+
117+
$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";
123+
$response->new_version = $remote->version;
124+
$response->tested = $remote->tested;
125+
$response->package = $remote->download_url;
126+
127+
$transient->response[ $response->plugin ] = $response;
128+
129+
}
130+
131+
return $transient;
132+
133+
}
134+
135+
public function purge( $upgrader, $options ) {
136+
137+
if ( $this->cache_allowed && 'update' === $options['action'] && 'plugin' === $options[ 'type' ] ) {
138+
// just clean the cache when new plugin version is installed
139+
delete_transient( $this->cache_key );
140+
}
141+
142+
}
143+
144+
}

0 commit comments

Comments
 (0)