2020-07-13 18:44:03 +08:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Plugin Name: WP-China-Yes
|
2020-08-15 15:29:26 +08:00
|
|
|
|
* Description: 将你的WordPress接入本土生态体系中,这将为你提供一个更贴近中国人使用习惯的WordPress
|
2020-07-13 18:44:03 +08:00
|
|
|
|
* Author: WP中国本土化社区
|
2020-08-15 15:34:46 +08:00
|
|
|
|
* Author URI:https://wp-china.org/
|
2020-08-26 17:24:57 +08:00
|
|
|
|
* Version: 3.1.0-Beta1
|
2020-07-13 18:44:03 +08:00
|
|
|
|
* License: GPLv3 or later
|
|
|
|
|
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-26 17:46:28 +08:00
|
|
|
|
if (is_admin() && !(defined('DOING_AJAX') && DOING_AJAX)) {
|
2020-07-16 09:17:47 +08:00
|
|
|
|
/**
|
|
|
|
|
* 引入设置页
|
|
|
|
|
*/
|
|
|
|
|
require __DIR__ . '/setting.php';
|
|
|
|
|
|
2020-07-13 18:44:03 +08:00
|
|
|
|
|
2020-08-15 15:30:30 +08:00
|
|
|
|
/**
|
|
|
|
|
* 插件列表项目中增加设置项
|
|
|
|
|
*/
|
|
|
|
|
add_filter('plugin_action_links', function ($links, $file) {
|
|
|
|
|
if ($file != plugin_basename(__FILE__)) {
|
|
|
|
|
return $links;
|
|
|
|
|
}
|
|
|
|
|
$settings_link = '<a href="' . menu_page_url('wp_china_yes', false) . '">设置</a>';
|
|
|
|
|
array_unshift($links, $settings_link);
|
|
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
|
}, 10, 2);
|
|
|
|
|
|
|
|
|
|
|
2020-07-19 16:07:52 +08:00
|
|
|
|
/**
|
|
|
|
|
* 初始化设置项
|
|
|
|
|
*/
|
2020-08-26 17:26:59 +08:00
|
|
|
|
if (empty(get_option('wpapi')) || empty(get_option('super_admin')) || empty(get_option('super_gravatar')) || empty(get_option('super_googlefonts'))) {
|
2020-08-15 15:30:52 +08:00
|
|
|
|
update_option("wpapi", '2');
|
2020-08-26 17:26:59 +08:00
|
|
|
|
update_option("super_admin", '1');
|
2020-07-19 16:07:52 +08:00
|
|
|
|
update_option("super_gravatar", '1');
|
|
|
|
|
update_option("super_googlefonts", '2');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 禁用插件时删除配置
|
|
|
|
|
*/
|
|
|
|
|
register_deactivation_hook(__FILE__, function () {
|
|
|
|
|
delete_option("wpapi");
|
2020-08-26 17:26:59 +08:00
|
|
|
|
delete_option("super_admin");
|
2020-07-19 16:07:52 +08:00
|
|
|
|
delete_option("super_gravatar");
|
|
|
|
|
delete_option("super_googlefonts");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2020-07-14 11:45:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 菜单注册
|
|
|
|
|
*/
|
|
|
|
|
add_action('admin_menu', function () {
|
2020-08-15 15:31:28 +08:00
|
|
|
|
add_options_page(
|
|
|
|
|
'WP-China-Yes',
|
|
|
|
|
'WP-China-Yes',
|
2020-07-14 11:45:12 +08:00
|
|
|
|
'manage_options',
|
2020-08-15 15:31:28 +08:00
|
|
|
|
'wp_china_yes',
|
|
|
|
|
'wpcy_options_page_html'
|
2020-07-14 11:45:12 +08:00
|
|
|
|
);
|
|
|
|
|
});
|
2020-07-16 09:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
2020-08-26 17:46:28 +08:00
|
|
|
|
/**
|
|
|
|
|
* 将WordPress核心所依赖的静态文件访问链接替换为jsDelivr提供的CDN节点
|
|
|
|
|
*/
|
|
|
|
|
if (get_option('super_admin') == 1) {
|
|
|
|
|
add_action('init', function () {
|
|
|
|
|
ob_start(function ($buffer) {
|
|
|
|
|
$buffer = str_replace(esc_url(home_url('/wp-includes/css/')), sprintf('https://cdn.jsdelivr.net/gh/WordPress/WordPress@%s/wp-includes/css/', $GLOBALS['wp_version']), $buffer);
|
|
|
|
|
return str_replace(esc_url(home_url('/wp-includes/js/')), sprintf('https://cdn.jsdelivr.net/gh/WordPress/WordPress@%s/wp-includes/js/', $GLOBALS['wp_version']), $buffer);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (is_admin()) {
|
2020-07-16 09:17:47 +08:00
|
|
|
|
/**
|
|
|
|
|
* 替换api.wordpress.org和downloads.wordpress.org为WP-China.org维护的大陆加速节点
|
|
|
|
|
* URL替换代码来自于我爱水煮鱼(http://blog.wpjam.com/)开发的WPJAM Basic插件
|
|
|
|
|
*/
|
|
|
|
|
add_filter('pre_http_request', function ($preempt, $r, $url) {
|
2020-08-26 18:11:45 +08:00
|
|
|
|
if ((!stristr($url, 'api.wordpress.org') && !stristr($url, 'downloads.wordpress.org')) || get_option('wpapi') == 3) {
|
2020-07-16 09:17:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-15 15:32:01 +08:00
|
|
|
|
if (get_option('wpapi') == 1) {
|
2020-07-16 09:17:47 +08:00
|
|
|
|
$url = str_replace('api.wordpress.org', 'api.wp-china-yes.net', $url);
|
2020-08-15 15:32:31 +08:00
|
|
|
|
$url = str_replace('downloads.wordpress.org', 'download.wp-china-yes.net', $url);
|
2020-07-16 09:17:47 +08:00
|
|
|
|
} else {
|
2020-08-15 15:32:31 +08:00
|
|
|
|
$url = str_replace('api.wordpress.org', 'api.w.org.ibadboy.net', $url);
|
|
|
|
|
$url = str_replace('downloads.wordpress.org', 'd.w.org.ibadboy.net', $url);
|
2020-07-16 09:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return wp_remote_request($url, $r);
|
|
|
|
|
}, 10, 3);
|
2020-08-26 17:46:28 +08:00
|
|
|
|
}
|
2020-08-26 17:26:59 +08:00
|
|
|
|
|
|
|
|
|
|
2020-08-26 17:46:28 +08:00
|
|
|
|
if (!(defined('DOING_AJAX') && DOING_AJAX)) {
|
2020-08-26 17:26:59 +08:00
|
|
|
|
/**
|
2020-08-26 17:46:28 +08:00
|
|
|
|
* 替换G家头像为WP-China.org维护的大陆加速节点
|
2020-08-26 17:26:59 +08:00
|
|
|
|
*/
|
2020-08-26 17:46:28 +08:00
|
|
|
|
if (get_option('super_gravatar') == 1) {
|
|
|
|
|
add_filter('get_avatar', function ($avatar) {
|
|
|
|
|
return str_replace([
|
|
|
|
|
'www.gravatar.com',
|
|
|
|
|
'0.gravatar.com',
|
|
|
|
|
'1.gravatar.com',
|
|
|
|
|
'2.gravatar.com',
|
|
|
|
|
'secure.gravatar.com',
|
|
|
|
|
'cn.gravatar.com'
|
|
|
|
|
], 'gravatar.wp-china-yes.net', $avatar);
|
2020-08-26 17:26:59 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-07-16 09:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
2020-08-26 17:46:28 +08:00
|
|
|
|
/**
|
|
|
|
|
* 替换谷歌字体为WP-China.org维护的大陆加速节点
|
|
|
|
|
*/
|
|
|
|
|
if (get_option('super_googlefonts') == 1) {
|
|
|
|
|
add_action('init', function () {
|
|
|
|
|
ob_start(function ($buffer) {
|
|
|
|
|
return str_replace('fonts.googleapis.com', 'googlefonts.wp-china-yes.net', $buffer);
|
|
|
|
|
});
|
2020-07-16 09:17:47 +08:00
|
|
|
|
});
|
2020-08-26 17:46:28 +08:00
|
|
|
|
}
|
2020-07-14 11:45:12 +08:00
|
|
|
|
}
|