Skip to content

Commit

Permalink
Footnotes: store in revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jul 17, 2023
1 parent 5e44d3d commit 35816cd
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion packages/block-library/src/footnotes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function render_block_core_footnotes( $attributes, $content, $block ) {
* Registers the `core/footnotes` block on the server.
*/
function register_block_core_footnotes() {
foreach ( array( 'post', 'page' ) as $post_type ) {
foreach ( array( 'post', 'page', 'revision' ) as $post_type ) {
register_post_meta(
$post_type,
'footnotes',
Expand All @@ -78,3 +78,45 @@ function register_block_core_footnotes() {
);
}
add_action( 'init', 'register_block_core_footnotes' );

// Revisions must be created after post meta is updated, otherwise we don't have
// access to the meta.
remove_action( 'post_updated', 'wp_save_post_revision', 10, 1 );
add_action( 'wp_after_insert_post', 'wp_save_post_revision', 10, 1 );

function gutenberg_save_footnotes_meta( $revision_id ) {

Check failure on line 87 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function gutenberg_save_footnotes_meta()
$post_id = wp_is_post_revision( $revision_id );

Check failure on line 88 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

if ( $post_id ) {

Check failure on line 90 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
$footnotes = get_post_meta( $post_id, 'footnotes', true );

Check failure on line 91 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

if ( $footnotes ) {

Check failure on line 93 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
// Can't use update_post_meta() because it doesn't allow revisions.
update_metadata( 'post', $revision_id, 'footnotes', $footnotes );

Check failure on line 95 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
}

Check failure on line 96 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
}

Check failure on line 97 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
}
add_action( 'wp_after_insert_post', 'gutenberg_save_footnotes_meta' );

function gutenberg_restore_footnotes_meta( $post_id, $revision_id ) {

Check failure on line 101 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function gutenberg_restore_footnotes_meta()
$footnotes = get_post_meta( $revision_id, 'footnotes', true );

Check failure on line 102 in packages/block-library/src/footnotes/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

if ( $footnotes ) {
update_post_meta( $post_id, 'footnotes', $footnotes );
} else {
delete_post_meta( $post_id, 'footnotes' );
}
}
add_action( 'wp_restore_post_revision', 'gutenberg_restore_footnotes_meta', 10, 2 );

function gutenberg_revision_fields( $fields ) {
$fields['footnotes'] = __( 'Footnotes' );
return $fields;
}
add_filter( '_wp_post_revision_fields', 'gutenberg_revision_fields' );

function gutenberg_revision_field_footnotes( $value, $field ) {
global $revision;
return get_metadata( 'post', $revision->ID, $field, true );
}
add_filter( 'wp_post_revision_field_footnotes', 'gutenberg_revision_field_footnotes', 10, 2 );

0 comments on commit 35816cd

Please sign in to comment.