Date/Time: Make `get_permalink()` more resilient against PHP timezone changes.

Overriding default PHP timezone with `date_default_timezone_set()`, while not recommended, should not inadvertently result in changing existing permalinks.

Add a unit test.

Props Rarst, steevithak, archon810, maciejmackowiak, Ov3rfly, Cybr, hometowntrailers, scvleon, miette49.
Fixes #48623.

git-svn-id: https://develop.svn.wordpress.org/trunk@46795 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-11-28 13:40:19 +00:00
parent 9737e5fdc3
commit e720a9ddc7
2 changed files with 48 additions and 6 deletions

View File

@ -166,7 +166,6 @@ function get_permalink( $post = 0, $leavename = false ) {
$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
if ( '' != $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
$unixtime = strtotime( $post->post_date );
$category = '';
if ( strpos( $permalink, '%category%' ) !== false ) {
@ -212,9 +211,11 @@ function get_permalink( $post = 0, $leavename = false ) {
$author = $authordata->user_nicename;
}
$date = explode( ' ', gmdate( 'Y m d H i s', $unixtime ) );
$rewritereplace =
array(
// This is not an API call because the permalink is based on the stored post_date value,
// which should be parsed as local time regardless of the default PHP timezone.
$date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );
$rewritereplace = array(
$date[0],
$date[1],
$date[2],
@ -227,8 +228,10 @@ function get_permalink( $post = 0, $leavename = false ) {
$author,
$post->post_name,
);
$permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
$permalink = user_trailingslashit( $permalink, 'single' );
$permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
$permalink = user_trailingslashit( $permalink, 'single' );
} else { // if they're not using the fancy permalink option
$permalink = home_url( '?p=' . $post->ID );
}

View File

@ -0,0 +1,39 @@
<?php
/**
* @group date
* @group datetime
* @group post
*/
class Tests_Date_Get_Permalink extends WP_UnitTestCase {
function tearDown() {
delete_option( 'permalink_structure' );
update_option( 'timezone_string', 'UTC' );
date_default_timezone_set( 'UTC' );
parent::tearDown();
}
/**
* @ticket 48623
*/
public function test_should_return_correct_date_permalink_with_changed_time_zone() {
$timezone = 'America/Chicago';
update_option( 'timezone_string', $timezone );
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%hour%/%minute%/%second%' );
date_default_timezone_set( 'UTC' );
$post_id = self::factory()->post->create(
array(
'post_date' => '2018-07-22 21:13:23',
'post_date_gmt' => '2018-07-23 03:13:23',
)
);
$this->assertEquals( 'http://example.org/2018/07/22/21/13/23', get_permalink( $post_id ) );
date_default_timezone_set( $timezone );
$this->assertEquals( 'http://example.org/2018/07/22/21/13/23', get_permalink( $post_id ) );
}
}