diff --git a/index.php b/index.php index 5d49bd4..ca15fea 100644 --- a/index.php +++ b/index.php @@ -9,11 +9,22 @@ * 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' @@ -22,20 +33,55 @@ 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' + )); add_action('wp_dashboard_setup', array( __CLASS__, 'donors_widgets' )); + + 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; } - $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); /** * 此处原本是复制了官方对外部请求处理的原始代码 @@ -45,7 +91,7 @@ class WP_CHINA_YES { } public static function plugin_row_meta($links, $file) { - $base = plugin_basename(__FILE__); + $base = plugin_basename(WP_CHINA_YES_BASE_FILE); if ($file == $base) { $links[] = '发布地址'; $links[] = 'GitHub'; @@ -54,9 +100,109 @@ 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 = '设置'; + + 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; + } + + 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(); + } + public static function donors_widgets() { wp_add_dashboard_widget('custom_help_widget', '《WordPress中国区仓库源建设计划》赞助者', function () { require_once plugin_dir_path(__FILE__) . 'donors_widgets.php'; }); } + + 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); + } } \ No newline at end of file diff --git a/settings.html b/settings.html new file mode 100644 index 0000000..fd285a2 --- /dev/null +++ b/settings.html @@ -0,0 +1,244 @@ + + + + + WP-China-Yes设置页 + + + +
+ + +

WP-China-Yes

+ 这是一个颠覆性的插件,她将全面改善中国大陆站点在访问WP官方服务时的用户体验
+ 原理是将位于国外的官方仓库源替换为由社区志愿者维护的国内源,以此达到加速访问的目的 +
+ + + + + + + + +
+ 使用社区源 +
+ + + + + + + +
+ +
+ 自定义源 + 若配置自定义源则会覆盖社区源 + 搭建方法 + +
+ + + + + + + + + + +
+ + 保存配置 + + +
+
+
+
+ + + + +
+
+ + + 这是一个开源项目,她需要每个人的支持和贡献才能健康长久的发展。
+ 项目地址:GitHub +
+
+
+ + + + + + + \ No newline at end of file