Skip to content

Commit

Permalink
add child theme warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente Canales committed Apr 15, 2024
1 parent 55397c0 commit 19b6421
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion .github/scripts/create-preview-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ function getThemeName(themeSlug) {
return themeName;
}

/*
* This function reads the `style.css` file of a theme and returns the name of the parent theme.
* If the theme is not a child theme, it returns an empty string.
*
* @param {string} themeSlug - The slug of the theme to get the parent theme name of.
* @returns {string} - The name of the parent theme as defined in the `style.css` file.
*/
function getParentThemeName(themeSlug) {
const styleCss = fs.readFileSync(`${themeSlug}/style.css`, 'utf8');
const parentTheme = styleCss.match(/Template:(.*)/i);
const isChildTheme = '' !== parentTheme && parentTheme[1].trim();

if (!isChildTheme) {
return '';
}

return '' !== parentTheme && parentTheme[1].trim()
? parentTheme[1].trim()
: '';
}

/*
* This function creates a comment on a PR with preview links for the changed themes.
* It is used by `preview-theme` workflow.
Expand All @@ -56,14 +77,24 @@ async function createPreviewLinksComment(github, context, changedThemeSlugs) {
const changedThemes = changedThemeSlugs.split(' ');
const previewLinks = changedThemes
.map((themeSlug) => {
const parentThemeName = getParentThemeName(themeSlug);
const note = parentThemeName
? ` (child theme of **${parentThemeName}**)`
: '';

return `- [Preview changes for **${getThemeName(
themeSlug
)}**](https://playground.wordpress.net/#${createBlueprint(
themeSlug,
context.payload.pull_request.head.ref
)})`;
)})${note}`;
})
.join('\n');

const includesChildThemes = changedThemes.some(
(themeSlug) => '' !== getParentThemeName(themeSlug)
);

const comment = `
I've detected changes to the following themes in this PR: ${changedThemes
.map((themeSlug) => getThemeName(themeSlug))
Expand All @@ -75,6 +106,11 @@ ${previewLinks}
I will update this comment with the latest preview links as you push more changes to this PR.
**⚠️ Note:** The preview sites are created using [WordPress Playground](https://wordpress.org/playground/). You can add content, edit settings, and test the themes as you would on a real site, but please note that changes are not saved between sessions.
${
includesChildThemes
? '\n**⚠️ Note:** Child themes are dependent on their parent themes. You will have to install the parent theme as well for the preview to work correctly.'
: ''
}
`;

const repoData = {
Expand Down

0 comments on commit 19b6421

Please sign in to comment.