

// ==================== 页面速度优化 ====================

// 1. 为内容中的图片添加原生懒加载
add_filter('the_content', 'speed_optimize_add_lazy_loading');
function speed_optimize_add_lazy_loading($content) {
    if (is_admin() || is_feed()) return $content;
    // 为没有loading属性的img添加loading="lazy"
    $content = preg_replace('/<img(?!.*\bloading=)/i', '<img loading="lazy"', $content);
    return $content;
}

// 2. 为缩略图添加懒加载
add_filter('post_thumbnail_html', 'speed_optimize_add_lazy_to_thumbnails');
function speed_optimize_add_lazy_to_thumbnails($html) {
    if (is_admin()) return $html;
    $html = preg_replace('/<img(?!.*\bloading=)/i', '<img loading="lazy"', $html);
    return $html;
}

// 3. 为头像添加懒加载
add_filter('get_avatar', 'speed_optimize_add_lazy_to_avatar');
function speed_optimize_add_lazy_to_avatar($avatar) {
    if (is_admin()) return $avatar;
    $avatar = preg_replace('/<img(?!.*\bloading=)/i', '<img loading="lazy"', $avatar);
    return $avatar;
}

// 4. 预连接到CDN域名，加速资源加载
add_action('wp_head', 'speed_optimize_preconnect', 1);
function speed_optimize_preconnect() {
    echo '<link rel="preconnect" href="//cdn.globalso.com">';
    echo '<link rel="dns-prefetch" href="//cdn.globalso.com">';
}

// 5. 禁用WordPress自带的emoji（已通过缓存插件禁用，这里双重保险）
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');

// 6. 移除wp-embed.min.js（如果不需要嵌入其他WordPress文章）
add_action('wp_footer', 'speed_optimize_deregister_embed');
function speed_optimize_deregister_embed() {
    wp_deregister_script('wp-embed');
}

// 7. 为图片添加width和height属性（防止布局偏移）
add_filter('the_content', 'speed_optimize_add_image_dimensions');
function speed_optimize_add_image_dimensions($content) {
    if (is_admin()) return $content;
    // 匹配没有width或height属性的img标签
    $pattern = '/<img([^>]*?)src=["\']([^"\']+)["\']([^>]*)>/i';
    preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $full_tag = $match[0];
        $before = $match[1];
        $src = $match[2];
        $after = $match[3];
        // 检查是否已有width和height
        if (stripos($full_tag, 'width=') === false || stripos($full_tag, 'height=') === false) {
            // 尝试获取图片尺寸（本地图片）
            $upload_dir = wp_upload_dir();
            $base_url = $upload_dir['baseurl'];
            if (strpos($src, $base_url) !== false) {
                $relative_path = str_replace($base_url, $upload_dir['basedir'], $src);
                if (file_exists($relative_path)) {
                    $size = getimagesize($relative_path);
                    if ($size && !empty($size[0]) && !empty($size[1])) {
                        $new_tag = '<img' . $before . 'src="' . $src . '" width="' . $size[0] . '" height="' . $size[1] . '"' . $after . '>';
                        $content = str_replace($full_tag, $new_tag, $content);
                    }
                }
            }
        }
    }
    return $content;
}
// ==================== 页面速度优化结束 ====================
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://www.ytopcaster.com/sitemap_addl.xml</loc>
	</sitemap>
	<sitemap>
		<loc>https://www.ytopcaster.com/sitemap_post.xml</loc>
	</sitemap>
	<sitemap>
		<loc>https://www.ytopcaster.com/sitemap_page.xml</loc>
	</sitemap>
	<sitemap>
		<loc>https://www.ytopcaster.com/sitemap_news.xml</loc>
	</sitemap>
	<sitemap>
		<loc>https://www.ytopcaster.com/sitemap_category.xml</loc>
	</sitemap>
	<sitemap>
		<loc>https://www.ytopcaster.com/sitemap_post_tag.xml</loc>
	</sitemap>
</sitemapindex>