mirror of
https://github.com/WenPai-org/wp-china-yes.git
synced 2025-08-06 21:53:56 +08:00
修复API接口编写不规范的问题
This commit is contained in:
parent
368818acd0
commit
8fe755c5cd
4 changed files with 88 additions and 59 deletions
65
index.php
65
index.php
|
@ -41,6 +41,18 @@ class WP_CHINA_YES {
|
|||
__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();
|
||||
|
@ -242,4 +254,55 @@ EOT;
|
|||
$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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue