-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Allow PHP-Only Block Registration #10932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
691cdf5
Allow PHP-Only Block Registration
t-hamano d1c5a3a
Improve PHPDoc
t-hamano 1cccbbc
Make variable names clearer
t-hamano 19e79f5
Merge branch 'trunk' into 64639-php-auto-register-block
t-hamano ac3ea46
Removed unused second parameter
t-hamano 0a7d27b
Add new function to detect auto-registered blocks
t-hamano 423d10a
Remove unnecessary unit test
t-hamano c808f99
Rearrange order of action hooks
t-hamano 4ede6de
Make PHPDoc comment third-person
t-hamano 723bd9b
Add @see tag
t-hamano 936b6bf
Make PHPDoc comment third-person
t-hamano c1c194a
Remove default value from filter hook
t-hamano 22fee8f
Merge branch 'trunk' into 64639-php-auto-register-block
t-hamano ef9b560
Relocate function and add underscore prefix
t-hamano 506f76f
Unit test: Add ticket number
t-hamano bc9ada0
Move require statement
t-hamano c9d599d
Make support key camelCase
t-hamano c606521
Fix unit test
t-hamano df85c59
Merge branch 'trunk' into 64639-php-auto-register-block
t-hamano 658b7af
Add server-side type check
t-hamano b6348ef
Fix PHP lint error
t-hamano e2b5ad4
Use null Coalescing Operator
t-hamano 7f465c3
Fix PHP fatal error
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
| /** | ||
| * Auto-register block support. | ||
| * | ||
| * @package WordPress | ||
| * @since 7.0.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Marks user-defined attributes for auto-generated inspector controls. | ||
| * | ||
| * This filter runs during block type registration, before the WP_Block_Type | ||
| * is instantiated. Block supports add their attributes AFTER the block type | ||
| * is created (via {@see WP_Block_Supports::register_attributes()}), so any attributes | ||
| * present at this stage are user-defined. | ||
| * | ||
| * The marker tells generateFieldsFromAttributes() which attributes should | ||
| * get auto-generated inspector controls. Attributes are excluded if they: | ||
| * - Have a 'source' (HTML-derived, edited inline not via inspector) | ||
| * - Have role 'local' (internal state, not user-configurable) | ||
| * - Have an unsupported type (only 'string', 'number', 'integer', 'boolean' are supported) | ||
| * - Were added by block supports (added after this filter runs) | ||
| * | ||
| * @since 7.0.0 | ||
| * @access private | ||
| * | ||
| * @param array<string, mixed> $args Array of arguments for registering a block type. | ||
| * @return array<string, mixed> Modified block type arguments. | ||
| */ | ||
| function wp_mark_auto_generate_control_attributes( array $args ): array { | ||
| if ( empty( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { | ||
| return $args; | ||
| } | ||
|
|
||
| $has_auto_register = ! empty( $args['supports']['autoRegister'] ); | ||
| if ( ! $has_auto_register ) { | ||
| return $args; | ||
| } | ||
|
|
||
| foreach ( $args['attributes'] as $attr_key => $attr_schema ) { | ||
| // Skip HTML-derived attributes (edited inline, not via inspector). | ||
| if ( ! empty( $attr_schema['source'] ) ) { | ||
| continue; | ||
| } | ||
| // Skip internal attributes (not user-configurable). | ||
| if ( isset( $attr_schema['role'] ) && 'local' === $attr_schema['role'] ) { | ||
| continue; | ||
| } | ||
| // Skip unsupported types (only 'string', 'number', 'integer', 'boolean' are supported). | ||
| $type = $attr_schema['type'] ?? null; | ||
| if ( ! in_array( $type, array( 'string', 'number', 'integer', 'boolean' ), true ) ) { | ||
| continue; | ||
| } | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $args['attributes'][ $attr_key ]['autoGenerateControl'] = true; | ||
| } | ||
|
|
||
| return $args; | ||
| } | ||
|
|
||
| // Priority 5 to mark original attributes before other filters (priority 10+) might add their own. | ||
| add_filter( 'register_block_type_args', 'wp_mark_auto_generate_control_attributes', 5 ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php | ||
| /** | ||
| * @group block-supports | ||
| * | ||
| * @covers ::wp_mark_auto_generate_control_attributes | ||
| */ | ||
| class Tests_Block_Supports_Auto_Register extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Tests that attributes are marked when autoRegister is enabled. | ||
| * | ||
| * @ticket 64639 | ||
| */ | ||
| public function test_marks_attributes_with_auto_register_flag() { | ||
| $settings = array( | ||
| 'supports' => array( 'autoRegister' => true ), | ||
| 'attributes' => array( | ||
| 'title' => array( 'type' => 'string' ), | ||
| 'count' => array( 'type' => 'integer' ), | ||
| ), | ||
| ); | ||
|
|
||
| $result = wp_mark_auto_generate_control_attributes( $settings ); | ||
|
|
||
| $this->assertTrue( $result['attributes']['title']['autoGenerateControl'] ); | ||
| $this->assertTrue( $result['attributes']['count']['autoGenerateControl'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that attributes are not marked without autoRegister flag. | ||
| * | ||
| * @ticket 64639 | ||
| */ | ||
| public function test_does_not_mark_attributes_without_auto_register() { | ||
| $settings = array( | ||
| 'attributes' => array( | ||
| 'title' => array( 'type' => 'string' ), | ||
| ), | ||
| ); | ||
|
|
||
| $result = wp_mark_auto_generate_control_attributes( $settings ); | ||
|
|
||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['title'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that attributes with source are excluded. | ||
| * | ||
| * @ticket 64639 | ||
| */ | ||
| public function test_excludes_attributes_with_source() { | ||
| $settings = array( | ||
| 'supports' => array( 'autoRegister' => true ), | ||
| 'attributes' => array( | ||
| 'title' => array( 'type' => 'string' ), | ||
| 'content' => array( | ||
| 'type' => 'string', | ||
| 'source' => 'html', | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $result = wp_mark_auto_generate_control_attributes( $settings ); | ||
|
|
||
| $this->assertTrue( $result['attributes']['title']['autoGenerateControl'] ); | ||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['content'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that attributes with role: local are excluded. | ||
| * | ||
| * Example: The 'blob' attribute in media blocks (image, video, file, audio) | ||
| * stores a temporary blob URL during file upload. This is internal state | ||
| * that shouldn't be shown in the inspector or saved to the database. | ||
| * | ||
| * @ticket 64639 | ||
| */ | ||
| public function test_excludes_attributes_with_role_local() { | ||
| $settings = array( | ||
| 'supports' => array( 'autoRegister' => true ), | ||
| 'attributes' => array( | ||
| 'title' => array( 'type' => 'string' ), | ||
| 'blob' => array( | ||
| 'type' => 'string', | ||
| 'role' => 'local', | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $result = wp_mark_auto_generate_control_attributes( $settings ); | ||
|
|
||
| $this->assertTrue( $result['attributes']['title']['autoGenerateControl'] ); | ||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['blob'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that empty attributes are handled gracefully. | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @ticket 64639 | ||
| */ | ||
| public function test_handles_empty_attributes() { | ||
| $settings = array( | ||
| 'supports' => array( 'autoRegister' => true ), | ||
| ); | ||
|
|
||
| $result = wp_mark_auto_generate_control_attributes( $settings ); | ||
|
|
||
| $this->assertSame( $settings, $result ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that only allowed attributes are marked. | ||
| * | ||
| * @ticket 64639 | ||
| */ | ||
| public function test_excludes_unsupported_types() { | ||
| $settings = array( | ||
| 'supports' => array( 'autoRegister' => true ), | ||
| 'attributes' => array( | ||
| // Supported types | ||
| 'text' => array( 'type' => 'string' ), | ||
| 'price' => array( 'type' => 'number' ), | ||
| 'count' => array( 'type' => 'integer' ), | ||
| 'enabled' => array( 'type' => 'boolean' ), | ||
| // Unsupported types | ||
| 'metadata' => array( 'type' => 'object' ), | ||
| 'items' => array( 'type' => 'array' ), | ||
| 'config' => array( 'type' => 'null' ), | ||
| 'unknown' => array( 'type' => 'unknown' ), | ||
| ), | ||
| ); | ||
|
|
||
| $result = wp_mark_auto_generate_control_attributes( $settings ); | ||
|
|
||
| $this->assertTrue( $result['attributes']['text']['autoGenerateControl'] ); | ||
| $this->assertTrue( $result['attributes']['price']['autoGenerateControl'] ); | ||
| $this->assertTrue( $result['attributes']['count']['autoGenerateControl'] ); | ||
| $this->assertTrue( $result['attributes']['enabled']['autoGenerateControl'] ); | ||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['metadata'] ); | ||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['items'] ); | ||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['config'] ); | ||
| $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['unknown'] ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.