Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Footnotes: autosave is not slashing JSON #53664

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/block-library/src/footnotes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function wp_save_footnotes_meta( $revision_id ) {

if ( $footnotes ) {
// Can't use update_post_meta() because it doesn't allow revisions.
update_metadata( 'post', $revision_id, 'footnotes', $footnotes );
update_metadata( 'post', $revision_id, 'footnotes', wp_slash( $footnotes ) );
}
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ function wp_add_footnotes_revisions_to_post_meta( $post ) {

if ( $footnotes ) {
// Can't use update_post_meta() because it doesn't allow revisions.
update_metadata( 'post', $wp_temporary_footnote_revision_id, 'footnotes', $footnotes );
update_metadata( 'post', $wp_temporary_footnote_revision_id, 'footnotes', wp_slash( $footnotes ) );
}
}
}
Expand All @@ -176,7 +176,7 @@ function wp_restore_footnotes_from_revision( $post_id, $revision_id ) {
$footnotes = get_post_meta( $revision_id, 'footnotes', true );

if ( $footnotes ) {
update_post_meta( $post_id, 'footnotes', $footnotes );
update_post_meta( $post_id, 'footnotes', wp_slash( $footnotes ) );
} else {
delete_post_meta( $post_id, 'footnotes' );
}
Expand Down Expand Up @@ -243,7 +243,7 @@ function _wp_rest_api_autosave_meta( $autosave ) {
return;
}

update_post_meta( $id, 'footnotes', $body['meta']['footnotes'] );
update_post_meta( $id, 'footnotes', wp_slash( $body['meta']['footnotes'] ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto saving still triggers the count() error for me, but not when I add wp_slash() to the update in wp_save_footnotes_meta, e.g.,

update_metadata( 'post', $revision_id, 'footnotes', wp_slash( $footnotes ) );

I guess that's being called as well?

}
// See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L391C1-L391C1.
add_action( 'wp_creating_autosave', '_wp_rest_api_autosave_meta' );
Expand Down
34 changes: 28 additions & 6 deletions test/e2e/specs/editor/various/footnotes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ test.describe( 'Footnotes', () => {
await editor.clickBlockToolbarButton( 'More' );
await page.locator( 'button:text("Footnote")' ).click();

await page.keyboard.type( 'first footnote' );
// Check if content is correctly slashed on save and restore.
await page.keyboard.type( 'first footnote"' );

const id1 = await editor.canvas.evaluate( () => {
return document.activeElement.id;
Expand All @@ -316,7 +317,7 @@ test.describe( 'Footnotes', () => {
id: id2,
},
{
content: 'first footnote',
content: 'first footnote"',
id: id1,
},
] );
Expand All @@ -329,7 +330,7 @@ test.describe( 'Footnotes', () => {
// This also saves the post!
expect( await getFootnotes( page ) ).toMatchObject( [
{
content: 'first footnote',
content: 'first footnote"',
id: id1,
},
{
Expand All @@ -338,6 +339,16 @@ test.describe( 'Footnotes', () => {
},
] );

const editorPage = page;
const previewPage = await editor.openPreviewPage();

await expect(
previewPage.locator( 'ol.wp-block-footnotes' )
).toHaveText( 'first footnote” ↩︎second footnote ↩︎' );

await previewPage.close();
await editorPage.bringToFront();

// Open revisions.
await editor.openDocumentSettingsSidebar();
await page
Expand All @@ -355,10 +366,19 @@ test.describe( 'Footnotes', () => {
id: id2,
},
{
content: 'first footnote',
content: 'first footnote"',
id: id1,
},
] );

const previewPage2 = await editor.openPreviewPage();

await expect(
previewPage2.locator( 'ol.wp-block-footnotes' )
).toHaveText( 'second footnote ↩︎first footnote” ↩︎' );

await previewPage2.close();
await editorPage.bringToFront();
} );

test( 'can be previewed when published', async ( { editor, page } ) => {
Expand Down Expand Up @@ -392,12 +412,14 @@ test.describe( 'Footnotes', () => {
// path).
await editor.canvas.click( 'ol.wp-block-footnotes li span' );
await page.keyboard.press( 'End' );
await page.keyboard.type( '3' );
// Test slashing.
await page.keyboard.type( '3"' );

const previewPage2 = await editor.openPreviewPage();

// Note: quote will get curled by wptexturize.
await expect(
previewPage2.locator( 'ol.wp-block-footnotes li' )
).toHaveText( '123 ↩︎' );
).toHaveText( '123 ↩︎' );
} );
} );