-
Notifications
You must be signed in to change notification settings - Fork 17
Added block and content resolver for CoreFootnotes #335
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --- | ||
| "@wpengine/wp-graphql-content-blocks": patch | ||
| --- | ||
|
|
||
| Added content resolver for CoreFootnotes when the post_meta isn't loaded | ||
|
|
||
|
|
||
| ```gql | ||
| fragment CoreFootnotesBlockFragment on CoreFootnotes { | ||
| innerBlocks { | ||
| renderedHtml | ||
| } | ||
| } | ||
|
|
||
| query Post($id: ID!) { | ||
| post(id: $id, idType: DATABASE_ID) { | ||
| databaseId | ||
| editorBlocks { | ||
| ...CoreFootnotesBlockFragment | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ```json | ||
| { | ||
| "data": { | ||
| "post": { | ||
| "databaseId": 16, | ||
| "editorBlocks": [ | ||
| {}, | ||
| { | ||
| "innerBlocks": [ | ||
| { | ||
| "renderedHtml": "<ol class=\"footnotes\"><li id=\"d4051e5e-1547-49ff-ab6d-bec1caa6aabc\"><a href=\"https://wpengine.com/about-us/\">https://wpengine.com/about-us/</a></li><li id=\"2e79de23-68a8-42eb-87ab-1f2467a21752\"><a href=\"https://wpengine.com/support/\">https://wpengine.com/support/</a></li></ol>" | ||
| } | ||
| ] | ||
| }, | ||
| {} | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
| /** | ||
| * Core Paragraph Block | ||
| * | ||
| * @package WPGraphQL\ContentBlocks\Blocks | ||
| */ | ||
|
|
||
| namespace WPGraphQL\ContentBlocks\Blocks; | ||
|
|
||
| /** | ||
| * Class CoreParagraph | ||
| */ | ||
| class CoreFootnotes extends Block { | ||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @var array|null | ||
| */ | ||
| protected ?array $additional_block_attributes = [ | ||
| 'cssClassName' => [ | ||
| 'type' => 'string', | ||
| 'selector' => 'p', | ||
| 'source' => 'attribute', | ||
| 'attribute' => 'class', | ||
| ], | ||
| ]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,7 @@ private static function handle_do_block( array $block ): ?array { | |
| $block = self::populate_post_content_inner_blocks( $block ); | ||
| $block = self::populate_reusable_blocks( $block ); | ||
| $block = self::populate_pattern_inner_blocks( $block ); | ||
| $block = self::populate_core_footnotes_inner_blocks( $block ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But core/footnotes doesn't have inner blocks...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually able to render the footnotes HTML without checking out this branch. This is on the main branch: Do we really need to expose the individual footnote items from this list? I'm not sure.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @theodesp @justlevine I think if we can get the data (as per @theodesp GQL query) then is there any need for this? I don't think we should add any new blocks/logic if we can already get the data? The other thing is footnotes are directly correlated with content so not sure if adding a new block for footnotes is needed on reflection.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree there's no need, and personally don't see the value, especially versus the cost of maintaining this (and any other manual deviation that we cant auto-infer from WP core in our schema generation) Unit tests are always nice to alert us if
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @justlevine I don't see any point either if we can just query the data. I will close this PR out. Thanks for the really good feedback aswell. |
||
|
|
||
| // Prepare innerBlocks. | ||
| if ( ! empty( $block['innerBlocks'] ) ) { | ||
|
|
@@ -292,6 +293,56 @@ private static function populate_pattern_inner_blocks( array $block ): array { | |
| } | ||
|
|
||
| $block['innerBlocks'] = $resolved_patterns; | ||
|
|
||
| return $block; | ||
| } | ||
|
|
||
| /** | ||
| * Populates the innerBlocks for core/footnotes | ||
| * | ||
| * @param array<string,mixed> $block The block to populate. | ||
| * | ||
| * @return array<string,mixed> The populated block. | ||
| */ | ||
| private static function populate_core_footnotes_inner_blocks( array $block ): array { | ||
|
|
||
| if ( 'core/footnotes' !== $block['blockName'] ) { | ||
| return $block; | ||
| } | ||
|
|
||
| $post = get_post(); | ||
| if ( ! is_object( $post ) || ! is_a( $post, \WP_Post::class ) ) { | ||
| return $block; | ||
| } | ||
|
|
||
| $post_meta = get_post_meta( $post->ID, 'footnotes', true ); | ||
| if ( ! $post_meta ) { | ||
| return $block; | ||
| } | ||
|
|
||
| $content = json_decode( $post_meta, true ); | ||
| if ( empty( $content ) ) { | ||
| return $block; | ||
| } | ||
|
|
||
| $html = ''; | ||
| /** @var array{id: string, content: string} $footnote */ | ||
| foreach ( $content as $footnote ) { | ||
| $id = $footnote['id'] ?? null; | ||
| $content = $footnote['content'] ?? null; | ||
| if ( ! $content ) { | ||
| continue; | ||
| } | ||
|
|
||
| $html .= "<li id=\"{$id}\">{$content}</li>"; | ||
| } | ||
|
|
||
| $parsed_blocks = parse_blocks( "<ol class=\"footnotes\">{$html}</ol>" ); | ||
|
|
||
| if ( empty( $parsed_blocks ) ) { | ||
| return $block; | ||
| } | ||
| $block['innerBlocks'] = $parsed_blocks; | ||
| return $block; | ||
| } | ||
|
|
||
|
|
@@ -307,6 +358,7 @@ private static function flatten_block_list( $blocks ): array { | |
| foreach ( $blocks as $block ) { | ||
| $result = array_merge( $result, self::flatten_inner_blocks( $block ) ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.