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
27 changes: 27 additions & 0 deletions src/wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,21 @@ function wp_get_document_title() {
$author = get_queried_object();
$title['title'] = $author->display_name;

/*
* If it's a weekly archive, use the week's date range as the title.
* Weekly archive links also contain the year, so check this before is_year().
*/
} elseif ( is_date() && get_query_var( 'w' ) && get_the_date( 'Y-m-d' ) ) {
$week = get_weekstartend( get_the_date( 'Y-m-d' ), get_option( 'start_of_week' ) );
$date_format = get_option( 'date_format' );

$title['title'] = sprintf(
/* translators: 1: Week start date, 2: Week end date. */
_x( '%1$s – %2$s', 'weekly archives date range' ),
date_i18n( $date_format, $week['start'] ),
date_i18n( $date_format, $week['end'] )
);

// If it's a date archive, use the date as the title.
} elseif ( is_year() ) {
$title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
Expand Down Expand Up @@ -2005,6 +2020,18 @@ function get_the_archive_title() {
} elseif ( is_author() ) {
$title = get_the_author();
$prefix = _x( 'Author:', 'author archive title prefix' );
} elseif ( is_date() && get_query_var( 'w' ) && get_the_date( 'Y-m-d' ) ) {
// Weekly archive links also contain the year, so check this before is_year().
$week = get_weekstartend( get_the_date( 'Y-m-d' ), get_option( 'start_of_week' ) );
$date_format = get_option( 'date_format' );

$title = sprintf(
/* translators: 1: Week start date, 2: Week end date. */
_x( '%1$s – %2$s', 'weekly archives date range' ),
date_i18n( $date_format, $week['start'] ),
date_i18n( $date_format, $week['end'] )
);
$prefix = _x( 'Week:', 'date archive title prefix' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string has to be translated for the different language packs when these changes are merged.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

} elseif ( is_year() ) {
/* translators: See https://www.php.net/manual/datetime.format.php */
$title = get_the_date( _x( 'Y', 'yearly archives date format' ) );
Expand Down
91 changes: 91 additions & 0 deletions tests/phpunit/tests/general/getTheArchiveTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* Tests for the `get_the_archive_title()` function on date archives.
*
* @group general
* @group template
*
* @covers ::get_the_archive_title
*/
class Tests_General_GetTheArchiveTitle extends WP_UnitTestCase {

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
$factory->post->create(
array(
'post_status' => 'publish',
'post_date' => '2015-09-22 18:52:17',
)
);
}

public function set_up() {
parent::set_up();

update_option( 'start_of_week', 1 );
update_option( 'date_format', 'F j, Y' );
}

/**
* Tests that a weekly archive uses the week's date range, not the year.
*
* @ticket 66489
*/
public function test_get_the_archive_title_on_weekly_archive() {
$this->go_to( '?m=2015&w=39' );

$this->assertTrue( is_year(), 'Weekly archive links also set is_year().' );
$this->assertSame( 'Week: <span>September 21, 2015 &#8211; September 27, 2015</span>', get_the_archive_title() );
}

/**
* Tests that a weekly archive respects the start_of_week option.
*
* @ticket 66489
*/
public function test_get_the_archive_title_on_weekly_archive_respects_start_of_week() {
update_option( 'start_of_week', 0 );

$this->go_to( '?m=2015&w=38' );

$this->assertSame( 'Week: <span>September 20, 2015 &#8211; September 26, 2015</span>', get_the_archive_title() );
}

/**
* Tests that a weekly archive without the year uses the week's date range.
*
* @ticket 66489
*/
public function test_get_the_archive_title_on_weekly_archive_without_year() {
$this->go_to( '?w=39' );

$this->assertSame( 'Week: <span>September 21, 2015 &#8211; September 27, 2015</span>', get_the_archive_title() );
}

/**
* @ticket 66489
*/
public function test_get_the_archive_title_on_yearly_archive() {
$this->go_to( '?m=2015' );

$this->assertSame( 'Year: <span>2015</span>', get_the_archive_title() );
}

/**
* @ticket 66489
*/
public function test_get_the_archive_title_on_monthly_archive() {
$this->go_to( '?m=201509' );

$this->assertSame( 'Month: <span>September 2015</span>', get_the_archive_title() );
}

/**
* @ticket 66489
*/
public function test_get_the_archive_title_on_daily_archive() {
$this->go_to( '?m=20150922' );

$this->assertSame( 'Day: <span>September 22, 2015</span>', get_the_archive_title() );
}
}
22 changes: 22 additions & 0 deletions tests/phpunit/tests/general/wpGetDocumentTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ public function test_day_title() {
$this->assertSame( sprintf( 'September 22, 2015 &#8211; %s', $this->blog_name ), wp_get_document_title() );
}

/**
* @ticket 66489
*/
Comment on lines +243 to +245

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment/doc is not necessary (it's not used in the rest of the file).

Suggested change
/**
* @ticket 66489
*/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'd prefer to keep it: per the Core handbook, @ticket marks tests that address a Trac ticket and is mapped to @group, so phpunit --group 66489 runs all tests for this change. It's also already used in this file (@ticket 6479 on test__wp_render_title_tag_with_blog_description).

public function test_week_title() {
update_option( 'start_of_week', 1 );

$this->go_to( '?m=2015&w=39' );

@mardelnet mardelnet Nov 27, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also test a URL like this: $this->go_to( '/2015?w=39' ); ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_week_title_without_year in c059a75.


$this->assertSame( sprintf( 'September 21, 2015 &#8211; September 27, 2015 &#8211; %s', $this->blog_name ), wp_get_document_title() );
}

/**
* @ticket 66489
*/
public function test_week_title_without_year() {
update_option( 'start_of_week', 1 );

$this->go_to( '?w=39' );

$this->assertSame( sprintf( 'September 21, 2015 &#8211; September 27, 2015 &#8211; %s', $this->blog_name ), wp_get_document_title() );
}

public function test_404_title() {
$this->go_to( '?m=404' );

Expand Down
Loading