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.