返璞归真

本次提交去除社区源选择、自定义源设置相关代码,并移除了插件设置页。只提供一个唯一的保证高可靠高速度社区源。
需要使用自定义源的小伙伴可以看一下custom分支,这里有提供一个简化版本,可以直接植入主题的functions.php文件中。
https://github.com/sunxiyuan/wp-china-yes/tree/custom
This commit is contained in:
sunxiyuan 2020-04-02 16:46:24 +08:00
parent 3c95035bba
commit bf6eebb8d5
2 changed files with 3 additions and 395 deletions

152
index.php
View file

@ -9,22 +9,11 @@
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
define('WP_CHINA_YES_PATH', __DIR__);
define('WP_CHINA_YES_BASE_FILE', __FILE__);
WP_CHINA_YES::init();
class WP_CHINA_YES {
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'
@ -33,51 +22,16 @@ 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'
));
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'
));
if (empty(get_option('wp_china_yes_options'))) {
self::wp_china_yes_activate();
}
}
}
public static function wp_china_yes_activate() {
self::set_wp_option();
}
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;
}
$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);
$url = str_replace('api.wordpress.org', 'api.w.org.ibadboy.net', $url);
$url = str_replace('downloads.wordpress.org', 'd.w.org.ibadboy.net', $url);
/**
* 此处原本是复制了官方对外部请求处理的原始代码
@ -87,7 +41,7 @@ class WP_CHINA_YES {
}
public static function plugin_row_meta($links, $file) {
$base = plugin_basename(WP_CHINA_YES_BASE_FILE);
$base = plugin_basename(__FILE__);
if ($file == $base) {
$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>';
@ -95,104 +49,4 @@ class WP_CHINA_YES {
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('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() {
$setting_page_url = plugins_url('settings.html', __FILE__);
echo <<<EOT
<div style="height: 20px"></div>
<iframe src="$setting_page_url"
frameborder="0" height="850" width="800px;" scrolling="No" leftmargin="0" topmargin="0">
</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() {
if (( ! array_key_exists('api_server', $_POST) && ! array_key_exists('download_server', $_POST)) ||
( ! array_key_exists('community', $_POST) && ! array_key_exists('custom_api_server', $_POST) && ! array_key_exists('custom_download_server', $_POST))) {
self::error('参数错误', - 1);
}
self::set_wp_option(
sanitize_text_field(trim($_POST['community'])),
sanitize_text_field(trim($_POST['api_server'])),
sanitize_text_field(trim($_POST['download_server'])),
sanitize_text_field(trim($_POST['custom_api_server'])),
sanitize_text_field(trim($_POST['custom_download_server']))
);
self::success();
}
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,
$api_server = 'api.w.org.ibadboy.net',
$download_server = 'd.w.org.ibadboy.net',
$custom_api_server = '',
$custom_download_server = ''
) {
$options = array();
$options['community'] = (int) $community;
$options['api_server'] = $api_server;
$options['download_server'] = $download_server;
$options['custom_api_server'] = $custom_api_server;
$options['custom_download_server'] = $custom_download_server;
update_option("wp_china_yes_options", $options);
}
}

View file

@ -1,246 +0,0 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>WP-China-Yes设置页</title>
<link rel="stylesheet"
href="https://static.ibadboy.net/element/static/css/index.css">
</head>
<body id="body" style="display: none;">
<div id="app">
<el-container>
<el-header style="height: 100px;">
<h2>WP-China-Yes</h2>
<span>这是一个颠覆性的插件她将全面改善中国大陆站点在访问WP官方服务时的用户体验</span><br/>
<span>原理是将位于国外的官方仓库源替换为由社区志愿者维护的国内源,以此达到加速访问的目的</span>
</el-header>
<el-main>
<el-divider></el-divider>
<el-tabs tab-position="right" style="height: 520px;">
<el-tab-pane label="插件设置">
<el-container>
<el-main class="no_top_main_box">
<el-form ref="mirrors_form" :model="mirrors_form"
label-width="100px" size="small"
style="width: 342px;">
<el-card class="box-card" shadow="hover">
<div slot="header">
<span>使用社区源</span>
</div>
<el-form-item label="选择社区源">
<el-select v-model="mirrors_form.community"
@change="changeServer"
placeholder="请选择社区源">
<el-option v-for="(item,index) in external_api.server"
:label="item.name"
:value="item.id"></el-option>
</el-select>
</el-form-item>
<el-alert
:title="server_desc"
type="info"
:closable="false"
style="width: 422px; margin-left: 20px;"
show-icon>
</el-alert>
</el-card>
<el-card class="box-card" shadow="hover">
<div slot="header">
<span>自定义源</span>
<span style="font-size: 12px; margin-left: 10px;">若配置自定义源则会覆盖社区源</span>
<el-link href="https://www.ibadboy.net/archives/3349.html"
style="float: right;"
type="primary"
target="_blank"
:underline="false">搭建方法
</el-link>
</div>
<el-form-item label="API服务器" style="width: 442px;">
<el-input clearable
v-model="mirrors_form.custom_api_server">
<template slot="prepend">https://</template>
</el-input>
</el-form-item>
<el-form-item label="下载服务器" style="width: 442px;">
<el-input clearable
v-model="mirrors_form.custom_download_server">
<template slot="prepend">https://</template>
</el-input>
</el-form-item>
</el-card>
<el-card class="box-card"
style="height: 60px; text-align:center;"
shadow="never">
<el-button style="margin-top: -7px;"
type="primary"
@click="submitMirrorsForm" size="medium">保存配置
</el-button>
</el-card>
</el-form>
</el-main>
</el-container>
</el-tab-pane>
<!--
<el-tab-pane label="贡献者名单">
</el-tab-pane>
<el-tab-pane label="捐助者名单">
</el-tab-pane>
-->
<el-tab-pane label="广告列表">
</el-tab-pane>
</el-tabs>
</el-main>
<el-divider></el-divider>
<el-footer>
<span class="desc">这是一个开源项目,她需要每个人的支持和贡献才能健康长久的发展。</span><br/>
<span class="desc">项目地址:<a target="_blank"
href="https://github.com/sunxiyuan/wp-china-yes">GitHub</a></span>
</el-footer>
</el-container>
</div>
</body>
<script src="https://static.ibadboy.net/element/static/js/vue.js"></script>
<script src="https://static.ibadboy.net/element/static/js/index.js"></script>
<script src="https://static.ibadboy.net/element/static/js/axios.min.js"></script>
<script>
const root_url = window.location.href.split('wp-content')[0];
function getCookie(name) {
let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
arr = document.cookie.match(reg);
if (arr)
return (decodeURIComponent(arr[2]));
else
return null;
}
new Vue({
el: '#app',
data() {
return {
external_api: {},
server_desc: '',
token: {
get_config: '',
set_config: ''
},
mirrors_form: {
community: '0',
custom_api_server: '',
custom_download_server: '',
api_server: '',
download_server: '',
}
}
},
methods: {
changeServer() {
let server = this.external_api.server[this.mirrors_form.community];
this.server_desc = server.desc;
this.mirrors_form.api_server = server.api_server;
this.mirrors_form.download_server = server.download_server;
},
getConfig() {
let vm = this;
let data = new FormData();
data.append('_ajax_nonce', this.token.get_config);
data.append('action', 'wpcy_get_config');
axios.post(root_url + 'wp-admin/admin-ajax.php', data)
.then(function (response) {
vm.mirrors_form = response.data.data;
axios.get('https://wp-china-yes.ibadboy.net')
.then(function (response) {
vm.external_api = response.data;
vm.changeServer();
})
.catch(function (error) {
vm.$message({
message: error,
type: 'error'
});
});
})
.catch(function (error) {
vm.$message({
message: error.response.data.message,
type: 'error'
});
});
},
submitMirrorsForm() {
let vm = this;
this.$confirm('这将覆盖旧有配置, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
const loading = this.$loading({
lock: true,
text: '提交中',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
let data = new FormData();
data.append('_ajax_nonce', this.token.set_config);
data.append('action', 'wpcy_set_config');
data.append('community', vm.mirrors_form.community);
data.append('custom_api_server', vm.mirrors_form.custom_api_server);
data.append('custom_download_server', vm.mirrors_form.custom_download_server);
data.append('api_server', vm.mirrors_form.api_server);
data.append('download_server', vm.mirrors_form.download_server);
axios.post(root_url + 'wp-admin/admin-ajax.php', data)
.then(function (response) {
vm.$message({
message: '保存成功',
type: 'success'
});
loading.close();
})
.catch(function (error) {
vm.$message({
message: error.response.data.message,
type: 'error'
});
loading.close();
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消保存'
});
});
}
},
created: function () {
document.getElementById("body").style.display = "inline";
this.token = JSON.parse(getCookie('wp-china-yes'));
this.getConfig();
}
})
</script>
<style>
.desc {
font-size: 14px;
font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
.box-card {
margin-top: 20px;
width: 500px;
}
.no_top_main_box {
padding-top: 0;
}
</style>
</html>