From a19025b561f1bec88deb3ee3ddd394dfab9a981b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 11:00:02 +0000 Subject: [PATCH 01/10] Use get_pages as block fallback --- .../block-library/src/navigation/index.php | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index e3848190c6e7..f83d9e487cb5 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -183,7 +183,7 @@ function render_block_core_navigation( $attributes, $content, $block ) { $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes ); } - if ( ! empty( $block->context['navigationArea'] ) ) { + if ( false && ! empty( $block->context['navigationArea'] ) ) { $area = $block->context['navigationArea']; $mapping = get_option( 'wp_navigation_areas', array() ); if ( ! empty( $mapping[ $area ] ) ) { @@ -215,7 +215,34 @@ function( $block ) { } if ( empty( $inner_blocks ) ) { - return ''; + $all_pages = get_pages( + array( + 'sort_column' => 'menu_order,post_title', + 'order' => 'asc', + 'number' => 4, + ) + ); + + // If thare are no pages, there is nothing to show. + if ( empty( $all_pages ) ) { + return; + } + + $wrapper_markup = ''; + + $items_markup = array_reduce( + $all_pages, + function( $acc, $page ) { + $acc .= '
  • ' . esc_attr( $page->post_title ) . '
  • '; + return $acc; + }, + '' + ); + + return sprintf( + $wrapper_markup, + $items_markup + ); } // Restore legacy classnames for submenu positioning. From 778dcb52c0540124205df15135c60229b86c8738 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 11:31:53 +0000 Subject: [PATCH 02/10] Simplify by using page list block --- .../block-library/src/navigation/index.php | 31 ++----------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index f83d9e487cb5..b54073d21924 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -214,35 +214,10 @@ function( $block ) { $inner_blocks = new WP_Block_List( $compacted_blocks, $attributes ); } + // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { - $all_pages = get_pages( - array( - 'sort_column' => 'menu_order,post_title', - 'order' => 'asc', - 'number' => 4, - ) - ); - - // If thare are no pages, there is nothing to show. - if ( empty( $all_pages ) ) { - return; - } - - $wrapper_markup = ''; - - $items_markup = array_reduce( - $all_pages, - function( $acc, $page ) { - $acc .= '
  • ' . esc_attr( $page->post_title ) . '
  • '; - return $acc; - }, - '' - ); - - return sprintf( - $wrapper_markup, - $items_markup - ); + $page_list_block = parse_blocks( '' ); + $inner_blocks = new WP_Block_List( $page_list_block, $attributes ); } // Restore legacy classnames for submenu positioning. From b457a91911b375d97a38d6f73fb1bbafabc8d275 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 11:38:47 +0000 Subject: [PATCH 03/10] Output special class when rendering the fallback --- packages/block-library/src/navigation/index.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index b54073d21924..4eb6da03130c 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -142,6 +142,7 @@ function block_core_navigation_render_submenu_icon() { * @return string Returns the post content with the legacy widget added. */ function render_block_core_navigation( $attributes, $content, $block ) { + $is_fallback = false; /** * Deprecated: * The rgbTextColor and rgbBackgroundColor attributes @@ -216,8 +217,9 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { + $is_fallback = true; // indicate we are rendering the fallback. $page_list_block = parse_blocks( '' ); - $inner_blocks = new WP_Block_List( $page_list_block, $attributes ); + $inner_blocks = new WP_Block_List( $page_list_block, $attributes ); } // Restore legacy classnames for submenu positioning. @@ -236,7 +238,8 @@ function( $block ) { $colors['css_classes'], $font_sizes['css_classes'], $is_responsive_menu ? array( 'is-responsive' ) : array(), - $layout_class ? array( $layout_class ) : array() + $layout_class ? array( $layout_class ) : array(), + $is_fallback ? array( 'is-fallback' ) : array() ); $inner_blocks_html = ''; From 34705d84f58f3d386e0e13884b766fafb7f0b105 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 11:41:03 +0000 Subject: [PATCH 04/10] Remove requirement for parsing the block --- packages/block-library/src/navigation/index.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 4eb6da03130c..133d41bfd38a 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -218,8 +218,13 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { $is_fallback = true; // indicate we are rendering the fallback. - $page_list_block = parse_blocks( '' ); - $inner_blocks = new WP_Block_List( $page_list_block, $attributes ); + + $page_list_block = array( + 'blockName' => 'core/page-list', + 'attrs' => array(), + ); + + $inner_blocks = new WP_Block_List( array( $page_list_block ), $attributes ); } // Restore legacy classnames for submenu positioning. From bbe684171e79c66d0b5d5c462006127571e9f376 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 13:02:30 +0000 Subject: [PATCH 05/10] Fix linting --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 133d41bfd38a..e63d67ccbca1 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -217,7 +217,7 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { - $is_fallback = true; // indicate we are rendering the fallback. + $is_fallback = true; // indicate we are rendering the fallback. $page_list_block = array( 'blockName' => 'core/page-list', From eb70667ba866aa206855c335f0e2c3f644876706 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 13:13:44 +0000 Subject: [PATCH 06/10] Limit max number of pages to 4 via context API --- packages/block-library/src/navigation/block.json | 6 +++++- packages/block-library/src/navigation/index.php | 2 ++ packages/block-library/src/page-list/block.json | 3 ++- packages/block-library/src/page-list/index.php | 4 ++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index 003da92bc497..815e187c6a75 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -55,6 +55,9 @@ }, "customOverlayTextColor": { "type": "string" + }, + "maxPages": { + "type": "number" } }, "usesContext": [ "navigationArea" ], @@ -72,7 +75,8 @@ "showSubmenuIcon": "showSubmenuIcon", "openSubmenusOnClick": "openSubmenusOnClick", "style": "style", - "orientation": "orientation" + "orientation": "orientation", + "maxPages": "maxPages" }, "supports": { "align": [ "wide", "full" ], diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index e63d67ccbca1..5d512bff7b89 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -142,6 +142,7 @@ function block_core_navigation_render_submenu_icon() { * @return string Returns the post content with the legacy widget added. */ function render_block_core_navigation( $attributes, $content, $block ) { + $is_fallback = false; /** * Deprecated: @@ -218,6 +219,7 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { $is_fallback = true; // indicate we are rendering the fallback. + $attributes['maxPages'] = 4; // set value to be passed as context to Page List block. $page_list_block = array( 'blockName' => 'core/page-list', diff --git a/packages/block-library/src/page-list/block.json b/packages/block-library/src/page-list/block.json index 3068a1fb8bc0..faf5253f3e15 100644 --- a/packages/block-library/src/page-list/block.json +++ b/packages/block-library/src/page-list/block.json @@ -21,7 +21,8 @@ "customFontSize", "showSubmenuIcon", "style", - "openSubmenusOnClick" + "openSubmenusOnClick", + "maxPages" ], "supports": { "reusable": false, diff --git a/packages/block-library/src/page-list/index.php b/packages/block-library/src/page-list/index.php index 53dec1f1957e..4099c8660110 100644 --- a/packages/block-library/src/page-list/index.php +++ b/packages/block-library/src/page-list/index.php @@ -293,6 +293,10 @@ function render_block_core_page_list( $attributes, $content, $block ) { $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); + if ( array_key_exists( 'maxPages', $block->context ) ) { + $nested_pages = array_slice( $nested_pages, 0, $block->context['maxPages'] ); + } + $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context ); $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false; From 337a8ac6562f72be5ce1336980a3392421dae958 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 14:21:13 +0000 Subject: [PATCH 07/10] 2nd attempt to fix PHP linting --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 5d512bff7b89..a0c0ea81a513 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -218,7 +218,7 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { - $is_fallback = true; // indicate we are rendering the fallback. + $is_fallback = true; // indicate we are rendering the fallback. $attributes['maxPages'] = 4; // set value to be passed as context to Page List block. $page_list_block = array( From df146db05118e2b3e9e5ff2f910cd8ccde393ba7 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 14:24:10 +0000 Subject: [PATCH 08/10] Revert debugging --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index a0c0ea81a513..79314bf4752e 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -185,7 +185,7 @@ function render_block_core_navigation( $attributes, $content, $block ) { $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes ); } - if ( false && ! empty( $block->context['navigationArea'] ) ) { + if ( ! empty( $block->context['navigationArea'] ) ) { $area = $block->context['navigationArea']; $mapping = get_option( 'wp_navigation_areas', array() ); if ( ! empty( $mapping[ $area ] ) ) { From ee5d0dbd94338e9991fa5e0dcb7b6b4b12174fed Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 14:48:18 +0000 Subject: [PATCH 09/10] Make attribute/context prop "unstable" --- packages/block-library/src/navigation/block.json | 4 ++-- packages/block-library/src/navigation/index.php | 2 +- packages/block-library/src/page-list/block.json | 2 +- packages/block-library/src/page-list/index.php | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index 815e187c6a75..664d814e88bb 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -56,7 +56,7 @@ "customOverlayTextColor": { "type": "string" }, - "maxPages": { + "__unstableMaxPages": { "type": "number" } }, @@ -76,7 +76,7 @@ "openSubmenusOnClick": "openSubmenusOnClick", "style": "style", "orientation": "orientation", - "maxPages": "maxPages" + "__unstableMaxPages": "__unstableMaxPages" }, "supports": { "align": [ "wide", "full" ], diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 79314bf4752e..544fa346f68d 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -219,7 +219,7 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { $is_fallback = true; // indicate we are rendering the fallback. - $attributes['maxPages'] = 4; // set value to be passed as context to Page List block. + $attributes['__unstableMaxPages'] = 4; // set value to be passed as context to Page List block. $page_list_block = array( 'blockName' => 'core/page-list', diff --git a/packages/block-library/src/page-list/block.json b/packages/block-library/src/page-list/block.json index faf5253f3e15..ffb8378176fc 100644 --- a/packages/block-library/src/page-list/block.json +++ b/packages/block-library/src/page-list/block.json @@ -22,7 +22,7 @@ "showSubmenuIcon", "style", "openSubmenusOnClick", - "maxPages" + "__unstableMaxPages" ], "supports": { "reusable": false, diff --git a/packages/block-library/src/page-list/index.php b/packages/block-library/src/page-list/index.php index 4099c8660110..9a58d06da727 100644 --- a/packages/block-library/src/page-list/index.php +++ b/packages/block-library/src/page-list/index.php @@ -293,8 +293,8 @@ function render_block_core_page_list( $attributes, $content, $block ) { $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); - if ( array_key_exists( 'maxPages', $block->context ) ) { - $nested_pages = array_slice( $nested_pages, 0, $block->context['maxPages'] ); + if ( array_key_exists( '__unstableMaxPages', $block->context ) ) { + $nested_pages = array_slice( $nested_pages, 0, $block->context['__unstableMaxPages'] ); } $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context ); From 24b160c3d6ad490c5d37cb6c0330e5efbdb45a04 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 15:15:43 +0000 Subject: [PATCH 10/10] More linting --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 544fa346f68d..6db226fe960f 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -218,7 +218,7 @@ function( $block ) { // If there are no inner blocks then fallback to rendering the Page List block. if ( empty( $inner_blocks ) ) { - $is_fallback = true; // indicate we are rendering the fallback. + $is_fallback = true; // indicate we are rendering the fallback. $attributes['__unstableMaxPages'] = 4; // set value to be passed as context to Page List block. $page_list_block = array(