| @@ -20,8 +20,10 @@ | ||
| 20 | 20 | 'footer-with-nav', |
| 21 | 21 | 'footer-with-center-logo-social-nav', |
| 22 | 22 | ]; |
| 23 | 23 | |
| 24 | +const MS_PER_DAY = 86_400_000; | |
| 25 | + | |
| 24 | 26 | export const updateOption = (option, value) => |
| 25 | 27 | apiFetch({ |
| 26 | 28 | path: '/extendify/v1/auto-launch/options', |
| 27 | 29 | method: 'POST', |
| @@ -132,9 +134,48 @@ | ||
| 132 | 134 | |
| 133 | 135 | export const createCategory = (data) => |
| 134 | 136 | apiFetch({ path: '/wp/v2/categories', method: 'POST', data }); |
| 135 | 137 | |
| 136 | -export const createBlogSampleData = async (siteStrings, siteImages) => { | |
| 138 | +const formatImageUrl = (image) => | |
| 139 | + image?.includes('?q=80&w=1470') ? image : `${image}?q=80&w=1470`; | |
| 140 | + | |
| 141 | +const replacePostContentImages = (content, images) => | |
| 142 | + (content.match(/https:\/\/images\.unsplash\.com\/[^\s"]+/g) || []).reduce( | |
| 143 | + (updated, match, i) => | |
| 144 | + updated.replace(match, formatImageUrl(images[i] || match)), | |
| 145 | + content, | |
| 146 | + ); | |
| 147 | + | |
| 148 | +// The blog-section images fill the newest posts so the home's date-desc loop matches | |
| 149 | +// the preview 1:1; extra posts fall back to the shared pool. | |
| 150 | +export const buildBlogPosts = ({ | |
| 151 | + blogTitles, | |
| 152 | + siteImages, | |
| 153 | + blogImages = [], | |
| 154 | + sampleData, | |
| 155 | +}) => { | |
| 156 | + const pool = siteImages || []; | |
| 157 | + // Every post shares the same sample body, so resolve its images once. | |
| 158 | + const postContent = replacePostContentImages(sampleData.post_content, pool); | |
| 159 | + return Array.from({ length: 8 }, (_, i) => { | |
| 160 | + const title = | |
| 161 | + blogTitles?.[i] || | |
| 162 | + // translators: %s is a post number | |
| 163 | + sprintf(__('Blog Post %s', 'extendify-local'), i + 1); | |
| 164 | + const image = blogImages[i] ?? pool[i % pool.length]; | |
| 165 | + return { | |
| 166 | + name: title, | |
| 167 | + featured_image: image ? formatImageUrl(image) : null, | |
| 168 | + post_content: postContent, | |
| 169 | + }; | |
| 170 | + }); | |
| 171 | +}; | |
| 172 | + | |
| 173 | +export const createBlogSampleData = async ( | |
| 174 | + siteStrings, | |
| 175 | + siteImages, | |
| 176 | + blogImages, | |
| 177 | +) => { | |
| 137 | 178 | const localizedBlogSampleData = |
| 138 | 179 | blogSampleData[window.extSharedData?.wpLanguage || 'en_US'] || |
| 139 | 180 | blogSampleData.en_US; |
| 140 | 181 | |
| @@ -140,35 +181,14 @@ | ||
| 140 | 181 | |
| 141 | 182 | const categories = |
| 142 | 183 | (await createWpCategories(localizedBlogSampleData.categories)) || []; |
| 143 | 184 | const tags = (await createWpTags(localizedBlogSampleData.tags)) || []; |
| 144 | - const formatImageUrl = (image) => | |
| 145 | - image?.includes('?q=80&w=1470') ? image : `${image}?q=80&w=1470`; | |
| 146 | - const imagesArray = (siteImages || []).sort(() => Math.random() - 0.5); | |
| 147 | 185 | |
| 148 | - const replacePostContentImages = (content, images) => | |
| 149 | - (content.match(/https:\/\/images\.unsplash\.com\/[^\s"]+/g) || []).reduce( | |
| 150 | - (updated, match, i) => | |
| 151 | - updated.replace(match, formatImageUrl(images[i] || match)), | |
| 152 | - content, | |
| 153 | - ); | |
| 154 | - | |
| 155 | - const posts = Array.from({ length: 8 }, (_, i) => { | |
| 156 | - const title = | |
| 157 | - siteStrings?.aiBlogTitles?.[i] || | |
| 158 | - // translators: %s is a post number | |
| 159 | - sprintf(__('Blog Post %s', 'extendify-local'), i + 1); | |
| 160 | - const featuredImage = imagesArray[i % imagesArray.length] | |
| 161 | - ? formatImageUrl(imagesArray[i % imagesArray.length]) | |
| 162 | - : null; | |
| 163 | - return { | |
| 164 | - name: title, | |
| 165 | - featured_image: featuredImage, | |
| 166 | - post_content: replacePostContentImages( | |
| 167 | - localizedBlogSampleData.post_content, | |
| 168 | - imagesArray, | |
| 169 | - ), | |
| 170 | - }; | |
| 186 | + const posts = buildBlogPosts({ | |
| 187 | + blogTitles: siteStrings?.aiBlogTitles, | |
| 188 | + siteImages, | |
| 189 | + blogImages, | |
| 190 | + sampleData: localizedBlogSampleData, | |
| 171 | 191 | }); |
| 172 | 192 | |
| 173 | 193 | for (const [index, post] of posts.entries()) { |
| 174 | 194 | try { |
| @@ -194,8 +214,13 @@ | ||
| 194 | 214 | const postData = { |
| 195 | 215 | title: post.name, |
| 196 | 216 | content: post.post_content, |
| 197 | 217 | status: 'publish', |
| 218 | + // Space posts a day apart, newest first, so the home's date-desc loop | |
| 219 | + // matches the preview order. | |
| 220 | + date_gmt: new Date(Date.now() - index * MS_PER_DAY) | |
| 221 | + .toISOString() | |
| 222 | + .slice(0, 19), | |
| 198 | 223 | featured_media: mediaId || null, |
| 199 | 224 | categories: category, |
| 200 | 225 | tags: tagFeaturedPost, |
| 201 | 226 | meta: { made_with_extendify_launch: true }, |