Skip to content
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

PHP: Use auto_prepend_file to preload mu-plugins (instead of creating them in wp-content/mu-plugins) #1366

Merged
merged 4 commits into from
May 9, 2024

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented May 6, 2024

Problem addressed by this PR

This PR moves the Playground mu-plugins from /wordpress/wp-content/mu-plugins, where they may interfere with the user-provided files and pollute the git repository, to /internal/mu-plugins, where they're invisible to the Playground consumer and don't show up in exports or OPFS mounts.

Closes #1318

Changes made

Technically, this PR uses the auto_prepend_file PHP options to pre-load the following script:

<?php
foreach (glob('/internal/preload/*.php') as $file) {
	require_once $file;
}

From here, preloading files is as simple as creating them in /internal/preload. This PR ships a few such preloaded files:

From there, the SQLite integration plugin and the Playground mu-plugins are placed in /internal/mu-plugins and do not affect the WordPress file structure.

This PR also reduces code duplication by moving some parts of the WordPress setup to the @wp-playground/wordpress package where they can be imported by both the web and the CLI versions of Playground. We'll only see more of these moving forward.

Follow-up work

  • Define clear use-cases and behaviors related to the location of Playground-provided files. E.g. Full site export, where we want to keep the SQLite integration plugin, or a GitHub repo, where we only want to keep track of our changes in wp-content.
  • Use @wp-playground/cli to build and setup WordPress in packages/playground/wordpress-builds/build/Dockerfile
  • Playground web: do not ship the SQLite integration plugin in the pre-built wp-content directory. Source it from the /internal/mu-plugins directory same way as in the CLI build.
  • Do not create the drop-in plugin wp-content/db.php
  • Do not install the WordPress importer plugin in wp-content/plugins
  • Figure out how to avoid touching wp-config.php – defining PHP constants may require rewriting it and it might be desirable to keep those changes on export

Testing instructions

  • Confirm the tests pass
  • In the browser – export Playground as zip, import it, also refresh the page and import again, confirm it all worked.

@adamziel adamziel requested review from bgrgicak and brandonpayton May 6, 2024 13:15
Copy link
Collaborator

@bgrgicak bgrgicak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the use of auto_prepend_file here, it seems like a better solution from mu-plugins.

One thing that came to my mind is that some plugins might want to use auto_prepend_file, for example Wordfence.
This is an edge case and they could add their PHP file to /internal/preload.

@@ -1,3 +1,107 @@
export * from './rewrite-rules';

export const RecommendedPHPVersion = '8.0';

export const envPHP_to_loadMuPlugins = `<?php
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why store this PHP code in JS? Would it be possible to load it from PHP files?

Copy link
Collaborator Author

@adamziel adamziel May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't figure out a way to do it that would work with all the build systems (Bun, ESBuild, Rollup). Bun has a .toml config file where we can define a custom loader, but it is only used with a few specific commands.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes, it's nice to be able to read see everything inline anyway.

@adamziel adamziel merged commit 24bcd5b into trunk May 9, 2024
5 checks passed
@adamziel adamziel deleted the preload-mu-plugins branch May 9, 2024 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Use global $wp_filter instead of an mu-plugin
3 participants