Changeset 56587
- Timestamp:
- 09/14/2023 01:23:13 PM (12 months ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/post.php
r56176 r56587 2208 2208 * @since 5.0.0 2209 2209 * @since 6.3.0 Added `selectors` field. 2210 * @since 6.4.0 Added `block_hooks` field. 2210 2211 * 2211 2212 * @return array An associative array of registered block data. … … 2222 2223 'provides_context' => 'providesContext', 2223 2224 'uses_context' => 'usesContext', 2225 'block_hooks' => 'blockHooks', 2224 2226 'selectors' => 'selectors', 2225 2227 'supports' => 'supports', -
trunk/src/wp-includes/blocks.php
r56560 r56587 343 343 * @since 6.1.0 Added support for `render` field. 344 344 * @since 6.3.0 Added `selectors` field. 345 * @since 6.4.0 Added support for `blockHooks` field. 345 346 * 346 347 * @param string $file_or_folder Path to the JSON file with metadata definition for … … 514 515 } 515 516 517 if ( ! empty( $metadata['blockHooks'] ) ) { 518 /** 519 * Map camelCased position string (from block.json) to snake_cased block type position. 520 * 521 * @var array 522 */ 523 $position_mappings = array( 524 'before' => 'before', 525 'after' => 'after', 526 'firstChild' => 'first_child', 527 'lastChild' => 'last_child', 528 ); 529 530 $settings['block_hooks'] = array(); 531 foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) { 532 // Avoid infinite recursion (hooking to itself). 533 if ( $metadata['name'] === $anchor_block_name ) { 534 _doing_it_wrong( 535 __METHOD__, 536 __( 'Cannot hook block to itself.', 'gutenberg' ), 537 '6.4.0' 538 ); 539 continue; 540 } 541 542 if ( ! isset( $position_mappings[ $position ] ) ) { 543 continue; 544 } 545 546 $settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ]; 547 } 548 } 549 516 550 if ( ! empty( $metadata['render'] ) ) { 517 551 $template_path = wp_normalize_path( -
trunk/src/wp-includes/class-wp-block-type.php
r56414 r56587 173 173 */ 174 174 public $provides_context = null; 175 176 /** 177 * Block hooks for this block type. 178 * 179 * A block hook is specified by a block type and a relative position. 180 * The hooked block will be automatically inserted in the given position 181 * next to the "anchor" block whenever the latter is encountered. 182 * 183 * @since 6.4.0 184 * @var array[] 185 */ 186 public $block_hooks = array(); 175 187 176 188 /** … … 255 267 * Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties. 256 268 * @since 6.3.0 Added the `selectors` property. 269 * @since 6.4.0 Added the `block_hooks` property. 257 270 * 258 271 * @see register_block_type() … … 285 298 * @type string[] $uses_context Context values inherited by blocks of this type. 286 299 * @type string[]|null $provides_context Context provided by blocks of this type. 300 * @type array[] $block_hooks Block hooks. 287 301 * @type string[] $editor_script_handles Block type editor only script handles. 288 302 * @type string[] $script_handles Block type front end and editor script handles. -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php
r56586 r56587 292 292 'style_handles', 293 293 'variations', 294 'block_hooks', 294 295 ), 295 296 $deprecated_fields … … 707 708 'keywords' => $keywords_definition, 708 709 'example' => $example_definition, 710 'block_hooks' => array( 711 'description' => __( 'This block is automatically inserted near any occurence of the block types used as keys of this map, into a relative position given by the corresponding value.' ), 712 'type' => 'object', 713 'patternProperties' => array( 714 '^[a-zA-Z0-9-]+/[a-zA-Z0-9-]+$' => array( 715 'type' => 'string', 716 'enum' => array( 'before', 'after', 'first_child', 'last_child' ), 717 ), 718 ), 719 'default' => array(), 720 'context' => array( 'embed', 'view', 'edit' ), 721 'readonly' => true, 722 ), 709 723 ), 710 724 ); -
trunk/tests/phpunit/data/blocks/notice/block.json
r55673 r56587 30 30 "selectors": { 31 31 "root": ".wp-block-notice" 32 }, 33 "blockHooks": { 34 "core/post-content": "before" 32 35 }, 33 36 "supports": { -
trunk/tests/phpunit/tests/admin/includesPost.php
r56559 r56587 919 919 'ancestor' => array( 'core/test-ancestor' ), 920 920 'selectors' => array( 'root' => '.wp-block-test' ), 921 'block_hooks' => array( 'core/post-content' => 'before' ), 921 922 ); 922 923 … … 938 939 ), 939 940 'usesContext' => array(), 941 'blockHooks' => array( 'core/post-content' => 'before' ), 940 942 'selectors' => array( 'root' => '.wp-block-test' ), 941 943 'category' => 'common', -
trunk/tests/phpunit/tests/blocks/register.php
r56559 r56587 645 645 'Block type should contain selectors from metadata.' 646 646 ); 647 // @ticket 59346 648 $this->assertSame( 649 array( 'core/post-content' => 'before' ), 650 $result->block_hooks, 651 'Block type should contain block hooks from metadata.' 652 ); 647 653 $this->assertSame( 648 654 array( -
trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php
r56548 r56587 196 196 * @ticket 47620 197 197 * @ticket 57585 198 * @ticket 59346 198 199 */ 199 200 public function test_get_item_invalid() { … … 206 207 'provides_context' => 'invalid_provides_context', 207 208 'uses_context' => 'invalid_uses_context', 209 'block_hooks' => 'invalid_block_hooks', 208 210 'category' => true, 209 211 'editor_script' => true, … … 245 247 ); 246 248 $this->assertSameSets( array( 'invalid_uses_context' ), $data['uses_context'] ); 249 $this->assertSameSets( array(), $data['block_hooks'], 'invalid block_hooks defaults to empty array' ); 247 250 $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); 248 251 $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); … … 267 270 * @ticket 47620 268 271 * @ticket 57585 272 * @ticket 59346 269 273 */ 270 274 public function test_get_item_defaults() { … … 277 281 'provides_context' => false, 278 282 'uses_context' => false, 283 'block_hooks' => false, 279 284 'category' => false, 280 285 'editor_script' => false, … … 315 320 ); 316 321 $this->assertSameSets( array(), $data['provides_context'] ); 322 $this->assertSameSets( array(), $data['block_hooks'], 'block_hooks defaults to empty array' ); 317 323 $this->assertSameSets( array(), $data['uses_context'] ); 318 324 $this->assertSameSets( array(), $data['keywords'] ); … … 539 545 * @ticket 47620 540 546 * @ticket 57585 547 * @ticket 59346 541 548 */ 542 549 public function test_get_item_schema() { … … 546 553 $data = $response->get_data(); 547 554 $properties = $data['schema']['properties']; 548 $this->assertCount( 29, $properties );555 $this->assertCount( 30, $properties ); 549 556 $this->assertArrayHasKey( 'api_version', $properties ); 550 557 $this->assertArrayHasKey( 'title', $properties ); … … 569 576 $this->assertArrayHasKey( 'uses_context', $properties ); 570 577 $this->assertArrayHasKey( 'provides_context', $properties ); 578 $this->assertArrayHasKey( 'block_hooks', $properties ); 571 579 $this->assertArrayHasKey( 'variations', $properties ); 572 580 $this->assertArrayHasKey( 'ancestor', $properties ); … … 665 673 * 666 674 * @since 5.5.0 675 * @since 6.4.0 Added the `block_hooks` extra field. 667 676 * 668 677 * @param WP_Block_Type $block_type Sample block type. … … 691 700 'provides_context', 692 701 'uses_context', 702 'block_hooks', 693 703 'supports', 694 704 'styles',
Note: See TracChangeset
for help on using the changeset viewer.