Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Test the author archive heading.
*
* @package WordPress\Pattern_Directory
*/

declare( strict_types = 1 );

namespace WordPressdotorg\Pattern_Directory\Tests;

use WP_HTML_Tag_Processor;
use WP_UnitTestCase;
use WP_UnitTest_Factory;
use function WordPressdotorg\Theme\Pattern_Directory_2024\Block_Config\update_archive_title;
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE;

require_once __DIR__ . '/trait-loads-theme.php';

/**
* Author archives show a heading that links to the author's WordPress.org profile (#696).
*/
class Theme_Author_Profile_Link_Test extends WP_UnitTestCase {
use Loads_Theme;

/**
* The pattern author.
*
* @var \WP_User
*/
protected static $author;

/**
* Create an author with a pattern, and load the theme's functions.
*
* @param WP_UnitTest_Factory $factory Test data factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
self::$author = $factory->user->create_and_get(
array(
'user_nicename' => 'pattern-author',
'display_name' => 'Pattern Author',
)
);
foreach ( array( POST_TYPE, 'post' ) as $post_type ) {
$factory->post->create(
array(
'post_type' => $post_type,
'post_author' => self::$author->ID,
'post_status' => 'publish',
)
);
}

self::load_theme();
}

/**
* Use pretty permalinks, as the site does.
*/
public function set_up(): void {
parent::set_up();

$this->set_permalink_structure( '/%postname%/' );
}

/**
* Render a filter-type query title block through the theme.
*
* @param array $attrs Block attributes.
* @return WP_HTML_Tag_Processor A processor positioned before the output.
*/
protected function render_title( array $attrs ): WP_HTML_Tag_Processor {
$block = array(
'blockName' => 'core/query-title',
'attrs' => array_merge( array( 'type' => 'filter' ), $attrs ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
);

return new WP_HTML_Tag_Processor( update_archive_title( '', $block ) );
}

/**
* Catches an author archive whose grid heading is hidden or has no link to the profile.
*/
public function test_grid_title_links_to_profile(): void {
$this->go_to( get_author_posts_url( self::$author->ID ) );
$this->assertTrue( is_author() );

$tags = $this->render_title(
array(
'level' => 1,
'className' => 'screen-reader-text',
)
);

$this->assertTrue( $tags->next_tag( 'h1' ) );
$this->assertFalse( $tags->has_class( 'screen-reader-text' ) );
$this->assertTrue( $tags->next_tag( 'a' ) );
$this->assertSame( 'https://profiles.wordpress.org/pattern-author/', $tags->get_attribute( 'href' ) );
}

/**
* Catches the profile link leaking into the local navigation bar's title.
*/
public function test_navigation_title_stays_plain(): void {
$this->go_to( get_author_posts_url( self::$author->ID ) );

$tags = $this->render_title( array( 'className' => 'wporg-local-navigation-bar__fade-in-scroll' ) );

$this->assertTrue( $tags->next_tag( 'h1' ) );
$this->assertFalse( $tags->next_tag( 'a' ) );
}

/**
* Catches the heading showing on archives that are not by an author.
*/
public function test_front_page_title_stays_hidden(): void {
$this->go_to( home_url( '/' ) );

$tags = $this->render_title(
array(
'level' => 1,
'className' => 'screen-reader-text',
)
);

$this->assertTrue( $tags->next_tag( 'h1' ) );
$this->assertTrue( $tags->has_class( 'screen-reader-text' ) );
$this->assertFalse( $tags->next_tag( 'a' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ function update_archive_title( $block_content, $block ) {
return '';
}

$term_names = get_applied_filter_list( false );
$author_link = '';
$term_names = get_applied_filter_list( false );
if ( ! empty( $term_names ) ) {
$term_names = wp_list_pluck( $term_names, 'name' );
// translators: %s list of terms used for filtering.
Expand All @@ -509,13 +510,32 @@ function update_archive_title( $block_content, $block ) {
if ( $author ) {
/* translators: %s: Author display name. */
$title = sprintf( __( 'Author: %s', 'wporg-patterns' ), $author->display_name );

$author_link = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( 'https://profiles.wordpress.org/' . $author->user_nicename . '/' ),
esc_html( $author->display_name )
);
} else {
$title = __( 'All patterns', 'wporg-patterns' );
}
}

if ( is_search() ) {
$title = __( 'Search results', 'wporg-patterns' );
$title = __( 'Search results', 'wporg-patterns' );
$author_link = '';
}

$content = esc_html( $title );

// Show the author heading in the grid, with a link to the author's WordPress.org profile.
// The heading in the local navigation bar stays plain text.
$class_names = isset( $attributes['className'] ) ? explode( ' ', $attributes['className'] ) : array();
if ( $author_link && in_array( 'screen-reader-text', $class_names, true ) ) {
$block['attrs']['className'] = implode( ' ', array_diff( $class_names, array( 'screen-reader-text' ) ) ) . ' is-author-title';

/* translators: %s: Author display name, linked to their WordPress.org profile. */
$content = sprintf( esc_html__( 'Author: %s', 'wporg-patterns' ), $author_link );
}

$allowed_tags = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' );
Expand All @@ -533,7 +553,7 @@ function update_archive_title( $block_content, $block ) {
'<%1$s %2$s>%3$s</%1$s>',
$tag_name,
$wrapper_attributes,
esc_html( $title )
$content
);
}
return $block_content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ body.page .wp-block-wporg-local-navigation-bar .wp-block-query-title {
display: none;
}

/* Show the author archive heading, which links to the author's profile. */
.wp-block-query-title.is-author-title {
margin-top: 0;
margin-bottom: var(--wp--preset--spacing--30);
font-family: var(--wp--preset--font-family--inter);
font-size: var(--wp--preset--font-size--large);
font-weight: 600;
}

.wp-block-query-title.is-author-title a {
color: inherit;
}

/* Hide the grid `h1` on the homepage, as the header has an h1. */
body.blog:not(.paged) .wp-block-query .wp-block-query-title {
display: none;
Expand Down
Loading