From ac19e71a135f5540891424fb7927b2386a43d48f Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Mon, 11 Oct 2021 17:58:03 -0700 Subject: [PATCH] Update logic for checking top-level and child pages Moves check for top level and child checks to its own function so it came be a little more clear what it is doing and why. When the only_child_pages flag is set, we need to treat direct child pages to the parent as top level so they show. And treat grand children pages as child pages so they nest. --- .../block-library/src/page-list/index.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/page-list/index.php b/packages/block-library/src/page-list/index.php index 9a203fe5718aed..3b39e48e19aa8d 100644 --- a/packages/block-library/src/page-list/index.php +++ b/packages/block-library/src/page-list/index.php @@ -226,6 +226,31 @@ function block_core_page_list_nest_pages( $current_level, $children ) { return $current_level; } +/** + * Determines if page should be classified as top-level or child page. Broken + * out to this function to explain instead of a complex if statement. + * When only_child_pages is true, force child pages to be considered top level. + * Thy are top level since the parent is not shown (only child pages). + * + * @param int $page_parent_id Parent id of the page being tested. + * @param boolean $only_child_pages Only showing child pages. + * @param int $parent_id Top level parent id for child pages, needed so we can nest grand children. + * @return boolean True if page should be considered a child page. + */ +function block_core_page_list_treat_as_child( $page_parent_id, $only_child_pages, $parent_id ) { + if ( ! $page_parent_id ) { + return false; + } + + // Check only child pages, and id matches parent, then it is top level and + // should not be treated like a child page. + if ( $only_child_pages && ( $page_parent_id === $parent_id ) ) { + return false; + } + + return true; +} + /** * Renders the `core/page-list` block on server. * @@ -278,9 +303,8 @@ function render_block_core_page_list( $attributes, $content, $block ) { $active_page_ancestor_ids = get_post_ancestors( $page->ID ); } - // Only set pages with children when only child pages is not set, since - // when set we want child pages to be top-level. - if ( $page->post_parent && ! $only_child_pages ) { + // See function for logic when pages are treated like child pages. + if ( block_core_page_list_treat_as_child( $page->post_parent, $only_child_pages, $parent_id ) ) { $pages_with_children[ $page->post_parent ][ $page->ID ] = array( 'page_id' => $page->ID, 'title' => $page->post_title,