Changeset 59523
- Timestamp:
- 12/17/2024 10:35:17 AM (2 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r59482 r59523 913 913 * Can be one of 'before', 'after', 'first_child', or 'last_child'. 914 914 * @param string $anchor_block_type The anchor block type. 915 * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,915 * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, 916 916 * or pattern that the anchor block belongs to. 917 917 */ … … 936 936 * @param string $relative_position The relative position of the hooked block. 937 937 * @param array $parsed_anchor_block The anchor block, in parsed block array format. 938 * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,938 * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, 939 939 * or pattern that the anchor block belongs to. 940 940 */ … … 952 952 * @param string $relative_position The relative position of the hooked block. 953 953 * @param array $parsed_anchor_block The anchor block, in parsed block array format. 954 * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,954 * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, 955 955 * or pattern that the anchor block belongs to. 956 956 */ … … 1040 1040 * @since 6.6.0 1041 1041 * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered. 1042 * @since 6.8.0 Have the `$context` parameter default to `null`, in which case `get_post()` will be called to use the current post as context. 1042 1043 * @access private 1043 1044 * 1044 * @param string $content Serialized content. 1045 * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, 1046 * or pattern that the blocks belong to. 1047 * @param callable $callback A function that will be called for each block to generate 1048 * the markup for a given list of blocks that are hooked to it. 1049 * Default: 'insert_hooked_blocks'. 1045 * @param string $content Serialized content. 1046 * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, or pattern 1047 * that the blocks belong to. If set to `null`, `get_post()` 1048 * will be called to use the current post as context. 1049 * Default: `null`. 1050 * @param callable $callback A function that will be called for each block to generate 1051 * the markup for a given list of blocks that are hooked to it. 1052 * Default: 'insert_hooked_blocks'. 1050 1053 * @return string The serialized markup. 1051 1054 */ 1052 function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) { 1055 function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) { 1056 // Default to the current post if no context is provided. 1057 if ( null === $context ) { 1058 $context = get_post(); 1059 } 1060 1053 1061 $hooked_blocks = get_hooked_blocks(); 1054 1062 … … 1166 1174 1167 1175 /** 1168 * Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.1169 * Currently only supports `wp_navigation` post types.1176 * Updates the wp_postmeta with the list of ignored hooked blocks 1177 * where the inner blocks are stored as post content. 1170 1178 * 1171 1179 * @since 6.6.0 1180 * @since 6.8.0 Support non-`wp_navigation` post types. 1172 1181 * @access private 1173 1182 * … … 1177 1186 function update_ignored_hooked_blocks_postmeta( $post ) { 1178 1187 /* 1179 * In this scenario the user has likely tried to create a n avigationvia the REST API.1188 * In this scenario the user has likely tried to create a new post object via the REST API. 1180 1189 * In which case we won't have a post ID to work with and store meta against. 1181 1190 */ … … 1185 1194 1186 1195 /* 1187 * Skip meta generation when consumers intentionally update specific Navigationfields1196 * Skip meta generation when consumers intentionally update specific fields 1188 1197 * and omit the content update. 1189 1198 */ … … 1193 1202 1194 1203 /* 1195 * Skip meta generation when the post content is not a navigation block.1204 * Skip meta generation if post type is not set. 1196 1205 */ 1197 if ( ! isset( $post->post_type ) || 'wp_navigation' !== $post->post_type) {1206 if ( ! isset( $post->post_type ) ) { 1198 1207 return $post; 1199 1208 } … … 1209 1218 } 1210 1219 1220 if ( 'wp_navigation' === $post->post_type ) { 1221 $wrapper_block_type = 'core/navigation'; 1222 } else { 1223 $wrapper_block_type = 'core/post-content'; 1224 } 1225 1211 1226 $markup = get_comment_delimited_block_content( 1212 'core/navigation',1227 $wrapper_block_type, 1213 1228 $attributes, 1214 1229 $post->post_content … … 1267 1282 1268 1283 /** 1269 * Hooks into the REST API response for the core/navigation blockand adds the first and last inner blocks.1284 * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks. 1270 1285 * 1271 1286 * @since 6.6.0 1287 * @since 6.8.0 Support non-`wp_navigation` post types. 1272 1288 * 1273 1289 * @param WP_REST_Response $response The response object. … … 1276 1292 */ 1277 1293 function insert_hooked_blocks_into_rest_response( $response, $post ) { 1278 if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) {1294 if ( empty( $response->data['content']['raw'] ) || empty( $response->data['content']['rendered'] ) ) { 1279 1295 return $response; 1280 1296 } … … 1288 1304 ); 1289 1305 } 1306 1307 if ( 'wp_navigation' === $post->post_type ) { 1308 $wrapper_block_type = 'core/navigation'; 1309 } else { 1310 $wrapper_block_type = 'core/post-content'; 1311 } 1312 1290 1313 $content = get_comment_delimited_block_content( 1291 'core/navigation',1314 $wrapper_block_type, 1292 1315 $attributes, 1293 1316 $response->data['content']['raw'] 1294 1317 ); 1295 1318 1296 $content = apply_block_hooks_to_content( $content, $post ); 1297 1298 // Remove mock Navigation block wrapper. 1319 $content = apply_block_hooks_to_content( 1320 $content, 1321 $post, 1322 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' 1323 ); 1324 1325 // Remove mock block wrapper. 1299 1326 $content = remove_serialized_parent_block( $content ); 1300 1327 1301 1328 $response->data['content']['raw'] = $content; 1329 1330 // `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter. 1331 $priority = has_filter( 'the_content', 'apply_block_hooks_to_content' ); 1332 if ( false !== $priority ) { 1333 remove_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); 1334 } 1302 1335 1303 1336 /** This filter is documented in wp-includes/post-template.php */ 1304 1337 $response->data['content']['rendered'] = apply_filters( 'the_content', $content ); 1338 1339 // Restore the filter if it was set initially. 1340 if ( false !== $priority ) { 1341 add_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); 1342 } 1305 1343 1306 1344 return $response; … … 1321 1359 * 1322 1360 * @param array $hooked_blocks An array of blocks hooked to another given block. 1323 * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation`post object,1361 * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object, 1324 1362 * or pattern that the blocks belong to. 1325 1363 * @param callable $callback A function that will be called for each block to generate … … 1378 1416 * 1379 1417 * @param array $hooked_blocks An array of blocks hooked to another block. 1380 * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation`post object,1418 * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object, 1381 1419 * or pattern that the blocks belong to. 1382 1420 * @param callable $callback A function that will be called for each block to generate -
trunk/src/wp-includes/default-filters.php
r59415 r59523 193 193 add_filter( 'the_title', 'trim' ); 194 194 195 add_filter( 'the_content', 'apply_block_hooks_to_content', 8 ); // BEFORE do_blocks(). 195 196 add_filter( 'the_content', 'do_blocks', 9 ); 196 197 add_filter( 'the_content', 'wptexturize' ); … … 761 762 762 763 // Update ignoredHookedBlocks postmeta for wp_navigation post type. 764 add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' ); 765 add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' ); 763 766 add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' ); 764 767 765 // Inject hooked blocks into the wp_navigation post type REST response. 768 // Inject hooked blocks into the Posts endpoint REST response for some given post types. 769 add_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response', 10, 2 ); 770 add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 ); 766 771 add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 ); 767 772 -
trunk/tests/phpunit/tests/blocks/applyBlockHooksToContent.php
r59124 r59523 93 93 94 94 /** 95 * @ticket 61074 96 */ 97 public function test_apply_block_hooks_to_content_with_context_set_to_null() { 98 $content = '<!-- wp:tests/anchor-block /-->'; 99 100 /* 101 * apply_block_hooks_to_content() will fall back to the global $post object (via get_post()) 102 * if the $context parameter is null. However, we'd also like to ensure that the function 103 * works as expected even when get_post() returns null. 104 */ 105 $this->assertNull( get_post() ); 106 107 $actual = apply_block_hooks_to_content( $content, null, 'insert_hooked_blocks' ); 108 $this->assertSame( 109 '<!-- wp:tests/anchor-block /--><!-- wp:tests/hooked-block /-->', 110 $actual 111 ); 112 } 113 114 /** 95 115 * @ticket 61902 96 116 */
Note: See TracChangeset
for help on using the changeset viewer.