Repository navigation
feat: apply post type templates to new posts #794
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
Merged
dcalhoun
merged 8 commits into
fix/seed-post-entity-resolution
from
feat/new-post-template-parity
Oct 30, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88387df
feat: seed new posts as auto-drafts
dcalhoun da4fd9d
feat: apply the post type template to empty new posts
dcalhoun 5c6625c
task: clarify the templated test post type
dcalhoun e977ba9
fix: seed only empty new posts as auto-drafts
dcalhoun 147823b
fix: keep an applied template from counting as a host change
dcalhoun da8e5e8
perf: wait for the post type before rendering only new posts
dcalhoun 91d5493
feat: lock post type templates as WP Admin does
dcalhoun a4f76ca
docs: clarify which posts get the post type template
dcalhoun 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 hidden or 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 hidden or 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,118 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
| import EditorPage from './editor-page'; | ||
|
|
||
| /** A new post of the type `wp-env` registers with a block template. */ | ||
| const NEW_TEMPLATED_POST = { | ||
| id: -1, | ||
| type: 'templated_post', | ||
| restBase: 'templated-posts', | ||
| restNamespace: 'wp/v2', | ||
| status: 'draft', | ||
| title: '', | ||
| content: '', | ||
| }; | ||
|
|
||
| test.describe( 'New post template', () => { | ||
| test( 'applies the post type template to an empty new post', async ( { | ||
| page, | ||
| } ) => { | ||
| const editor = new EditorPage( page ); | ||
| // An empty preload makes the editor request the post types, as hosts | ||
| // preload the site's real ones. | ||
| await editor.setup( { post: NEW_TEMPLATED_POST, preloadData: {} } ); | ||
|
|
||
| const blocks = await editor.getBlocks(); | ||
| expect( blocks.map( ( { name } ) => name ) ).toEqual( [ | ||
| 'core/heading', | ||
| 'core/paragraph', | ||
| ] ); | ||
| expect( blocks[ 0 ].attributes.content ).toBe( | ||
| 'Prefilled heading from the template' | ||
| ); | ||
| expect( blocks[ 1 ].attributes.placeholder ).toBe( | ||
| 'Placeholder paragraph from the template' | ||
| ); | ||
|
|
||
| const state = await page.evaluate( () => { | ||
| const editorSelect = window.wp.data.select( 'core/editor' ); | ||
| return { | ||
| isNew: editorSelect.isEditedPostNew(), | ||
| status: editorSelect.getEditedPostAttribute( 'status' ), | ||
| }; | ||
| } ); | ||
| expect( state ).toEqual( { isNew: true, status: 'auto-draft' } ); | ||
| } ); | ||
|
|
||
| test( 'does not report an untouched template as a change', async ( { | ||
| page, | ||
| } ) => { | ||
| const editor = new EditorPage( page ); | ||
| await editor.setup( { post: NEW_TEMPLATED_POST, preloadData: {} } ); | ||
|
|
||
| const { content, changed } = await page.evaluate( () => | ||
| window.editor.getTitleAndContent() | ||
| ); | ||
| expect( content ).toContain( 'Prefilled heading from the template' ); | ||
| expect( changed ).toBe( false ); | ||
| } ); | ||
|
|
||
| test( "locks the post type's template", async ( { page } ) => { | ||
| await page.route( | ||
| /\/wp\/v2\/types\/templated_post\b/, | ||
| async ( route ) => { | ||
| const response = await route.fetch(); | ||
| const body = await response.json(); | ||
| await route.fulfill( { | ||
| response, | ||
| json: { ...body, template_lock: 'all' }, | ||
| } ); | ||
| } | ||
| ); | ||
|
|
||
| const editor = new EditorPage( page ); | ||
| await editor.setup( { post: NEW_TEMPLATED_POST, preloadData: {} } ); | ||
|
|
||
| await expect | ||
| .poll( () => | ||
| page.evaluate( () => | ||
| window.wp.data | ||
| .select( 'core/block-editor' ) | ||
| .getTemplateLock() | ||
| ) | ||
| ) | ||
| .toBe( 'all' ); | ||
| } ); | ||
|
|
||
| test( 'keeps the content of a new post that has some', async ( { | ||
| page, | ||
| } ) => { | ||
| const editor = new EditorPage( page ); | ||
| await editor.setup( { | ||
| post: { | ||
| ...NEW_TEMPLATED_POST, | ||
| content: | ||
| '<!-- wp:paragraph -->\n<p>Local draft</p>\n<!-- /wp:paragraph -->', | ||
| }, | ||
| preloadData: {}, | ||
| } ); | ||
|
|
||
| const blocks = await editor.getBlocks(); | ||
| expect( blocks.map( ( { name } ) => name ) ).toEqual( [ | ||
| 'core/paragraph', | ||
| ] ); | ||
| await expect( page.getByText( 'Local draft' ) ).toBeVisible(); | ||
| } ); | ||
|
|
||
| test( 'keeps the title of a new post that has one', async ( { page } ) => { | ||
| const editor = new EditorPage( page ); | ||
| await editor.setup( { | ||
| post: { ...NEW_TEMPLATED_POST, title: 'Trip notes' }, | ||
| preloadData: {}, | ||
| } ); | ||
|
|
||
| await expect( | ||
| page.getByRole( 'textbox', { name: 'Add title' } ) | ||
| ).toHaveText( 'Trip notes' ); | ||
| expect( await editor.getBlocks() ).toEqual( [] ); | ||
| } ); | ||
| } ); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.