| Server IP : 45.152.65.97 / Your IP : 216.73.217.140 Web Server : Apache/2.4.54 (Win64) OpenSSL/1.1.1p mod_fcgid/2.3.9a System : Windows NT C20260123164925 10.0 build 20348 (Windows Server 2016) AMD64 User : SYSTEM ( 0) PHP Version : 7.4.33 Disable Function : passthru,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,putenv,exec MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/wwwroot/shujupai.cn/wp-content/plugins/jingcaizuqiu/includes/ |
Upload File : |
<?php
if (!defined('ABSPATH')) {
exit;
}
class JCZP_Admin {
public function __construct() {
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'register_settings'));
add_action('wp_ajax_jczp_manual_predict', array($this, 'manual_predict'));
add_action('wp_ajax_jczp_get_match_result', array($this, 'get_match_result'));
add_action('wp_ajax_jczp_test_api', array($this, 'test_api_connection'));
}
public function add_admin_menu() {
add_menu_page(
'竞彩足球预测器',
'竞彩足球预测',
'manage_options',
'jingcaizuqiu-predictor',
array($this, 'render_admin_page'),
'dashicons-chart-bar',
20
);
}
public function register_settings() {
register_setting('jczp_settings_group', 'jczp_api_key');
register_setting('jczp_settings_group', 'jczp_post_status');
register_setting('jczp_settings_group', 'jczp_post_category');
register_setting('jczp_settings_group', 'jczp_automode');
}
public function render_admin_page() {
if (!current_user_can('manage_options')) {
return;
}
?>
<div class="wrap jczp-wrap">
<h1>竞彩足球预测器</h1>
<div class="jczp-header">
<p>自动获取竞彩足球比赛数据,使用AI算法预测胜平负结果,并自动发布文章。</p>
</div>
<div class="jczp-main">
<div class="jczp-left">
<h2>手动操作</h2>
<button id="jczp-manual-predict" class="button button-primary button-large">立即预测并发布</button>
<button id="jczp-test-api" class="button button-secondary">测试API连接</button>
<p class="description">点击按钮立即获取今日比赛数据、预测结果并发布文章。</p>
<div id="jczp-result" style="display:none; margin-top: 20px;"></div>
</div>
<div class="jczp-right">
<h2>设置</h2>
<form method="post" action="options.php">
<?php
settings_fields('jczp_settings_group');
do_settings_sections('jczp_settings_group');
?>
<table class="form-table">
<tr valign="top">
<th scope="row">API密钥</th>
<td>
<input type="text" name="jczp_api_key" value="<?php echo esc_attr(get_option('jczp_api_key', '')); ?>" class="regular-text" />
<p class="description">输入football-data.org的API密钥。如不设置,将使用模拟数据。</p>
</td>
</tr>
<tr valign="top">
<th scope="row">发布状态</th>
<td>
<select name="jczp_post_status">
<option value="publish" <?php selected(get_option('jczp_post_status'), 'publish'); ?>>已发布</option>
<option value="draft" <?php selected(get_option('jczp_post_status'), 'draft'); ?>>草稿</option>
<option value="pending" <?php selected(get_option('jczp_post_status'), 'pending'); ?>>待审核</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">文章分类</th>
<td>
<?php
$categories = get_categories(array('hide_empty' => false));
$selected = get_option('jczp_post_category', 1);
?>
<select name="jczp_post_category">
<?php foreach ($categories as $cat): ?>
<option value="<?php echo $cat->term_id; ?>" <?php selected($selected, $cat->term_id); ?>>
<?php echo $cat->name; ?>
</option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">自动模式</th>
<td>
<label>
<input type="checkbox" name="jczp_automode" value="1" <?php checked(get_option('jczp_automode'), 1); ?>>
启用每日自动预测并发布
</label>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
</div>
</div>
<script type="text/javascript">
var jczp_nonce = '<?php echo wp_create_nonce("jczp_nonce"); ?>';
</script>
<?php
}
public function manual_predict() {
check_ajax_referer('jczp_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_send_json_error('权限不足');
}
$predictor = new JCZP_Predictor();
$results = $predictor->get_today_matches();
if (empty($results)) {
wp_send_json_error('未获取到今日比赛数据');
}
$post_creator = new JCZP_Post_Creator();
$post_id = $post_creator->create_post($results);
if (is_wp_error($post_id)) {
wp_send_json_error($post_id->get_error_message());
}
wp_send_json_success(array(
'matches' => $results,
'post_id' => $post_id,
'post_url' => get_permalink($post_id)
));
}
public function get_match_result() {
check_ajax_referer('jczp_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_send_json_error('权限不足');
}
$match_id = isset($_POST['match_id']) ? $_POST['match_id'] : '';
if (empty($match_id)) {
wp_send_json_error('缺少比赛ID');
}
$api = new JCZP_API();
$result = $api->get_match_result($match_id);
if (!$result || $result['status'] !== 'FINISHED') {
wp_send_json_error('比赛结果未更新');
}
$actual_result = $this->determine_result($result['home_score'], $result['away_score']);
wp_send_json_success(array(
'home_score' => $result['home_score'],
'away_score' => $result['away_score'],
'actual_result' => $actual_result,
'is_correct' => false
));
}
public function test_api_connection() {
check_ajax_referer('jczp_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_send_json_error('权限不足');
}
// 加载API类
require_once JCZP_PLUGIN_DIR . 'includes/api.php';
$api = new JCZP_API();
$matches = $api->get_today_matches();
if (!empty($matches)) {
$api_key = get_option('jczp_api_key', '');
$is_real_data = !empty($api_key) && strpos($matches[0]['id'], 'match_') === false;
wp_send_json_success(array(
'success' => true,
'matches_count' => count($matches),
'is_real_data' => $is_real_data,
'message' => $is_real_data ? 'API连接成功,获取到真实比赛数据' : '使用的是模拟数据,请检查API密钥设置'
));
} else {
wp_send_json_error('未获取到比赛数据');
}
}
private function determine_result($home_score, $away_score) {
if ($home_score > $away_score) {
return '胜';
} elseif ($home_score < $away_score) {
return '负';
} else {
return '平';
}
}
}