Changeset 56305 for branches/6.3/src/wp-includes/blocks/footnotes.php
- Timestamp:
- 07/25/2023 03:03:22 PM (17 months ago)
- Location:
- branches/6.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.3
-
branches/6.3/src/wp-includes/blocks/footnotes.php
r56127 r56305 8 8 /** 9 9 * Renders the `core/footnotes` block on the server. 10 * 11 * @since 6.3.0 10 12 * 11 13 * @param array $attributes Block attributes. … … 58 60 /** 59 61 * Registers the `core/footnotes` block on the server. 62 * 63 * @since 6.3.0 60 64 */ 61 65 function register_block_core_footnotes() { … … 79 83 } 80 84 add_action( 'init', 'register_block_core_footnotes' ); 85 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 * @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', $footnotes ); 158 } 159 } 160 } 161 } 162 163 foreach ( array( 'post', 'page' ) as $post_type ) { 164 add_action( "rest_after_insert_{$post_type}", 'wp_add_footnotes_revisions_to_post_meta' ); 165 } 166 167 /** 168 * Restores the footnotes meta value from the revision. 169 * 170 * @since 6.3.0 171 * 172 * @param int $post_id The post ID. 173 * @param int $revision_id The revision ID. 174 */ 175 function wp_restore_footnotes_from_revision( $post_id, $revision_id ) { 176 $footnotes = get_post_meta( $revision_id, 'footnotes', true ); 177 178 if ( $footnotes ) { 179 update_post_meta( $post_id, 'footnotes', $footnotes ); 180 } else { 181 delete_post_meta( $post_id, 'footnotes' ); 182 } 183 } 184 add_action( 'wp_restore_post_revision', 'wp_restore_footnotes_from_revision', 10, 2 ); 185 186 /** 187 * Adds the footnotes field to the revision. 188 * 189 * @since 6.3.0 190 * 191 * @param array $fields The revision fields. 192 * @return array The revision fields. 193 */ 194 function wp_add_footnotes_to_revision( $fields ) { 195 $fields['footnotes'] = __( 'Footnotes' ); 196 return $fields; 197 } 198 add_filter( '_wp_post_revision_fields', 'wp_add_footnotes_to_revision' ); 199 200 /** 201 * Gets the footnotes field from the revision. 202 * 203 * @since 6.3.0 204 * 205 * @param string $revision_field The field value, but $revision->$field 206 * (footnotes) does not exist. 207 * @param string $field The field name, in this case "footnotes". 208 * @param object $revision The revision object to compare against. 209 * @return string The field value. 210 */ 211 function wp_get_footnotes_from_revision( $revision_field, $field, $revision ) { 212 return get_metadata( 'post', $revision->ID, $field, true ); 213 } 214 add_filter( 'wp_post_revision_field_footnotes', 'wp_get_footnotes_from_revision', 10, 3 );
Note: See TracChangeset
for help on using the changeset viewer.