From 06dc5e170fb2683b25937a6e0c0c740dd8fe8128 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Thu, 11 Nov 2021 16:27:44 +0000 Subject: [PATCH] Template: Fix "undefined index: 00" when archive month query is empty in `wp_title()`. When `m` query_tag has a valid year, i.e. `?m=2021`, and there are posts for that year, `substr()` returns a `false` on PHP 5.6 and an empty string on PHP 7.0+. Passing either of those values to `$wp_locale->get_month()` results in a PHP notice on PHP 5.6 to PHP 7.4 and a PHP Warning on PHP 8.0+. Why? The `$month` lookup table has zeroized keys from '01' to '12'. A empty value is passed to `zeroise()` returns `'00'` which is directly passed as a key in the month property. That key does not exist. While `$wp_locale->get_month()` would benefit from guarding/validation, this fix ensures a falsey value is not passed as a month. Tests are added including a test that fails with this fix not applied. Follow-up to [801], [35294], [35624]. Props antpb, audrasjb, costdev, davidmosterd, drewapicture, herregroen, hellofromTonya, michelwppi, sergeybiryukov. Fixes #31521. git-svn-id: https://develop.svn.wordpress.org/trunk@52136 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 6 ++- tests/phpunit/tests/general/wpTitle.php | 62 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/general/wpTitle.php diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 4a9835a478..1b743eddba 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -1376,9 +1376,11 @@ function wp_title( $sep = '»', $display = true, $seplocation = '' ) { // If there's a month. if ( is_archive() && ! empty( $m ) ) { $my_year = substr( $m, 0, 4 ); - $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) ); + $my_month = substr( $m, 4, 2 ); $my_day = (int) substr( $m, 6, 2 ); - $title = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' ); + $title = $my_year . + ( $my_month ? $t_sep . $wp_locale->get_month( $my_month ) : '' ) . + ( $my_day ? $t_sep . $my_day : '' ); } // If there's a year. diff --git a/tests/phpunit/tests/general/wpTitle.php b/tests/phpunit/tests/general/wpTitle.php new file mode 100644 index 0000000000..272441a968 --- /dev/null +++ b/tests/phpunit/tests/general/wpTitle.php @@ -0,0 +1,62 @@ +post->create( + array( + 'post_status' => 'publish', + 'post_title' => 'Test Post', + 'post_type' => 'post', + 'post_date' => '2021-11-01 18:52:17', + ) + ); + $this->go_to( '?m=' . $query ); + + $this->assertSame( $expected, wp_title( '»', false ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_wp_title_archive() { + return array( + 'year with posts' => array( + 'query' => '2021', + 'expected' => ' » 2021', + ), + 'year without posts' => array( + 'query' => '1910', + 'expected' => ' » Page not found', + ), + 'year and month with posts' => array( + 'query' => '202111', + 'expected' => ' » 2021 » November', + ), + 'year and month without posts' => array( + 'query' => '202101', + 'expected' => ' » Page not found', + ), + 'year, month, day with posts' => array( + 'query' => '20211101', + 'expected' => ' » 2021 » November » 1', + ), + 'year, month, day without posts' => array( + 'query' => '20210101', + 'expected' => ' » Page not found', + ), + ); + } +}