@@ -83,141 +83,130 @@ function register_block_core_footnotes() {
8383}
8484add_action ( 'init ' , 'register_block_core_footnotes ' );
8585
86- add_action (
87- 'wp_after_insert_post ' ,
88- /**
89- * Saves the footnotes meta value to the revision.
90- *
91- * @since 6.3.0
92- *
93- * @param int $revision_id The revision ID.
94- */
95- static function ( $ revision_id ) {
96- $ post_id = wp_is_post_revision ( $ revision_id );
97-
98- if ( $ post_id ) {
86+ /**
87+ * Saves the footnotes meta value to the revision.
88+ *
89+ * @since 6.3.0
90+ *
91+ * @param int $revision_id The revision ID.
92+ */
93+ function wp_save_footnotes_meta ( $ revision_id ) {
94+ $ post_id = wp_is_post_revision ( $ revision_id );
95+
96+ if ( $ post_id ) {
97+ $ footnotes = get_post_meta ( $ post_id , 'footnotes ' , true );
98+
99+ if ( $ footnotes ) {
100+ // Can't use update_post_meta() because it doesn't allow revisions.
101+ update_metadata ( 'post ' , $ revision_id , 'footnotes ' , $ footnotes );
102+ }
103+ }
104+ }
105+ add_action ( 'wp_after_insert_post ' , 'wp_save_footnotes_meta ' );
106+
107+ /**
108+ * Keeps track of the revision ID for "rest_after_insert_{$post_type}".
109+ *
110+ * @since 6.3.0
111+ *
112+ * @param int $revision_id The revision ID.
113+ */
114+ function wp_keep_revision_id ( $ revision_id ) {
115+ global $ wp_temporary_footnote_revision_id ;
116+ $ wp_temporary_footnote_revision_id = $ revision_id ;
117+ }
118+ add_action ( '_wp_put_post_revision ' , 'wp_keep_revision_id ' );
119+
120+ /**
121+ * This is a specific fix for the REST API. The REST API doesn't update
122+ * the post and post meta in one go (through `meta_input`). While it
123+ * does fix the `wp_after_insert_post` hook to be called correctly after
124+ * updating meta, it does NOT fix hooks such as post_updated and
125+ * save_post, which are normally also fired after post meta is updated
126+ * in `wp_insert_post()`. Unfortunately, `wp_save_post_revision` is
127+ * added to the `post_updated` action, which means the meta is not
128+ * available at the time, so we have to add it afterwards through the
129+ * `"rest_after_insert_{$post_type}"` action.
130+ *
131+ * @since 6.3.0
132+ *
133+ * @global int $wp_temporary_footnote_revision_id The footnote revision ID.
134+ *
135+ * @param WP_Post $post The post object.
136+ */
137+ function wp_add_revisions_to_post_meta ( $ post ) {
138+ global $ wp_temporary_footnote_revision_id ;
139+
140+ if ( $ wp_temporary_footnote_revision_id ) {
141+ $ revision = get_post ( $ wp_temporary_footnote_revision_id );
142+
143+ if ( ! $ revision ) {
144+ return ;
145+ }
146+
147+ $ post_id = $ revision ->post_parent ;
148+
149+ // Just making sure we're updating the right revision.
150+ if ( $ post ->ID === $ post_id ) {
99151 $ footnotes = get_post_meta ( $ post_id , 'footnotes ' , true );
100152
101153 if ( $ footnotes ) {
102154 // Can't use update_post_meta() because it doesn't allow revisions.
103- update_metadata ( 'post ' , $ revision_id , 'footnotes ' , $ footnotes );
155+ update_metadata ( 'post ' , $ wp_temporary_footnote_revision_id , 'footnotes ' , $ footnotes );
104156 }
105157 }
106158 }
107- );
108-
109- add_action (
110- '_wp_put_post_revision ' ,
111- /**
112- * Keeps track of the revision ID for "rest_after_insert_{$post_type}".
113- *
114- * @param int $revision_id The revision ID.
115- */
116- static function ( $ revision_id ) {
117- global $ _gutenberg_revision_id ;
118- $ _gutenberg_revision_id = $ revision_id ;
119- }
120- );
159+ }
121160
122161foreach ( array ( 'post ' , 'page ' ) as $ post_type ) {
123- add_action (
124- "rest_after_insert_ {$ post_type }" ,
125- /**
126- * This is a specific fix for the REST API. The REST API doesn't update
127- * the post and post meta in one go (through `meta_input`). While it
128- * does fix the `wp_after_insert_post` hook to be called correctly after
129- * updating meta, it does NOT fix hooks such as post_updated and
130- * save_post, which are normally also fired after post meta is updated
131- * in `wp_insert_post()`. Unfortunately, `wp_save_post_revision` is
132- * added to the `post_updated` action, which means the meta is not
133- * available at the time, so we have to add it afterwards through the
134- * `"rest_after_insert_{$post_type}"` action.
135- *
136- * @since 6.3.0
137- *
138- * @param WP_Post $post The post object.
139- */
140- static function ( $ post ) {
141- global $ _gutenberg_revision_id ;
142-
143- if ( $ _gutenberg_revision_id ) {
144- $ revision = get_post ( $ _gutenberg_revision_id );
145-
146- if ( ! $ revision ) {
147- return ;
148- }
149-
150- $ post_id = $ revision ->post_parent ;
151-
152- // Just making sure we're updating the right revision.
153- if ( $ post ->ID === $ post_id ) {
154- $ footnotes = get_post_meta ( $ post_id , 'footnotes ' , true );
155-
156- if ( $ footnotes ) {
157- // Can't use update_post_meta() because it doesn't allow revisions.
158- update_metadata ( 'post ' , $ _gutenberg_revision_id , 'footnotes ' , $ footnotes );
159- }
160- }
161- }
162- }
163- );
162+ add_action ( "rest_after_insert_ {$ post_type }" , 'wp_add_revisions_to_post_meta ' );
164163}
165164
166- add_action (
167- 'wp_restore_post_revision ' ,
168- /**
169- * Restores the footnotes meta value from the revision.
170- *
171- * @since 6.3.0
172- *
173- * @param int $post_id The post ID.
174- * @param int $revision_id The revision ID.
175- */
176- static function ( $ post_id , $ revision_id ) {
177- $ footnotes = get_post_meta ( $ revision_id , 'footnotes ' , true );
165+ /**
166+ * Restores the footnotes meta value from the revision.
167+ *
168+ * @since 6.3.0
169+ *
170+ * @param int $post_id The post ID.
171+ * @param int $revision_id The revision ID.
172+ */
173+ function wp_restore_footnotes_from_revision ( $ post_id , $ revision_id ) {
174+ $ footnotes = get_post_meta ( $ revision_id , 'footnotes ' , true );
178175
179- if ( $ footnotes ) {
180- update_post_meta ( $ post_id , 'footnotes ' , $ footnotes );
181- } else {
182- delete_post_meta ( $ post_id , 'footnotes ' );
183- }
184- },
185- 10 ,
186- 2
187- );
188-
189- add_filter (
190- '_wp_post_revision_fields ' ,
191- /**
192- * Adds the footnotes field to the revision.
193- *
194- * @since 6.3.0
195- *
196- * @param array $fields The revision fields.
197- * @return array The revision fields.
198- */
199- static function ( $ fields ) {
200- $ fields ['footnotes ' ] = __ ( 'Footnotes ' );
201- return $ fields ;
176+ if ( $ footnotes ) {
177+ update_post_meta ( $ post_id , 'footnotes ' , $ footnotes );
178+ } else {
179+ delete_post_meta ( $ post_id , 'footnotes ' );
202180 }
203- );
204-
205- add_filter (
206- 'wp_post_revision_field_footnotes ' ,
207- /**
208- * Gets the footnotes field from the revision.
209- *
210- * @since 6.3.0
211- *
212- * @param string $revision_field The field value, but $revision->$field
213- * (footnotes) does not exist.
214- * @param string $field The field name, in this case "footnotes".
215- * @param object $revision The revision object to compare against.
216- * @return string The field value.
217- */
218- static function ( $ revision_field , $ field , $ revision ) {
219- return get_metadata ( 'post ' , $ revision ->ID , $ field , true );
220- },
221- 10 ,
222- 3
223- );
181+ }
182+ add_action ( 'wp_restore_post_revision ' , 'wp_restore_footnotes_from_revision ' , 10 , 2 );
183+
184+ /**
185+ * Adds the footnotes field to the revision.
186+ *
187+ * @since 6.3.0
188+ *
189+ * @param array $fields The revision fields.
190+ * @return array The revision fields.
191+ */
192+ function wp_add_footnotes_to_revision ( $ fields ) {
193+ $ fields ['footnotes ' ] = __ ( 'Footnotes ' );
194+ return $ fields ;
195+ }
196+ add_filter ( '_wp_post_revision_fields ' , 'wp_add_footnotes_to_revision ' );
197+
198+ /**
199+ * Gets the footnotes field from the revision.
200+ *
201+ * @since 6.3.0
202+ *
203+ * @param string $revision_field The field value, but $revision->$field
204+ * (footnotes) does not exist.
205+ * @param string $field The field name, in this case "footnotes".
206+ * @param object $revision The revision object to compare against.
207+ * @return string The field value.
208+ */
209+ function wp_get_footnotes_from_revision ( $ revision_field , $ field , $ revision ) {
210+ return get_metadata ( 'post ' , $ revision ->ID , $ field , true );
211+ }
212+ add_filter ( 'wp_post_revision_field_footnotes ' , 'wp_get_footnotes_from_revision ' , 10 , 3 );
0 commit comments