| @@ -52,8 +52,14 @@ | ||
| 52 | 52 | $block_type->attributes['fontSize'] = array( |
| 53 | 53 | 'type' => 'string', |
| 54 | 54 | ); |
| 55 | 55 | } |
| 56 | + | |
| 57 | + if ( $has_font_family_support && ! array_key_exists( 'fontFamily', $block_type->attributes ) ) { | |
| 58 | + $block_type->attributes['fontFamily'] = array( | |
| 59 | + 'type' => 'string', | |
| 60 | + ); | |
| 61 | + } | |
| 56 | 62 | } |
| 57 | 63 | |
| 58 | 64 | /** |
| 59 | 65 | * Add CSS classes and inline styles for typography features such as font sizes |
| @@ -74,17 +80,12 @@ | ||
| 74 | 80 | if ( ! $typography_supports ) { |
| 75 | 81 | return array(); |
| 76 | 82 | } |
| 77 | 83 | |
| 78 | - $skip_typography_serialization = _wp_array_get( $typography_supports, array( '__experimentalSkipSerialization' ), false ); | |
| 79 | - if ( $skip_typography_serialization ) { | |
| 84 | + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'typography' ) ) { | |
| 80 | 85 | return array(); |
| 81 | 86 | } |
| 82 | 87 | |
| 83 | - $attributes = array(); | |
| 84 | - $classes = array(); | |
| 85 | - $styles = array(); | |
| 86 | - | |
| 87 | 88 | $has_font_family_support = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false ); |
| 88 | 89 | $has_font_size_support = _wp_array_get( $typography_supports, array( 'fontSize' ), false ); |
| 89 | 90 | $has_font_style_support = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false ); |
| 90 | 91 | $has_font_weight_support = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false ); |
| @@ -92,91 +93,116 @@ | ||
| 92 | 93 | $has_line_height_support = _wp_array_get( $typography_supports, array( 'lineHeight' ), false ); |
| 93 | 94 | $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false ); |
| 94 | 95 | $has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false ); |
| 95 | 96 | |
| 96 | - if ( $has_font_size_support ) { | |
| 97 | - $has_named_font_size = array_key_exists( 'fontSize', $block_attributes ); | |
| 98 | - $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ); | |
| 97 | + // Whether to skip individual block support features. | |
| 98 | + $should_skip_font_size = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' ); | |
| 99 | + $should_skip_font_family = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' ); | |
| 100 | + $should_skip_font_style = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' ); | |
| 101 | + $should_skip_font_weight = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' ); | |
| 102 | + $should_skip_line_height = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' ); | |
| 103 | + $should_skip_text_decoration = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' ); | |
| 104 | + $should_skip_text_transform = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' ); | |
| 105 | + $should_skip_letter_spacing = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' ); | |
| 99 | 106 | |
| 100 | - if ( $has_named_font_size ) { | |
| 101 | - $classes[] = sprintf( 'has-%s-font-size', _wp_to_kebab_case( $block_attributes['fontSize'] ) ); | |
| 102 | - } elseif ( $has_custom_font_size ) { | |
| 103 | - $styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] ); | |
| 104 | - } | |
| 107 | + $typography_block_styles = array(); | |
| 108 | + if ( $has_font_size_support && ! $should_skip_font_size ) { | |
| 109 | + $preset_font_size = array_key_exists( 'fontSize', $block_attributes ) ? "var:preset|font-size|{$block_attributes['fontSize']}" : null; | |
| 110 | + $custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ) ? $block_attributes['style']['typography']['fontSize'] : null; | |
| 111 | + $typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : gutenberg_get_typography_font_size_value( | |
| 112 | + array( | |
| 113 | + 'size' => $custom_font_size, | |
| 114 | + ) | |
| 115 | + ); | |
| 105 | 116 | } |
| 106 | 117 | |
| 107 | - if ( $has_font_family_support ) { | |
| 108 | - $has_named_font_family = array_key_exists( 'fontFamily', $block_attributes ); | |
| 109 | - $has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] ); | |
| 118 | + if ( $has_font_family_support && ! $should_skip_font_family ) { | |
| 119 | + $preset_font_family = array_key_exists( 'fontFamily', $block_attributes ) ? "var:preset|font-family|{$block_attributes['fontFamily']}" : null; | |
| 120 | + $custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] ) ? gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontFamily'], 'font-family' ) : null; | |
| 121 | + $typography_block_styles['fontFamily'] = $preset_font_family ? $preset_font_family : $custom_font_family; | |
| 122 | + } | |
| 110 | 123 | |
| 111 | - if ( $has_named_font_family ) { | |
| 112 | - $classes[] = sprintf( 'has-%s-font-family', _wp_to_kebab_case( $block_attributes['fontFamily'] ) ); | |
| 113 | - } elseif ( $has_custom_font_family ) { | |
| 114 | - // Before using classes, the value was serialized as a CSS Custom Property. | |
| 115 | - // We don't need this code path when it lands in core. | |
| 116 | - $font_family_custom = $block_attributes['style']['typography']['fontFamily']; | |
| 117 | - if ( strpos( $font_family_custom, 'var:preset|font-family' ) !== false ) { | |
| 118 | - $index_to_splice = strrpos( $font_family_custom, '|' ) + 1; | |
| 119 | - $font_family_slug = _wp_to_kebab_case( substr( $font_family_custom, $index_to_splice ) ); | |
| 120 | - $font_family_custom = sprintf( 'var(--wp--preset--font-family--%s)', $font_family_slug ); | |
| 121 | - } | |
| 122 | - $styles[] = sprintf( 'font-family: %s;', $font_family_custom ); | |
| 123 | - } | |
| 124 | + if ( $has_font_style_support && ! $should_skip_font_style && isset( $block_attributes['style']['typography']['fontStyle'] ) ) { | |
| 125 | + $typography_block_styles['fontStyle'] = | |
| 126 | + gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontStyle'], 'font-style' ); | |
| 124 | 127 | } |
| 125 | 128 | |
| 126 | - if ( $has_font_style_support ) { | |
| 127 | - $font_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' ); | |
| 128 | - if ( $font_style ) { | |
| 129 | - $styles[] = $font_style; | |
| 130 | - } | |
| 129 | + if ( $has_font_weight_support && ! $should_skip_font_weight && isset( $block_attributes['style']['typography']['fontWeight'] ) ) { | |
| 130 | + $typography_block_styles['fontWeight'] = | |
| 131 | + gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontWeight'], 'font-weight' ); | |
| 131 | 132 | } |
| 132 | 133 | |
| 133 | - if ( $has_font_weight_support ) { | |
| 134 | - $font_weight = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' ); | |
| 135 | - if ( $font_weight ) { | |
| 136 | - $styles[] = $font_weight; | |
| 137 | - } | |
| 134 | + if ( $has_line_height_support && ! $should_skip_line_height ) { | |
| 135 | + $typography_block_styles['lineHeight'] = _wp_array_get( $block_attributes, array( 'style', 'typography', 'lineHeight' ), null ); | |
| 138 | 136 | } |
| 139 | 137 | |
| 140 | - if ( $has_line_height_support ) { | |
| 141 | - $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] ); | |
| 142 | - if ( $has_line_height ) { | |
| 143 | - $styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); | |
| 144 | - } | |
| 138 | + if ( $has_text_decoration_support && ! $should_skip_text_decoration && isset( $block_attributes['style']['typography']['textDecoration'] ) ) { | |
| 139 | + $typography_block_styles['textDecoration'] = | |
| 140 | + gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['textDecoration'], 'text-decoration' ); | |
| 145 | 141 | } |
| 146 | 142 | |
| 147 | - if ( $has_text_decoration_support ) { | |
| 148 | - $text_decoration_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' ); | |
| 149 | - if ( $text_decoration_style ) { | |
| 150 | - $styles[] = $text_decoration_style; | |
| 151 | - } | |
| 143 | + if ( $has_text_transform_support && ! $should_skip_text_transform && isset( $block_attributes['style']['typography']['textTransform'] ) ) { | |
| 144 | + $typography_block_styles['textTransform'] = | |
| 145 | + gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['textTransform'], 'text-transform' ); | |
| 152 | 146 | } |
| 153 | 147 | |
| 154 | - if ( $has_text_transform_support ) { | |
| 155 | - $text_transform_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' ); | |
| 156 | - if ( $text_transform_style ) { | |
| 157 | - $styles[] = $text_transform_style; | |
| 158 | - } | |
| 148 | + if ( $has_letter_spacing_support && ! $should_skip_letter_spacing && isset( $block_attributes['style']['typography']['letterSpacing'] ) ) { | |
| 149 | + $typography_block_styles['letterSpacing'] = | |
| 150 | + gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['letterSpacing'], 'letter-spacing' ); | |
| 159 | 151 | } |
| 160 | 152 | |
| 161 | - if ( $has_letter_spacing_support ) { | |
| 162 | - $letter_spacing_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' ); | |
| 163 | - if ( $letter_spacing_style ) { | |
| 164 | - $styles[] = $letter_spacing_style; | |
| 165 | - } | |
| 153 | + $attributes = array(); | |
| 154 | + $styles = gutenberg_style_engine_get_styles( | |
| 155 | + array( 'typography' => $typography_block_styles ), | |
| 156 | + array( 'convert_vars_to_classnames' => true ) | |
| 157 | + ); | |
| 158 | + | |
| 159 | + if ( ! empty( $styles['classnames'] ) ) { | |
| 160 | + $attributes['class'] = $styles['classnames']; | |
| 166 | 161 | } |
| 167 | 162 | |
| 168 | - if ( ! empty( $classes ) ) { | |
| 169 | - $attributes['class'] = implode( ' ', $classes ); | |
| 163 | + if ( ! empty( $styles['css'] ) ) { | |
| 164 | + $attributes['style'] = $styles['css']; | |
| 170 | 165 | } |
| 171 | - if ( ! empty( $styles ) ) { | |
| 172 | - $attributes['style'] = implode( ' ', $styles ); | |
| 166 | + | |
| 167 | + return $attributes; | |
| 168 | +} | |
| 169 | + | |
| 170 | +/** | |
| 171 | + * Note: this method is for backwards compatibility. | |
| 172 | + * It mostly replaces `gutenberg_typography_get_css_variable_inline_style()`. | |
| 173 | + * | |
| 174 | + * Generates an inline style value for a typography feature e.g. text decoration, | |
| 175 | + * text transform, and font style. | |
| 176 | + * | |
| 177 | + * @param string $style_value A raw style value for a single typography feature from a block's style attribute. | |
| 178 | + * @param string $css_property Slug for the CSS property the inline style sets. | |
| 179 | + * | |
| 180 | + * @return string? A CSS inline style value. | |
| 181 | + */ | |
| 182 | +function gutenberg_typography_get_preset_inline_style_value( $style_value, $css_property ) { | |
| 183 | + // If the style value is not a preset CSS variable go no further. | |
| 184 | + if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) { | |
| 185 | + return $style_value; | |
| 173 | 186 | } |
| 174 | 187 | |
| 175 | - return $attributes; | |
| 188 | + // For backwards compatibility. | |
| 189 | + // Presets were removed in https://github.com/WordPress/gutenberg/pull/27555. | |
| 190 | + // We have a preset CSS variable as the style. | |
| 191 | + // Get the style value from the string and return CSS style. | |
| 192 | + $index_to_splice = strrpos( $style_value, '|' ) + 1; | |
| 193 | + $slug = _wp_to_kebab_case( substr( $style_value, $index_to_splice ) ); | |
| 194 | + | |
| 195 | + // Return the actual CSS inline style value e.g. `var(--wp--preset--text-decoration--underline);`. | |
| 196 | + return sprintf( 'var(--wp--preset--%s--%s);', $css_property, $slug ); | |
| 176 | 197 | } |
| 177 | 198 | |
| 178 | 199 | /** |
| 200 | + * Deprecated. | |
| 201 | + * This method is no longer used and will have to be deprecated in Core. | |
| 202 | + * | |
| 203 | + * It can be deleted once migrated to the next WordPress version. | |
| 204 | + * | |
| 179 | 205 | * Generates an inline style for a typography feature e.g. text decoration, |
| 180 | 206 | * text transform, and font style. |
| 181 | 207 | * |
| 182 | 208 | * @param array $attributes Block's attributes. |
| @@ -192,9 +218,9 @@ | ||
| 192 | 218 | return; |
| 193 | 219 | } |
| 194 | 220 | |
| 195 | 221 | // If we don't have a preset CSS variable, we'll assume it's a regular CSS value. |
| 196 | - if ( strpos( $style_value, "var:preset|{$css_property}|" ) === false ) { | |
| 222 | + if ( ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) { | |
| 197 | 223 | return sprintf( '%s:%s;', $css_property, $style_value ); |
| 198 | 224 | } |
| 199 | 225 | |
| 200 | 226 | // We have a preset CSS variable as the style. |
| @@ -205,8 +231,322 @@ | ||
| 205 | 231 | // Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`. |
| 206 | 232 | return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug ); |
| 207 | 233 | } |
| 208 | 234 | |
| 235 | +/** | |
| 236 | + * Renders typography styles/content to the block wrapper. | |
| 237 | + * | |
| 238 | + * @param string $block_content Rendered block content. | |
| 239 | + * @param array $block Block object. | |
| 240 | + * @return string Filtered block content. | |
| 241 | + */ | |
| 242 | +function gutenberg_render_typography_support( $block_content, $block ) { | |
| 243 | + if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) { | |
| 244 | + return $block_content; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $custom_font_size = $block['attrs']['style']['typography']['fontSize']; | |
| 248 | + $fluid_font_size = gutenberg_get_typography_font_size_value( array( 'size' => $custom_font_size ) ); | |
| 249 | + | |
| 250 | + /* | |
| 251 | + * Checks that $fluid_font_size does not match $custom_font_size, | |
| 252 | + * which means it's been mutated by the fluid font size functions. | |
| 253 | + */ | |
| 254 | + if ( ! empty( $fluid_font_size ) && $fluid_font_size !== $custom_font_size ) { | |
| 255 | + // Replaces the first instance of `font-size:$custom_font_size` with `font-size:$fluid_font_size`. | |
| 256 | + return preg_replace( '/font-size\s*:\s*' . preg_quote( $custom_font_size, '/' ) . '\s*;?/', 'font-size:' . esc_attr( $fluid_font_size ) . ';', $block_content, 1 ); | |
| 257 | + } | |
| 258 | + | |
| 259 | + return $block_content; | |
| 260 | + | |
| 261 | +} | |
| 262 | + | |
| 263 | +/** | |
| 264 | + * Internal method that checks a string for a unit and value and returns an array consisting of `'value'` and `'unit'`, e.g., [ '42', 'rem' ]. | |
| 265 | + * A raw font size of `value + unit` is expected. If the value is a number, it will convert to `value + 'px'`. | |
| 266 | + * | |
| 267 | + * @access private | |
| 268 | + * | |
| 269 | + * @param string|int|float $raw_value Raw size value from theme.json. | |
| 270 | + * @param array $options { | |
| 271 | + * Optional. An associative array of options. Default is empty array. | |
| 272 | + * | |
| 273 | + * @type string $coerce_to Coerce the value to rem or px. Default `'rem'`. | |
| 274 | + * @type int $root_size_value Value of root font size for rem|em <-> px conversion. Default `16`. | |
| 275 | + * @type array<string> $acceptable_units An array of font size units. Default `[ 'rem', 'px', 'em' ]`; | |
| 276 | + * } | |
| 277 | + * @return array An array consisting of `'value'` and `'unit'` properties. | |
| 278 | + */ | |
| 279 | +function gutenberg_get_typography_value_and_unit( $raw_value, $options = array() ) { | |
| 280 | + if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) { | |
| 281 | + _doing_it_wrong( | |
| 282 | + __FUNCTION__, | |
| 283 | + __( 'Raw size value must be a string, integer or a float.', 'gutenberg' ), | |
| 284 | + '6.1.0' | |
| 285 | + ); | |
| 286 | + return null; | |
| 287 | + } | |
| 288 | + | |
| 289 | + // Converts numeric values to pixel values by default. | |
| 290 | + if ( empty( $raw_value ) ) { | |
| 291 | + return null; | |
| 292 | + } | |
| 293 | + | |
| 294 | + // Converts numbers to pixel values by default. | |
| 295 | + if ( is_numeric( $raw_value ) ) { | |
| 296 | + $raw_value = $raw_value . 'px'; | |
| 297 | + } | |
| 298 | + | |
| 299 | + $defaults = array( | |
| 300 | + 'coerce_to' => '', | |
| 301 | + 'root_size_value' => 16, | |
| 302 | + 'acceptable_units' => array( 'rem', 'px', 'em' ), | |
| 303 | + ); | |
| 304 | + | |
| 305 | + $options = wp_parse_args( $options, $defaults ); | |
| 306 | + | |
| 307 | + $acceptable_units_group = implode( '|', $options['acceptable_units'] ); | |
| 308 | + $pattern = '/^(\d*\.?\d+)(' . $acceptable_units_group . '){1,1}$/'; | |
| 309 | + | |
| 310 | + preg_match( $pattern, $raw_value, $matches ); | |
| 311 | + | |
| 312 | + // We need a number value and a px or rem unit. | |
| 313 | + if ( ! isset( $matches[1] ) || ! isset( $matches[2] ) ) { | |
| 314 | + return null; | |
| 315 | + } | |
| 316 | + | |
| 317 | + $value = $matches[1]; | |
| 318 | + $unit = $matches[2]; | |
| 319 | + | |
| 320 | + // Default browser font size. Later we could inject some JS to compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`. | |
| 321 | + if ( 'px' === $options['coerce_to'] && ( 'em' === $unit || 'rem' === $unit ) ) { | |
| 322 | + $value = $value * $options['root_size_value']; | |
| 323 | + $unit = $options['coerce_to']; | |
| 324 | + } | |
| 325 | + | |
| 326 | + if ( 'px' === $unit && ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) ) { | |
| 327 | + $value = $value / $options['root_size_value']; | |
| 328 | + $unit = $options['coerce_to']; | |
| 329 | + } | |
| 330 | + | |
| 331 | + /* | |
| 332 | + * No calculation is required if swapping between em and rem yet, | |
| 333 | + * since we assume a root size value. Later we might like to differentiate between | |
| 334 | + * :root font size (rem) and parent element font size (em) relativity. | |
| 335 | + */ | |
| 336 | + if ( ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) && ( 'em' === $unit || 'rem' === $unit ) ) { | |
| 337 | + $unit = $options['coerce_to']; | |
| 338 | + } | |
| 339 | + | |
| 340 | + return array( | |
| 341 | + 'value' => round( $value, 3 ), | |
| 342 | + 'unit' => $unit, | |
| 343 | + ); | |
| 344 | +} | |
| 345 | + | |
| 346 | +/** | |
| 347 | + * Internal implementation of clamp() based on available min/max viewport width, and min/max font sizes. | |
| 348 | + * | |
| 349 | + * @access private | |
| 350 | + * | |
| 351 | + * @param array $args { | |
| 352 | + * Optional. An associative array of values to calculate a fluid formula for font size. Default is empty array. | |
| 353 | + * | |
| 354 | + * @type string $maximum_viewport_width Maximum size up to which type will have fluidity. | |
| 355 | + * @type string $minimum_viewport_width Minimum viewport size from which type will have fluidity. | |
| 356 | + * @type string $maximum_font_size Maximum font size for any clamp() calculation. | |
| 357 | + * @type string $minimum_font_size Minimum font size for any clamp() calculation. | |
| 358 | + * @type int $scale_factor A scale factor to determine how fast a font scales within boundaries. | |
| 359 | + * } | |
| 360 | + * @return string|null A font-size value using clamp(). | |
| 361 | + */ | |
| 362 | +function gutenberg_get_computed_fluid_typography_value( $args = array() ) { | |
| 363 | + $maximum_viewport_width_raw = isset( $args['maximum_viewport_width'] ) ? $args['maximum_viewport_width'] : null; | |
| 364 | + $minimum_viewport_width_raw = isset( $args['minimum_viewport_width'] ) ? $args['minimum_viewport_width'] : null; | |
| 365 | + $maximum_font_size_raw = isset( $args['maximum_font_size'] ) ? $args['maximum_font_size'] : null; | |
| 366 | + $minimum_font_size_raw = isset( $args['minimum_font_size'] ) ? $args['minimum_font_size'] : null; | |
| 367 | + $scale_factor = isset( $args['scale_factor'] ) ? $args['scale_factor'] : null; | |
| 368 | + | |
| 369 | + // Normalizes the minimum font size in order to use the value for calculations. | |
| 370 | + $minimum_font_size = gutenberg_get_typography_value_and_unit( $minimum_font_size_raw ); | |
| 371 | + | |
| 372 | + /* | |
| 373 | + * We get a 'preferred' unit to keep units consistent when calculating, | |
| 374 | + * otherwise the result will not be accurate. | |
| 375 | + */ | |
| 376 | + $font_size_unit = isset( $minimum_font_size['unit'] ) ? $minimum_font_size['unit'] : 'rem'; | |
| 377 | + | |
| 378 | + // Grabs the maximum font size and normalize it in order to use the value for calculations. | |
| 379 | + $maximum_font_size = gutenberg_get_typography_value_and_unit( | |
| 380 | + $maximum_font_size_raw, | |
| 381 | + array( | |
| 382 | + 'coerce_to' => $font_size_unit, | |
| 383 | + ) | |
| 384 | + ); | |
| 385 | + | |
| 386 | + // Checks for mandatory min and max sizes, and protects against unsupported units. | |
| 387 | + if ( ! $maximum_font_size || ! $minimum_font_size ) { | |
| 388 | + return null; | |
| 389 | + } | |
| 390 | + | |
| 391 | + // Uses rem for accessible fluid target font scaling. | |
| 392 | + $minimum_font_size_rem = gutenberg_get_typography_value_and_unit( | |
| 393 | + $minimum_font_size_raw, | |
| 394 | + array( | |
| 395 | + 'coerce_to' => 'rem', | |
| 396 | + ) | |
| 397 | + ); | |
| 398 | + | |
| 399 | + // Viewport widths defined for fluid typography. Normalize units. | |
| 400 | + $maximum_viewport_width = gutenberg_get_typography_value_and_unit( | |
| 401 | + $maximum_viewport_width_raw, | |
| 402 | + array( | |
| 403 | + 'coerce_to' => $font_size_unit, | |
| 404 | + ) | |
| 405 | + ); | |
| 406 | + $minimum_viewport_width = gutenberg_get_typography_value_and_unit( | |
| 407 | + $minimum_viewport_width_raw, | |
| 408 | + array( | |
| 409 | + 'coerce_to' => $font_size_unit, | |
| 410 | + ) | |
| 411 | + ); | |
| 412 | + | |
| 413 | + // Build CSS rule. | |
| 414 | + // Borrowed from https://websemantics.uk/tools/responsive-font-calculator/. | |
| 415 | + $view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit; | |
| 416 | + $linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $maximum_viewport_width['value'] - $minimum_viewport_width['value'] ) ); | |
| 417 | + $linear_factor_scaled = round( $linear_factor * $scale_factor, 3 ); | |
| 418 | + $linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled; | |
| 419 | + $fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)"; | |
| 420 | + | |
| 421 | + return "clamp($minimum_font_size_raw, $fluid_target_font_size, $maximum_font_size_raw)"; | |
| 422 | +} | |
| 423 | + | |
| 424 | +/** | |
| 425 | + * Returns a font-size value based on a given font-size preset. | |
| 426 | + * Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values. | |
| 427 | + * | |
| 428 | + * @param array $preset { | |
| 429 | + * Required. fontSizes preset value as seen in theme.json. | |
| 430 | + * | |
| 431 | + * @type string $name Name of the font size preset. | |
| 432 | + * @type string $slug Kebab-case unique identifier for the font size preset. | |
| 433 | + * @type string|int|float $size CSS font-size value, including units where applicable. | |
| 434 | + * } | |
| 435 | + * @param bool $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing. Default is `false`. | |
| 436 | + * | |
| 437 | + * @return string|null Font-size value or `null` if a size is not passed in $preset. | |
| 438 | + */ | |
| 439 | +function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) { | |
| 440 | + if ( ! isset( $preset['size'] ) ) { | |
| 441 | + return null; | |
| 442 | + } | |
| 443 | + | |
| 444 | + /* | |
| 445 | + * Catches empty values and 0/'0'. | |
| 446 | + * Fluid calculations cannot be performed on 0. | |
| 447 | + */ | |
| 448 | + if ( empty( $preset['size'] ) ) { | |
| 449 | + return $preset['size']; | |
| 450 | + } | |
| 451 | + | |
| 452 | + // Checks if fluid font sizes are activated. | |
| 453 | + $typography_settings = gutenberg_get_global_settings( array( 'typography' ) ); | |
| 454 | + $should_use_fluid_typography = isset( $typography_settings['fluid'] ) && true === $typography_settings['fluid'] ? true : $should_use_fluid_typography; | |
| 455 | + | |
| 456 | + if ( ! $should_use_fluid_typography ) { | |
| 457 | + return $preset['size']; | |
| 458 | + } | |
| 459 | + | |
| 460 | + // Defaults. | |
| 461 | + $default_maximum_viewport_width = '1600px'; | |
| 462 | + $default_minimum_viewport_width = '768px'; | |
| 463 | + $default_minimum_font_size_factor = 0.75; | |
| 464 | + $default_maximum_font_size_factor = 1.5; | |
| 465 | + $default_scale_factor = 1; | |
| 466 | + $default_minimum_font_size_limit = '14px'; | |
| 467 | + | |
| 468 | + // Font sizes. | |
| 469 | + $fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null; | |
| 470 | + | |
| 471 | + // A font size has explicitly bypassed fluid calculations. | |
| 472 | + if ( false === $fluid_font_size_settings ) { | |
| 473 | + return $preset['size']; | |
| 474 | + } | |
| 475 | + | |
| 476 | + // Try to grab explicit min and max fluid font sizes. | |
| 477 | + $minimum_font_size_raw = isset( $fluid_font_size_settings['min'] ) ? $fluid_font_size_settings['min'] : null; | |
| 478 | + $maximum_font_size_raw = isset( $fluid_font_size_settings['max'] ) ? $fluid_font_size_settings['max'] : null; | |
| 479 | + | |
| 480 | + // Font sizes. | |
| 481 | + $preferred_size = gutenberg_get_typography_value_and_unit( $preset['size'] ); | |
| 482 | + | |
| 483 | + // Protect against unsupported units. | |
| 484 | + if ( empty( $preferred_size['unit'] ) ) { | |
| 485 | + return $preset['size']; | |
| 486 | + } | |
| 487 | + | |
| 488 | + // If no fluid max font size is available, create one using max font size factor. | |
| 489 | + if ( ! $maximum_font_size_raw ) { | |
| 490 | + $maximum_font_size_raw = round( $preferred_size['value'] * $default_maximum_font_size_factor, 3 ) . $preferred_size['unit']; | |
| 491 | + } | |
| 492 | + | |
| 493 | + // If no fluid min font size is available, create one using min font size factor. | |
| 494 | + if ( ! $minimum_font_size_raw ) { | |
| 495 | + $minimum_font_size_raw = round( $preferred_size['value'] * $default_minimum_font_size_factor, 3 ) . $preferred_size['unit']; | |
| 496 | + } | |
| 497 | + | |
| 498 | + // Parses the minimum font size limit, so we can perform checks using it. | |
| 499 | + $minimum_font_size_limit = gutenberg_get_typography_value_and_unit( | |
| 500 | + $default_minimum_font_size_limit, | |
| 501 | + array( | |
| 502 | + 'coerce_to' => $preferred_size['unit'], | |
| 503 | + ) | |
| 504 | + ); | |
| 505 | + | |
| 506 | + if ( ! empty( $minimum_font_size_limit ) ) { | |
| 507 | + /* | |
| 508 | + * If a minimum size was not passed to this function | |
| 509 | + * and the user-defined font size is lower than $minimum_font_size_limit, | |
| 510 | + * then uses the user-defined font size as the minimum font-size. | |
| 511 | + */ | |
| 512 | + if ( ! isset( $fluid_font_size_settings['min'] ) && $preferred_size['value'] < $minimum_font_size_limit['value'] ) { | |
| 513 | + $minimum_font_size_raw = implode( '', $preferred_size ); | |
| 514 | + } else { | |
| 515 | + $minimum_font_size_parsed = gutenberg_get_typography_value_and_unit( | |
| 516 | + $minimum_font_size_raw, | |
| 517 | + array( | |
| 518 | + 'coerce_to' => $preferred_size['unit'], | |
| 519 | + ) | |
| 520 | + ); | |
| 521 | + | |
| 522 | + /* | |
| 523 | + * If the passed or calculated minimum font size is lower than $minimum_font_size_limit | |
| 524 | + * use $minimum_font_size_limit instead. | |
| 525 | + */ | |
| 526 | + if ( ! empty( $minimum_font_size_parsed ) && $minimum_font_size_parsed['value'] < $minimum_font_size_limit['value'] ) { | |
| 527 | + $minimum_font_size_raw = implode( '', $minimum_font_size_limit ); | |
| 528 | + } | |
| 529 | + } | |
| 530 | + } | |
| 531 | + | |
| 532 | + $fluid_font_size_value = gutenberg_get_computed_fluid_typography_value( | |
| 533 | + array( | |
| 534 | + 'minimum_viewport_width' => $default_minimum_viewport_width, | |
| 535 | + 'maximum_viewport_width' => $default_maximum_viewport_width, | |
| 536 | + 'minimum_font_size' => $minimum_font_size_raw, | |
| 537 | + 'maximum_font_size' => $maximum_font_size_raw, | |
| 538 | + 'scale_factor' => $default_scale_factor, | |
| 539 | + ) | |
| 540 | + ); | |
| 541 | + | |
| 542 | + if ( ! empty( $fluid_font_size_value ) ) { | |
| 543 | + return $fluid_font_size_value; | |
| 544 | + } | |
| 545 | + | |
| 546 | + return $preset['size']; | |
| 547 | +} | |
| 548 | + | |
| 209 | 549 | // Register the block support. |
| 210 | 550 | WP_Block_Supports::get_instance()->register( |
| 211 | 551 | 'typography', |
| 212 | 552 | array( |
| @@ -213,4 +553,9 @@ | ||
| 213 | 553 | 'register_attribute' => 'gutenberg_register_typography_support', |
| 214 | 554 | 'apply' => 'gutenberg_apply_typography_support', |
| 215 | 555 | ) |
| 216 | 556 | ); |
| 557 | + | |
| 558 | +if ( function_exists( 'wp_render_typography_support' ) ) { | |
| 559 | + remove_filter( 'render_block', 'wp_render_typography_support' ); | |
| 560 | +} | |
| 561 | +add_filter( 'render_block', 'gutenberg_render_typography_support', 10, 2 ); | |