From 1fcbffcaa1ba4d3a048da84252fe0d0e952712db 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 | 25 +++++++++++++++++-- .../block-library/src/page-list/index.php | 22 +++++++++++----- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/page-list/block.json b/packages/block-library/src/page-list/block.json index 25118d2dbeffb4..28515395b64292 100644 --- a/packages/block-library/src/page-list/block.json +++ b/packages/block-library/src/page-list/block.json @@ -16,6 +16,11 @@ "showSubmenuIcon", "style" ], + "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 4274b3d2d5b1e6..00949e7eb6da22 100644 --- a/packages/block-library/src/page-list/edit.js +++ b/packages/block-library/src/page-list/edit.js @@ -10,9 +10,10 @@ import { BlockControls, useBlockProps, store as blockEditorStore, + 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'; @@ -28,7 +29,12 @@ import ConvertToLinksModal from './convert-to-links-modal'; // Performance of Navigation Links is not good past this value. const MAX_PAGE_COUNT = 100; -export default function PageListEdit( { context, clientId } ) { +export default function PageListEdit( { + attributes, + context, + clientId, + setAttributes, +} ) { const { textColor, backgroundColor, showSubmenuIcon, style } = context || {}; @@ -78,8 +84,23 @@ export default function PageListEdit( { context, clientId } ) { const openModal = () => setOpen( true ); const closeModal = () => setOpen( false ); + 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 a0cadde487d571..cc8c165d5681a6 100644 --- a/packages/block-library/src/page-list/index.php +++ b/packages/block-library/src/page-list/index.php @@ -146,27 +146,37 @@ function 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(); foreach ( (array) $all_pages as $page ) { - if ( $page->post_parent ) { + if ( ( $page->post_parent && !$childrenOnly ) || + ( $childrenOnly && $page->post_parent !== $parent_id ) ) { $pages_with_children[ $page->post_parent ][ $page->ID ] = array( 'title' => $page->post_title, 'link' => get_permalink( $page->ID ),