diff --git a/lib/templates.php b/lib/templates.php index b459ca758ccefc..5101c0a91d2966 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -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' => '
', + ) + ); + $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' ); diff --git a/packages/blocks/src/api/templates.js b/packages/blocks/src/api/templates.js index d09be49453e702..8bde810549d0d7 100644 --- a/packages/blocks/src/api/templates.js +++ b/packages/blocks/src/api/templates.js @@ -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 ) { diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 2360bfb4b20b6d..37954d8c2dcd9a 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -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 ); @@ -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,