PluginProbe
Gutenberg / 10.1.0
Gutenberg v10.1.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 / custom-classname.php

custom-classname.php in Gutenberg 10.1.0, at lib/block-supports/custom-classname.php

64 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 * Custom classname block support flag.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Registers the custom classname block attribute for block types that support it.
10 *
11 * @param WP_Block_Type $block_type Block Type.
12 */
13 function gutenberg_register_custom_classname_support( $block_type ) {
14 $has_custom_classname_support = true;
15 if ( property_exists( $block_type, 'supports' ) ) {
16 $has_custom_classname_support = gutenberg_experimental_get( $block_type->supports, array( 'customClassName' ), true );
17 }
18 if ( $has_custom_classname_support ) {
19 if ( ! $block_type->attributes ) {
20 $block_type->attributes = array();
21 }
22
23 if ( ! array_key_exists( 'className', $block_type->attributes ) ) {
24 $block_type->attributes['className'] = array(
25 'type' => 'string',
26 );
27 }
28 }
29 }
30
31 /**
32 * Add the custom classnames to the output.
33 *
34 * @param WP_Block_Type $block_type Block Type.
35 * @param array $block_attributes Block attributes.
36 *
37 * @return array Block CSS classes and inline styles.
38 */
39 function gutenberg_apply_custom_classname_support( $block_type, $block_attributes ) {
40 $has_custom_classname_support = true;
41 $attributes = array();
42 if ( property_exists( $block_type, 'supports' ) ) {
43 $has_custom_classname_support = gutenberg_experimental_get( $block_type->supports, array( 'customClassName' ), true );
44 }
45 if ( $has_custom_classname_support ) {
46 $has_custom_classnames = array_key_exists( 'className', $block_attributes );
47
48 if ( $has_custom_classnames ) {
49 $attributes['class'] = $block_attributes['className'];
50 }
51 }
52
53 return $attributes;
54 }
55
56 // Register the block support.
57 WP_Block_Supports::get_instance()->register(
58 'custom-classname',
59 array(
60 'register_attribute' => 'gutenberg_register_custom_classname_support',
61 'apply' => 'gutenberg_apply_custom_classname_support',
62 )
63 );
64