Skip to content

Commit

Permalink
Fix recursion for bp_docs_find_closest_ancestor_of_excerpt().
Browse files Browse the repository at this point in the history
See #728.
  • Loading branch information
boonebgorges committed Dec 20, 2023
1 parent 85b8851 commit 3562224
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions includes/theme-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,17 +425,24 @@ function bp_docs_provide_block_template_for_docs_directory( $templates, $query )
*/
function bp_docs_find_closest_ancestor_of_excerpt( $blocks ) {
foreach ( $blocks as $block ) {
if ( ! empty( $block['innerBlocks'] ) ) {
$ancestor = bp_docs_find_closest_ancestor_of_excerpt( $block['innerBlocks'] );
if ( null !== $ancestor ) {
return $ancestor;
}
}

$rendered = render_block( $block );

if ( false !== strpos( $rendered, 'wp-block-post-excerpt' ) ) {
return $block;
}
// If the block contains 'post-excerpt' and has no inner blocks,
// it is the closest ancestor.
if ( empty( $block['innerBlocks'] ) ) {
return $block;
}

// If the block has inner blocks, recursively search them.
$innerAncestor = bp_docs_find_closest_ancestor_of_excerpt( $block['innerBlocks'] );
if ( null !== $innerAncestor ) {
return $innerAncestor;
}

// If no inner block is a valid ancestor, return the current block.
return $block;
}
}

return null;
Expand Down

0 comments on commit 3562224

Please sign in to comment.