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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ add_action( 'init', 'myplugin_register_template' );

*Options:*

- `all` — prevents all operations. It is not possible to insert new blocks, move existing blocks, or delete blocks.
- `readonly` — prevents all operations. It is not possible to edit any blocks, insert new blocks, move or delete existing blocks.
- `all` — prevents all block operations. It is not possible to insert new blocks, move or delete existing blocks.
- `insert` — prevents inserting or removing blocks, but allows moving existing blocks.

## Nested Templates
Expand Down
65 changes: 65 additions & 0 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ _Returns_

- `Array`: Records.

<a name="getSiteOptions" href="#getSiteOptions">#</a> **getSiteOptions**

Return site options as they exist locally.

_Related_

- isSiteOptionsDirty

_Parameters_

- _state_ `Object`: Data state.

_Returns_

- `Object`: Site options.

<a name="getThemeSupports" href="#getThemeSupports">#</a> **getThemeSupports**

Return theme supports data in the index.
Expand Down Expand Up @@ -242,6 +258,19 @@ _Returns_

- `boolean`: Whether a request is in progress for an embed preview.

<a name="isSiteOptionsDirty" href="#isSiteOptionsDirty">#</a> **isSiteOptionsDirty**

Return whether the client has local changes to site options which haven't
yet been saved to the server.

_Parameters_

- _state_ `Object`: Data state.

_Returns_

- `boolean`: Whether or not the local site options state is dirty.


<!-- END TOKEN(Autogenerated selectors) -->

Expand Down Expand Up @@ -317,6 +346,18 @@ _Returns_

- `Object`: Action object.

<a name="receiveSiteOptions" href="#receiveSiteOptions">#</a> **receiveSiteOptions**

Returns an action object used in signalling that site options have been received.

_Parameters_

- _siteOptions_ `Object`: Site options.

_Returns_

- `Object`: Action object.

<a name="receiveThemeSupports" href="#receiveThemeSupports">#</a> **receiveThemeSupports**

Returns an action object used in signalling that the index has been received.
Expand Down Expand Up @@ -382,4 +423,28 @@ _Returns_

- `Object`: Updated record.

<a name="saveSiteOptions" href="#saveSiteOptions">#</a> **saveSiteOptions**

Action triggered to save site options.

_Parameters_

- _siteOptions_ `Object`: Site options.

_Returns_

- `Object`: Updated site options.

<a name="updateSiteOptions" href="#updateSiteOptions">#</a> **updateSiteOptions**

Returns an action object used in signalling that site options have been locally updated.

_Parameters_

- _siteOptions_ `Object`: Site options.

_Returns_

- `Object`: Action object.

<!-- END TOKEN(Autogenerated actions) -->
26 changes: 26 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ function gutenberg_reregister_core_block_types() {
'rss.php' => 'core/rss',
'shortcode.php' => 'core/shortcode',
'search.php' => 'core/search',
'site-title.php' => 'core/site-title',
'tag-cloud.php' => 'core/tag-cloud',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-date.php' => 'core/post-date',
'template-part.php' => 'core/template-part',
);

$registry = WP_Block_Type_Registry::get_instance();
Expand All @@ -45,3 +50,24 @@ function gutenberg_reregister_core_block_types() {
}
}
add_action( 'init', 'gutenberg_reregister_core_block_types' );

/**
* Adds new block categories needed by the Gutenberg plugin.
*
* @param array $categories Array of block categories.
*
* @return array Array of block categories plus the new categories added.
*/
function gutenberg_block_categories( $categories ) {
return array_merge(
$categories,
array(
array(
'slug' => 'theme',
'title' => __( 'Theme Blocks' ),
'icon' => null,
),
)
);
}
add_filter( 'block_categories', 'gutenberg_block_categories' );
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
191 changes: 191 additions & 0 deletions lib/templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?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,
'show_ui' => true,
)
);

$template_query = new WP_Query(
array(
'post_type' => 'wp_template',
'name' => 'single-post',
)
);

$template;
if ( ! $template_query->have_posts() ) {
$footer_template_part_id = wp_insert_post(
array(
'post_type' => 'wp_template',
'post_title' => 'Footer',
'post_name' => 'footer-template-part',
'post_content' => "<!-- wp:paragraph -->\n<p>Template part</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:categories /-->",
)
);

$template_id = wp_insert_post(
array(
'post_type' => 'wp_template',
'post_name' => 'single-post',
'post_content' => '<!-- wp:navigation-menu {"align":"wide"} -->
<nav class="wp-block-navigation-menu alignwide"><!-- wp:navigation-menu-item {"label":"Home","destination":"http://yah.local/"} -->
<a href="http://yah.local/" class="wp-block-navigation-menu-item">Home</a>
<!-- /wp:navigation-menu-item -->

<!-- wp:navigation-menu-item {"label":"My best posts","destination":"http://yah.local/best"} -->
<a href="http://yah.local/best" class="wp-block-navigation-menu-item">My best posts</a>
<!-- /wp:navigation-menu-item --></nav>
<!-- /wp:navigation-menu -->

<!-- wp:columns {"align":"wide"} -->
<div class="wp-block-columns alignwide"><!-- wp:column {"width":66.66} -->
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:post-title /-->

<!-- wp:post-date {"format":"datetime"} /-->

<!-- wp:post-content /--></div>
<!-- /wp:column -->

<!-- wp:column {"width":33.34} -->
<div class="wp-block-column" style="flex-basis:33.34%"><!-- wp:group {"backgroundColor":"very-dark-gray"} -->
<div class="wp-block-group has-very-dark-gray-background-color has-background"><div class="wp-block-group__inner-container"><!-- wp:paragraph {"textColor":"very-light-gray"} -->
<p class="has-text-color has-very-light-gray-color">This is a <strong>Sidebar</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"textColor":"very-light-gray"} -->
<p class="has-text-color has-very-light-gray-color">With some block widgets.</p>
<!-- /wp:paragraph -->

<!-- wp:tag-cloud /-->

<!-- wp:paragraph -->
<p><strong>You are now reading post:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:post-title /-->

<!-- wp:spacer {"height":223} -->
<div style="height:223px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer --></div></div>
<!-- /wp:group --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
<!-- wp:template-part {"id":' . $footer_template_part_id . ',"name":"Footer"} /-->',
)
);
$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_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 );

/**
* Returns an array of paths to template parts that should be preloaded, based on what template part blocks exist in the blocks passed.
*
* @param array $blocks Array of block objects.
*
* @return array Array of paths to template parts posts.
*/
function get_template_part_preload_paths( $blocks ) {
if ( empty( $blocks ) ) {
return array();
}
$parts = array();
foreach ( $blocks as $block ) {
if ( 'core/template-part' === $block['blockName'] &&
isset( $block['attrs']['id'] )
) {
$parts[] = sprintf( '/wp/v2/wp_template/%s?context=edit', $block['attrs']['id'] );
}
$parts = array_merge(
$parts,
get_template_part_preload_paths( $block['innerBlocks'] )
);
}
return $parts;
}

/**
* Filter the preload_paths to include paths to template part posts referenced in the current post template.
*
* @param array $preload_paths Array of paths to preload.
*
* @return array Array of paths to preload with template part paths added.
*/
function gutenberg_preload_template_parts( $preload_paths ) {
global $post;
$post_type_object = get_post_type_object( get_post_type( $post ) );
if ( ! empty( $post_type_object->template_post ) ) {
$blocks = parse_blocks( $post_type_object->template_post->post_content );
return array_merge(
$preload_paths,
get_template_part_preload_paths( $blocks )
);
}
return $preload_paths;
}

add_filter( 'block_editor_preload_paths', 'gutenberg_preload_template_parts' );

/**
* 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' );
11 changes: 4 additions & 7 deletions packages/block-editor/src/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ class BlockDropZone extends Component {
}

render() {
const { isLockedAll, index } = this.props;
if ( isLockedAll ) {
return null;
}
const { index } = this.props;
const isAppender = index === undefined;

return (
Expand Down Expand Up @@ -155,10 +152,10 @@ export default compose(
},
};
} ),
withSelect( ( select, { rootClientId } ) => {
const { getClientIdsOfDescendants, getTemplateLock, getBlockIndex } = select( 'core/block-editor' );
withSelect( ( select ) => {
const { getClientIdsOfDescendants, getBlockIndex } = select( 'core/block-editor' );

return {
isLockedAll: getTemplateLock( rootClientId ) === 'all',
getClientIdsOfDescendants,
getBlockIndex,
};
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/block-edit/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { Consumer, Provider } = createContext( {
focusedElement: null,
setFocusedElement: noop,
clientId: null,
isReadOnly: null,
} );

export { Provider as BlockEditContextProvider };
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/block-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class BlockEdit extends Component {
);
}

propsToContext( name, isSelected, clientId, onFocus, onCaretVerticalPositionChange ) {
return { name, isSelected, clientId, onFocus, onCaretVerticalPositionChange };
propsToContext( name, isSelected, clientId, onFocus, onCaretVerticalPositionChange, isReadOnly ) {
return { name, isSelected, clientId, onFocus, onCaretVerticalPositionChange, isReadOnly };
}

render() {
const { name, isSelected, clientId, onFocus, onCaretVerticalPositionChange } = this.props;
const value = this.propsToContext( name, isSelected, clientId, onFocus, onCaretVerticalPositionChange );
const { name, isSelected, clientId, onFocus, onCaretVerticalPositionChange, isReadOnly } = this.props;
const value = this.propsToContext( name, isSelected, clientId, onFocus, onCaretVerticalPositionChange, isReadOnly );

return (
<BlockEditContextProvider value={ value }>
Expand Down
Loading