register_block_script_handle

函数
register_block_script_handle ( $metadata, $field_name, $index = 0 )
参数
  • (array) $metadata Block metadata.
    Required:
  • (string) $field_name Field name to pick from metadata.
    Required:
  • (int) $index Optional. Index of the script to register when multiple items passed. Default 0.
    Required:
返回值
  • (string|false) Script handle provided directly or created through script's registration, or false on failure.
定义位置
相关方法
register_block_style_handleregister_block_styleregister_block_core_fileregister_block_core_post_dateunregister_block_style
引入
5.5.0
弃用
-

register_block_script_handle: 这个函数为一个区块类型注册了一个脚本句柄。脚本句柄用于为该块类型的脚本排队。

为选定的区块元数据字段找到一个脚本句柄。它检测是否提供了文件的路径,并找到一个相应的资产文件,该文件具有在自动生成的句柄名称下注册脚本所需的细节。否则它将返回未处理的脚本句柄。

function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
	if ( empty( $metadata[ $field_name ] ) ) {
		return false;
	}

	$script_handle = $metadata[ $field_name ];
	if ( is_array( $script_handle ) ) {
		if ( empty( $script_handle[ $index ] ) ) {
			return false;
		}
		$script_handle = $script_handle[ $index ];
	}

	$script_path = remove_block_asset_path_prefix( $script_handle );
	if ( $script_handle === $script_path ) {
		return $script_handle;
	}

	$script_handle     = generate_block_asset_handle( $metadata['name'], $field_name, $index );
	$script_asset_path = wp_normalize_path(
		realpath(
			dirname( $metadata['file'] ) . '/' .
			substr_replace( $script_path, '.asset.php', - strlen( '.js' ) )
		)
	);
	if ( ! file_exists( $script_asset_path ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: Field name, 2: Block name. */
				__( 'The asset file for the "%1$s" defined in "%2$s" block definition is missing.' ),
				$field_name,
				$metadata['name']
			),
			'5.5.0'
		);
		return false;
	}

	// Path needs to be normalized to work in Windows env.
	static $wpinc_path_norm = '';
	if ( ! $wpinc_path_norm ) {
		$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
	}

	$theme_path_norm  = wp_normalize_path( get_theme_file_path() );
	$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );

	$is_core_block  = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
	$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );

	$script_uri = plugins_url( $script_path, $metadata['file'] );
	if ( $is_core_block ) {
		$script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) );
	} elseif ( $is_theme_block ) {
		$script_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $script_path_norm ) );
	}

	$script_asset        = require $script_asset_path;
	$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
	$result              = wp_register_script(
		$script_handle,
		$script_uri,
		$script_dependencies,
		isset( $script_asset['version'] ) ? $script_asset['version'] : false
	);
	if ( ! $result ) {
		return false;
	}

	if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) {
		wp_set_script_translations( $script_handle, $metadata['textdomain'] );
	}

	return $script_handle;
}

常见问题

FAQs
查看更多 >