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

Block Library: Update FSE blocks to use block context #21696

Merged
merged 8 commits into from
May 7, 2020

Conversation

aduth
Copy link
Member

@aduth aduth commented Apr 17, 2020

Closes #21662
Closes #21665

This pull request seeks to update existing full-site editing blocks to use the block context feature implemented in #21467 in place of the existing gutenberg_get_post_from_context for accessing the "current" post. In so doing, the gutenberg_get_post_from_context function has been deprecated.

It does not seek to make any other improvements to these blocks. Existing functionality is retained.

Testing Instructions:

Verify all affected blocks behave as expected:

  • core/post-content
  • core/post-author
  • core/post-comments
  • core/post-comments-count
  • core/post-comments-form
  • core/post-date
  • core/post-excerpt
  • core/post-featured-image
  • core/post-tags
  • core/site-title
  • core/template-part

This requires to create a template using the Full Site Editing feature, enabled under Gutenberg > Experiments. The easiest way to do this is to create a new template single, add each of these as a block somewhere in the template, then navigate to the preview of an existing post on the site.

Ideally there are automated tests for these blocks. I will create a separate tracking task for this.

@github-actions
Copy link

github-actions bot commented Apr 17, 2020

Size Change: +8 B (0%)

Total Size: 822 kB

Filename Size Change
build/block-library/index.js 115 kB +8 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 4.08 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.6 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/index.js 101 kB 0 B
build/block-editor/style-rtl.css 10.2 kB 0 B
build/block-editor/style.css 10.2 kB 0 B
build/block-library/editor-rtl.css 7.08 kB 0 B
build/block-library/editor.css 7.08 kB 0 B
build/block-library/style-rtl.css 7.28 kB 0 B
build/block-library/style.css 7.29 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 179 kB 0 B
build/components/style-rtl.css 16.9 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.44 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/index.js 4.07 kB 0 B
build/edit-navigation/style-rtl.css 485 B 0 B
build/edit-navigation/style.css 485 B 0 B
build/edit-post/index.js 28.1 kB 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 12.3 kB 0 B
build/edit-site/style-rtl.css 5.19 kB 0 B
build/edit-site/style.css 5.2 kB 0 B
build/edit-widgets/index.js 8.37 kB 0 B
build/edit-widgets/style-rtl.css 4.68 kB 0 B
build/edit-widgets/style.css 4.68 kB 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/index.js 44.3 kB 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 734 B 0 B
build/format-library/index.js 7.63 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.13 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.67 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@epiqueras
Copy link
Contributor

Nice work!

We should also update the client-side implementations in this PR.

@aduth
Copy link
Member Author

aduth commented May 5, 2020

I'm currently working to rebase this after having merged #21925, and I'm encountering an issue I expect likely had existed previously but I failed to notice.

Basically, the site errors with a memory exhausted error when trying to load the Site Editor page.

I have a feeling it's related to the fact that we're not longer "taking advantage of" this fix:

gutenberg/lib/compat.php

Lines 167 to 171 in 2beb598

// TODO: Without this temporary fix, an infinite loop can occur where
// posts with post content blocks render themselves recursively.
if ( is_admin() || defined( 'REST_REQUEST' ) ) {
return null;
}

I guess we need something more permanent here...

I still need to dig in more. Curious if you have any thoughts @epiqueras.

My first intuition is:

  • Why is the parse happening in the first place? I guess probably related to REST API: Omit content.rendered from preloaded post data #18988 (at-load parsing resulting from content.rendered preloading)
  • Should the template be setting (or unsetting) a different post context value here? Is that the responsibility of: The template? The content block? The framework in managing recursive rendering of content?

@epiqueras
Copy link
Contributor

The template should not be setting its ID and post type into context.

@aduth
Copy link
Member Author

aduth commented May 5, 2020

The template should not be setting its ID and post type into context.

As I understand it, this is the current behavior, but it's also not unsetting it either, so when it's attempting to render a Post Content block within the template, the block inherits the ID of the template, which in turn renders the template, which in turn renders the Post Content block within the template, inherits the same template ID, ... (ad infinitum).

@epiqueras
Copy link
Contributor

That's why the top-level post ID and post type should not be set when rendering templates or template parts.

I.e., in gutenberg_render_block_with_assigned_block_context.

@aduth aduth force-pushed the update/21662-deprecate-get-post-from-context branch from 16c8b30 to 8193c87 Compare May 6, 2020 13:53
@aduth aduth requested review from nerrad and ntwb as code owners May 6, 2020 13:53
@aduth
Copy link
Member Author

aduth commented May 6, 2020

That's why the top-level post ID and post type should not be set when rendering templates or template parts.

Yeah, it makes sense. It took me some time to think it through, but since the template is meant to describe a block arrangement for any post which matches that template, it makes sense that we should want to avoid assigning post values into block context until the point at which that template rendering occurs.

I was reluctant to put a hard-coded condition here in render_block, and tried a few iterations. One of the other ideas I had was to avoid assigning any default context at all, and to only initialize the post context at the point at which it's assumed the post content is being rendered (the_content, gutenberg_render_the_template). Generally speaking, it is an option to consider, but it doesn't really solve this specific issue, since "post content is being rendered" is exactly what's happening in the scenario described in #21696 (comment).

I think where I landed with a862753 and 9a837f3 is a happy medium: The post context is there by default, but the default context can be filtered. The code (file) relevant for template rendering hooks this filter to unset the post context when a template post is being rendered.

@aduth
Copy link
Member Author

aduth commented May 6, 2020

In the course of refactoring these blocks, it's been interesting to note there have been several occurrences where we previously unknowingly depended on the $post global. It becomes more obviously an issue once refactoring to use block context. I fixed a few here, though there may be additional ones we've yet to encounter.

Examples:

@aduth
Copy link
Member Author

aduth commented May 6, 2020

We should also update the client-side implementations in this PR.

Given the additional challenges which have arisen here already out of scope (supporting filterable context, unsetting post context for templates, restoring E2E tests, fixing blocks depending on $post global), I'm inclined to not let this grow any further, unless it becomes an issue that the client-side implementations must be updated as a result of these changes.

Personally, I'd rather do a quick follow-on after this has landed.

@epiqueras
Copy link
Contributor

Sounds good.

lib/template-loader.php Outdated Show resolved Hide resolved
lib/template-loader.php Outdated Show resolved Hide resolved
aduth added 5 commits May 6, 2020 16:08
Presumably the previous implementation relied on the fact that calling gutenberg_get_post_from_context would reset post global, referenced internally in get_the_content implicitly by call to get_post
With testing of availability of default post contxt
@aduth aduth force-pushed the update/21662-deprecate-get-post-from-context branch from 74cc973 to 63f66c3 Compare May 6, 2020 20:31
@epiqueras
Copy link
Contributor

LGTM

@aduth aduth merged commit 94ad701 into master May 7, 2020
@aduth aduth deleted the update/21662-deprecate-get-post-from-context branch May 7, 2020 13:05
@github-actions github-actions bot added this to the Gutenberg 8.1 milestone May 7, 2020
@ellatrix ellatrix mentioned this pull request Jun 16, 2020
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block library /packages/block-library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Block Library: FSE Blocks: Use block context Framework: Deprecate gutenberg_get_post_from_context
3 participants