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.
Built from https://develop.svn.wordpress.org/trunk@46795


git-svn-id: http://core.svn.wordpress.org/trunk@46595 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-11-28 13:42:00 +00:00
parent 7acfab22b8
commit 9d14355bf5
2 changed files with 10 additions and 7 deletions

View File

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

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.4-alpha-46793'; $wp_version = '5.4-alpha-46795';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.