Skip to content

Commit

Permalink
Theme grid: Ensure pages have a top-level heading.
Browse files Browse the repository at this point in the history
 This adds a hidden `h1` to all grid pages (except home,  where the site title is h1).
  • Loading branch information
ryelle committed May 22, 2024
1 parent b3d3e52 commit 90ffcd0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions .
27 changes: 27 additions & 0 deletions source/wp-content/themes/wporg-themes-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,33 @@ function get_query_tags() {
return array_merge ( $ tags , $ tags_and );
}

/**
* Given a set of tag slugs, fetch the labels from the feature list.
*
* Using the featured list ensures the label will be translated (if available).
* If a tag is not found, it will be silently filtered out.
*
* @param string[] $tags List of tags.
*
* @return array
*/
function get_tag_labels ( $ tags ) {
$ features = wporg_themes_get_feature_list ( ' active ' );
$ labels = array_map (
function ( $ tag ) use ( $ features ) {
foreach ( $ features as $ list ) {
if ( isset ( $ list [ $ tag ] ) ) {
return $ list [ $ tag ];
}
}
return false ;
},
$ tags
);

return array_filter ( $ labels );
}

/**
* This is a copy of get_theme_feature_list(), but with the wporg-themes text domain
*
Expand Down
72 changes: 70 additions & 2 deletions source/wp-content/themes/wporg-themes-2024/inc/block-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace WordPressdotorg \ Theme \ Theme_Directory_2024 \ Block_Config ;

use WP_HTML_Tag_Processor ;
use function WordPressdotorg \ Theme \ Theme_Directory_2024 \{ get_query_tags, wporg_themes_get_feature_list };
use WP_HTML_Tag_Processor , WP_Block_Supports ;
use function WordPressdotorg \ Theme \ Theme_Directory_2024 \{ get_query_tags, get_tag_labels, wporg_themes_get_feature_list };

add_filter ( ' wporg_query_total_label ' , __NAMESPACE__ . ' \update_query_total_label ' , ten , two );
add_filter ( ' wporg_query_filter_options_layouts ' , __NAMESPACE__ . ' \get_layouts_options ' );
Expand All @@ -17,6 +17,7 @@
add_filter ( ' render_block_wporg/link-wrapper ' , __NAMESPACE__ . ' \inject_permalink_link_wrapper ' );
add_filter ( ' render_block_wporg/language-suggest ' , __NAMESPACE__ . ' \inject_language_suggest_endpoint ' );
add_filter ( ' render_block_core/search ' , __NAMESPACE__ . ' \inject_browse_search_block ' );
add_filter ( ' render_block_core/query-title ' , __NAMESPACE__ . ' \update_archive_title ' , ten , three );

/**
* Update the query total label to reflect "patterns" found.
Expand Down Expand Up @@ -286,3 +287,70 @@ function inject_browse_search_block( $block_content ) {

return str_replace ( ' </form> ' , $ inputs . ' </form> ' , $ block_content );
}

/**
* Update the archive title for all filter views.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*/
function update_archive_title ( $ block_content , $ block , $ instance ) {
global $ wp_query ;
$ attributes = $ block [ ' attrs ' ];

if ( isset ( $ attributes [ ' type ' ] ) && ' filter ' === $ attributes [ ' type ' ] ) {
// Skip output if there are no results. The `query-no-results` has an h1.
if ( ! $ wp_query -> found_posts ) {
return '' ;
}

$ author = isset ( $ wp_query -> query [ ' author_name ' ] ) ? get_user_by ( ' slug ' , $ wp_query -> query [ ' author_name ' ] ) : false ;
$ current_browse = $ wp_query -> query [ ' browse ' ] ?? false ;
$ tags = get_query_tags ();

if ( is_front_page () && ! $ current_browse && ! is_paged () ) {
return '' ;
}

if ( $ author ) {
// translators: %s Author name.
$ title = sprintf ( __ ( ' Author: %s ' , ' wporg-themes ' ), $ author -> display_name );
} else if ( is_search () ) {
$ title = __ ( ' Search results ' , ' wporg-themes ' );
} else if ( ! empty ( $ tags ) ) {
$ labels = get_tag_labels ( $ tags );
// translators: %s List of applied tags.
$ title = sprintf ( __ ( ' Themes: %s ' , ' wporg-themes ' ), wp_sprintf_l ( ' %l ' , $ labels ) );
} else if ( ' community ' === $ current_browse ) {
$ title = __ ( ' Community themes ' , ' wporg-themes ' );
} else if ( ' commercial ' === $ current_browse ) {
$ title = __ ( ' Commercial themes ' , ' wporg-themes ' );
} else if ( ' new ' === $ current_browse ) {
$ title = __ ( ' Latest themes ' , ' wporg-themes ' );
} else if ( ' updated ' === $ current_browse ) {
$ title = __ ( ' Recently updated themes ' , ' wporg-themes ' );
} else if ( ' favorites ' === $ current_browse ) {
$ title = __ ( ' My favorites ' , ' wporg-themes ' );
} else {
$ title = __ ( ' All themes ' , ' wporg-themes ' );
}

$ tag_name = isset ( $ attributes [ ' level ' ] ) ? ' h ' . ( int ) $ attributes [ ' level ' ] : ' h1 ' ;
$ align_class_name = empty ( $ attributes [ ' textAlign ' ] ) ? '' : " has-text-align- { $ attributes [ ' textAlign ' ]}" ;

// Required to prevent `block_to_render` from being null in `get_block_wrapper_attributes`.
$ parent = WP_Block_Supports :: $ block_to_render ;
WP_Block_Supports :: $ block_to_render = $ block ;
$ wrapper_attributes = get_block_wrapper_attributes ( array ( ' class ' => $ align_class_name ) );
WP_Block_Supports :: $ block_to_render = $ parent ;

return sprintf (
' <%1$s %2$s>%3$s</%1$s> ' ,
$ tag_name ,
$ wrapper_attributes ,
$ title
);
}
return $ block_content ;
}
2 changes: 2 additions & 0 deletions source/wp-content/themes/wporg-themes-2024/patterns/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<div style="margin-top:0;margin-bottom:0;height:var(--wp--preset--spacing--50)" aria-hidden="true" class="wp-block-spacer"></div>
<!-- / wp:spacer -->

<!-- wp:query-title {"type":"filter","level":1,"className":"screen-reader-text"} /-->

<!-- wp:post-template {"style":{"spacing":{"blockGap":"var:preset|spacing|40"}},"layout":{"type":"grid","columnCount":3}} -->
<!-- wp:wporg/link-wrapper -->
<a class="wp-block-wporg-link-wrapper" href="">
Expand Down

0 comments on commit 90ffcd0

Please sign in to comment.