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
5 changes: 5 additions & 0 deletions admin-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,11 @@ function duplicate_post_create_duplicate( $post, $status = '', $parent_id = '' )

delete_post_meta( $new_post_id, '_dp_original' );
add_post_meta( $new_post_id, '_dp_original', $post->ID );

// Sticky is only a feature of the post post type.
if ( $post->post_type === 'post' && is_sticky( $post->ID ) ) {
stick_post( $new_post_id );
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/post-duplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public function create_duplicate( WP_Post $post, array $options = [] ) {
if ( ! \is_wp_error( $new_post_id ) ) {
\delete_post_meta( $new_post_id, '_dp_original' );
\add_post_meta( $new_post_id, '_dp_original', $post->ID );

// Sticky is only a feature of the post post type.
if ( $post->post_type === 'post' && \is_sticky( $post->ID ) ) {
\stick_post( $new_post_id );
}
}

return $new_post_id;
Expand Down
33 changes: 33 additions & 0 deletions tests/WP/Legacy_Duplication_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,39 @@ public function test_clone_of_clone_points_to_direct_source() {
$this->assertSame( $first_copy_id, (int) \get_post_meta( $second_copy_id, '_dp_original', true ) );
}

/**
* Tests that the sticky setting is copied to the duplicate.
*
* @covers ::duplicate_post_create_duplicate
*
* @return void
*/
public function test_copies_sticky_state() {
$original = $this->create_original_post();
\stick_post( $original->ID );

$new_id = \duplicate_post_create_duplicate( $original );

$this->assertIsInt( $new_id );
$this->assertTrue( \is_sticky( $new_id ) );
}

/**
* Tests that a non sticky post does not produce a sticky duplicate.
*
* @covers ::duplicate_post_create_duplicate
*
* @return void
*/
public function test_does_not_stick_duplicate_of_non_sticky_post() {
$original = $this->create_original_post();

$new_id = \duplicate_post_create_duplicate( $original );

$this->assertIsInt( $new_id );
$this->assertFalse( \is_sticky( $new_id ) );
}

/**
* Tests that a disabled copystatus setting forces draft even when a status is passed.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/WP/Post_Republisher_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,42 @@ public function test_create_duplicate_for_rewrite_and_republish_copies_content()
$this->assertSame( 'draft', $copy->post_status );
}

/**
* Tests that create_duplicate_for_rewrite_and_republish copies sticky state.
*
* @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::create_duplicate_for_rewrite_and_republish
* @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::create_duplicate
*
* @return void
*/
public function test_create_duplicate_for_rewrite_and_republish_copies_sticky_state() {
$original = $this->create_original_post();
\stick_post( $original->ID );

$copy = $this->create_rewrite_and_republish_copy( $original );

$this->assertTrue( \is_sticky( $copy->ID ) );
}

/**
* Tests that republish does not clear the original post sticky state.
*
* @covers ::republish
* @covers ::republish_post_elements
*
* @return void
*/
public function test_republish_preserves_original_sticky_state() {
$original = $this->create_original_post();
\stick_post( $original->ID );

$copy = $this->create_rewrite_and_republish_copy( $original );

$this->instance->republish( $copy, $original );

$this->assertTrue( \is_sticky( $original->ID ) );
}

/**
* Tests that create_duplicate_for_rewrite_and_republish copies taxonomies.
*
Expand Down