// ------------------------------------------------------------------
// ZEN MOVE: Vimeoサムネイルをサイト裏側で少しずつ自動取得する
//   ・表示側（page-member.php）は取得済みのものを読むだけ。ページ表示は常に軽い・安全
//   ・10件ずつ、時間を空けながら自動で全件に行き渡らせる
// ------------------------------------------------------------------
if (!function_exists('zenmove_fetch_and_cache_vimeo_thumb')) {
    function zenmove_fetch_and_cache_vimeo_thumb($post_id) {
        $cached = get_post_meta($post_id, '_zenmove_vimeo_thumb', true);
        if (!empty($cached)) {
            return $cached;
        }

        $video_tag = get_post_meta($post_id, 'video_tag', true);
        if (!preg_match('/video\/(\d+)/', $video_tag, $matches)) {
            return '';
        }
        $video_id = $matches[1];

        $response = wp_remote_get(
            'https://vimeo.com/api/oembed.json?url=' . urlencode('https://vimeo.com/' . $video_id),
            array('timeout' => 4)
        );

        if (is_wp_error($response)) {
            return '';
        }

        $data = json_decode(wp_remote_retrieve_body($response), true);
        if (empty($data['thumbnail_url'])) {
            return '';
        }

        update_post_meta($post_id, '_zenmove_vimeo_thumb', $data['thumbnail_url']);
        return $data['thumbnail_url'];
    }
}

if (!function_exists('zenmove_do_backfill_vimeo_thumbs')) {
    function zenmove_do_backfill_vimeo_thumbs() {
        $posts = get_posts(array(
            'post_type'      => 'post',
            'post_status'    => 'publish',
            'posts_per_page' => 10,
            'fields'         => 'ids',
            'meta_query'      => array(
                array(
                    'key'     => '_zenmove_vimeo_thumb',
                    'compare' => 'NOT EXISTS',
                ),
            ),
        ));

        if (empty($posts)) {
            return; // すべて取得済み
        }

        foreach ($posts as $post_id) {
            zenmove_fetch_and_cache_vimeo_thumb($post_id);
        }

        // まだ残りがあれば、少し時間を空けて続きを予約する
        if (!wp_next_scheduled('zenmove_backfill_vimeo_thumbs')) {
            wp_schedule_single_event(time() + 20, 'zenmove_backfill_vimeo_thumbs');
        }
    }
}
add_action('zenmove_backfill_vimeo_thumbs', 'zenmove_do_backfill_vimeo_thumbs');

// サイトが閲覧されるたびに、未取得分の取得予約がまだなければ予約する
add_action('init', function() {
    if (!wp_next_scheduled('zenmove_backfill_vimeo_thumbs')) {
        wp_schedule_single_event(time() + 10, 'zenmove_backfill_vimeo_thumbs');
    }
});<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://katoarisa.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://katoarisa.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://katoarisa.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://katoarisa.com/wp-sitemap-posts-news-1.xml</loc></sitemap><sitemap><loc>https://katoarisa.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://katoarisa.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
