函数文档

wp_migrate_old_typography_shape()

💡 云策文档标注

概述

wp_migrate_old_typography_shape() 函数用于将块类型元数据中旧版排版支持键从 supports.* 迁移到 supports.typography.*,并在检测到旧格式时触发 _doing_it_wrong() 警告。

关键要点

  • 函数接收块类型元数据数组作为参数,返回过滤后的元数据数组。
  • 迁移的排版键包括 __experimentalFontFamily、__experimentalFontStyle、__experimentalFontWeight、__experimentalLetterSpacing、__experimentalTextDecoration、__experimentalTextTransform、fontSize 和 lineHeight。
  • 使用 _wp_array_set() 将旧键值移动到新路径,并移除旧键。
  • 自 WordPress 5.8.0 版本引入此函数。

代码示例

function wp_migrate_old_typography_shape( $metadata ) {
    if ( ! isset( $metadata['supports'] ) ) {
        return $metadata;
    }

    $typography_keys = array(
        '__experimentalFontFamily',
        '__experimentalFontStyle',
        '__experimentalFontWeight',
        '__experimentalLetterSpacing',
        '__experimentalTextDecoration',
        '__experimentalTextTransform',
        'fontSize',
        'lineHeight',
    );

    foreach ( $typography_keys as $typography_key ) {
        $support_for_key = isset( $metadata['supports'][ $typography_key ] ) ? $metadata['supports'][ $typography_key ] : null;

        if ( null !== $support_for_key ) {
            _doing_it_wrong(
                'register_block_type_from_metadata()',
                sprintf(
                    __( 'Block "%1$s" is declaring %2$s support in %3$s file under %4$s. %2$s support is now declared under %5$s.' ),
                    $metadata['name'],
                    "$typography_key",
                    'block.json',
                    "supports.$typography_key",
                    "supports.typography.$typography_key"
                ),
                '5.8.0'
            );

            _wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key );
            unset( $metadata['supports'][ $typography_key ] );
        }
    }

    return $metadata;
}

注意事项

  • 此函数主要用于内部迁移,开发者应确保块类型元数据使用新版 supports.typography.* 格式以避免警告。
  • 相关函数包括 _wp_array_set()、__() 和 _doing_it_wrong(),用于数组操作、翻译和错误处理。

📄 原文内容

Converts typography keys declared under supports.* to supports.typography.*.

Description

Displays a _doing_it_wrong() notice when a block using the older format is detected.

Parameters

$metadataarrayrequired
Metadata for registering a block type.

Return

array Filtered metadata for registering a block type.

Source

function wp_migrate_old_typography_shape( $metadata ) {
	if ( ! isset( $metadata['supports'] ) ) {
		return $metadata;
	}

	$typography_keys = array(
		'__experimentalFontFamily',
		'__experimentalFontStyle',
		'__experimentalFontWeight',
		'__experimentalLetterSpacing',
		'__experimentalTextDecoration',
		'__experimentalTextTransform',
		'fontSize',
		'lineHeight',
	);

	foreach ( $typography_keys as $typography_key ) {
		$support_for_key = isset( $metadata['supports'][ $typography_key ] ) ? $metadata['supports'][ $typography_key ] : null;

		if ( null !== $support_for_key ) {
			_doing_it_wrong(
				'register_block_type_from_metadata()',
				sprintf(
					/* translators: 1: Block type, 2: Typography supports key, e.g: fontSize, lineHeight, etc. 3: block.json, 4: Old metadata key, 5: New metadata key. */
					__( 'Block "%1$s" is declaring %2$s support in %3$s file under %4$s. %2$s support is now declared under %5$s.' ),
					$metadata['name'],
					"<code>$typography_key</code>",
					'<code>block.json</code>',
					"<code>supports.$typography_key</code>",
					"<code>supports.typography.$typography_key</code>"
				),
				'5.8.0'
			);

			_wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key );
			unset( $metadata['supports'][ $typography_key ] );
		}
	}

	return $metadata;
}

Changelog

Version Description
5.8.0 Introduced.