[create-block] Add ABSPATH check #55533
Merged
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.
What?
This PR adds the direct file access call to the create-block stub files. This impacts users that run
npx @wordpress/create-block my-plugin
Why?
To prevent direct access to the file by default.
(I was also reminded by the plugin team a recent submission of mine was missing it)
How?
Added the following to the
/plugin-name.php
stub filesI know there are many ways to write this code, so I arbitrarily picked one I've seen in a core project (performance plugin)
(edit) Technically I think
defined( 'ABSPATH' ) or exit;
would actually be the fastest given the theNOT
opcode call is not there, but arguably it's less readable code for newer devs.Alternatives in core:
https://github.com/WordPress/gutenberg/blob/trunk/lib/demo.php#L8
https://github.com/WordPress/gutenberg/blob/trunk/lib/demo.php#L8
https://github.com/WordPress/WordPress/blob/master/wp-includes/rss-functions.php#L9
https://github.com/WordPress/WordPress/blob/master/wp-admin/admin-footer.php#L10
Health Check:
https://github.com/WordPress/health-check/blob/trunk/pages/site-status.php#L9
Performance plugin:
https://github.com/WordPress/performance/blob/trunk/modules/images/webp-uploads/can-load.php#L9
Some others I've seen in the wild