mirror of
https://github.com/WenPai-org/wp-china-yes.git
synced 2025-08-06 21:53:56 +08:00
完成社区源列表、自定义源 本次提交修改较多,难免存在BUG,不建议用于生产环节
This commit is contained in:
parent
29785f6992
commit
6343b1dfbc
5 changed files with 383 additions and 66 deletions
134
index.php
134
index.php
|
@ -1,21 +1,33 @@
|
|||
<?php
|
||||
/*
|
||||
Plugin Name: WP-China-Yes
|
||||
Description: 此插件将全面替换WP访问官方服务的链接为高速稳定的中国大陆节点,以此加快站点更新版本、安装升级插件主题的速度,并彻底解决429报错问题。
|
||||
Author: 孙锡源
|
||||
Version: 1.0.1
|
||||
Author URI:https://www.ibadboy.net/
|
||||
*/
|
||||
* Plugin Name: WP-China-Yes
|
||||
* Description: 此插件将全面改善中国大陆站点在访问官方服务时的用户体验,原理是将官方仓库源替换为由社区志愿者维护的国内源。
|
||||
* Author: 孙锡源
|
||||
* Version: 1.9.9
|
||||
* Author URI:https://www.ibadboy.net/
|
||||
* License: GPLv3 or later
|
||||
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*/
|
||||
|
||||
define('WP_CHINA_YES_PATH', __DIR__);
|
||||
define('WP_CHINA_YES_BASE_FILE', __FILE__);
|
||||
define('WP_CHINA_YES_VERSION', '1.0.0');
|
||||
|
||||
WP_CHINA_YES::init();
|
||||
|
||||
class WP_CHINA_YES {
|
||||
public static string $name = 'wp_china_yes';
|
||||
public static string $option_name = 'wp_china_yes_option';
|
||||
|
||||
public static function init() {
|
||||
if (is_admin()) {
|
||||
register_activation_hook(WP_CHINA_YES_BASE_FILE, array(
|
||||
__CLASS__,
|
||||
'wp_china_yes_activate'
|
||||
));
|
||||
register_deactivation_hook(WP_CHINA_YES_BASE_FILE, array(
|
||||
__CLASS__,
|
||||
'wp_china_yes_deactivate'
|
||||
));
|
||||
add_filter('pre_http_request', array(
|
||||
__CLASS__,
|
||||
'pre_http_request'
|
||||
|
@ -24,16 +36,41 @@ class WP_CHINA_YES {
|
|||
__CLASS__,
|
||||
'plugin_row_meta'
|
||||
), 10, 2);
|
||||
add_filter('plugin_action_links', array(
|
||||
__CLASS__,
|
||||
'action_links'
|
||||
), 10, 2);
|
||||
add_action('admin_menu', array(
|
||||
__CLASS__,
|
||||
'admin_menu'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public static function wp_china_yes_activate() {
|
||||
$options = array();
|
||||
$options['community'] = 0;
|
||||
$options['custom_api_server'] = '';
|
||||
$options['custom_download_server'] = '';
|
||||
$options['api_server'] = 'api.w.org.ibadboy.net';
|
||||
$options['download_server'] = 'd.w.org.ibadboy.net';
|
||||
add_option('wp_china_yes_options', $options);
|
||||
}
|
||||
|
||||
public static function wp_china_yes_deactivate() {
|
||||
delete_option('wp_china_yes_options');
|
||||
}
|
||||
|
||||
public static function pre_http_request($preempt, $r, $url) {
|
||||
if ( ! stristr($url, 'api.wordpress.org') && ! stristr($url, 'downloads.wordpress.org')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$url = str_replace('api.wordpress.org', 'api.w.org.ibadboy.net', $url);
|
||||
$url = str_replace('downloads.wordpress.org', 'd.w.org.ibadboy.net', $url);
|
||||
$options = get_option('wp_china_yes_options');
|
||||
$api_server = $options["custom_api_server"] ?: $options["api_server"];
|
||||
$download_server = $options["custom_download_server"] ?: $options["download_server"];
|
||||
$url = str_replace('api.wordpress.org', $api_server, $url);
|
||||
$url = str_replace('downloads.wordpress.org', $download_server, $url);
|
||||
|
||||
if (function_exists('wp_kses_bad_protocol')) {
|
||||
if ($r['reject_unsafe_urls']) {
|
||||
|
@ -54,14 +91,11 @@ class WP_CHINA_YES {
|
|||
return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
|
||||
}
|
||||
|
||||
// If we are streaming to a file but no filename was given drop it in the WP temp dir
|
||||
// and pick its name using the basename of the $url
|
||||
if ($r['stream']) {
|
||||
if (empty($r['filename'])) {
|
||||
$r['filename'] = get_temp_dir() . basename($url);
|
||||
}
|
||||
|
||||
// Force some settings if we are streaming to a file and check for existence and perms of destination directory
|
||||
$r['blocking'] = true;
|
||||
if ( ! wp_is_writable(dirname($r['filename']))) {
|
||||
return new WP_Error('http_request_failed', __('Destination directory for file streaming does not exist or is not writable.'));
|
||||
|
@ -72,13 +106,11 @@ class WP_CHINA_YES {
|
|||
$r['headers'] = array();
|
||||
}
|
||||
|
||||
// WP allows passing in headers as a string, weirdly.
|
||||
if ( ! is_array($r['headers'])) {
|
||||
$processedHeaders = WP_Http::processHeaders($r['headers']);
|
||||
$r['headers'] = $processedHeaders['headers'];
|
||||
}
|
||||
|
||||
// Setup arguments
|
||||
$headers = $r['headers'];
|
||||
$data = $r['body'];
|
||||
$type = $r['method'];
|
||||
|
@ -98,17 +130,14 @@ class WP_CHINA_YES {
|
|||
$options['redirects'] = $r['redirection'];
|
||||
}
|
||||
|
||||
// Use byte limit, if we can
|
||||
if (isset($r['limit_response_size'])) {
|
||||
$options['max_bytes'] = $r['limit_response_size'];
|
||||
}
|
||||
|
||||
// If we've got cookies, use and convert them to Requests_Cookie.
|
||||
if ( ! empty($r['cookies'])) {
|
||||
$options['cookies'] = WP_Http::normalize_cookies($r['cookies']);
|
||||
}
|
||||
|
||||
// SSL certificate handling
|
||||
if ( ! $r['sslverify']) {
|
||||
$options['verify'] = false;
|
||||
$options['verifyname'] = false;
|
||||
|
@ -116,24 +145,12 @@ class WP_CHINA_YES {
|
|||
$options['verify'] = $r['sslcertificates'];
|
||||
}
|
||||
|
||||
// All non-GET/HEAD requests should put the arguments in the form body.
|
||||
if ('HEAD' !== $type && 'GET' !== $type) {
|
||||
$options['data_format'] = 'body';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters whether SSL should be verified for non-local requests.
|
||||
*
|
||||
* @param bool $ssl_verify Whether to verify the SSL connection. Default true.
|
||||
* @param string $url The request URL.
|
||||
*
|
||||
* @since 2.8.0
|
||||
* @since 5.1.0 The `$url` parameter was added.
|
||||
*
|
||||
*/
|
||||
$options['verify'] = apply_filters('https_ssl_verify', $options['verify'], $url);
|
||||
|
||||
// Check for proxies.
|
||||
$proxy = new WP_HTTP_Proxy();
|
||||
if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
|
||||
$options['proxy'] = new Requests_Proxy_HTTP($proxy->host() . ':' . $proxy->port());
|
||||
|
@ -145,17 +162,14 @@ class WP_CHINA_YES {
|
|||
}
|
||||
}
|
||||
|
||||
// Avoid issues where mbstring.func_overload is enabled
|
||||
mbstring_binary_safe_encoding();
|
||||
|
||||
try {
|
||||
$requests_response = Requests::request($url, $headers, $data, $type, $options);
|
||||
|
||||
// Convert the response into an array
|
||||
$http_response = new WP_HTTP_Requests_Response($requests_response, $r['filename']);
|
||||
$response = $http_response->to_array();
|
||||
|
||||
// Add the original object to the array.
|
||||
$response['http_response'] = $http_response;
|
||||
} catch (Requests_Exception $e) {
|
||||
$response = new WP_Error('http_request_failed', $e->getMessage());
|
||||
|
@ -163,18 +177,6 @@ class WP_CHINA_YES {
|
|||
|
||||
reset_mbstring_encoding();
|
||||
|
||||
/**
|
||||
* Fires after an HTTP API response is received and before the response is returned.
|
||||
*
|
||||
* @param array|WP_Error $response HTTP response or WP_Error object.
|
||||
* @param string $context Context under which the hook is fired.
|
||||
* @param string $class HTTP transport used.
|
||||
* @param array $r HTTP request arguments.
|
||||
* @param string $url The request URL.
|
||||
*
|
||||
* @since 2.8.0
|
||||
*
|
||||
*/
|
||||
do_action('http_api_debug', $response, 'response', 'Requests', $r, $url);
|
||||
if (is_wp_error($response)) {
|
||||
return $response;
|
||||
|
@ -193,25 +195,47 @@ class WP_CHINA_YES {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the HTTP API response immediately before the response is returned.
|
||||
*
|
||||
* @param array $response HTTP response.
|
||||
* @param array $r HTTP request arguments.
|
||||
* @param string $url The request URL.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
*/
|
||||
return apply_filters('http_response', $response, $r, $url);
|
||||
}
|
||||
|
||||
public static function plugin_row_meta($links, $file) {
|
||||
$base = plugin_basename(WP_CHINA_YES_BASE_FILE);
|
||||
if ($file == $base) {
|
||||
$links[] = '<a href="https://github.com/sunxiyuan/wp-china-yes">插件发布页</a>';
|
||||
$links[] = '<a target="_blank" href="https://www.ibadboy.net/archives/3204.html">发布地址</a>';
|
||||
$links[] = '<a target="_blank" href="https://github.com/sunxiyuan/wp-china-yes">GitHub</a>';
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
public static function action_links($links, $file) {
|
||||
if ($file != plugin_basename(WP_CHINA_YES_BASE_FILE)) {
|
||||
return $links;
|
||||
}
|
||||
|
||||
$settings_link = '<a href="' . menu_page_url(self::$name, false) . '">设置</a>';
|
||||
|
||||
array_unshift($links, $settings_link);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
public static function admin_menu() {
|
||||
add_options_page(
|
||||
'WP-China-Yes',
|
||||
'WP-China-Yes',
|
||||
'manage_options',
|
||||
self::$name,
|
||||
array(__CLASS__, 'settings')
|
||||
);
|
||||
}
|
||||
|
||||
public static function settings() {
|
||||
echo <<<EOT
|
||||
<div style="height: 20px"></div>
|
||||
<iframe src="/wp-content/plugins/wp-china-yes/settings.html"
|
||||
frameborder="0" height="850" width="800px;" scrolling="No" leftmargin="0" topmargin="0">
|
||||
</iframe>
|
||||
EOT;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue