[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Typography block support flag. 4 * 5 * @package WordPress 6 * @since 5.6.0 7 */ 8 9 /** 10 * Registers the style and typography block attributes for block types that support it. 11 * 12 * @since 5.6.0 13 * @access private 14 * 15 * @param WP_Block_Type $block_type Block Type. 16 */ 17 function wp_register_typography_support( $block_type ) { 18 if ( ! property_exists( $block_type, 'supports' ) ) { 19 return; 20 } 21 22 $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false ); 23 if ( ! $typography_supports ) { 24 return; 25 } 26 27 $has_font_family_support = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false ); 28 $has_font_size_support = _wp_array_get( $typography_supports, array( 'fontSize' ), false ); 29 $has_font_style_support = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false ); 30 $has_font_weight_support = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false ); 31 $has_letter_spacing_support = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false ); 32 $has_line_height_support = _wp_array_get( $typography_supports, array( 'lineHeight' ), false ); 33 $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false ); 34 $has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false ); 35 36 $has_typography_support = $has_font_family_support 37 || $has_font_size_support 38 || $has_font_style_support 39 || $has_font_weight_support 40 || $has_letter_spacing_support 41 || $has_line_height_support 42 || $has_text_decoration_support 43 || $has_text_transform_support; 44 45 if ( ! $block_type->attributes ) { 46 $block_type->attributes = array(); 47 } 48 49 if ( $has_typography_support && ! array_key_exists( 'style', $block_type->attributes ) ) { 50 $block_type->attributes['style'] = array( 51 'type' => 'object', 52 ); 53 } 54 55 if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) { 56 $block_type->attributes['fontSize'] = array( 57 'type' => 'string', 58 ); 59 } 60 } 61 62 /** 63 * Adds CSS classes and inline styles for typography features such as font sizes 64 * to the incoming attributes array. This will be applied to the block markup in 65 * the front-end. 66 * 67 * @since 5.6.0 68 * @access private 69 * 70 * @param WP_Block_Type $block_type Block type. 71 * @param array $block_attributes Block attributes. 72 * @return array Typography CSS classes and inline styles. 73 */ 74 function wp_apply_typography_support( $block_type, $block_attributes ) { 75 if ( ! property_exists( $block_type, 'supports' ) ) { 76 return array(); 77 } 78 79 $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false ); 80 if ( ! $typography_supports ) { 81 return array(); 82 } 83 84 if ( wp_should_skip_block_supports_serialization( $block_type, 'typography' ) ) { 85 return array(); 86 } 87 88 $attributes = array(); 89 $classes = array(); 90 $styles = array(); 91 92 $has_font_family_support = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false ); 93 $has_font_size_support = _wp_array_get( $typography_supports, array( 'fontSize' ), false ); 94 $has_font_style_support = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false ); 95 $has_font_weight_support = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false ); 96 $has_letter_spacing_support = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false ); 97 $has_line_height_support = _wp_array_get( $typography_supports, array( 'lineHeight' ), false ); 98 $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false ); 99 $has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false ); 100 101 if ( $has_font_size_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' ) ) { 102 $has_named_font_size = array_key_exists( 'fontSize', $block_attributes ); 103 $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ); 104 105 if ( $has_named_font_size ) { 106 $classes[] = sprintf( 'has-%s-font-size', _wp_to_kebab_case( $block_attributes['fontSize'] ) ); 107 } elseif ( $has_custom_font_size ) { 108 $styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] ); 109 } 110 } 111 112 if ( $has_font_family_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' ) ) { 113 $has_named_font_family = array_key_exists( 'fontFamily', $block_attributes ); 114 $has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] ); 115 116 if ( $has_named_font_family ) { 117 $classes[] = sprintf( 'has-%s-font-family', _wp_to_kebab_case( $block_attributes['fontFamily'] ) ); 118 } elseif ( $has_custom_font_family ) { 119 // Before using classes, the value was serialized as a CSS Custom Property. 120 // We don't need this code path when it lands in core. 121 $font_family_custom = $block_attributes['style']['typography']['fontFamily']; 122 if ( strpos( $font_family_custom, 'var:preset|font-family' ) !== false ) { 123 $index_to_splice = strrpos( $font_family_custom, '|' ) + 1; 124 $font_family_slug = _wp_to_kebab_case( substr( $font_family_custom, $index_to_splice ) ); 125 $font_family_custom = sprintf( 'var(--wp--preset--font-family--%s)', $font_family_slug ); 126 } 127 $styles[] = sprintf( 'font-family: %s;', $font_family_custom ); 128 } 129 } 130 131 if ( $has_font_style_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' ) ) { 132 $font_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' ); 133 if ( $font_style ) { 134 $styles[] = $font_style; 135 } 136 } 137 138 if ( $has_font_weight_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' ) ) { 139 $font_weight = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' ); 140 if ( $font_weight ) { 141 $styles[] = $font_weight; 142 } 143 } 144 145 if ( $has_line_height_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' ) ) { 146 $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] ); 147 if ( $has_line_height ) { 148 $styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); 149 } 150 } 151 152 if ( $has_text_decoration_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' ) ) { 153 $text_decoration_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' ); 154 if ( $text_decoration_style ) { 155 $styles[] = $text_decoration_style; 156 } 157 } 158 159 if ( $has_text_transform_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' ) ) { 160 $text_transform_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' ); 161 if ( $text_transform_style ) { 162 $styles[] = $text_transform_style; 163 } 164 } 165 166 if ( $has_letter_spacing_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' ) ) { 167 $letter_spacing_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' ); 168 if ( $letter_spacing_style ) { 169 $styles[] = $letter_spacing_style; 170 } 171 } 172 173 if ( ! empty( $classes ) ) { 174 $attributes['class'] = implode( ' ', $classes ); 175 } 176 if ( ! empty( $styles ) ) { 177 $attributes['style'] = implode( ' ', $styles ); 178 } 179 180 return $attributes; 181 } 182 183 /** 184 * Generates an inline style for a typography feature e.g. text decoration, 185 * text transform, and font style. 186 * 187 * @since 5.8.0 188 * @access private 189 * 190 * @param array $attributes Block's attributes. 191 * @param string $feature Key for the feature within the typography styles. 192 * @param string $css_property Slug for the CSS property the inline style sets. 193 * @return string CSS inline style. 194 */ 195 function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) { 196 // Retrieve current attribute value or skip if not found. 197 $style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false ); 198 if ( ! $style_value ) { 199 return; 200 } 201 202 // If we don't have a preset CSS variable, we'll assume it's a regular CSS value. 203 if ( strpos( $style_value, "var:preset|{$css_property}|" ) === false ) { 204 return sprintf( '%s:%s;', $css_property, $style_value ); 205 } 206 207 // We have a preset CSS variable as the style. 208 // Get the style value from the string and return CSS style. 209 $index_to_splice = strrpos( $style_value, '|' ) + 1; 210 $slug = substr( $style_value, $index_to_splice ); 211 212 // Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`. 213 return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug ); 214 } 215 216 // Register the block support. 217 WP_Block_Supports::get_instance()->register( 218 'typography', 219 array( 220 'register_attribute' => 'wp_register_typography_support', 221 'apply' => 'wp_apply_typography_support', 222 ) 223 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |