Skip to content

Commit

Permalink
Templates: Implement persistence using a template CPT.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Jul 8, 2019
1 parent 867e563 commit 3d7df87
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
38 changes: 33 additions & 5 deletions lib/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,39 @@
* Registers Gutenberg core block editor templates.
*/
function gutenberg_register_templates() {
$post_post_type_object = get_post_type_object( 'post' );
$post_post_type_object->template = array(
array( 'core/post-title' ),
array( 'core/post-content', array(), array( array( 'core/paragraph' ) ) ),
register_post_type(
'wp_template',
array(
'labels' => array(
'name' => __( 'Templates', 'gutenberg' ),
),
'show_in_rest' => true,
)
);
$post_post_type_object->template_lock = 'insert';

$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];
}

$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = $template;
}
add_action( 'init', 'gutenberg_register_templates' );
2 changes: 1 addition & 1 deletion packages/blocks/src/api/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function synchronizeBlocksWithTemplate( blocks = [], template ) {
return blocks;
}

return map( template, ( [ name, attributes, innerBlocksTemplate ], index ) => {
return map( template, ( { name, attributes, innerBlocks: innerBlocksTemplate }, index ) => {
const block = blocks[ index ];

if ( block && block.name === name ) {
Expand Down
19 changes: 15 additions & 4 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ export function* setupEditor( post, edits, template ) {
let blocks = parse( content );

// Apply a template for new posts only, if exists.
const isNewPost = post.status === 'auto-draft';
if ( isNewPost && template ) {
blocks = synchronizeBlocksWithTemplate( blocks, template );
if ( template ) {
blocks = synchronizeBlocksWithTemplate( blocks, parse( template.post_content ) );
}

yield resetPost( post );
Expand Down Expand Up @@ -440,11 +439,23 @@ export function* savePost( options = {} ) {
'getCurrentPost'
);

const editedPostContent = yield select(
let editedPostContent = yield select(
STORE_KEY,
'getEditedPostContent'
);

const template = ( yield select( STORE_KEY, 'getEditorSettings' ) ).template;
if ( template ) {
yield apiFetch( {
path: `/wp/v2/${ template.post_type }/${ template.ID }`,
method: 'PUT',
data: {
content: editedPostContent,
},
} );
editedPostContent = '';
}

let toSend = {
...edits,
content: editedPostContent,
Expand Down

0 comments on commit 3d7df87

Please sign in to comment.