Changeset 56755 for trunk/src/wp-includes/blocks/footnotes.php
- Timestamp:
- 10/02/2023 06:55:27 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/footnotes.php
r56419 r56755 40 40 41 41 $wrapper_attributes = get_block_wrapper_attributes(); 42 $footnote_index = 1; 42 43 43 44 $block_content = ''; 44 45 45 46 foreach ( $footnotes as $footnote ) { 47 // Translators: %d: Integer representing the number of return links on the page. 48 $aria_label = sprintf( __( 'Jump to footnote reference %1$d' ), $footnote_index ); 46 49 $block_content .= sprintf( 47 '<li id="%1$s">%2$s <a href="#%1$s-link" >↩︎</a></li>',50 '<li id="%1$s">%2$s <a href="#%1$s-link" aria-label="%3$s">↩︎</a></li>', 48 51 $footnote['id'], 49 $footnote['content'] 52 $footnote['content'], 53 $aria_label 50 54 ); 55 ++$footnote_index; 51 56 } 52 57 … … 69 74 'footnotes', 70 75 array( 71 'show_in_rest' => true, 72 'single' => true, 73 'type' => 'string', 76 'show_in_rest' => true, 77 'single' => true, 78 'type' => 'string', 79 'revisions_enabled' => true, 74 80 ) 75 81 ); … … 85 91 86 92 /** 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', wp_slash( $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 * @global int $wp_temporary_footnote_revision_id The footnote revision ID. 113 * 114 * @param int $revision_id The revision ID. 115 */ 116 function wp_keep_footnotes_revision_id( $revision_id ) { 117 global $wp_temporary_footnote_revision_id; 118 $wp_temporary_footnote_revision_id = $revision_id; 119 } 120 add_action( '_wp_put_post_revision', 'wp_keep_footnotes_revision_id' ); 121 122 /** 123 * This is a specific fix for the REST API. The REST API doesn't update 124 * the post and post meta in one go (through `meta_input`). While it 125 * does fix the `wp_after_insert_post` hook to be called correctly after 126 * updating meta, it does NOT fix hooks such as post_updated and 127 * save_post, which are normally also fired after post meta is updated 128 * in `wp_insert_post()`. Unfortunately, `wp_save_post_revision` is 129 * added to the `post_updated` action, which means the meta is not 130 * available at the time, so we have to add it afterwards through the 131 * `"rest_after_insert_{$post_type}"` action. 132 * 133 * @since 6.3.0 134 * 135 * @global int $wp_temporary_footnote_revision_id The footnote revision ID. 136 * 137 * @param WP_Post $post The post object. 138 */ 139 function wp_add_footnotes_revisions_to_post_meta( $post ) { 140 global $wp_temporary_footnote_revision_id; 141 142 if ( $wp_temporary_footnote_revision_id ) { 143 $revision = get_post( $wp_temporary_footnote_revision_id ); 144 145 if ( ! $revision ) { 146 return; 147 } 148 149 $post_id = $revision->post_parent; 150 151 // Just making sure we're updating the right revision. 152 if ( $post->ID === $post_id ) { 153 $footnotes = get_post_meta( $post_id, 'footnotes', true ); 154 155 if ( $footnotes ) { 156 // Can't use update_post_meta() because it doesn't allow revisions. 157 update_metadata( 'post', $wp_temporary_footnote_revision_id, 'footnotes', wp_slash( $footnotes ) ); 158 } 159 } 160 } 161 } 162 163 add_action( 'rest_after_insert_post', 'wp_add_footnotes_revisions_to_post_meta' ); 164 add_action( 'rest_after_insert_page', 'wp_add_footnotes_revisions_to_post_meta' ); 165 166 /** 167 * Restores the footnotes meta value from the revision. 168 * 169 * @since 6.3.0 170 * 171 * @param int $post_id The post ID. 172 * @param int $revision_id The revision ID. 173 */ 174 function wp_restore_footnotes_from_revision( $post_id, $revision_id ) { 175 $footnotes = get_post_meta( $revision_id, 'footnotes', true ); 176 177 if ( $footnotes ) { 178 update_post_meta( $post_id, 'footnotes', wp_slash( $footnotes ) ); 179 } else { 180 delete_post_meta( $post_id, 'footnotes' ); 181 } 182 } 183 add_action( 'wp_restore_post_revision', 'wp_restore_footnotes_from_revision', 10, 2 ); 184 185 /** 186 * Adds the footnotes field to the revision. 93 * Adds the footnotes field to the revisions display. 187 94 * 188 95 * @since 6.3.0 … … 198 105 199 106 /** 200 * Gets the footnotes field from the revision .107 * Gets the footnotes field from the revision for the revisions screen. 201 108 * 202 109 * @since 6.3.0 … … 212 119 } 213 120 add_filter( '_wp_post_revision_field_footnotes', 'wp_get_footnotes_from_revision', 10, 3 ); 214 215 /**216 * The REST API autosave endpoint doesn't save meta, so we can use the217 * `wp_creating_autosave` when it updates an exiting autosave, and218 * `_wp_put_post_revision` when it creates a new autosave.219 *220 * @since 6.3.0221 *222 * @param int|array $autosave The autosave ID or array.223 */224 function _wp_rest_api_autosave_meta( $autosave ) {225 // Ensure it's a REST API request.226 if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {227 return;228 }229 230 $body = rest_get_server()->get_raw_data();231 $body = json_decode( $body, true );232 233 if ( ! isset( $body['meta']['footnotes'] ) ) {234 return;235 }236 237 // `wp_creating_autosave` passes the array,238 // `_wp_put_post_revision` passes the ID.239 $id = is_int( $autosave ) ? $autosave : $autosave['ID'];240 241 if ( ! $id ) {242 return;243 }244 245 update_post_meta( $id, 'footnotes', wp_slash( $body['meta']['footnotes'] ) );246 }247 // See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L391C1-L391C1.248 add_action( 'wp_creating_autosave', '_wp_rest_api_autosave_meta' );249 // See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L398.250 // Then https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/revision.php#L367.251 add_action( '_wp_put_post_revision', '_wp_rest_api_autosave_meta' );252 253 /**254 * This is a workaround for the autosave endpoint returning early if the255 * revision field are equal. The problem is that "footnotes" is not real256 * revision post field, so there's nothing to compare against.257 *258 * This trick sets the "footnotes" field (value doesn't matter), which will259 * cause the autosave endpoint to always update the latest revision. That should260 * be fine, it should be ok to update the revision even if nothing changed. Of261 * course, this is temporary fix.262 *263 * @since 6.3.0264 *265 * @param WP_Post $prepared_post The prepared post object.266 * @param WP_REST_Request $request The request object.267 *268 * See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L365-L384.269 * See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L219.270 */271 function _wp_rest_api_force_autosave_difference( $prepared_post, $request ) {272 // We only want to be altering POST requests.273 if ( $request->get_method() !== 'POST' ) {274 return $prepared_post;275 }276 277 // Only alter requests for the '/autosaves' route.278 if ( substr( $request->get_route(), -strlen( '/autosaves' ) ) !== '/autosaves' ) {279 return $prepared_post;280 }281 282 $prepared_post->footnotes = '[]';283 return $prepared_post;284 }285 286 add_filter( 'rest_pre_insert_post', '_wp_rest_api_force_autosave_difference', 10, 2 );
Note: See TracChangeset
for help on using the changeset viewer.