| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP-only block registration for WordPress 6.9+ |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Exposes blocks with autoRegister flag for ServerSideRender in the editor. |
| 10 |
* |
| 11 |
* Detects blocks that have the autoRegister flag set in their supports |
| 12 |
* and passes them to JavaScript for auto-registration with ServerSideRender. |
| 13 |
*/ |
| 14 |
function _gutenberg_enqueue_auto_register_blocks( $settings ) { |
| 15 |
$auto_register_blocks = array(); |
| 16 |
$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered(); |
| 17 |
|
| 18 |
foreach ( $registered_blocks as $block_name => $block_type ) { |
| 19 |
if ( ! empty( $block_type->supports['autoRegister'] ) && ! empty( $block_type->render_callback ) ) { |
| 20 |
$auto_register_blocks[] = $block_name; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
if ( ! empty( $auto_register_blocks ) ) { |
| 25 |
wp_add_inline_script( |
| 26 |
'wp-block-library', |
| 27 |
sprintf( 'window.__unstableAutoRegisterBlocks = %s;', wp_json_encode( $auto_register_blocks ) ), |
| 28 |
'before' |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
return $settings; |
| 33 |
} |
| 34 |
|
| 35 |
if ( has_action( 'enqueue_block_editor_assets', '_wp_enqueue_auto_register_blocks' ) ) { |
| 36 |
remove_action( 'enqueue_block_editor_assets', '_wp_enqueue_auto_register_blocks' ); |
| 37 |
} |
| 38 |
add_action( 'enqueue_block_editor_assets', '_gutenberg_enqueue_auto_register_blocks' ); |
| 39 |
|
| 40 |
/** |
| 41 |
* Marks user-defined attributes for auto-generated inspector controls. |
| 42 |
* |
| 43 |
* This filter runs during block type registration, before the WP_Block_Type |
| 44 |
* is instantiated. Block supports add their attributes AFTER the block type |
| 45 |
* is created (via {@see WP_Block_Supports::register_attributes()}), so any attributes |
| 46 |
* present at this stage are user-defined. |
| 47 |
* |
| 48 |
* The marker tells generateFieldsFromAttributes() which attributes should |
| 49 |
* get auto-generated inspector controls. Attributes are excluded if they: |
| 50 |
* - Have a 'source' (HTML-derived, edited inline not via inspector) |
| 51 |
* - Have role 'local' (internal state, not user-configurable) |
| 52 |
* - Were added by block supports (added after this filter runs) |
| 53 |
* |
| 54 |
* @param array<string, mixed> $args Array of arguments for registering a block type. |
| 55 |
* @return array<string, mixed> Modified block type arguments. |
| 56 |
*/ |
| 57 |
function gutenberg_mark_auto_generate_control_attributes( array $args ): array { |
| 58 |
if ( empty( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { |
| 59 |
return $args; |
| 60 |
} |
| 61 |
|
| 62 |
$has_auto_register = ! empty( $args['supports']['autoRegister'] ); |
| 63 |
if ( ! $has_auto_register ) { |
| 64 |
return $args; |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( $args['attributes'] as $attr_key => $attr_schema ) { |
| 68 |
// Skip HTML-derived attributes (edited inline, not via inspector). |
| 69 |
if ( ! empty( $attr_schema['source'] ) ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
// Skip internal attributes (not user-configurable). |
| 73 |
if ( isset( $attr_schema['role'] ) && 'local' === $attr_schema['role'] ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
// Skip unsupported types (only 'string', 'number', 'integer', 'boolean' are supported). |
| 77 |
$type = $attr_schema['type'] ?? null; |
| 78 |
if ( ! in_array( $type, array( 'string', 'number', 'integer', 'boolean' ), true ) ) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
$args['attributes'][ $attr_key ]['autoGenerateControl'] = true; |
| 82 |
} |
| 83 |
|
| 84 |
return $args; |
| 85 |
} |
| 86 |
|
| 87 |
$filter_priority = has_filter( 'register_block_type_args', 'wp_mark_auto_generate_control_attributes' ); |
| 88 |
if ( $filter_priority ) { |
| 89 |
remove_filter( 'register_block_type_args', 'wp_mark_auto_generate_control_attributes', $filter_priority ); |
| 90 |
} |
| 91 |
// Priority 5 to mark original attributes before other filters (priority 10+) might add their own. |
| 92 |
add_filter( 'register_block_type_args', 'gutenberg_mark_auto_generate_control_attributes', 5 ); |
| 93 |
|