PageSourceSearch

https://smhc.co.jp/js/wordpress.js?v=20260421c

js smhc.co.jp collected 2026-10-07 15:45:06 UTC 4,484 bytes, 123 lines download raw bytes

1/**
2 * SMHC用 WordPress API クライアント
3 */
4
5const WordPressAPI = {
6    // 開発環境(localhost)では絶対パス、本番環境では相対パスを使用
7    endpoint: window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
8        ? 'https://3.113.1.142/wp-json/wp/v2'
9        : '/wp-json/wp/v2',
10
11    /**
12     * 特定の投稿をIDで取得する
13     * @param {number|string} id 投稿ID
14     * @returns {Promise<Object|null>}
15     */
16    async getPostById(id, isPreview = false) {
17        try {
18            let url = `${this.endpoint}/posts/${id}?_embed`;
19            if (isPreview) {
20                // プレビューモードの場合はステータスを限定しない
21                url += `&status=any`;
22            }
23            const response = await fetch(url);
24            if (!response.ok) throw new Error('Post not found');
25
26            const post = await response.json();
27
28            // アイキャッチ画像のAltテキスト取得
29            let featuredImageAlt = '';
30            try {
31                if (post._embedded && post._embedded['wp:featuredmedia']) {
32                    featuredImageAlt = post._embedded['wp:featuredmedia'][0].alt_text || post.title.rendered;
33                }
34            } catch (e) { }
35
36            return {
37                id: post.id,
38                date: this.formatDate(post.date),
39                title: this.decodeHTMLEntities(post.title.rendered),
40                content: post.content.rendered,
41                category: this.getCategoryName(post),
42                url: post.link,
43                featured_image: post._embedded?.['wp:featuredmedia']?.[0]?.source_url || '',
44                featured_image_alt: featuredImageAlt
45            };
46        } catch (error) {
47            console.error('WordPress 記事取得エラー:', error);
48            return null;
49        }
50    },
51
52    /**
53     * 日付のフォーマット (YYYY.MM.DD)
54     */
55    formatDate(dateString) {
56        const dateObj = new Date(dateString);
57        return `${dateObj.getFullYear()}.${(dateObj.getMonth() + 1).toString().padStart(2, '0')}.${dateObj.getDate().toString().padStart(2, '0')}`;
58    },
59
60    /**
61     * 最新の投稿を取得する
62     * @param {number} count 取得件数
63     * @param {number} page ページ番号
64     * @param {number|string} categories カテゴリーID (カンマ区切り)
65     * @returns {Promise<Array>}
66     */
67    async getLatestPosts(count = 5, page = 1, categories = '') {
68        try {
69            let url = `${this.endpoint}/posts?per_page=${count}&page=${page}&_embed`;
70            if (categories) {
71                url += `&categories=${categories}`;
72            }
73
74            const response = await fetch(url);
75            if (!response.ok) throw new Error('Network response was not ok');
76
77            // 全ページ数をヘッダーから取得 (ページネーション用)
78            const totalPages = parseInt(response.headers.get('X-WP-TotalPages') || '1');
79
80            const posts = await response.json();
81
82            return {
83                posts: posts.map(post => {
84                    return {
85                        id: post.id,
86                        date: this.formatDate(post.date),
87                        title: this.decodeHTMLEntities(post.title.rendered),
88                        // カテゴリー取得 (埋め込みデータから)
89                        category: this.getCategoryName(post),
90                        url: post.link
91                    };
92                }),
93                totalPages
94            };
95        } catch (error) {
96            console.error('WordPress 取得エラー:', error);
97            return { posts: [], totalPages: 0 };
98        }
99    },
100
101    /**
102     * HTMLエンティティのデコード
103     */
104    decodeHTMLEntities(text) {
105        const textArea = document.createElement('textarea');
106        textArea.innerHTML = text;
107        return textArea.value;
108    },
109
110    /**
111     * カテゴリー名の抽出 (_embed利用)
112     */
113    getCategoryName(post) {
114        try {
115            if (post._embedded && post._embedded['wp:term'] && post._embedded['wp:term'][0]) {
116                return post._embedded['wp:term'][0][0].name;
117            }
118        } catch (e) { }
119        return 'ニュース'; // デフォルト
120    }
121};
122
123export default WordPressAPI;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.