-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdater.php
164 lines (141 loc) · 4.12 KB
/
Updater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Endurance_WP_Plugin_Updater;
use NewfoldLabs\Container\Container;
/**
* Class Updater
*
* @package Endurance_WP_Plugin_Updater
*/
class Updater {
/**
* Updater constructor.
*
* @param string $vendor The vendor name.
* @param string $package The package name.
* @param string $plugin_basename The plugin basename.
*/
public function __construct( $vendor, $package, $plugin_basename ) {
$container = new Container(
array(
'vendor' => $vendor, // abstract of GitHub org
'package' => $package, // abstract of GitHub repo name
'plugin_basename' => $plugin_basename, // bluehost-wordpress-plugin/bluehost-wordpress-plugin.php
)
);
$container->set(
'plugin',
$container->service(
function ( Container $c ) {
$path = WP_PLUGIN_DIR . '/' . $c['plugin_basename'];
return new Plugin( $path );
}
)
);
$container->set(
'cache_key',
$container->service(
function ( Container $c ) {
return str_replace( '-', '_', $c['plugin']->slug() ) . '_github_api_latest_release';
}
)
);
$container->set(
'query_release_api',
$container->service(
function ( Container $c ) {
$package_info = array(
'vendorName' => $c['vendor'],
'packageName' => $c['package'],
'pluginBasename' => $c['plugin_basename'],
);
$query_string = '?' . http_build_query( $package_info, null, '&' );
return wp_remote_get( 'https://bluehost-wp-release.com/v1/' . $query_string );
}
)
);
$container->set(
'get_release_data',
$container->service(
function ( Container $c ) {
$payload = get_transient( $c['cache_key'] );
if ( ! $payload ) {
$payload = new \stdClass();
$response = $c['query_release_api'];
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$body = wp_remote_retrieve_body( $response );
if ( $body ) {
$data = json_decode( $body );
if ( $data && property_exists( $data, 'package' ) && false !== stripos( $data->package, '.zip' ) ) {
$payload = $data;
set_transient( $c['cache_key'], $payload, HOUR_IN_SECONDS * 6 );
}
}
}
}
return $payload;
}
)
);
add_filter(
'site_transient_update_plugins',
function ( $transient ) use ( $container ) {
if ( empty( $transient ) || ! is_object( $transient ) ) {
return $transient;
}
/**
* The plugin instance.
*
* @var Plugin $plugin
*/
$plugin = $container['plugin'];
/**
* Decoded JSON from Bluehost Release API
*/
$release = $container['get_release_data'];
if ( isset( $release->new_version ) && version_compare( $release->new_version, $plugin->version(), '>' ) ) {
$transient->response[ $plugin->basename() ] = $release;
} else {
$transient->no_update[ $plugin->basename() ] = (object) array(
'id' => $plugin->basename(),
'slug' => $plugin->slug(),
'plugin' => $plugin->basename(),
'new_version' => $plugin->version(),
'url' => $plugin->uri(),
'package' => '',
'icons' => array(),
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'requires_php' => $plugin->requires_php_version(),
'compatibility' => new \stdClass(),
);
}
return $transient;
}
);
add_action(
'plugins_api',
function ( $response, $action, $args ) use ( $container ) {
$plugin = $container['plugin'];
if ( isset( $args->slug ) && $args->slug === $plugin->slug() ) {
$release = $container['get_release_data'];
$response = (object) array(
'author' => $plugin->author(),
'homepage' => $plugin->uri(),
'last_updated' => $release->last_updated,
'name' => $plugin->name(),
'plugin_name' => $plugin->name(),
'sections' => array(
'Description' => $plugin->description(),
),
'slug' => $plugin->slug(),
'version' => $release->new_version,
);
}
return $response;
},
20,
3
);
}
}