-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-uri-improved.php
195 lines (158 loc) · 5.17 KB
/
update-uri-improved.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
<?php
/**
* Plugin Name: Update URI Improved
* Description: Improves the behavior of the "Update URI" plugin header so that plugin information is no longer sent to wordpress.org during update checks.
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Requires at least: 6.6
* Requires PHP: 7.4
* Update URI: false
*/
/*
Copyright (C) 2024 siliconforks
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace siliconforks\update_uri_improved;
use WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* See WP_CLI\Utils\get_plugin_name()
*/
function get_plugin_slug( $basename ) {
if ( false === strpos( $basename, '/' ) ) {
$name = basename( $basename, '.php' );
} else {
$name = dirname( $basename );
}
return $name;
}
function skip_updates( $update_uri_header ) {
if ( ! $update_uri_header ) {
return false;
}
if ( preg_match( '#^(?:https?://)?w(?:ordpress)?\.org/plugins/#', $update_uri_header ) ) {
return false;
}
return true;
}
function skip_updates_for_plugin( $plugin_slug ) {
static $plugins = null;
static $slug_to_file_map = null;
if ( $plugins === null ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();
$slug_to_file_map = [];
foreach ( $plugins as $plugin_file => $plugin_headers ) {
$key = get_plugin_slug( $plugin_file );
$slug_to_file_map[ $key ] = $plugin_file;
}
}
if ( isset( $slug_to_file_map[ $plugin_slug ] ) ) {
$plugin_file = $slug_to_file_map[ $plugin_slug ];
if ( isset( $plugins[ $plugin_file ] ) ) {
$plugin_headers = $plugins[ $plugin_file ];
if ( is_array( $plugin_headers ) && isset( $plugin_headers['UpdateURI'] ) && skip_updates( $plugin_headers['UpdateURI'] ) ) {
return true;
} else {
return false;
}
}
}
return false;
}
function http_request_args( $parsed_args, $url ) {
$parsed_url = wp_parse_url( $url );
if ( ! is_array( $parsed_url ) ) {
return $parsed_args;
}
if ( ! isset( $parsed_url['host'] ) ) {
return $parsed_args;
}
$host = $parsed_url['host'];
$host = strtolower( $host );
if ( $host !== 'api.wordpress.org' ) {
return $parsed_args;
}
if ( ! isset( $parsed_url['path'] ) ) {
return $parsed_args;
}
/*
Check the version number.
The version number is currently '1.1' but it is likely to change to '1.2' in the future.
See https://core.trac.wordpress.org/ticket/30465 for details.
We will assume the version is acceptable if the major version number is 1.
*/
$path = $parsed_url['path'];
if ( ! preg_match( '#^/plugins/update-check/([^/]+)/$#D', $path, $matches ) ) {
return $parsed_args;
}
$version = $matches[1];
$version_components = explode( '.', $version );
if ( $version_components[0] !== '1' ) {
return $parsed_args;
}
if ( ! isset( $parsed_args['method'] ) ) {
return $parsed_args;
}
$method = $parsed_args['method'];
if ( $method !== 'POST' ) {
return $parsed_args;
}
if ( ! isset( $parsed_args['body'] ) ) {
return $parsed_args;
}
$body = $parsed_args['body'];
if ( is_array( $body ) && isset( $body['plugins'] ) ) {
$json = $body['plugins'];
$json = json_decode( $json, true );
if ( is_array( $json ) && isset( $json['plugins'], $json['active'] ) ) {
$plugins = $json['plugins'];
$active = $json['active'];
$new_plugins = [];
foreach ( $plugins as $plugin_file => $plugin_headers ) {
if ( is_array( $plugin_headers ) && isset( $plugin_headers['UpdateURI'] ) && skip_updates( $plugin_headers['UpdateURI'] ) ) {
continue;
}
$new_plugins[ $plugin_file ] = $plugin_headers;
}
$new_active = [];
foreach ( $active as $plugin_file ) {
if ( isset( $plugins[ $plugin_file ] ) ) {
$plugin_headers = $plugins[ $plugin_file ];
if ( is_array( $plugin_headers ) && isset( $plugin_headers['UpdateURI'] ) && skip_updates( $plugin_headers['UpdateURI'] ) ) {
continue;
}
}
$new_active[] = $plugin_file;
}
$to_send = [
'plugins' => $new_plugins,
'active' => $new_active,
];
$parsed_args['body']['plugins'] = wp_json_encode( $to_send );
}
}
return $parsed_args;
}
add_filter( 'http_request_args', __NAMESPACE__ . '\http_request_args', 10, 2 );
function plugins_api( $result, $action, $args ) {
if ( $action === 'plugin_information' && is_object( $args ) && property_exists( $args, 'slug' ) && skip_updates_for_plugin( $args->slug ) ) {
$result = new WP_Error( 'plugins_api_failed', 'Request for plugin with Update URI blocked.' );
}
return $result;
}
add_filter( 'plugins_api', __NAMESPACE__ . '\plugins_api', 10, 3 );