$r['timeout'],
'useragent' => $r['user-agent'],
'blocking' => $r['blocking'],
'hooks' => new WP_HTTP_Requests_Hooks($url, $r),
);
if ($r['stream']) {
$options['filename'] = $r['filename'];
}
if (empty($r['redirection'])) {
$options['follow_redirects'] = false;
} else {
$options['redirects'] = $r['redirection'];
}
if (isset($r['limit_response_size'])) {
$options['max_bytes'] = $r['limit_response_size'];
}
if ( ! empty($r['cookies'])) {
$options['cookies'] = WP_Http::normalize_cookies($r['cookies']);
}
if ( ! $r['sslverify']) {
$options['verify'] = false;
$options['verifyname'] = false;
} else {
$options['verify'] = $r['sslcertificates'];
}
if ('HEAD' !== $type && 'GET' !== $type) {
$options['data_format'] = 'body';
}
$options['verify'] = apply_filters('https_ssl_verify', $options['verify'], $url);
$proxy = new WP_HTTP_Proxy();
if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
$options['proxy'] = new Requests_Proxy_HTTP($proxy->host() . ':' . $proxy->port());
if ($proxy->use_authentication()) {
$options['proxy']->use_authentication = true;
$options['proxy']->user = $proxy->username();
$options['proxy']->pass = $proxy->password();
}
}
mbstring_binary_safe_encoding();
try {
$requests_response = Requests::request($url, $headers, $data, $type, $options);
$http_response = new WP_HTTP_Requests_Response($requests_response, $r['filename']);
$response = $http_response->to_array();
$response['http_response'] = $http_response;
} catch (Requests_Exception $e) {
$response = new WP_Error('http_request_failed', $e->getMessage());
}
reset_mbstring_encoding();
do_action('http_api_debug', $response, 'response', 'Requests', $r, $url);
if (is_wp_error($response)) {
return $response;
}
if ( ! $r['blocking']) {
return array(
'headers' => array(),
'body' => '',
'response' => array(
'code' => false,
'message' => false,
),
'cookies' => array(),
'http_response' => null,
);
}
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[] = '发布地址';
$links[] = 'GitHub';
}
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() {
$iframe_str = <<
EOT;
$plugin_root_url = plugins_url();
$iframe_str = str_replace('/wp-content/plugins', $plugin_root_url, $iframe_str);
echo $iframe_str;
}
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);
}
$options = array();
$options['community'] = sanitize_text_field(trim($_POST['community']));
$options['custom_api_server'] = sanitize_text_field(trim($_POST['custom_api_server']));
$options['custom_download_server'] = sanitize_text_field(trim($_POST['custom_download_server']));
$options["api_server"] = sanitize_text_field(trim($_POST['api_server']));
$options["download_server"] = sanitize_text_field(trim($_POST['download_server']));
update_option("wp_china_yes_options", $options);
self::success('', $options);
}
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;
}
}