From 93e8fdf0457d823bb02d32362ee468f37fd57453 Mon Sep 17 00:00:00 2001 From: yaosanqi137 Date: Fri, 4 Sep 2026 17:07:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dheader=E6=9D=BF?= =?UTF-8?q?=E5=9D=97=E6=8C=89=E9=92=AE=E6=97=A0=E6=B3=95=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E8=8B=B1=E6=96=87=E6=A0=87=E9=A2=98=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/ITStudioMainSite/functions.php | 138 ++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/themes/ITStudioMainSite/functions.php b/themes/ITStudioMainSite/functions.php index 6c11e0a..cddde79 100644 --- a/themes/ITStudioMainSite/functions.php +++ b/themes/ITStudioMainSite/functions.php @@ -18,6 +18,144 @@ function itstudio_theme_setup() { } add_action('after_setup_theme', 'itstudio_theme_setup'); +function itstudio_get_default_menu_item_english_title($title_cn) { + $titles = array( + '首页' => 'Home', + '公告通知' => 'Announcements', + '新闻' => 'News', + '社团新闻' => 'Club News', + '博客' => 'Blog', + '便民服务' => 'Services', + '加入我们' => 'Join Us', + '社团介绍' => 'About Us', + ); + + return isset($titles[$title_cn]) ? $titles[$title_cn] : $title_cn; +} + +function itstudio_render_menu_item_english_field($item_id, $item, $depth, $args, $current_object_id) { + $title_en = get_post_meta($item_id, '_itstudio_menu_title_en', true); + if ($title_en === '') { + $title_en = itstudio_get_default_menu_item_english_title(wp_strip_all_tags((string) $item->title)); + } + ?> +

+ +

+ ID, '_itstudio_menu_title_en', true); + if ($title_en === '') { + $title_en = itstudio_get_default_menu_item_english_title($title_cn); + } + + return sprintf('', esc_attr($title_cn), esc_attr($title_en)); +} +add_filter('nav_menu_item_title', 'itstudio_render_bilingual_menu_item_title', 10, 4); + +function itstudio_is_tidb_database() { + static $is_tidb = null; + + if ($is_tidb !== null) { + return $is_tidb; + } + + global $wpdb; + $server_info = method_exists($wpdb, 'db_server_info') ? (string) $wpdb->db_server_info() : ''; + if ($server_info === '') { + $server_info = (string) $wpdb->get_var('SELECT VERSION()'); + } + + $is_tidb = stripos($server_info, 'tidb') !== false; + return $is_tidb; +} + +function itstudio_tidb_media_query_args($query) { + if (!wp_doing_ajax() || !itstudio_is_tidb_database()) { + return $query; + } + + $query['no_found_rows'] = true; + $query['itstudio_tidb_media_query'] = true; + return $query; +} +add_filter('ajax_query_attachments_args', 'itstudio_tidb_media_query_args'); + +function itstudio_tidb_media_query_found_posts($posts, $query) { + if (!$query->get('itstudio_tidb_media_query') || !is_array($posts)) { + return $posts; + } + + $query->found_posts = count($posts); + $posts_per_page = (int) $query->get('posts_per_page'); + $query->max_num_pages = $posts_per_page > 0 ? (int) ceil($query->found_posts / $posts_per_page) : 0; + return $posts; +} +add_filter('the_posts', 'itstudio_tidb_media_query_found_posts', 10, 2); + +function itstudio_tidb_remove_sql_calc_found_rows($request, $query) { + if (!itstudio_is_tidb_database() || stripos($request, 'SQL_CALC_FOUND_ROWS') === false) { + return $request; + } + + $query_id = spl_object_id($query); + $GLOBALS['itstudio_tidb_found_rows_queries'][$query_id] = true; + $query->set('itstudio_tidb_found_rows_query', true); + + return preg_replace('/SQL_CALC_FOUND_ROWS\s+/i', '', $request, 1); +} +add_filter('posts_request', 'itstudio_tidb_remove_sql_calc_found_rows', 10, 2); + +function itstudio_tidb_calculate_found_posts($found_posts, $query) { + $query_id = spl_object_id($query); + $is_marked = !empty($GLOBALS['itstudio_tidb_found_rows_queries'][$query_id]) || $query->get('itstudio_tidb_found_rows_query'); + if (!$is_marked) { + return $found_posts; + } + + unset($GLOBALS['itstudio_tidb_found_rows_queries'][$query_id]); + + global $wpdb; + $count_query = preg_replace('/^\s*SELECT\s+.*?\s+FROM\s+/is', 'SELECT COUNT(*) FROM ', $query->request, 1); + $count_query = preg_replace('/\s+ORDER BY\s+.*?(?=\s+LIMIT\s+|\s*$)/is', ' ', $count_query, 1); + $count_query = preg_replace('/\s+LIMIT\s+\d+(?:\s*,\s*\d+)?\s*$/i', '', trim($count_query), 1); + + if ($count_query === null || $count_query === '') { + return $found_posts; + } + + $count = $wpdb->get_var($count_query); + return is_numeric($count) ? (int) $count : $found_posts; +} +add_filter('found_posts', 'itstudio_tidb_calculate_found_posts', 10, 2); + function itstudio_open_honor_menu_item_in_new_tab($atts, $item, $args) { $href = isset($atts['href']) ? (string) $atts['href'] : ''; $host = wp_parse_url($href, PHP_URL_HOST);