-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Editor: Operate on template CPT posts and add a default template with post title and content blocks. #16485
Closed
Closed
Editor: Operate on template CPT posts and add a default template with post title and content blocks. #16485
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc7530f
Block Library: Add new post-title block.
epiqueras 3a5f1b8
Edit Post: Set a default post template that uses the core/post-title …
epiqueras 4b29072
Block Library: Add new post-content block and use it in the default p…
epiqueras 177357b
Templates: Implement persistence using a template CPT.
epiqueras 809186f
Templates: Integrate with demo content.
epiqueras 22f9ce5
Editor: Integrate post-title and post-content with custom sources.
epiqueras 5afab34
Editor: Keep compatibility with current template format.
epiqueras ac44a03
Block Editor: Add block zoom support.
epiqueras 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
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,53 @@ | ||
<?php | ||
/** | ||
* Register Gutenberg core block editor templates. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Registers Gutenberg core block editor templates. | ||
*/ | ||
function gutenberg_register_templates() { | ||
register_post_type( | ||
'wp_template', | ||
array( | ||
'labels' => array( | ||
'name' => __( 'Templates', 'gutenberg' ), | ||
), | ||
'show_in_rest' => true, | ||
) | ||
); | ||
|
||
$template_query = new WP_Query( | ||
array( | ||
'post_type' => 'wp_template', | ||
'name' => 'single-post', | ||
) | ||
); | ||
|
||
$template; | ||
if ( ! $template_query->have_posts() ) { | ||
$template_id = wp_insert_post( | ||
array( | ||
'post_type' => 'wp_template', | ||
'post_name' => 'single-post', | ||
'post_content' => '<!-- wp:post-title /--><!-- wp:post-content --><!-- wp:paragraph --><p></p><!-- /wp:paragraph --><!-- /wp:post-content -->', | ||
) | ||
); | ||
$template = get_post( $template_id ); | ||
} else { | ||
$template = $template_query->get_posts(); | ||
$template = $template[0]; | ||
} | ||
|
||
if ( isset( $_GET['gutenberg-demo'] ) ) { | ||
ob_start(); | ||
include gutenberg_dir_path() . 'post-content.php'; | ||
$template->post_content = ob_get_clean(); | ||
} | ||
|
||
$post_type_object = get_post_type_object( 'post' ); | ||
$post_type_object->template = $template; | ||
} | ||
add_action( 'init', 'gutenberg_register_templates' ); |
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
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,8 @@ | ||
{ | ||
"name": "core/post-content", | ||
"category": "common", | ||
"supports": { | ||
"multiple": false, | ||
"zoom": true | ||
} | ||
} |
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,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
export default function PostContentEdit() { | ||
return <InnerBlocks templateLock={ false } />; | ||
} |
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,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Post Content' ), | ||
edit, | ||
}; |
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,11 @@ | ||
{ | ||
"name": "core/post-title", | ||
"category": "common", | ||
"attributes": { | ||
"title": { | ||
"type": "string", | ||
"source": "post", | ||
"attribute": "title" | ||
} | ||
} | ||
} |
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,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText } from '@wordpress/block-editor'; | ||
|
||
export default function PostTitleEdit( { | ||
attributes: { title }, | ||
setAttributes, | ||
} ) { | ||
return ( | ||
<RichText | ||
value={ title } | ||
onChange={ ( value ) => setAttributes( { title: value } ) } | ||
tagName="h1" | ||
placeholder={ __( 'Title' ) } | ||
formattingControls={ [] } | ||
/> | ||
); | ||
} |
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,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Post Title' ), | ||
edit, | ||
}; |
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
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,6 +1,7 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as post from './post'; | ||
import * as meta from './meta'; | ||
|
||
export { meta }; | ||
export { post, meta }; |
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,46 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get, set } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { select } from '@wordpress/data-controls'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { editPost } from '../actions'; | ||
import { EDIT_MERGE_PROPERTIES } from '../constants'; | ||
|
||
export function* getDependencies() { | ||
return { | ||
post: yield select( 'core/editor', 'getCurrentPost' ), | ||
content: yield select( 'core/editor', 'getEditedPostContent' ), | ||
edits: yield select( 'core/editor', 'getPostEdits' ), | ||
}; | ||
} | ||
|
||
export function apply( { attribute }, { post, content, edits } ) { | ||
if ( 'content' === attribute ) { | ||
return content; | ||
} | ||
|
||
if ( undefined === get( edits, attribute ) ) { | ||
return get( post, attribute ); | ||
} | ||
|
||
if ( EDIT_MERGE_PROPERTIES.has( attribute ) ) { | ||
return { | ||
...get( post, attribute ), | ||
...get( edits, attribute ), | ||
}; | ||
} | ||
|
||
return get( edits, attribute ); | ||
} | ||
|
||
export function* update( { attribute }, value ) { | ||
yield editPost( set( {}, attribute, value ) ); | ||
} |
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.
I might think to call this
property
, particularly if we just consider the post an object from which we're picking property values. Also avoids ambiguity on block attributes.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 went with attributes because all the selectors use "post attributes" as the terminology.
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.
Hmm, that's fair. In retrospect I think they might have been more canonically referred to as properties of the post, but I'm fine targeting consistency if even for consistency's sake.
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.
Yeah I like "properties" more too.