wp_get_block_style_variation_name_from_registered_style() │ WP 7.0.0

Gets the first style variation name from a className string that matches a registered style.

Хуков нет.

Возвращает

string|null. The name of the first registered variation, or null if none found.

Использование

wp_get_block_style_variation_name_from_registered_style( $class_name, $registered_styles ): ?string;
$class_name(строка) (обязательный)
CSS class string for a block.
$registered_styles(массив)
.
По умолчанию: array()

Список изменений

С версии 7.0.0 Введена.

Код wp_get_block_style_variation_name_from_registered_style() WP 7.1.2

function wp_get_block_style_variation_name_from_registered_style( string $class_name, array $registered_styles = array() ): ?string {
	if ( ! $class_name ) {
		return null;
	}

	$registered_names = array_filter( array_column( $registered_styles, 'name' ) );

	$prefix = 'is-style-';
	$length = strlen( $prefix );

	foreach ( explode( ' ', $class_name ) as $class ) {
		if ( str_starts_with( $class, $prefix ) ) {
			$variation = substr( $class, $length );
			if ( 'default' !== $variation && in_array( $variation, $registered_names, true ) ) {
				return $variation;
			}
		}
	}

	return null;
}