diff --git a/.changeset/eighty-cameras-join.md b/.changeset/eighty-cameras-join.md new file mode 100644 index 00000000..92aaa277 --- /dev/null +++ b/.changeset/eighty-cameras-join.md @@ -0,0 +1,45 @@ +--- +"@wpengine/wp-graphql-content-blocks": minor +--- + +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; }