From af2f510780cee4282d80514540d8b49d978742be Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Wed, 14 Jun 2023 09:23:27 +0200 Subject: [PATCH 1/5] Home link block: Add 'current-menu-item' --- packages/block-library/src/home-link/index.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index 459979b0d43893..a13746854e2454 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -94,11 +94,13 @@ function block_core_home_link_build_css_font_sizes( $context ) { function block_core_home_link_build_li_wrapper_attributes( $context ) { $colors = block_core_home_link_build_css_colors( $context ); $font_sizes = block_core_home_link_build_css_font_sizes( $context ); + $is_active = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ); $classes = array_merge( $colors['css_classes'], $font_sizes['css_classes'] ); $classes[] = 'wp-block-navigation-item'; + $classes[] = $is_active ? 'current-menu-item' : ''; $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); $wrapper_attributes = get_block_wrapper_attributes( From d7e692f78e4c86763d6d82e05cb340046cc98b41 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Mon, 26 Jun 2023 13:02:50 +0200 Subject: [PATCH 2/5] Update index.php --- .../block-library/src/home-link/index.php | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index a13746854e2454..513e2698949d90 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -94,14 +94,23 @@ function block_core_home_link_build_css_font_sizes( $context ) { function block_core_home_link_build_li_wrapper_attributes( $context ) { $colors = block_core_home_link_build_css_colors( $context ); $font_sizes = block_core_home_link_build_css_font_sizes( $context ); - $is_active = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ); $classes = array_merge( $colors['css_classes'], $font_sizes['css_classes'] ); - $classes[] = 'wp-block-navigation-item'; - $classes[] = $is_active ? 'current-menu-item' : ''; $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); + $classes[] = 'wp-block-navigation-item'; + // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. + $page_for_posts = get_option( 'page_for_posts' ); + // The ID of the current page + $page_id = get_queried_object_id(); + + if ( is_front_page() ) { + $classes[] = 'current-menu-item'; + } elseif ( is_home() && ( (int)$page_for_posts !== $page_id ) ) { + // Edge case where the Reading settings has a posts page set but not a static homepage. + $classes[] = 'current-menu-item'; + } $wrapper_attributes = get_block_wrapper_attributes( array( @@ -126,8 +135,18 @@ function render_block_core_home_link( $attributes, $content, $block ) { if ( empty( $attributes['label'] ) ) { return ''; } - - $aria_current = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) ? ' aria-current="page"' : ''; + $aria_current = ''; + // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. + $page_for_posts = get_option( 'page_for_posts' ); + // The ID of the current page + $page_id = get_queried_object_id(); + + if ( is_front_page() ) { + $aria_current = ' aria-current="page"'; + } elseif ( is_home() && ( (int)$page_for_posts !== $page_id ) ) { + // Edge case where the Reading settings has a posts page set but not a static homepage. + $aria_current = ' aria-current="page"'; + } return sprintf( '
  • %4$s
  • ', From b9bf3b14db5ce632791989d1e78790a726749906 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Mon, 26 Jun 2023 13:15:29 +0200 Subject: [PATCH 3/5] CS --- packages/block-library/src/home-link/index.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index 513e2698949d90..e87f8556ad366e 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -101,13 +101,13 @@ function block_core_home_link_build_li_wrapper_attributes( $context ) { $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); $classes[] = 'wp-block-navigation-item'; // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. - $page_for_posts = get_option( 'page_for_posts' ); - // The ID of the current page - $page_id = get_queried_object_id(); + $page_for_posts = get_option( 'page_for_posts' ); + // The ID of the current page. + $page_id = get_queried_object_id(); if ( is_front_page() ) { $classes[] = 'current-menu-item'; - } elseif ( is_home() && ( (int)$page_for_posts !== $page_id ) ) { + } elseif ( is_home() && ( (int) $page_for_posts !== $page_id ) ) { // Edge case where the Reading settings has a posts page set but not a static homepage. $classes[] = 'current-menu-item'; } @@ -135,10 +135,10 @@ function render_block_core_home_link( $attributes, $content, $block ) { if ( empty( $attributes['label'] ) ) { return ''; } - $aria_current = ''; + $aria_current = ''; // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. $page_for_posts = get_option( 'page_for_posts' ); - // The ID of the current page + // The ID of the current page. $page_id = get_queried_object_id(); if ( is_front_page() ) { From f876b355f57c9acb26bcfe8f21c3c0c10eca2a5c Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Mon, 26 Jun 2023 13:50:35 +0200 Subject: [PATCH 4/5] Update index.php --- packages/block-library/src/home-link/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index e87f8556ad366e..39b1d5271c06cb 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -103,7 +103,7 @@ function block_core_home_link_build_li_wrapper_attributes( $context ) { // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. $page_for_posts = get_option( 'page_for_posts' ); // The ID of the current page. - $page_id = get_queried_object_id(); + $page_id = get_queried_object_id(); if ( is_front_page() ) { $classes[] = 'current-menu-item'; @@ -139,11 +139,11 @@ function render_block_core_home_link( $attributes, $content, $block ) { // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. $page_for_posts = get_option( 'page_for_posts' ); // The ID of the current page. - $page_id = get_queried_object_id(); + $page_id = get_queried_object_id(); if ( is_front_page() ) { $aria_current = ' aria-current="page"'; - } elseif ( is_home() && ( (int)$page_for_posts !== $page_id ) ) { + } elseif ( is_home() && ( (int) $page_for_posts !== $page_id ) ) { // Edge case where the Reading settings has a posts page set but not a static homepage. $aria_current = ' aria-current="page"'; } From 5a54899ad3536bb324e5d51face41f901ef69f14 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Wed, 28 Jun 2023 10:47:12 +0200 Subject: [PATCH 5/5] Remove variables that were only used once --- packages/block-library/src/home-link/index.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index 39b1d5271c06cb..8fb5ed109019d3 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -100,14 +100,10 @@ function block_core_home_link_build_li_wrapper_attributes( $context ) { ); $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); $classes[] = 'wp-block-navigation-item'; - // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. - $page_for_posts = get_option( 'page_for_posts' ); - // The ID of the current page. - $page_id = get_queried_object_id(); if ( is_front_page() ) { $classes[] = 'current-menu-item'; - } elseif ( is_home() && ( (int) $page_for_posts !== $page_id ) ) { + } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) { // Edge case where the Reading settings has a posts page set but not a static homepage. $classes[] = 'current-menu-item'; } @@ -136,14 +132,10 @@ function render_block_core_home_link( $attributes, $content, $block ) { return ''; } $aria_current = ''; - // The ID of the static page assigned to the blog posts index (posts page). String, '0' if not set. - $page_for_posts = get_option( 'page_for_posts' ); - // The ID of the current page. - $page_id = get_queried_object_id(); if ( is_front_page() ) { $aria_current = ' aria-current="page"'; - } elseif ( is_home() && ( (int) $page_for_posts !== $page_id ) ) { + } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) { // Edge case where the Reading settings has a posts page set but not a static homepage. $aria_current = ' aria-current="page"'; }