Skip to content
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. #16565

Closed
wants to merge 70 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
3e54df4
Block Library: Add new post-title block.
epiqueras Jul 2, 2019
8265a39
Edit Post: Set a default post template that uses the core/post-title …
epiqueras Jul 4, 2019
f1a7d9e
Block Library: Add new post-content block and use it in the default p…
epiqueras Jul 4, 2019
e2f298b
Templates: Implement persistence using a template CPT.
epiqueras Jul 5, 2019
301d578
Templates: Integrate with demo content.
epiqueras Jul 8, 2019
657f4e9
Editor: Integrate post-title and post-content with custom sources.
epiqueras Jul 8, 2019
b7dc9a5
Editor: Keep compatibility with current template format.
epiqueras Jul 11, 2019
f2abe6f
Editor: If a template is supplied, apply it if it's editable or if it…
epiqueras Jul 12, 2019
07392e0
Post Title Block: Sync value with post slug.
epiqueras Jul 12, 2019
b09a54d
Editor: Make post templates a separate feature from block templates a…
epiqueras Jul 16, 2019
6136f29
Templates: Add render callbacks for post title and content blocks and…
epiqueras Jul 18, 2019
26e6731
Adding a post date block
youknowriad Jul 19, 2019
efde081
Editor: Fix template post setup.
epiqueras Jul 19, 2019
ce46501
Add: Post content does not show theme blocks (#16676)
jorgefilipecosta Jul 19, 2019
f15e621
Fix date php code.
mtias Jul 19, 2019
4973188
Enable navigation menu in the inserter. Make menu block part of the t…
jorgefilipecosta Jul 19, 2019
6a71161
Update initial template to a more advanced design that includes navig…
jorgefilipecosta Jul 19, 2019
3c00dcc
Add Site Title block and support for site options attribute source
mcsf Jul 19, 2019
3f898cd
Full Site Editing: Add view editing modes picker (#16680)
gziolo Jul 19, 2019
01ab3a1
Editor: Change save copy for new editing modes.
epiqueras Jul 20, 2019
0739ef2
Try template part
jorgefilipecosta Jul 20, 2019
8fbda62
Move view mode to the center.
mtias Jul 20, 2019
58094ad
Remove nested appenders
youknowriad Jul 20, 2019
7188528
Add all modes.
mtias Jul 20, 2019
dd5b8fa
Site Title: Add color support
mcsf Jul 20, 2019
3a1e8de
Add icons to new blocks.
mtias Jul 20, 2019
3027216
Templates: Show admin UI.
epiqueras Jul 20, 2019
0f6be5c
Try adding the new post block
youknowriad Jul 20, 2019
dd1b149
Add template part saving
jorgefilipecosta Jul 20, 2019
28f237e
Add icons for new blocks.
mtias Jul 20, 2019
4d7c8ae
Refactor mode editing
youknowriad Jul 20, 2019
a47cb23
COVER: Allow all blocks in inner blocks.
mtias Jul 20, 2019
5cbb4a7
Editor: Support nested post content blocks.
epiqueras Jul 20, 2019
a7960d3
Disable template blocks in preview mode
gziolo Jul 20, 2019
f91b41b
Disable movers for post content block as well when in preview mode
gziolo Jul 20, 2019
5ef619f
Add template part name
jorgefilipecosta Jul 20, 2019
96a7bff
Add new focus mode for the editor
gziolo Jul 20, 2019
7d92e52
Update site title font size.
mtias Jul 20, 2019
0085e57
Design mode: render as faded-out boxes
mcsf Jul 20, 2019
9f4bbbd
Add logic to update template part ids saved on the template during th…
jorgefilipecosta Jul 20, 2019
40afa03
Editor: Add template part view editing modes.
epiqueras Jul 20, 2019
df26883
Use saveEntityRecord to save template parts
jorgefilipecosta Jul 20, 2019
2613a46
Better template part mode.
epiqueras Jul 20, 2019
39aeaf8
Fix template part saving.
epiqueras Jul 20, 2019
ce01735
Fix show post title.
epiqueras Jul 20, 2019
74edaa5
Fixed templatePart hasInnerBlocks definition
jorgefilipecosta Jul 20, 2019
7c6297d
Add emoji to modes.
mtias Jul 20, 2019
d89dd64
More design mode CSS.
mtias Jul 20, 2019
e0f8fcf
Design mode: add mock columns gradient
mcsf Jul 20, 2019
d14fab6
Constrain frame editor of template parts.
mtias Jul 20, 2019
018924f
Add template part preloading mechanism
jorgefilipecosta Jul 20, 2019
1a77aa0
Site Title: Ascribe `theme` category
mcsf Jul 20, 2019
19adb5c
Template Locking: readonly / disable editing attributes (without rewr…
ellatrix Jul 22, 2019
6bc9cf8
Don't select template part on load
jorgefilipecosta Jul 22, 2019
edd9eeb
Enable readonly post content
ellatrix Jul 22, 2019
1aff7ae
Avoid race conditions when running multiple animations in a row
youknowriad Jul 22, 2019
7502c8b
Revert "Enable readonly post content"
ellatrix Jul 22, 2019
edbd90f
Extend template mode lock to title and date blocks
gziolo Jul 22, 2019
d2c2530
Add locking for post blocks when in template mode
gziolo Jul 22, 2019
44838bb
Fix animation retriggered on each change
youknowriad Jul 22, 2019
8cfb1e4
Design mode: draw grid lines (hardcoded for gutenberg-theme)
mcsf Jul 22, 2019
dedc11c
Cache edits across modes.
epiqueras Jul 22, 2019
6c0879b
Implement template locking
ellatrix Jul 22, 2019
fbb9dc8
Fix demo template.
epiqueras Jul 22, 2019
2e60962
Design mode: correct and widen grid lines
mcsf Jul 22, 2019
d3e6561
Design mode: invert colours; correct width
mcsf Jul 22, 2019
941fe2b
Design mode: Background colour for lateral spaces
mcsf Jul 22, 2019
36e28f9
Fix a bunch of locking stuff
ellatrix Jul 22, 2019
e0f5ad4
Add template part display name logic
jorgefilipecosta Jul 22, 2019
d4f4840
Locking: fix problem with movers
ellatrix Jul 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function gutenberg_reregister_core_block_types() {
'shortcode.php' => 'core/shortcode',
'search.php' => 'core/search',
'tag-cloud.php' => 'core/tag-cloud',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
);

$registry = WP_Block_Type_Registry::get_instance();
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

require dirname( __FILE__ ) . '/compat.php';
require dirname( __FILE__ ) . '/blocks.php';
require dirname( __FILE__ ) . '/templates.php';
require dirname( __FILE__ ) . '/client-assets.php';
require dirname( __FILE__ ) . '/demo.php';
require dirname( __FILE__ ) . '/widgets.php';
Expand Down
98 changes: 98 additions & 0 deletions lib/templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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();
echo '<!-- wp:post-title /--><!-- wp:post-content -->';
include gutenberg_dir_path() . 'post-content.php';
echo '<!-- /wp:post-content -->';
$template->post_content = ob_get_clean();
}

$post_type_object = get_post_type_object( 'post' );
$post_type_object->template_post = $template;
}
add_action( 'init', 'gutenberg_register_templates' );

/**
* Filters the block editor settings object to add the post's template post.
*
* @param array $editor_settings The block editor settings object.
* @param \WP_Post $post The post object.
*
* @return array The maybe modified block editor settings object.
*/
function gutenberg_filter_block_editor_settings( $editor_settings, $post ) {
$post_type_object = get_post_type_object( get_post_type( $post ) );
if ( ! empty( $post_type_object->template_post ) ) {
$editor_settings['templatePost'] = $post_type_object->template_post;
}
return $editor_settings;
}
add_filter( 'block_editor_settings', 'gutenberg_filter_block_editor_settings', 10, 2 );

/**
* Filters template inclusion in pages to hijack the `single.php` template
* and load the Gutenberg editable counterpart instead.
*
* @param string $template The included template file name.
*
* @return string The passed in file name unless the process is hijacked.
*/
function gutenberg_filter_template_include( $template ) {
if ( ! preg_match( '/single\.php$/', $template ) ) {
return $template;
}

$template_query = new WP_Query(
array(
'post_type' => 'wp_template',
'name' => 'single-post',
)
);
$template = $template_query->get_posts();
$template = $template[0];
echo wp_head() . apply_filters( 'the_content', $template->post_content ) . wp_footer();
exit;
}
add_filter( 'template_include', 'gutenberg_filter_template_include' );
8 changes: 8 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ import * as tagCloud from './tag-cloud';

import * as classic from './classic';

// Top-level template blocks.
import * as postTitle from './post-title';
import * as postContent from './post-content';

/**
* Function to register core blocks provided by the block editor.
*
Expand Down Expand Up @@ -124,6 +128,10 @@ export const registerCoreBlocks = () => {
textColumns,
verse,
video,

// Register top-level template blocks.
postTitle,
postContent,
].forEach( ( block ) => {
if ( ! block ) {
return;
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "core/post-content",
"category": "common",
"supports": {
"multiple": false
}
}
8 changes: 8 additions & 0 deletions packages/block-library/src/post-content/edit.js
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 } />;
}
19 changes: 19 additions & 0 deletions packages/block-library/src/post-content/index.js
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,
};
30 changes: 30 additions & 0 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Server-side rendering of the `core/post-content` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-content` block on the server.
*
* @return string Returns the filtered post content of the current post.
*/
function render_block_core_post_content() {
rewind_posts();
the_post();
return apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', get_the_content() ) );
}

/**
* Registers the `core/post-content` block on the server.
*/
function register_block_core_post_content() {
register_block_type(
'core/post-content',
array(
'render_callback' => 'render_block_core_post_content',
)
);
}
add_action( 'init', 'register_block_core_post_content' );
16 changes: 16 additions & 0 deletions packages/block-library/src/post-title/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "core/post-title",
"category": "common",
"attributes": {
"title": {
"type": "string",
"source": "post",
"attribute": "title"
},
"slug": {
"type": "string",
"source": "post",
"attribute": "slug"
}
}
}
21 changes: 21 additions & 0 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { cleanForSlug } from '@wordpress/editor';
import { RichText } from '@wordpress/block-editor';

export default function PostTitleEdit( {
attributes: { title },
setAttributes,
} ) {
return (
<RichText
value={ title }
onChange={ ( value ) => setAttributes( { title: value, slug: cleanForSlug( value ) } ) }
tagName="h1"
placeholder={ __( 'Title' ) }
formattingControls={ [] }
/>
);
}
19 changes: 19 additions & 0 deletions packages/block-library/src/post-title/index.js
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,
};
30 changes: 30 additions & 0 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Server-side rendering of the `core/post-title` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-title` block on the server.
*
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
*/
function render_block_core_post_title() {
rewind_posts();
the_post();
return the_title( '<h1>', '</h1>', false );
}

/**
* Registers the `core/post-title` block on the server.
*/
function register_block_core_post_title() {
register_block_type(
'core/post-title',
array(
'render_callback' => 'render_block_core_post_title',
)
);
}
add_action( 'init', 'register_block_core_post_title' );
18 changes: 14 additions & 4 deletions packages/blocks/src/api/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ export function synchronizeBlocksWithTemplate( blocks = [], template ) {
return blocks;
}

return map( template, ( [ name, attributes, innerBlocksTemplate ], index ) => {
return map( template, ( templateBlock, index ) => {
const [ name, attributes, _innerBlocksTemplate ] = Array.isArray( templateBlock ) ?
templateBlock :
[ templateBlock.name, templateBlock.attributes, templateBlock.innerBlocks ];
const block = blocks[ index ];
let innerBlocksTemplate = _innerBlocksTemplate;

if ( block && block.name === name ) {
const innerBlocks = synchronizeBlocksWithTemplate( block.innerBlocks, innerBlocksTemplate );
return { ...block, innerBlocks };
if ( block ) {
if ( block.name === name ) {
const innerBlocks = synchronizeBlocksWithTemplate( block.innerBlocks, innerBlocksTemplate );
return { ...block, innerBlocks };
}

if ( 'core/post-content' === name ) {
innerBlocksTemplate = blocks;
}
}

// To support old templates that were using the "children" format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TableOfContents,
EditorHistoryRedo,
EditorHistoryUndo,
ToggleEditTemplatePost,
} from '@wordpress/editor';

function HeaderToolbar( { hasFixedToolbar, isLargeViewport, showInserter, isTextModeEnabled } ) {
Expand Down Expand Up @@ -45,6 +46,7 @@ function HeaderToolbar( { hasFixedToolbar, isLargeViewport, showInserter, isText
<BlockToolbar />
</div>
) }
<ToggleEditTemplatePost />
</NavigableToolbar>
);
}
Expand Down
29 changes: 18 additions & 11 deletions packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function Layout( {
isSaving,
isMobileViewport,
isRichEditingEnabled,
isEditingTemplatePost,
} ) {
const sidebarIsOpened = editorSidebarOpened || pluginSidebarOpened || publishSidebarOpened;

Expand Down Expand Up @@ -91,7 +92,9 @@ function Layout( {
<ManageBlocksModal />
<OptionsModal />
{ ( mode === 'text' || ! isRichEditingEnabled ) && <TextEditor /> }
{ isRichEditingEnabled && mode === 'visual' && <VisualEditor /> }
{ isRichEditingEnabled && mode === 'visual' && (
<VisualEditor withPostTitle={ ! isEditingTemplatePost } />
) }
<div className="edit-post-layout__metaboxes">
<MetaBoxes location="normal" />
</div>
Expand Down Expand Up @@ -135,16 +138,20 @@ function Layout( {
}

export default compose(
withSelect( ( select ) => ( {
mode: select( 'core/edit-post' ).getEditorMode(),
editorSidebarOpened: select( 'core/edit-post' ).isEditorSidebarOpened(),
pluginSidebarOpened: select( 'core/edit-post' ).isPluginSidebarOpened(),
publishSidebarOpened: select( 'core/edit-post' ).isPublishSidebarOpened(),
hasFixedToolbar: select( 'core/edit-post' ).isFeatureActive( 'fixedToolbar' ),
hasActiveMetaboxes: select( 'core/edit-post' ).hasMetaBoxes(),
isSaving: select( 'core/edit-post' ).isSavingMetaBoxes(),
isRichEditingEnabled: select( 'core/editor' ).getEditorSettings().richEditingEnabled,
} ) ),
withSelect( ( select ) => {
const editorSettings = select( 'core/editor' ).getEditorSettings();
return {
mode: select( 'core/edit-post' ).getEditorMode(),
editorSidebarOpened: select( 'core/edit-post' ).isEditorSidebarOpened(),
pluginSidebarOpened: select( 'core/edit-post' ).isPluginSidebarOpened(),
publishSidebarOpened: select( 'core/edit-post' ).isPublishSidebarOpened(),
hasFixedToolbar: select( 'core/edit-post' ).isFeatureActive( 'fixedToolbar' ),
hasActiveMetaboxes: select( 'core/edit-post' ).hasMetaBoxes(),
isSaving: select( 'core/edit-post' ).isSavingMetaBoxes(),
isRichEditingEnabled: editorSettings.richEditingEnabled,
isEditingTemplatePost: editorSettings.editTemplatePost,
};
} ),
withDispatch( ( dispatch ) => {
const { closePublishSidebar, togglePublishSidebar } = dispatch( 'core/edit-post' );
return {
Expand Down
Loading