-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wp now: activate plugin and theme the first time #351
Merged
sejas
merged 47 commits into
WordPress:trunk
from
Automattic:add/wp-now-plugin-theme-activation
May 17, 2023
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
db5ef96
blueprints: install JSDOM. DOMParser is not available in Node
sejas c7275d3
wp-now: activate plugin and theme the first time
sejas f57d901
playground-client: add jsdom as external
sejas 932dd3c
blueprints: improve activateTheme docblock
sejas e9010af
chore: move jsdom from devDependencies to dependencies in package.json
sejas 4bfe33a
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas cb7c70d
wp-now: await unitl the installation finishes
sejas d9505de
Merge branch 'trunk' into add/wp-now-plugin-theme-activation
sejas 37458fb
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas f2756fb
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas 24821ee
blueprints: refactor activateTheme to use switch_theme
sejas 43c9023
Merge branch 'add/wp-now-plugin-theme-activation' of github.com:Autom…
sejas c02ed81
blueprints: remove unused import for activateTheme
sejas ed28a2b
blueprints: make asDOM isomorphic conditionally loading jsdom for NodeJS
sejas 571ce93
wp-now: remove unused extractThemeName
sejas 5a44dca
fix code style
sejas 4a5b2be
blueprints: refactor activatePlugin to accept path and run php commands
sejas d814103
wp-now: refactor plugin activation to use the file path
sejas fe116cb
blueprints: undo asDOM function and restore jsdom to devDependencies
sejas d630396
wp-now: add tests for getPluginFile
sejas 7fba1f4
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas 11422b8
wp-now: refactor isPluginDirectory to use getPluginFile
sejas 87fdf1f
playground: undo unintended changes
sejas 83a794f
wp-now: remove unused slugify
sejas 12e6eaf
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas 28b2bac
wp-now: remove pluginFile console log
sejas 1871991
wp-now: display plugin file not found and avoid activate the plugin
sejas 91e4888
wp-now: getPluginFile use regular expression to match lower cases
sejas e07ca89
wp-now: limit plugin search to first 4KB of the file
sejas b9fe2c0
wp-now: search to be inside a comment block on getPluginFile
sejas ca5e41c
wp-now: add heuristicSort to getPluginFile
sejas 4d01b54
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas 659d75e
wp-now: remove condition before activatePlugin.
sejas d161ef4
blueprints: throw error if required files to activate plugin or theme…
sejas 5c1f71d
wp-now: add auto activateTheme activatePlugin integration tests
sejas 4438ed0
wp-now: include tests for second time the plugin should not be installed
sejas bda1d0d
wp-now: include tests for second time the theme should not be installed
sejas c6f4d3a
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas 2db98e9
wp-now: update getPluginFile regex
sejas 460fcfb
Merge branch 'trunk' of github.com:Automattic/wordpress-playground in…
sejas f719eba
wp-now: refactor activate plugin/theme tests to use getWpNowConfig
sejas 7eb9767
blueprints: add guard statements for activatePlugin and activateTheme
sejas 380d9e5
wp-now: increase readFileHead to 8KB
sejas 49818b0
wp-now: use WordPress core RegEx to match Plugin name
sejas ff48d9d
wp-now: refactor readFileHead to independent file
sejas a829a6a
wp-now: improve theme inference by using same core regex
sejas f736d00
blueprints: fix condition on activatePlugin
sejas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/playground/blueprints/src/lib/steps/activate-theme.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { StepHandler } from '.'; | ||
|
||
export interface ActivateThemeStep { | ||
step: 'activateTheme'; | ||
themeFolderName: string; | ||
} | ||
|
||
/** | ||
* Activates a WordPress theme in the Playground. | ||
* | ||
* @param playground The playground client. | ||
* @param themeFolderName The theme folder name. | ||
*/ | ||
export const activateTheme: StepHandler<ActivateThemeStep> = async ( | ||
playground, | ||
{ themeFolderName }, | ||
progress | ||
) => { | ||
progress?.tracker.setCaption(`Activating ${themeFolderName}`); | ||
const wpLoadPath = `${playground.documentRoot}/wp-load.php`; | ||
if (!playground.fileExists(wpLoadPath)) { | ||
throw new Error( | ||
`Required WordPress file does not exist: ${wpLoadPath}` | ||
); | ||
} | ||
await playground.run({ | ||
code: `<?php | ||
require_once( '${wpLoadPath}' ); | ||
switch_theme( '${themeFolderName}' ); | ||
`, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Theme Name: Twenty Twenty | ||
Theme Name: Yolo Theme | ||
Text Domain: twentytwenty | ||
Version: 2.2 | ||
Tested up to: 6.2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/wp-now/src/wp-playground-wordpress/get-plugin-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import fs from 'fs-extra'; | ||
import path, { basename } from 'path'; | ||
import { readFileHead } from './read-file-head'; | ||
|
||
/** | ||
* Sorts the files in the given array using a heuristic. | ||
* The plugin file usually has the same name as the project folder. | ||
* @param files The files to sort. | ||
* @returns The sorted files. | ||
*/ | ||
function heuristicSort(files: string[], projectPath: string) { | ||
const heuristicsBestGuess = `${basename(projectPath)}.php`; | ||
const heuristicsBestGuessIndex = files.indexOf(heuristicsBestGuess); | ||
if (heuristicsBestGuessIndex !== -1) { | ||
files.splice(heuristicsBestGuessIndex, 1); | ||
files.unshift(heuristicsBestGuess); | ||
} | ||
return files; | ||
} | ||
|
||
/** | ||
* | ||
* @param projectPath The path to the plugin. | ||
* @returns Path to the plugin file relative to the plugins directory. | ||
*/ | ||
export function getPluginFile(projectPath: string) { | ||
const files = heuristicSort(fs.readdirSync(projectPath), projectPath); | ||
for (const file of files) { | ||
if (file.endsWith('.php')) { | ||
const fileContent = readFileHead(path.join(projectPath, file)); | ||
const pluginNameRegex = | ||
/^(?:[ \t]*<\?php)?[ \t/*#@]*Plugin Name:(.*)$/im; | ||
if (pluginNameRegex.test(fileContent)) { | ||
return path.join(path.basename(projectPath), file); | ||
} | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/wp-now/src/wp-playground-wordpress/read-file-head.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import fs from 'fs-extra'; | ||
|
||
/** | ||
* | ||
* @param filePath The path to the file to read. | ||
* @param length The number of bytes to read from the file. By default 8KB. | ||
* @returns The first `length` bytes of the file as string. | ||
* @see https://developer.wordpress.org/reference/functions/get_file_data/ | ||
*/ | ||
export function readFileHead(filePath: string, length = 8192) { | ||
const buffer = Buffer.alloc(length); | ||
const fd = fs.openSync(filePath, 'r'); | ||
fs.readSync(fd, buffer, 0, buffer.length, 0); | ||
const fileContentBuffer = buffer.toString('utf8'); | ||
fs.closeSync(fd); | ||
return fileContentBuffer.toString(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also activate everything in
wp-content
mode?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💥, I think it is a different use case. But it makes sense to me.
@wojtekn, Would you like to code that functionality?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sejas @wojtekn Let's hold off activating everything in
wp-content
mode until we receive some explicit feedback indicating such.