get('Version') ?: '0.0.0'; } /** * Get update endpoint URL */ private function get_endpoint(): string { if (class_exists('AKSRT_Settings')) { $endpoint = AKSRT_Settings::get_value('update_endpoint', ''); if (!empty($endpoint)) return $endpoint; } return 'https://api.aksrt.com/version.json'; } /** * Maybe check for updates (once per day) */ public function maybe_check(): void { $stored = get_option(self::OPTION_KEY, []); $last_check = $stored['last_check'] ?? 0; if ((time() - $last_check) < self::CHECK_INTERVAL) { return; } $this->do_check(); } /** * Force check for updates */ public function do_check(): void { $endpoint = $this->get_endpoint(); $response = wp_remote_get($endpoint, [ 'timeout' => 5, 'headers' => [ 'Accept' => 'application/json', ], ]); if (is_wp_error($response)) { update_option(self::OPTION_KEY, [ 'last_check' => time(), 'error' => $response->get_error_message(), ]); return; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (!is_array($data) || empty($data['version'])) { return; } $current = $this->get_current_version(); $remote_version = $data['version']; $update_available = version_compare($remote_version, $current, '>'); update_option(self::OPTION_KEY, [ 'last_check' => time(), 'current_version' => $current, 'remote_version' => $remote_version, 'update_available' => $update_available, 'download_url' => $data['download_url'] ?? '', 'changelog' => $data['changelog'] ?? '', 'notify' => !empty($data['notify']), ]); } /** * Show admin notice if update is available */ public function show_update_notice(): void { if (!current_user_can('update_themes')) return; $stored = get_option(self::OPTION_KEY, []); if (empty($stored['update_available'])) return; $remote = $stored['remote_version'] ?? ''; $current = $stored['current_version'] ?? ''; $download_url = $stored['download_url'] ?? ''; $changelog = $stored['changelog'] ?? ''; // User dismissed this version? $dismissed = get_user_meta(get_current_user_id(), 'aksrt_dismissed_update', true); if ($dismissed === $remote) return; $dismiss_url = wp_nonce_url( add_query_arg('aksrt_dismiss_update', $remote, admin_url()), 'aksrt_dismiss_update_' . $remote ); echo '
'; echo '

' . esc_html__('AKSRT 主题更新', 'aksrt') . '

'; echo '

' . sprintf( esc_html__('新版本 %s 可用(当前版本 %s)。', 'aksrt'), '' . esc_html($remote) . '', esc_html($current) ) . '

'; if (!empty($changelog)) { echo '

' . esc_html($changelog) . '

'; } echo '

'; if (!empty($download_url)) { echo '' . esc_html__('下载更新', 'aksrt') . ' '; } echo '' . esc_html__('忽略此版本', 'aksrt') . ''; echo '

'; echo '
'; } /** * Handle dismiss action */ public static function handle_dismiss(): void { if (!isset($_GET['aksrt_dismiss_update'])) return; $version = sanitize_text_field($_GET['aksrt_dismiss_update']); if (!wp_verify_nonce($_GET['_wp_nonce'] ?? '', 'aksrt_dismiss_update_' . $version)) return; update_user_meta(get_current_user_id(), 'aksrt_dismissed_update', $version); wp_safe_redirect(remove_query_arg(['aksrt_dismiss_update', '_wp_nonce'])); exit; } /** * Get stored update info */ public function get_update_info(): array { return get_option(self::OPTION_KEY, []); } } // Handle dismiss on admin_init add_action('admin_init', function () { if (isset($_GET['aksrt_dismiss_update'])) { AKSRT_Update_Checker::handle_dismiss(); } });