2020-02-29 19:21:23 +08:00
|
|
|
|
<?php
|
|
|
|
|
/*
|
2020-03-12 17:14:13 +08:00
|
|
|
|
* Plugin Name: WP-China-Yes
|
2020-03-12 19:40:51 +08:00
|
|
|
|
* Description: 这是一个颠覆性的插件,她将全面改善中国大陆站点在访问WP官方服务时的用户体验,其原理是将位于国外的官方仓库源替换为由社区志愿者维护的国内源,以此达到加速访问的目的。
|
2020-04-21 01:49:08 +08:00
|
|
|
|
* Author: WP-China-Yes
|
|
|
|
|
* Version: 2.2.0
|
|
|
|
|
* Author URI:https://wp-china-yes.org/
|
2020-03-12 17:14:13 +08:00
|
|
|
|
* License: GPLv3 or later
|
|
|
|
|
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
*/
|
2020-02-29 19:21:23 +08:00
|
|
|
|
|
2020-04-10 14:59:34 +08:00
|
|
|
|
define('WP_CHINA_YES_PATH', __DIR__);
|
|
|
|
|
define('WP_CHINA_YES_BASE_FILE', __FILE__);
|
|
|
|
|
|
2020-02-29 19:21:23 +08:00
|
|
|
|
WP_CHINA_YES::init();
|
|
|
|
|
|
|
|
|
|
class WP_CHINA_YES {
|
|
|
|
|
public static function init() {
|
2020-04-27 09:09:40 +08:00
|
|
|
|
add_filter('pre_http_request', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'pre_http_request'
|
|
|
|
|
), 10, 3);
|
|
|
|
|
$post_action = isset($_POST['action']) ? sanitize_text_field(trim($_POST['action'])) : ' ';
|
|
|
|
|
if( defined( 'DOING_AJAX' ) && DOING_AJAX && !in_array($post_action,array('wpcy_set_config','wpcy_get_config'))){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-29 19:21:23 +08:00
|
|
|
|
if (is_admin()) {
|
2020-04-27 13:21:38 +08:00
|
|
|
|
if (empty(get_option('wp_china_yes_options'))) {
|
|
|
|
|
self::set_wp_option();
|
|
|
|
|
}
|
2020-04-10 14:59:34 +08:00
|
|
|
|
register_deactivation_hook(WP_CHINA_YES_BASE_FILE, array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'wp_china_yes_deactivate'
|
|
|
|
|
));
|
2020-02-29 19:21:23 +08:00
|
|
|
|
add_filter('plugin_row_meta', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'plugin_row_meta'
|
|
|
|
|
), 10, 2);
|
2020-04-10 14:59:34 +08:00
|
|
|
|
add_filter('plugin_action_links', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'action_links'
|
|
|
|
|
), 10, 2);
|
|
|
|
|
add_action('admin_menu', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'admin_menu'
|
|
|
|
|
));
|
|
|
|
|
add_action('init', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'set_cookie'
|
|
|
|
|
));
|
|
|
|
|
add_action('wp_ajax_wpcy_get_config', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'get_config'
|
|
|
|
|
));
|
|
|
|
|
add_action('wp_ajax_wpcy_set_config', array(
|
|
|
|
|
__CLASS__,
|
|
|
|
|
'set_config'
|
|
|
|
|
));
|
2020-04-04 12:06:39 +08:00
|
|
|
|
add_action('wp_dashboard_setup', array(
|
|
|
|
|
__CLASS__,
|
2020-04-10 16:50:14 +08:00
|
|
|
|
'sponsor_widget'
|
2020-04-04 12:06:39 +08:00
|
|
|
|
));
|
2020-02-29 19:21:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 14:59:34 +08:00
|
|
|
|
public static function wp_china_yes_deactivate() {
|
|
|
|
|
delete_option('wp_china_yes_options');
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 19:21:23 +08:00
|
|
|
|
public static function pre_http_request($preempt, $r, $url) {
|
|
|
|
|
if ( ! stristr($url, 'api.wordpress.org') && ! stristr($url, 'downloads.wordpress.org')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 15:07:28 +08:00
|
|
|
|
$options = get_option('wp_china_yes_options');
|
|
|
|
|
|
|
|
|
|
if ($options["community"] == 1) {
|
|
|
|
|
$api_server = 'api.w.org.ixmu.net';
|
|
|
|
|
$download_server = 'd.w.org.ixmu.net';
|
|
|
|
|
} else {
|
|
|
|
|
$api_server = 'api.w.org.ibadboy.net';
|
|
|
|
|
$download_server = 'd.w.org.ibadboy.net';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$api_server = $options["custom_api_server"] ?: $api_server;
|
|
|
|
|
$download_server = $options["custom_download_server"] ?: $download_server;
|
2020-04-10 14:59:34 +08:00
|
|
|
|
$url = str_replace('api.wordpress.org', $api_server, $url);
|
|
|
|
|
$url = str_replace('downloads.wordpress.org', $download_server, $url);
|
2020-02-29 19:21:23 +08:00
|
|
|
|
|
2020-03-25 09:48:42 +08:00
|
|
|
|
/**
|
|
|
|
|
* 此处原本是复制了官方对外部请求处理的原始代码
|
|
|
|
|
* 后经我爱水煮鱼(http://blog.wpjam.com/)提醒,可以直接调用wp_remote_request达成相同目的,由此精简掉100余行代码。
|
|
|
|
|
*/
|
2020-03-27 15:13:07 +08:00
|
|
|
|
return wp_remote_request($url, $r);
|
2020-02-29 19:21:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function plugin_row_meta($links, $file) {
|
2020-04-10 14:59:34 +08:00
|
|
|
|
$base = plugin_basename(WP_CHINA_YES_BASE_FILE);
|
2020-02-29 19:21:23 +08:00
|
|
|
|
if ($file == $base) {
|
2020-03-12 17:14:13 +08:00
|
|
|
|
$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;
|
|
|
|
|
}
|
2020-04-04 12:06:39 +08:00
|
|
|
|
|
2020-04-10 14:59:34 +08:00
|
|
|
|
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('wp_china_yes', 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',
|
|
|
|
|
'wp_china_yes',
|
|
|
|
|
array(__CLASS__, 'settings')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function settings() {
|
2020-04-27 13:23:03 +08:00
|
|
|
|
$setting_page_url = plugins_url('settings.html', __FILE__) . '?v=2.2.0';
|
2020-04-10 14:59:34 +08:00
|
|
|
|
echo <<<EOT
|
2020-04-27 13:23:03 +08:00
|
|
|
|
<iframe src="$setting_page_url" style="margin-top: 20px;"
|
|
|
|
|
frameborder="0" height="700px;" width="600px;" scrolling="No" leftmargin="0" topmargin="0">
|
2020-04-10 14:59:34 +08:00
|
|
|
|
</iframe>
|
|
|
|
|
EOT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function set_cookie() {
|
|
|
|
|
if ( ! isset($_COOKIE['wp-china-yes']) && current_user_can('manage_options')) {
|
|
|
|
|
setcookie('wp-china-yes', json_encode([
|
|
|
|
|
'get_config' => wp_create_nonce('wpcy_get_config'),
|
|
|
|
|
'set_config' => wp_create_nonce('wpcy_set_config')
|
|
|
|
|
], JSON_UNESCAPED_UNICODE), time() + 1209600, COOKIEPATH, COOKIE_DOMAIN, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function get_config() {
|
|
|
|
|
self::success('', get_option('wp_china_yes_options'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function set_config() {
|
2020-04-10 15:07:28 +08:00
|
|
|
|
if ( ! array_key_exists('community', $_POST) ||
|
|
|
|
|
( ! array_key_exists('custom_api_server', $_POST) && ! array_key_exists('custom_download_server', $_POST))) {
|
2020-04-10 14:59:34 +08:00
|
|
|
|
self::error('参数错误', - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::set_wp_option(
|
|
|
|
|
sanitize_text_field(trim($_POST['community'])),
|
|
|
|
|
sanitize_text_field(trim($_POST['custom_api_server'])),
|
|
|
|
|
sanitize_text_field(trim($_POST['custom_download_server']))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self::success();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 16:50:14 +08:00
|
|
|
|
public static function sponsor_widget() {
|
2020-04-21 05:38:08 +08:00
|
|
|
|
wp_add_dashboard_widget('sponsor_widget', '《WordPress中国区仓库源建设计划》赞助商', function () {
|
2020-04-10 16:50:14 +08:00
|
|
|
|
require_once plugin_dir_path(__FILE__) . 'sponsor_widget.php';
|
2020-04-04 12:06:39 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-10 14:59:34 +08:00
|
|
|
|
|
|
|
|
|
private static function success($message = '', $data = []) {
|
|
|
|
|
header('Content-Type:application/json; charset=utf-8');
|
|
|
|
|
|
|
|
|
|
echo json_encode([
|
|
|
|
|
'code' => 0,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'data' => $data
|
|
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function error($message = '', $code = - 1) {
|
|
|
|
|
header('Content-Type:application/json; charset=utf-8');
|
|
|
|
|
header('Status:500');
|
|
|
|
|
|
|
|
|
|
echo json_encode([
|
|
|
|
|
'code' => $code,
|
|
|
|
|
'message' => $message
|
|
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function set_wp_option(
|
|
|
|
|
$community = 0,
|
|
|
|
|
$custom_api_server = '',
|
|
|
|
|
$custom_download_server = ''
|
|
|
|
|
) {
|
|
|
|
|
$options = array();
|
|
|
|
|
$options['community'] = (int) $community;
|
|
|
|
|
$options['custom_api_server'] = $custom_api_server;
|
|
|
|
|
$options['custom_download_server'] = $custom_download_server;
|
|
|
|
|
update_option("wp_china_yes_options", $options);
|
|
|
|
|
}
|
2020-04-27 09:09:40 +08:00
|
|
|
|
}
|