forked from ilaoniu/pluto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.php
218 lines (192 loc) · 6.11 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/* Load Class. */
$nokonoko_theme_updater = new nokonoko_Theme_Updater();
/**
* Updater Class
**/
class nokonoko_Theme_Updater{
/**
* Class Constructor
*/
public function __construct() {
/* Updater Config */
$this->config = array(
'server' => 'http://zhw-island.com/',
'type' => 'theme',
'id' => get_template(),
'api' => '1.0.0',
'post' => array(),
);
/* Admin Init */
add_action( 'admin_init', array( $this, 'admin_init' ) );
/* Fix Install Folder */
add_filter( 'upgrader_post_install', array( $this, 'fix_install_folder' ), 11, 3 );
}
/**
* Admin Init.
* Some functions only available in admin.
*/
public function admin_init(){
/* Add theme update data */
if( 'plugin' !== $this->config['type'] ){
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'add_theme_update_data' ), 10, 2 );
}
/* Add plugin update data */
if( 'theme' !== $this->config['type'] ){
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'add_plugin_update_data' ), 10, 2 );
}
/* Plugin Information */
if( 'theme' !== $this->config['type'] ){
add_filter( 'plugins_api_result', array( $this, 'plugin_info' ), 10, 3 );
}
}
/**
* Add theme update data if available
*/
public function add_theme_update_data( $value, $transient ){
if( isset( $value->response ) ){
$update_data = $this->get_data( 'query_themes' );
foreach( $update_data as $theme => $data ){
if( isset( $data['new_version'], $data['theme'], $data['url'] ) ){
$value->response[$theme] = (array)$data;
}
else{
unset( $value->response[$theme] );
}
}
}
return $value;
}
/**
* Add plugin update data if available
*/
public function add_plugin_update_data( $value, $transient ){
if( isset( $value->response ) ){
$update_data = $this->get_data( 'query_plugins' );
foreach( $update_data as $plugin => $data ){
if( isset( $data['new_version'], $data['slug'], $data['plugin'] ) ){
$value->response[$plugin] = (object)$data;
}
else{
unset( $value->response[$plugin] );
}
}
}
return $value;
}
/**
* Plugin Information
*/
public function plugin_info( $res, $action, $args ){
/* Get list plugin */
if( 'group' == $this->config['type'] ){
$list_plugins = $this->get_data( 'list_plugins' );
}
else{
$slug = dirname( $this->config['id'] );
$list_plugins = array(
$slug => $this->config['id'],
);
}
/* If in our list, add our data. */
if( 'plugin_information' == $action && isset( $args->slug ) && array_key_exists( $args->slug, $list_plugins ) ){
$info = $this->get_data( 'plugin_information', $list_plugins[$args->slug] );
if( isset( $info['name'], $info['slug'], $info['external'], $info['sections'] ) ){
$res = (object)$info;
}
}
return $res;
}
/**
* Get update data from server
*/
public function get_data( $action, $plugin = '' ){
/* Get WP Version */
global $wp_version;
/* Remote Options */
$body = $this->config['post'];
if( 'query_plugins' == $action ){
$body['plugins'] = get_plugins();
}
elseif( 'query_themes' == $action ){
$themes = array();
$get_themes = wp_get_themes();
foreach( $get_themes as $theme ){
$stylesheet = $theme->get_stylesheet();
$themes[$stylesheet] = array(
'Name' => $theme->get( 'Name' ),
'ThemeURI' => $theme->get( 'ThemeURI' ),
'Description' => $theme->get( 'Description' ),
'Author' => $theme->get( 'Author' ),
'AuthorURI' => $theme->get( 'AuthorURI' ),
'Version' => $theme->get( 'Version' ),
'Template' => $theme->get( 'Template' ),
'Status' => $theme->get( 'Status' ),
'Tags' => $theme->get( 'Tags' ),
'TextDomain' => $theme->get( 'TextDomain' ),
'DomainPath' => $theme->get( 'DomainPath' ),
);
}
$body['themes'] = $themes;
}
elseif( 'plugin_information' == $action ){
$body['plugin'] = $plugin;
}
$options = array(
'timeout' => 20,
'body' => $body,
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
);
/* Remote URL */
$url_args = array(
'fx_updater' => $action,
$this->config['type'] => $this->config['id'],
);
$server = set_url_scheme( $this->config['server'], 'http' );
$url = $http_url = add_query_arg( $url_args, $server );
if ( $ssl = wp_http_supports( array( 'ssl' ) ) ){
$url = set_url_scheme( $url, 'https' );
}
/* Try HTTPS */
$raw_response = wp_remote_post( esc_url_raw( $url ), $options );
/* Fail, try HTTP */
if ( is_wp_error( $raw_response ) ) {
$raw_response = wp_remote_post( esc_url_raw( $http_url ), $options );
}
/* Still fail, bail. */
if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
return array();
}
/* return array */
$data = json_decode( trim( wp_remote_retrieve_body( $raw_response ) ), true );
return is_array( $data ) ? $data : array();
}
/**
* Fix Install Folder
*/
public function fix_install_folder( $true, $hook_extra, $result ){
if ( isset( $hook_extra['plugin'] ) ){
global $wp_filesystem;
$proper_destination = trailingslashit( $result['local_destination'] ) . dirname( $hook_extra['plugin'] );
$wp_filesystem->move( $result['destination'], $proper_destination );
$result['destination'] = $proper_destination;
$result['destination_name'] = dirname( $hook_extra['plugin'] );
global $hook_suffix;
if( 'update.php' == $hook_suffix && isset( $_GET['action'], $_GET['plugin'] ) && 'upgrade-plugin' == $_GET['action'] && $hook_extra['plugin'] == $_GET['plugin'] ){
activate_plugin( $hook_extra['plugin'] );
}
}
elseif( isset( $hook_extra['theme'] ) ){
global $wp_filesystem;
$proper_destination = trailingslashit( $result['local_destination'] ) . $hook_extra['theme'];
$wp_filesystem->move( $result['destination'], $proper_destination );
if( get_option( 'theme_switched' ) == $hook_extra['theme'] && $result['destination_name'] == get_stylesheet() ){
wp_clean_themes_cache();
switch_theme( $hook_extra['theme'] );
}
$result['destination'] = $proper_destination;
$result['destination_name'] = $hook_extra['theme'];
}
return $true;
}
}