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
30 changes: 30 additions & 0 deletions src/wp-includes/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
return null;
}

$redirect_post_id = 0;
foreach ( array( 'p', 'page_id', 'attachment_id' ) as $query_var ) {
$redirect_post_id = get_query_var( $query_var );

if ( $redirect_post_id ) {
$redirect_post_id = absint( $redirect_post_id );
break;
}
}

if ( $redirect_post_id ) {
$redirect_post = get_post( $redirect_post_id );

if (
(
$redirect_post &&
'future' === get_post_status( $redirect_post )
)
||
(
$redirect_post &&
'attachment' === $redirect_post->post_type &&
$redirect_post->post_parent &&
'future' === get_post_status( $redirect_post->post_parent )
)
) {
return null;
}
}

if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
// Build the URL in the address bar.
$requested_url = is_ssl() ? 'https://' : 'http://';
Expand Down
5 changes: 5 additions & 0 deletions src/wp-includes/link-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ function wp_force_plain_post_permalink( $post = null, $sample = null ) {
if (
// Publicly viewable links never have plain permalinks.
is_post_status_viewable( $post_status_obj ) ||
(
// Scheduled posts use their future pretty permalink.
'future' === $post->post_status &&
is_post_type_viewable( $post_type_obj )
) ||
(
// Private posts don't have plain permalinks if the user can read them.
$post_status_obj->private &&
Expand Down
16 changes: 12 additions & 4 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7351,8 +7351,12 @@ function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
return;
}

// We're only concerned with published, non-hierarchical objects.
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) )
// We're only concerned with published or scheduled non-hierarchical objects.
if (
! (
in_array( $post->post_status, array( 'publish', 'future' ), true ) ||
( 'attachment' === $post->post_type && 'inherit' === $post->post_status )
)
|| is_post_type_hierarchical( $post->post_type )
) {
return;
Expand Down Expand Up @@ -7399,8 +7403,12 @@ function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
return;
}

// We're only concerned with published, non-hierarchical objects.
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) )
// We're only concerned with published or scheduled non-hierarchical objects.
if (
! (
in_array( $post->post_status, array( 'publish', 'future' ), true ) ||
( 'attachment' === $post->post_type && 'inherit' === $post->post_status )
)
|| is_post_type_hierarchical( $post->post_type )
) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,10 @@ function wp_old_slug_redirect() {
return;
}

if ( 'future' === get_post_status( $id ) ) {
return;
}

$link = get_permalink( $id );

if ( get_query_var( 'paged' ) > 1 ) {
Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/tests/canonical/postStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,19 @@ public function test_canonical_redirects_to_pretty_permalinks( $post_key, $user_
$this->assertCanonical( $requested, $expected );
}

/**
* @ticket 49871
*/
public function test_canonical_does_not_redirect_plain_permalink_for_scheduled_post() {
wp_set_current_user( 0 );
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );

$post = self::$posts['future'];
clean_post_cache( $post->ID );

$this->assertCanonical( '/?p=' . $post->ID, '/?p=' . $post->ID );
}

/**
* Data provider for test_canonical_redirects_to_pretty_permalinks.
*
Expand Down
32 changes: 14 additions & 18 deletions tests/phpunit/tests/link.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,30 @@ public function test_wp_get_shortlink_with_home_page() {

/**
* @ticket 30910
* @ticket 49871
*/
public function test_get_permalink_should_not_reveal_post_name_for_post_with_post_status_future() {
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );

flush_rewrite_rules();
public function test_get_permalink_should_return_pretty_permalink_for_post_with_post_status_future() {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );

$p = self::factory()->post->create(
array(
'post_status' => 'publish',
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
'post_status' => 'future',
'post_name' => 'message-from-the-past',
'post_date' => '2037-12-09 20:19:00',
)
);

$non_pretty_permalink = add_query_arg( 'p', $p, trailingslashit( home_url() ) );
$pretty_permalink = home_url( '/2037/12/09/message-from-the-past/' );

$this->assertSame( $non_pretty_permalink, get_permalink( $p ) );
$this->assertSame( $pretty_permalink, get_permalink( $p ) );
}

/**
* @ticket 30910
* @ticket 49871
*/
public function test_get_permalink_should_not_reveal_post_name_for_cpt_with_post_status_future() {
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
public function test_get_permalink_should_return_pretty_permalink_for_cpt_with_post_status_future() {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );

register_post_type( 'wptests_pt', array( 'public' => true ) );

Expand All @@ -109,19 +110,14 @@ public function test_get_permalink_should_not_reveal_post_name_for_cpt_with_post
array(
'post_status' => 'future',
'post_type' => 'wptests_pt',
'post_name' => 'future-cpt',
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
)
);

$non_pretty_permalink = add_query_arg(
array(
'post_type' => 'wptests_pt',
'p' => $p,
),
trailingslashit( home_url() )
);
$pretty_permalink = home_url( '/wptests_pt/future-cpt/' );

$this->assertSame( $non_pretty_permalink, get_permalink( $p ) );
$this->assertSame( $pretty_permalink, get_permalink( $p ) );
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,30 @@ public function test_get_item() {
$this->check_get_post_response( $response, 'view' );
}

/**
* @ticket 49871
*/
public function test_get_item_should_return_pretty_permalink_for_scheduled_post() {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
wp_set_current_user( self::$editor_id );

$post_id = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'message-from-the-past',
'post_date' => '2037-12-09 20:19:00',
)
);

$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
$request->set_param( 'context', 'edit' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 200, $response->get_status() );
$this->assertSame( home_url( '/2037/12/09/message-from-the-past/' ), $data['link'] );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 56481
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/tests/rewrite/oldDateRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,68 @@ public function test_old_date_redirect() {
$this->assertSame( $permalink, $this->old_date_redirect_url );
}

/**
* @ticket 49871
*/
public function test_old_date_redirect_should_not_redirect_scheduled_post() {
$post_id = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'scheduled-old-date',
'post_date' => '2037-12-09 20:19:00',
)
);

$old_permalink = user_trailingslashit( get_permalink( $post_id ) );
$time = '2037-12-10 20:19:00';

wp_update_post(
array(
'ID' => $post_id,
'post_date' => $time,
'post_date_gmt' => get_gmt_from_date( $time ),
)
);

$this->assertContains( '2037-12-09', get_post_meta( $post_id, '_wp_old_date' ) );

$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertNull( $this->old_date_redirect_url );
}

/**
* @ticket 49871
*/
public function test_old_date_redirect_should_redirect_scheduled_post_after_publication() {
$post_id = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'scheduled-publish-old-date',
'post_date' => '2037-12-09 20:19:00',
)
);

$old_permalink = user_trailingslashit( get_permalink( $post_id ) );
$time = '2037-12-10 20:19:00';

wp_update_post(
array(
'ID' => $post_id,
'post_date' => $time,
'post_date_gmt' => get_gmt_from_date( $time ),
)
);

wp_publish_post( $post_id );

$permalink = user_trailingslashit( get_permalink( $post_id ) );

$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertSame( $permalink, $this->old_date_redirect_url );
}

public function test_old_date_slug_redirect() {
$old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );

Expand Down
58 changes: 58 additions & 0 deletions tests/phpunit/tests/rewrite/oldSlugRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,64 @@ public function test_old_slug_redirect() {
$this->assertSame( $permalink, $this->old_slug_redirect_url );
}

/**
* @ticket 49871
*/
public function test_old_slug_redirect_should_not_redirect_scheduled_post() {
$post_id = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'scheduled-old-slug',
'post_date' => '2037-12-09 20:19:00',
)
);

$old_permalink = user_trailingslashit( get_permalink( $post_id ) );

wp_update_post(
array(
'ID' => $post_id,
'post_name' => 'scheduled-new-slug',
)
);

$this->assertContains( 'scheduled-old-slug', get_post_meta( $post_id, '_wp_old_slug' ) );

$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertNull( $this->old_slug_redirect_url );
}

/**
* @ticket 49871
*/
public function test_old_slug_redirect_should_redirect_scheduled_post_after_publication() {
$post_id = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'scheduled-publish-old-slug',
'post_date' => '2037-12-09 20:19:00',
)
);

$old_permalink = user_trailingslashit( get_permalink( $post_id ) );

wp_update_post(
array(
'ID' => $post_id,
'post_name' => 'scheduled-publish-new-slug',
)
);

wp_publish_post( $post_id );

$permalink = user_trailingslashit( get_permalink( $post_id ) );

$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertSame( $permalink, $this->old_slug_redirect_url );
}

/**
* @ticket 36723
*/
Expand Down
Loading