From 6ae09aa289546814ed7a2d13ffd3d0d81cf05ede Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Mon, 20 Jan 2025 18:37:39 +0000 Subject: [PATCH 1/2] Added block and content resolver for CoreFootnotes Added content block resolver for getting post meta for footnotes. --- .changeset/eighty-cameras-join.md | 45 +++++++++++++++++++++ includes/Blocks/CoreFootnotes.php | 27 +++++++++++++ includes/Data/ContentBlocksResolver.php | 52 +++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 .changeset/eighty-cameras-join.md create mode 100644 includes/Blocks/CoreFootnotes.php diff --git a/.changeset/eighty-cameras-join.md b/.changeset/eighty-cameras-join.md new file mode 100644 index 00000000..a824083a --- /dev/null +++ b/.changeset/eighty-cameras-join.md @@ -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": "
  1. https://wpengine.com/about-us/
  2. https://wpengine.com/support/
" + } + ] + }, + {} + ] + } + } +} +``` \ No newline at end of file diff --git a/includes/Blocks/CoreFootnotes.php b/includes/Blocks/CoreFootnotes.php new file mode 100644 index 00000000..2154d56b --- /dev/null +++ b/includes/Blocks/CoreFootnotes.php @@ -0,0 +1,27 @@ + [ + 'type' => 'string', + 'selector' => 'p', + 'source' => 'attribute', + 'attribute' => 'class', + ], + ]; +} diff --git a/includes/Data/ContentBlocksResolver.php b/includes/Data/ContentBlocksResolver.php index e4c589ad..6ebfe43d 100644 --- a/includes/Data/ContentBlocksResolver.php +++ b/includes/Data/ContentBlocksResolver.php @@ -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 ); // 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 $block The block to populate. + * + * @return array 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 .= "
  • {$content}
  • "; + } + + $parsed_blocks = parse_blocks( "
      {$html}
    " ); + + 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; } From d1ce72c690ab765bdf2206e9c64112ddd8d5f09c Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Tue, 21 Jan 2025 15:40:56 +0000 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Dovid Levine --- .changeset/eighty-cameras-join.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/eighty-cameras-join.md b/.changeset/eighty-cameras-join.md index a824083a..92aaa277 100644 --- a/.changeset/eighty-cameras-join.md +++ b/.changeset/eighty-cameras-join.md @@ -1,5 +1,5 @@ --- -"@wpengine/wp-graphql-content-blocks": patch +"@wpengine/wp-graphql-content-blocks": minor --- Added content resolver for CoreFootnotes when the post_meta isn't loaded