1<?php
2/**
3 * Generated classname block support flag.
4 *
5 * @package WordPress
6 * @since 5.6.0
7 */
8
9/**
10 * Gets the generated classname from a given block name.
11 *
12 * @since 5.6.0
13 *
14 * @access private
15 *
16 * @param string $block_name Block Name.
17 * @return string Generated classname.
18 */
19function wp_get_block_default_classname( $block_name ) {
20 // Generated HTML classes for blocks follow the `wp-block-{name}` nomenclature.
21 // Blocks provided by WordPress drop the prefixes 'core/' or 'core-' (historically used in 'core-embed/').
22 $classname = 'wp-block-' . preg_replace(
23 '/^core-/',
24 '',
25 str_replace( '/', '-', $block_name )
26 );
27
28 /**
29 * Filters the default block className for server rendered blocks.
30 *
31 * @since 5.6.0
32 *
33 * @param string $class_name The current applied classname.
34 * @param string $block_name The block name.
35 */
36 $classname = apply_filters( 'block_default_classname', $classname, $block_name );
37
38 return $classname;
39}
40
41/**
42 * Adds the generated classnames to the output.
43 *
44 * @since 5.6.0
45 *
46 * @access private
47 *
48 * @param WP_Block_Type $block_type Block Type.
49 * @return array Block CSS classes and inline styles.
50 */
51function wp_apply_generated_classname_support( $block_type ) {
52 $attributes = array();
53 $has_generated_classname_support = block_has_support( $block_type, 'className', true );
54 if ( $has_generated_classname_support ) {
55 $block_classname = wp_get_block_default_classname( $block_type->name );
56
57 if ( $block_classname ) {
58 $attributes['class'] = $block_classname;
59 }
60 }
61
62 return $attributes;
63}
64
65// Register the block support.
66WP_Block_Supports::get_instance()->register(
67 'generated-classname',
68 array(
69 'apply' => 'wp_apply_generated_classname_support',
70 )
71);
72