Changeset 57802
- Timestamp:
- 03/11/2024 02:12:20 PM (7 months ago)
- Location:
- branches/6.5
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.5/src/wp-includes/block-template-utils.php
r57771 r57802 1433 1433 return $template_hierarchy; 1434 1434 } 1435 1435 1436 /** 1436 1437 * Inject ignoredHookedBlocks metadata attributes into a template or template part. 1437 1438 * 1438 * Given a `wp_template` or `wp_template_part` post object, locate all blocks that have 1439 * Given an object that represents a `wp_template` or `wp_template_part` post object 1440 * prepared for inserting or updating the database, locate all blocks that have 1439 1441 * hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor 1440 1442 * blocks to reflect the latter. 1441 1443 * 1442 * @param WP_Post $post A post object with post type set to `wp_template` or `wp_template_part`. 1443 * @return WP_Post The updated post object. 1444 */ 1445 function inject_ignored_hooked_blocks_metadata_attributes( $post ) { 1444 * @since 6.5.0 1445 * @access private 1446 * 1447 * @param stdClass $post An object representing a template or template part 1448 * prepared for inserting or updating the database. 1449 * @param WP_REST_Request $request Request object. 1450 * @return stdClass The updated object representing a template or template part. 1451 */ 1452 function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { 1453 $filter_name = current_filter(); 1454 if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) { 1455 return $post; 1456 } 1457 $post_type = str_replace( 'rest_pre_insert_', '', $filter_name ); 1458 1446 1459 $hooked_blocks = get_hooked_blocks(); 1447 1460 if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { 1448 return ;1461 return $post; 1449 1462 } 1450 1463 … … 1453 1466 // To that end, we need to suppress hooked blocks from getting inserted into the template. 1454 1467 add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); 1455 $template = _build_block_template_result_from_post( $post );1468 $template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null; 1456 1469 remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); 1457 1470 … … 1459 1472 $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); 1460 1473 1461 $blocks = parse_blocks( $ template->content );1474 $blocks = parse_blocks( $post->post_content ); 1462 1475 $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); 1463 1476 1464 wp_update_post( 1465 array( 1466 'ID' => $post->ID, 1467 'post_content' => $content, 1468 ) 1469 ); 1470 } 1477 $post->post_content = $content; 1478 return $post; 1479 } -
branches/6.5/src/wp-includes/default-filters.php
r57627 r57802 753 753 add_action( 'init', '_wp_register_default_font_collections' ); 754 754 755 // It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't 756 // provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`). 757 add_action( 'rest_after_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 ); 758 add_action( 'rest_after_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 ); 755 // Add ignoredHookedBlocks metadata attribute to the template and template part post types. 756 add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 ); 757 add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 ); 759 758 760 759 unset( $filter, $action ); -
branches/6.5/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r57374 r57802 620 620 } 621 621 622 return $changes; 622 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ 623 return apply_filters( "rest_pre_insert_{$this->post_type}", $changes, $request ); 623 624 } 624 625 -
branches/6.5/tests/phpunit/tests/block-template-utils.php
r56983 r57802 88 88 89 89 /** 90 * Tear down after each test. 91 * 92 * @since 6.5.0 93 */ 94 public function tear_down() { 95 global $wp_current_filter; 96 97 if ( 98 'rest_pre_insert_wp_template' === current_filter() || 99 'rest_pre_insert_wp_template_part' === current_filter() 100 ) { 101 array_pop( $wp_current_filter ); 102 } 103 104 if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/hooked-block' ) ) { 105 unregister_block_type( 'tests/hooked-block' ); 106 } 107 108 parent::tear_down(); 109 } 110 111 /** 90 112 * @ticket 59338 91 113 * … … 391 413 $this->assertTrue( $has_html_files, 'contains at least one html file' ); 392 414 } 415 416 /** 417 * @ticket 60671 418 * 419 * @covers inject_ignored_hooked_blocks_metadata_attributes 420 */ 421 public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { 422 global $wp_current_filter; 423 // Mock currently set filter. 424 $wp_current_filter[] = 'rest_pre_insert_wp_template'; 425 426 register_block_type( 427 'tests/hooked-block', 428 array( 429 'block_hooks' => array( 430 'tests/anchor-block' => 'after', 431 ), 432 ) 433 ); 434 435 $id = self::TEST_THEME . '//' . 'my_template'; 436 $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); 437 438 $changes = new stdClass(); 439 $changes->post_content = '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->'; 440 441 $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); 442 $this->assertSame( 443 '<!-- wp:tests/anchor-block {"metadata":{"ignoredHookedBlocks":["tests/hooked-block"]}} -->Hello<!-- /wp:tests/anchor-block -->', 444 $post->post_content, 445 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' 446 ); 447 } 448 449 /** 450 * @ticket 60671 451 * 452 * @covers inject_ignored_hooked_blocks_metadata_attributes 453 */ 454 public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { 455 global $wp_current_filter; 456 // Mock currently set filter. 457 $wp_current_filter[] = 'rest_pre_insert_wp_template_part'; 458 459 register_block_type( 460 'tests/hooked-block', 461 array( 462 'block_hooks' => array( 463 'tests/anchor-block' => 'after', 464 ), 465 ) 466 ); 467 468 $id = self::TEST_THEME . '//' . 'my_template_part'; 469 $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); 470 471 $changes = new stdClass(); 472 $changes->post_content = '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->'; 473 474 $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); 475 $this->assertSame( 476 '<!-- wp:tests/anchor-block {"metadata":{"ignoredHookedBlocks":["tests/hooked-block"]}} -->Hello<!-- /wp:tests/anchor-block -->', 477 $post->post_content, 478 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' 479 ); 480 } 393 481 } -
branches/6.5/tests/phpunit/tests/rest-api/wpRestTemplatesController.php
r57366 r57802 48 48 public static function wpTearDownAfterClass() { 49 49 wp_delete_post( self::$post->ID ); 50 } 51 52 /** 53 * Tear down after each test. 54 * 55 * @since 6.5.0 56 */ 57 public function tear_down() { 58 if ( has_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ) ) { 59 remove_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10 ); 60 } 61 if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/block' ) ) { 62 unregister_block_type( 'tests/hooked-block' ); 63 } 64 65 parent::tear_down(); 50 66 } 51 67 … … 912 928 $this->assertEmpty( $prepared->post_content, 'The content was not correct in the prepared template part.' ); 913 929 } 930 931 /** 932 * @ticket 60671 933 * 934 * @covers WP_REST_Templates_Controller::prepare_item_for_database 935 * @covers inject_ignored_hooked_blocks_metadata_attributes 936 */ 937 public function test_prepare_item_for_database_injects_hooked_block() { 938 register_block_type( 939 'tests/hooked-block', 940 array( 941 'block_hooks' => array( 942 'tests/anchor-block' => 'after', 943 ), 944 ) 945 ); 946 947 add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 ); 948 949 $endpoint = new WP_REST_Templates_Controller( 'wp_template_part' ); 950 951 $prepare_item_for_database = new ReflectionMethod( $endpoint, 'prepare_item_for_database' ); 952 $prepare_item_for_database->setAccessible( true ); 953 954 $body_params = array( 955 'title' => 'Untitled Template Part', 956 'slug' => 'untitled-template-part', 957 'content' => '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->', 958 ); 959 960 $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' ); 961 $request->set_body_params( $body_params ); 962 963 $prepared = $prepare_item_for_database->invoke( $endpoint, $request ); 964 $this->assertSame( 965 '<!-- wp:tests/anchor-block {"metadata":{"ignoredHookedBlocks":["tests/hooked-block"]}} -->Hello<!-- /wp:tests/anchor-block -->', 966 $prepared->post_content, 967 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' 968 ); 969 } 914 970 }
Note: See TracChangeset
for help on using the changeset viewer.