fix: 修复header板块按钮无法设置英文标题的问题
This commit is contained in:
@@ -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));
|
||||
}
|
||||
?>
|
||||
<p class="description description-wide">
|
||||
<label for="edit-menu-item-itstudio-title-en-<?php echo esc_attr($item_id); ?>">
|
||||
<?php esc_html_e('英文名称', 'itstudio'); ?><br>
|
||||
<input type="text" id="edit-menu-item-itstudio-title-en-<?php echo esc_attr($item_id); ?>" class="widefat" name="itstudio_menu_title_en[<?php echo esc_attr($item_id); ?>]" value="<?php echo esc_attr($title_en); ?>" placeholder="<?php esc_attr_e('例如 Announcements', 'itstudio'); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
add_action('wp_nav_menu_item_custom_fields', 'itstudio_render_menu_item_english_field', 10, 5);
|
||||
|
||||
function itstudio_save_menu_item_english_field($menu_id, $menu_item_db_id, $args) {
|
||||
if (!current_user_can('edit_theme_options') || !isset($_POST['itstudio_menu_title_en']) || !is_array($_POST['itstudio_menu_title_en'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$titles_en = wp_unslash($_POST['itstudio_menu_title_en']);
|
||||
$title_en = isset($titles_en[$menu_item_db_id]) ? sanitize_text_field($titles_en[$menu_item_db_id]) : '';
|
||||
if ($title_en === '') {
|
||||
delete_post_meta($menu_item_db_id, '_itstudio_menu_title_en');
|
||||
return;
|
||||
}
|
||||
|
||||
update_post_meta($menu_item_db_id, '_itstudio_menu_title_en', $title_en);
|
||||
}
|
||||
add_action('wp_update_nav_menu_item', 'itstudio_save_menu_item_english_field', 10, 3);
|
||||
|
||||
function itstudio_render_bilingual_menu_item_title($title, $item, $args, $depth) {
|
||||
if (is_admin()) {
|
||||
return $title;
|
||||
}
|
||||
|
||||
$title_cn = wp_strip_all_tags((string) $title);
|
||||
$title_en = (string) get_post_meta($item->ID, '_itstudio_menu_title_en', true);
|
||||
if ($title_en === '') {
|
||||
$title_en = itstudio_get_default_menu_item_english_title($title_cn);
|
||||
}
|
||||
|
||||
return sprintf('<span data-cn="%s" data-en="%s"></span>', 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);
|
||||
|
||||
Reference in New Issue
Block a user