Pattern CPT: Add basic content validation when saving a pattern #38
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.
See #22 — this PR addresses some easily-automated validation of the pattern content. I'm validating at the API level, since someone could theoretically handcraft an API request to
/wp-json/wp/v2/wporg-pattern/
.✅ The REST API's default validation will prevent any invalid JSON content.
✅ Initial validation checks that there is content, and that the content is parsed as valid blocks.
✅ Content will pass the validation if there is at least one block with non-default content.
These should be stripped out by
wp_kses
for users, but we'll want to check that when we figure out permissions in general, #30I'm not sure if we need to check for this - on the frontend, any content generated by the editor should be valid. But in PHP, GB's parser is very forgiving. So "invalid" code, ex:
<pThis is content
, passes as a valid paragraph via the API. When that's rendered in the frontend, it triggers the invalid content warning, so anyone looking at the block would know it's broken. Checking for this would probably require something like the block directory e2e tests.It will only pass blocks that are currently registered, so we could unregister blocks we don't want people to use (see #35).
To test
yarn test:php
or, to manually test (in wp-admin, there's no error handling on the frontend yet)
/wp-admin/post-new.php?post_type=wporg-pattern