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
11 changes: 10 additions & 1 deletion src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
* Can restore a past revision using all fields of the post revision, or only selected fields.
*
* @since 2.6.0
* @since 7.3.0 The `wp_after_insert_post` action fires after the `wp_restore_post_revision` action.
*
* @param int|WP_Post $revision Revision ID or revision object.
* @param array $fields Optional. What fields to restore from. Defaults to all.
Expand Down Expand Up @@ -501,7 +502,13 @@ function wp_restore_post_revision( $revision, $fields = null ) {

$update = wp_slash( $update ); // Since data is from DB.

$post_id = wp_update_post( $update );
$post_before = is_int( $revision['post_parent'] ) ? get_post( $revision['post_parent'] ) : null;

/*
* The after-insert hooks are fired below, once the meta is restored, so that
* the revision stored for this update holds the restored meta.
*/
$post_id = wp_update_post( $update, false, false );
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved

if ( ! $post_id || is_wp_error( $post_id ) ) {
return $post_id;
Expand All @@ -520,6 +527,8 @@ function wp_restore_post_revision( $revision, $fields = null ) {
*/
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );

wp_after_insert_post( $post_id, true, $post_before );

return $post_id;
}

Expand Down
101 changes: 101 additions & 0 deletions tests/e2e/specs/restore-revision-footnotes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Internal dependencies
*/
import { test, expect } from '../fixtures';

const FOOTNOTE_ID = '3a7c5f0e-8d1b-4f6a-9c2e-5b4d7e8f9a01';

/**
* Builds the content and the footnotes meta of a post with one footnote.
*
* @param {string} text Text of the paragraph.
* @param {string} footnote Text of the footnote.
* @return {{content: string, meta: {footnotes: string}}} Post data.
*/
function postWithFootnote( text, footnote ) {
return {
content: `<!-- wp:paragraph -->
<p>${ text }<sup data-fn="${ FOOTNOTE_ID }" class="fn"><a href="#${ FOOTNOTE_ID }" id="${ FOOTNOTE_ID }-link">1</a></sup></p>
<!-- /wp:paragraph -->

<!-- wp:footnotes /-->`,
meta: {
footnotes: JSON.stringify( [
{ id: FOOTNOTE_ID, content: footnote },
] ),
},
};
}

/**
* Gets the footnotes stored in a post or in a revision.
*
* @param {{meta: {footnotes: string}}} record Post or revision, as returned by the REST API.
* @return {string[]} Text of each footnote.
*/
function footnotesOf( record ) {
return JSON.parse( record.meta.footnotes || '[]' ).map(
( footnote ) => footnote.content
);
}

test.describe( 'Restoring a revision of a post with footnotes', () => {
test( 'stores the restored footnotes in the new revision', async ( {
page,
admin,
postFactory,
requestUtils,
} ) => {
const post = await postFactory.create( {
title: postFactory.uniqueTitle( 'Restore footnotes' ),
status: 'publish',
} );
const postPath = `/wp/v2/posts/${ post.id }`;

// Two saves, so that a revision holds each version of the footnote.
for ( const data of [
postWithFootnote( 'First text.', 'First footnote' ),
postWithFootnote( 'Second text.', 'Second footnote' ),
] ) {
await requestUtils.rest( { method: 'POST', path: postPath, data } );
}

const revisionsBefore = await requestUtils.rest( {
path: `${ postPath }/revisions`,
params: { context: 'edit' },
} );
const firstRevision = revisionsBefore.find( ( revision ) =>
revision.content.raw.includes( 'First text.' )
);
expect( footnotesOf( firstRevision ) ).toEqual( [ 'First footnote' ] );

await admin.visitAdminPage(
'revision.php',
`revision=${ firstRevision.id }`
);
await page
.getByRole( 'button', { name: 'Restore This Revision' } )
.click();
await page.waitForURL( /post\.php\?.*message=5/ );

const restoredPost = await requestUtils.rest( {
path: postPath,
params: { context: 'edit' },
} );
expect( restoredPost.content.raw ).toContain( 'First text.' );
expect( footnotesOf( restoredPost ) ).toEqual( [ 'First footnote' ] );

const revisionsAfter = await requestUtils.rest( {
path: `${ postPath }/revisions`,
params: { context: 'edit' },
} );
expect( revisionsAfter ).toHaveLength( revisionsBefore.length + 1 );

// The restore is recorded as a new revision, which has to match the post.
const latestRevision = revisionsAfter.reduce( ( latest, revision ) =>
revision.id > latest.id ? revision : latest
);
expect( latestRevision.content.raw ).toContain( 'First text.' );
expect( footnotesOf( latestRevision ) ).toEqual( [ 'First footnote' ] );
} );
} );
139 changes: 139 additions & 0 deletions tests/phpunit/tests/post/metaRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,4 +718,143 @@ public static function page_post_type_data_provider() {
),
);
}

/**
* Tests that the revision stored by a restore holds the restored meta.
*
* @ticket 66859
*
* @covers ::wp_restore_post_revision
*/
public function test_restoring_a_revision_stores_the_restored_meta_in_the_new_revision(): void {
add_filter( 'wp_post_revision_meta_keys', array( $this, 'add_revisioned_keys' ) );

$post_id = self::factory()->post->create();

update_post_meta( $post_id, 'meta_revision_test', 'first meta' );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'first content',
)
);

$first_revision = current( wp_get_post_revisions( $post_id ) );
$this->assertInstanceOf( WP_Post::class, $first_revision );

update_post_meta( $post_id, 'meta_revision_test', 'second meta' );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'second content',
)
);

$revisions_before = wp_get_post_revisions( $post_id );

wp_restore_post_revision( $first_revision->ID );

$revisions = wp_get_post_revisions( $post_id );
$latest_revision = current( $revisions );
$this->assertInstanceOf( WP_Post::class, $latest_revision );

$this->assertCount( count( $revisions_before ) + 1, $revisions, 'The restore should store one revision.' );
$this->assertSame( 'first content', $latest_revision->post_content, 'The new revision should hold the restored content.' );
$this->assertSame( 'first meta', get_post_meta( $latest_revision->ID, 'meta_revision_test', true ), 'The new revision should hold the restored meta.' );
}

/**
* Tests that restoring a revision that only differs from the post in its meta stores a revision.
*
* @ticket 66859
*
* @covers ::wp_restore_post_revision
*/
public function test_restoring_a_revision_that_only_differs_in_meta_stores_a_revision(): void {
add_filter( 'wp_post_revision_meta_keys', array( $this, 'add_revisioned_keys' ) );

$post_id = self::factory()->post->create();

update_post_meta( $post_id, 'meta_revision_test', 'first meta' );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'the content',
)
);

$first_revision = current( wp_get_post_revisions( $post_id ) );
$this->assertInstanceOf( WP_Post::class, $first_revision );

update_post_meta( $post_id, 'meta_revision_test', 'second meta' );
wp_update_post( array( 'ID' => $post_id ) );

$revisions_before = wp_get_post_revisions( $post_id );
$this->assertSame( 'second meta', get_post_meta( current( $revisions_before )->ID, 'meta_revision_test', true ) );

wp_restore_post_revision( $first_revision->ID );

$revisions = wp_get_post_revisions( $post_id );
$latest_revision = current( $revisions );
$this->assertInstanceOf( WP_Post::class, $latest_revision );

$this->assertSame( 'first meta', get_post_meta( $post_id, 'meta_revision_test', true ), 'The meta should be restored.' );
$this->assertCount( count( $revisions_before ) + 1, $revisions, 'The restore should store one revision.' );
$this->assertSame( 'first meta', get_post_meta( $latest_revision->ID, 'meta_revision_test', true ), 'The new revision should hold the restored meta.' );
}

/**
* Tests that the `wp_after_insert_post` action fires once, after the meta is restored.
*
* @ticket 66859
*
* @covers ::wp_restore_post_revision
*/
public function test_restoring_a_revision_fires_wp_after_insert_post_after_the_meta_is_restored(): void {
add_filter( 'wp_post_revision_meta_keys', array( $this, 'add_revisioned_keys' ) );

$post_id = self::factory()->post->create();

update_post_meta( $post_id, 'meta_revision_test', 'first meta' );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'first content',
)
);

$first_revision = current( wp_get_post_revisions( $post_id ) );
$this->assertInstanceOf( WP_Post::class, $first_revision );

update_post_meta( $post_id, 'meta_revision_test', 'second meta' );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'second content',
)
);

$meta_seen = array();
$post_before = null;
add_action(
'wp_after_insert_post',
static function ( $inserted_post_id, $post, $update, $before ) use ( $post_id, &$meta_seen, &$post_before ) {
// The action also fires for the revision that the restore stores.
if ( $inserted_post_id !== $post_id ) {
return;
}

$meta_seen[] = get_post_meta( $inserted_post_id, 'meta_revision_test', true );
$post_before = $before;
},
10,
4
);

wp_restore_post_revision( $first_revision->ID );

$this->assertSame( array( 'first meta' ), $meta_seen, 'The action should fire once, with the meta restored.' );
$this->assertInstanceOf( WP_Post::class, $post_before );
$this->assertSame( 'second content', $post_before->post_content, 'The action should receive the post from before the restore.' );
}
}
Loading