From c3076b2fccdcec55d4754343d0e57a56da389a7e Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Tue, 4 May 2021 10:16:10 -0700 Subject: [PATCH] Allow Page List to only show children - Add a childrenOnly attribute to Edit - Update render to use attribute and only pull in children Fixes #31063 ?? This uses global $post which is probably not ideal --- .../block-library/src/page-list/block.json | 5 ++++ packages/block-library/src/page-list/edit.js | 21 ++++++++++++++-- .../block-library/src/page-list/index.php | 24 ++++++++++++++----- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/page-list/block.json b/packages/block-library/src/page-list/block.json index a16a0b0abeb0f..14e8b9ee379f7 100644 --- a/packages/block-library/src/page-list/block.json +++ b/packages/block-library/src/page-list/block.json @@ -56,6 +56,11 @@ "style", "openSubmenusOnClick" ], + "attributes": { + "childrenOnly": { + "type": "boolean" + } + }, "supports": { "reusable": false, "html": false diff --git a/packages/block-library/src/page-list/edit.js b/packages/block-library/src/page-list/edit.js index 2311308c41b16..9c5ca90817c0f 100644 --- a/packages/block-library/src/page-list/edit.js +++ b/packages/block-library/src/page-list/edit.js @@ -11,9 +11,10 @@ import { useBlockProps, store as blockEditorStore, getColorClassName, + InspectorControls, } from '@wordpress/block-editor'; import ServerSideRender from '@wordpress/server-side-render'; -import { ToolbarButton } from '@wordpress/components'; +import { PanelBody, ToggleControl, ToolbarButton } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { useEffect, useState } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; @@ -69,7 +70,8 @@ export default function PageListEdit( { context.customOverlayBackgroundColor, ] ); - const { textColor, backgroundColor, style } = context || {}; + const { textColor, backgroundColor, showSubmenuIcon, style } = + context || {}; const [ allowConvertToLinks, setAllowConvertToLinks ] = useState( false ); @@ -135,8 +137,23 @@ export default function PageListEdit( { showSubmenuIcon: !! context.showSubmenuIcon, }; + const { childrenOnly } = attributes; + return ( <> + + + { + setAttributes( { + childrenOnly: ! childrenOnly, + } ); + } } + /> + + { allowConvertToLinks && ( diff --git a/packages/block-library/src/page-list/index.php b/packages/block-library/src/page-list/index.php index 6f99ab14c95ef..2238710c63483 100644 --- a/packages/block-library/src/page-list/index.php +++ b/packages/block-library/src/page-list/index.php @@ -235,21 +235,30 @@ function block_core_page_list_nest_pages( $current_level, $children ) { * @return string Returns the page list markup. */ function render_block_core_page_list( $attributes, $content, $block ) { + global $post; // ?? static $block_id = 0; $block_id++; + + $childrenOnly = ( isset( $attributes['childrenOnly'] ) && $attributes['childrenOnly'] ); + $parent_id = ( $post->post_parent ) ? $post->post_parent : $post->ID; + // TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby values is resolved, // update 'sort_column' to 'menu_order, post_title'. Sorting by both menu_order and post_title ensures a stable sort. // Otherwise with pages that have the same menu_order value, we can see different ordering depending on how DB // queries are constructed internally. For example we might see a different order when a limit is set to <499 // versus >= 500. - $all_pages = get_pages( - array( - 'sort_column' => 'menu_order', - 'order' => 'asc', - ) + $query_args = array( + 'sort_column' => 'menu_order', + 'order' => 'asc', ); + if ( $childrenOnly ) { + $query_args['child_of'] = $parent_id; + } + + $all_pages = get_pages( $query_args ); + $top_level_pages = array(); $pages_with_children = array(); @@ -257,13 +266,16 @@ function render_block_core_page_list( $attributes, $content, $block ) { $active_page_ancestor_ids = array(); foreach ( (array) $all_pages as $page ) { + $is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID ); if ( $is_active ) { $active_page_ancestor_ids = get_post_ancestors( $page->ID ); } - if ( $page->post_parent ) { + if ( ( $page->post_parent && !$childrenOnly ) || + ( $childrenOnly && $page->post_parent !== $parent_id ) ) { + $pages_with_children[ $page->post_parent ][ $page->ID ] = array( 'page_id' => $page->ID, 'title' => $page->post_title,