-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Update @wordpress
packages for 6.3 RC2
#4892
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
/** | ||
* Renders the `core/footnotes` block on the server. | ||
* | ||
* @since 6.3.0 | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
|
@@ -57,6 +59,8 @@ function render_block_core_footnotes( $attributes, $content, $block ) { | |
|
||
/** | ||
* Registers the `core/footnotes` block on the server. | ||
* | ||
* @since 6.3.0 | ||
*/ | ||
function register_block_core_footnotes() { | ||
foreach ( array( 'post', 'page' ) as $post_type ) { | ||
|
@@ -78,3 +82,137 @@ function register_block_core_footnotes() { | |
); | ||
} | ||
add_action( 'init', 'register_block_core_footnotes' ); | ||
|
||
add_action( | ||
'wp_after_insert_post', | ||
/** | ||
* Saves the footnotes meta value to the revision. | ||
* | ||
* @since 6.3.0 | ||
* | ||
* @param int $revision_id The revision ID. | ||
*/ | ||
static function( $revision_id ) { | ||
$post_id = wp_is_post_revision( $revision_id ); | ||
|
||
if ( $post_id ) { | ||
$footnotes = get_post_meta( $post_id, 'footnotes', true ); | ||
|
||
if ( $footnotes ) { | ||
// Can't use update_post_meta() because it doesn't allow revisions. | ||
update_metadata( 'post', $revision_id, 'footnotes', $footnotes ); | ||
} | ||
} | ||
} | ||
); | ||
|
||
add_action( | ||
'_wp_put_post_revision', | ||
/** | ||
* Keeps track of the revision ID for "rest_after_insert_{$post_type}". | ||
* | ||
* @param int $revision_id The revision ID. | ||
*/ | ||
static function( $revision_id ) { | ||
global $_gutenberg_revision_id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also quite a hacky workaround in general having to use such a global here :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand this is a temporary situation until revisions properly support post meta in core. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Open for any better workarounds. It's quite late now to be adding general post meta revision support or start changing the REST API hook order. |
||
$_gutenberg_revision_id = $revision_id; | ||
} | ||
); | ||
|
||
foreach ( array( 'post', 'page' ) as $post_type ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this support all post types using the block editor and/or have revision support enabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this conversation / issue may be related: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
add_action( | ||
"rest_after_insert_{$post_type}", | ||
/** | ||
* This is a specific fix for the REST API. The REST API doesn't update | ||
* the post and post meta in one go (through `meta_input`). While it | ||
* does fix the `wp_after_insert_post` hook to be called correctly after | ||
* updating meta, it does NOT fix hooks such as post_updated and | ||
* save_post, which are normally also fired after post meta is updated | ||
* in `wp_insert_post()`. Unfortunately, `wp_save_post_revision` is | ||
* added to the `post_updated` action, which means the meta is not | ||
* available at the time, so we have to add it afterwards through the | ||
* `"rest_after_insert_{$post_type}"` action. | ||
* | ||
* @since 6.3.0 | ||
* | ||
* @param WP_Post $post The post object. | ||
*/ | ||
static function( $post ) { | ||
global $_gutenberg_revision_id; | ||
|
||
if ( $_gutenberg_revision_id ) { | ||
$revision = get_post( $_gutenberg_revision_id ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we should use wp_get_post_revision here? It implements Well, namely if ( 'revision' !== $revision->post_type ) {
return null;
} 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answered here: WordPress/gutenberg#52870 (comment) |
||
$post_id = $revision->post_parent; | ||
|
||
// Just making sure we're updating the right revision. | ||
if ( $post->ID === $post_id ) { | ||
$footnotes = get_post_meta( $post_id, 'footnotes', true ); | ||
|
||
if ( $footnotes ) { | ||
// Can't use update_post_meta() because it doesn't allow revisions. | ||
update_metadata( 'post', $_gutenberg_revision_id, 'footnotes', $footnotes ); | ||
} | ||
} | ||
} | ||
} | ||
); | ||
} | ||
|
||
add_action( | ||
'wp_restore_post_revision', | ||
/** | ||
* Restores the footnotes meta value from the revision. | ||
* | ||
* @since 6.3.0 | ||
* | ||
* @param int $post_id The post ID. | ||
* @param int $revision_id The revision ID. | ||
audrasjb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
static function( $post_id, $revision_id ) { | ||
$footnotes = get_post_meta( $revision_id, 'footnotes', true ); | ||
|
||
if ( $footnotes ) { | ||
update_post_meta( $post_id, 'footnotes', $footnotes ); | ||
} else { | ||
delete_post_meta( $post_id, 'footnotes' ); | ||
} | ||
}, | ||
10, | ||
2 | ||
); | ||
|
||
add_filter( | ||
'_wp_post_revision_fields', | ||
/** | ||
* Adds the footnotes field to the revision. | ||
* | ||
* @since 6.3.0 | ||
* | ||
* @param array $fields The revision fields. | ||
* @return array The revision fields. | ||
*/ | ||
static function( $fields ) { | ||
$fields['footnotes'] = __( 'Footnotes' ); | ||
return $fields; | ||
} | ||
); | ||
|
||
add_filter( | ||
'wp_post_revision_field_footnotes', | ||
/** | ||
* Gets the footnotes field from the revision. | ||
* | ||
* @since 6.3.0 | ||
* | ||
* @param string $revision_field The field value, but $revision->$field | ||
* (footnotes) does not exist. | ||
* @param string $field The field name, in this case "footnotes". | ||
* @param object $revision The revision object to compare against. | ||
* @return string The field value. | ||
*/ | ||
static function( $revision_field, $field, $revision ) { | ||
return get_metadata( 'post', $revision->ID, $field, true ); | ||
}, | ||
10, | ||
3 | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's missing a since declaration here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so anonymous functions should also receive
@since
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these all anonymous functions in the first place?
Makes it impossible for developers to unhook them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally like anonymous functions for handling the footnote meta because it makes the way to move to handling postmeta revisions in a more standard way in 6.4.
Unless you think we'd still want the named functions after doing so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would indeed be an argument for keeping them, but not sure if that's the reason for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is a temporary fix and to avoid having to rename/naming clashes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this seem like an appropriate reason for using anonymous functions to you, @swissspidy ? (+cc: @peterwilsoncc and @joemcgill ).
If not, do you think we should continue discussion about this in the existing issue about revision support or open a new issue?
It sounded like there may be platform considerations for using named functions here, and I want to make sure those are well understood.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, yes