-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorpheus.php
211 lines (170 loc) · 6.29 KB
/
morpheus.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
<?php
/*
Plugin Name: Morpheus
Description: Redirects WordPress core, plugin, and theme updates to a mirror when wp.org is not reachable.
Version: 1.10
Author: Blogvault
Author URI: https://blogvault.net
*/
if (!defined('ABSPATH')) {
exit;
}
class Morpheus {
private $mirror_url = 'https://morpheus.blogvault.net';
public function __construct() {
add_filter('pre_set_site_transient_update_core', array($this, 'check_core_updates'), 10, 2);
add_filter('pre_set_site_transient_update_plugins', array($this, 'check_plugin_updates'), 10, 2);
add_filter('pre_set_site_transient_update_themes', array($this, 'check_theme_updates'), 10, 2);
}
public function check_core_updates($transient, $transient_name) {
if (!is_object($transient)) {
$transient = new stdClass();
}
if (!isset($transient->updates)) {
$transient->updates = array();
}
global $wp_version;
$locale = get_locale();
$payload = array(
'version' => $wp_version,
'locale' => $locale,
'php' => phpversion(),
'mysql' => $GLOBALS['wpdb']->db_version(),
'all' => true
);
$response = $this->make_api_request('/core/version-check/1.7/', 'POST', $payload);
if ($response && isset($response['offers'])) {
foreach ($response['offers'] as $offer) {
$transient->updates[] = json_decode(json_encode($offer));
}
}
return $transient;
}
public function check_plugin_updates($transient, $transient_name) {
if (!is_object($transient)) {
$transient = new stdClass();
}
if (!isset($transient->response)) {
$transient->response = array();
}
$plugins = get_plugins();
$active_plugins = get_option('active_plugins');
$payload = array(
'plugins' => json_encode(array('plugins' => $plugins, 'active' => $active_plugins)),
'translations' => json_encode(wp_get_installed_translations('plugins')),
'locale' => json_encode(array(get_locale())),
'all' => json_encode(true)
);
$response = $this->make_api_request('/plugins/update-check/1.1/', 'POST', $payload);
if ($response && isset($response['plugins'])) {
foreach ($response['plugins'] as $plugin_slug => $plugin_data) {
// Continue if $plugin_data is an empty array
if (empty($plugin_data)) {
continue;
}
// If plugin is already in transient->response (WordPress update data)
if (isset($transient->response[$plugin_slug])) {
$wp_version = $transient->response[$plugin_slug]->new_version;
$server_version = $plugin_data['new_version']; // Get version from your server
// Continue if server version is empty
if (empty($server_version)) {
continue;
}
// Skip if WordPress version is greater than or equal to the server version
if (!empty($wp_version) && version_compare($server_version, $wp_version, '<=')) {
continue;
}
}
// Convert plugin_data array to stdClass object
$plugin_data_object = json_decode(json_encode($plugin_data));
// Add the plugin to transient->response
$transient->response[$plugin_slug] = $plugin_data_object;
}
// Handle translations and no_update
if (isset($response['translations']) && is_array($response['translations'])) {
$transient->translations = $response['translations'];
}
if (isset($response['no_update']) && is_array($response['no_update'])) {
$transient->no_update = $response['no_update'];
}
}
return $transient;
}
public function check_theme_updates($transient, $transient_name) {
if (!is_object($transient)) {
$transient = new stdClass();
}
if (!isset($transient->response)) {
$transient->response = array();
}
$installed_themes = wp_get_themes(); // Get all themes, but we will not send the active theme in the payload
$themes = array();
foreach ( $installed_themes as $theme ) {
$checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
$themes[ $theme->get_stylesheet() ] = array(
'Name' => $theme->get( 'Name' ),
'Title' => $theme->get( 'Name' ),
'Version' => $theme->get( 'Version' ),
'Author' => $theme->get( 'Author' ),
'Author URI' => $theme->get( 'AuthorURI' ),
'UpdateURI' => $theme->get( 'UpdateURI' ),
'Template' => $theme->get_template(),
'Stylesheet' => $theme->get_stylesheet()
);
}
$payload = array(
'themes' => json_encode(array('themes' => $themes)),
'translations' => json_encode(wp_get_installed_translations('themes')),
'locale' => json_encode(array(get_locale())),
'all' => json_encode(true)
);
$response = $this->make_api_request('/themes/update-check/1.1/', 'POST', $payload);
if ($response && isset($response['themes'])) {
foreach ($response['themes'] as $theme_slug => $theme_data) {
// Continue if $theme_data is an empty array
if (empty($theme_data)) {
continue;
}
// If theme is already in transient->response (WordPress update data)
if (isset($transient->response[$theme_slug])) {
$wp_version = $transient->response[$theme_slug]['new_version'];
$server_version = $theme_data['new_version']; // Get version from your server
// Continue if server version is empty
if (empty($server_version)) {
continue;
}
// Skip if WordPress version is greater than or equal to the server version
if (!empty($wp_version) && version_compare($server_version, $wp_version, '<=')) {
continue;
}
}
// Add the theme to transient->response
$transient->response[$theme_slug] = $theme_data;
}
// Handle translations and no_update
if (isset($response['translations']) && is_array($response['translations'])) {
$transient->translations = $response['translations'];
}
if (isset($response['no_update']) && is_array($response['no_update'])) {
$transient->no_update = $response['no_update'];
}
}
return $transient;
}
private function make_api_request($endpoint, $method = 'GET', $body = null) {
$args = array(
'timeout' => 30,
'method' => $method,
);
if ($body) {
$args['body'] = $body;
}
$response = wp_remote_request($this->mirror_url . $endpoint, $args);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
return json_decode($body, true);
}
}
new Morpheus();