PluginProbe
Gutenberg / 9.8.0
Gutenberg v9.8.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / block-supports / generated-classname.php

generated-classname.php in Gutenberg 9.8.0, at lib/block-supports/generated-classname.php

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