Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-inclu.../block-su...
File: typography.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Typography block support flag.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @since 5.6.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Registers the style and typography block attributes for block types that support it.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 5.6.0
[11] Fix | Delete
* @since 6.3.0 Added support for text-columns.
[12] Fix | Delete
* @access private
[13] Fix | Delete
*
[14] Fix | Delete
* @param WP_Block_Type $block_type Block Type.
[15] Fix | Delete
*/
[16] Fix | Delete
function wp_register_typography_support( $block_type ) {
[17] Fix | Delete
if ( ! ( $block_type instanceof WP_Block_Type ) ) {
[18] Fix | Delete
return;
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
$typography_supports = isset( $block_type->supports['typography'] ) ? $block_type->supports['typography'] : false;
[22] Fix | Delete
if ( ! $typography_supports ) {
[23] Fix | Delete
return;
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
$has_font_family_support = isset( $typography_supports['__experimentalFontFamily'] ) ? $typography_supports['__experimentalFontFamily'] : false;
[27] Fix | Delete
$has_font_size_support = isset( $typography_supports['fontSize'] ) ? $typography_supports['fontSize'] : false;
[28] Fix | Delete
$has_font_style_support = isset( $typography_supports['__experimentalFontStyle'] ) ? $typography_supports['__experimentalFontStyle'] : false;
[29] Fix | Delete
$has_font_weight_support = isset( $typography_supports['__experimentalFontWeight'] ) ? $typography_supports['__experimentalFontWeight'] : false;
[30] Fix | Delete
$has_letter_spacing_support = isset( $typography_supports['__experimentalLetterSpacing'] ) ? $typography_supports['__experimentalLetterSpacing'] : false;
[31] Fix | Delete
$has_line_height_support = isset( $typography_supports['lineHeight'] ) ? $typography_supports['lineHeight'] : false;
[32] Fix | Delete
$has_text_align_support = isset( $typography_supports['textAlign'] ) ? $typography_supports['textAlign'] : false;
[33] Fix | Delete
$has_text_columns_support = isset( $typography_supports['textColumns'] ) ? $typography_supports['textColumns'] : false;
[34] Fix | Delete
$has_text_decoration_support = isset( $typography_supports['__experimentalTextDecoration'] ) ? $typography_supports['__experimentalTextDecoration'] : false;
[35] Fix | Delete
$has_text_transform_support = isset( $typography_supports['__experimentalTextTransform'] ) ? $typography_supports['__experimentalTextTransform'] : false;
[36] Fix | Delete
$has_writing_mode_support = isset( $typography_supports['__experimentalWritingMode'] ) ? $typography_supports['__experimentalWritingMode'] : false;
[37] Fix | Delete
[38] Fix | Delete
$has_typography_support = $has_font_family_support
[39] Fix | Delete
|| $has_font_size_support
[40] Fix | Delete
|| $has_font_style_support
[41] Fix | Delete
|| $has_font_weight_support
[42] Fix | Delete
|| $has_letter_spacing_support
[43] Fix | Delete
|| $has_line_height_support
[44] Fix | Delete
|| $has_text_align_support
[45] Fix | Delete
|| $has_text_columns_support
[46] Fix | Delete
|| $has_text_decoration_support
[47] Fix | Delete
|| $has_text_transform_support
[48] Fix | Delete
|| $has_writing_mode_support;
[49] Fix | Delete
[50] Fix | Delete
if ( ! $block_type->attributes ) {
[51] Fix | Delete
$block_type->attributes = array();
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
if ( $has_typography_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
[55] Fix | Delete
$block_type->attributes['style'] = array(
[56] Fix | Delete
'type' => 'object',
[57] Fix | Delete
);
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) {
[61] Fix | Delete
$block_type->attributes['fontSize'] = array(
[62] Fix | Delete
'type' => 'string',
[63] Fix | Delete
);
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
if ( $has_font_family_support && ! array_key_exists( 'fontFamily', $block_type->attributes ) ) {
[67] Fix | Delete
$block_type->attributes['fontFamily'] = array(
[68] Fix | Delete
'type' => 'string',
[69] Fix | Delete
);
[70] Fix | Delete
}
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* Adds CSS classes and inline styles for typography features such as font sizes
[75] Fix | Delete
* to the incoming attributes array. This will be applied to the block markup in
[76] Fix | Delete
* the front-end.
[77] Fix | Delete
*
[78] Fix | Delete
* @since 5.6.0
[79] Fix | Delete
* @since 6.1.0 Used the style engine to generate CSS and classnames.
[80] Fix | Delete
* @since 6.3.0 Added support for text-columns.
[81] Fix | Delete
* @access private
[82] Fix | Delete
*
[83] Fix | Delete
* @param WP_Block_Type $block_type Block type.
[84] Fix | Delete
* @param array $block_attributes Block attributes.
[85] Fix | Delete
* @return array Typography CSS classes and inline styles.
[86] Fix | Delete
*/
[87] Fix | Delete
function wp_apply_typography_support( $block_type, $block_attributes ) {
[88] Fix | Delete
if ( ! ( $block_type instanceof WP_Block_Type ) ) {
[89] Fix | Delete
return array();
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
$typography_supports = isset( $block_type->supports['typography'] )
[93] Fix | Delete
? $block_type->supports['typography']
[94] Fix | Delete
: false;
[95] Fix | Delete
if ( ! $typography_supports ) {
[96] Fix | Delete
return array();
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
if ( wp_should_skip_block_supports_serialization( $block_type, 'typography' ) ) {
[100] Fix | Delete
return array();
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
$has_font_family_support = isset( $typography_supports['__experimentalFontFamily'] ) ? $typography_supports['__experimentalFontFamily'] : false;
[104] Fix | Delete
$has_font_size_support = isset( $typography_supports['fontSize'] ) ? $typography_supports['fontSize'] : false;
[105] Fix | Delete
$has_font_style_support = isset( $typography_supports['__experimentalFontStyle'] ) ? $typography_supports['__experimentalFontStyle'] : false;
[106] Fix | Delete
$has_font_weight_support = isset( $typography_supports['__experimentalFontWeight'] ) ? $typography_supports['__experimentalFontWeight'] : false;
[107] Fix | Delete
$has_letter_spacing_support = isset( $typography_supports['__experimentalLetterSpacing'] ) ? $typography_supports['__experimentalLetterSpacing'] : false;
[108] Fix | Delete
$has_line_height_support = isset( $typography_supports['lineHeight'] ) ? $typography_supports['lineHeight'] : false;
[109] Fix | Delete
$has_text_align_support = isset( $typography_supports['textAlign'] ) ? $typography_supports['textAlign'] : false;
[110] Fix | Delete
$has_text_columns_support = isset( $typography_supports['textColumns'] ) ? $typography_supports['textColumns'] : false;
[111] Fix | Delete
$has_text_decoration_support = isset( $typography_supports['__experimentalTextDecoration'] ) ? $typography_supports['__experimentalTextDecoration'] : false;
[112] Fix | Delete
$has_text_transform_support = isset( $typography_supports['__experimentalTextTransform'] ) ? $typography_supports['__experimentalTextTransform'] : false;
[113] Fix | Delete
$has_writing_mode_support = isset( $typography_supports['__experimentalWritingMode'] ) ? $typography_supports['__experimentalWritingMode'] : false;
[114] Fix | Delete
[115] Fix | Delete
// Whether to skip individual block support features.
[116] Fix | Delete
$should_skip_font_size = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' );
[117] Fix | Delete
$should_skip_font_family = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' );
[118] Fix | Delete
$should_skip_font_style = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' );
[119] Fix | Delete
$should_skip_font_weight = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' );
[120] Fix | Delete
$should_skip_line_height = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' );
[121] Fix | Delete
$should_skip_text_align = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textAlign' );
[122] Fix | Delete
$should_skip_text_columns = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textColumns' );
[123] Fix | Delete
$should_skip_text_decoration = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' );
[124] Fix | Delete
$should_skip_text_transform = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' );
[125] Fix | Delete
$should_skip_letter_spacing = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' );
[126] Fix | Delete
$should_skip_writing_mode = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'writingMode' );
[127] Fix | Delete
[128] Fix | Delete
$typography_block_styles = array();
[129] Fix | Delete
if ( $has_font_size_support && ! $should_skip_font_size ) {
[130] Fix | Delete
$preset_font_size = array_key_exists( 'fontSize', $block_attributes )
[131] Fix | Delete
? "var:preset|font-size|{$block_attributes['fontSize']}"
[132] Fix | Delete
: null;
[133] Fix | Delete
$custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] )
[134] Fix | Delete
? $block_attributes['style']['typography']['fontSize']
[135] Fix | Delete
: null;
[136] Fix | Delete
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : wp_get_typography_font_size_value(
[137] Fix | Delete
array(
[138] Fix | Delete
'size' => $custom_font_size,
[139] Fix | Delete
)
[140] Fix | Delete
);
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
if ( $has_font_family_support && ! $should_skip_font_family ) {
[144] Fix | Delete
$preset_font_family = array_key_exists( 'fontFamily', $block_attributes )
[145] Fix | Delete
? "var:preset|font-family|{$block_attributes['fontFamily']}"
[146] Fix | Delete
: null;
[147] Fix | Delete
$custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] )
[148] Fix | Delete
? wp_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontFamily'], 'font-family' )
[149] Fix | Delete
: null;
[150] Fix | Delete
$typography_block_styles['fontFamily'] = $preset_font_family ? $preset_font_family : $custom_font_family;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
if (
[154] Fix | Delete
$has_font_style_support &&
[155] Fix | Delete
! $should_skip_font_style &&
[156] Fix | Delete
isset( $block_attributes['style']['typography']['fontStyle'] )
[157] Fix | Delete
) {
[158] Fix | Delete
$typography_block_styles['fontStyle'] = wp_typography_get_preset_inline_style_value(
[159] Fix | Delete
$block_attributes['style']['typography']['fontStyle'],
[160] Fix | Delete
'font-style'
[161] Fix | Delete
);
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
if (
[165] Fix | Delete
$has_font_weight_support &&
[166] Fix | Delete
! $should_skip_font_weight &&
[167] Fix | Delete
isset( $block_attributes['style']['typography']['fontWeight'] )
[168] Fix | Delete
) {
[169] Fix | Delete
$typography_block_styles['fontWeight'] = wp_typography_get_preset_inline_style_value(
[170] Fix | Delete
$block_attributes['style']['typography']['fontWeight'],
[171] Fix | Delete
'font-weight'
[172] Fix | Delete
);
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
if ( $has_line_height_support && ! $should_skip_line_height ) {
[176] Fix | Delete
$typography_block_styles['lineHeight'] = isset( $block_attributes['style']['typography']['lineHeight'] )
[177] Fix | Delete
? $block_attributes['style']['typography']['lineHeight']
[178] Fix | Delete
: null;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
if ( $has_text_align_support && ! $should_skip_text_align ) {
[182] Fix | Delete
$typography_block_styles['textAlign'] = isset( $block_attributes['style']['typography']['textAlign'] )
[183] Fix | Delete
? $block_attributes['style']['typography']['textAlign']
[184] Fix | Delete
: null;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
if ( $has_text_columns_support && ! $should_skip_text_columns && isset( $block_attributes['style']['typography']['textColumns'] ) ) {
[188] Fix | Delete
$typography_block_styles['textColumns'] = isset( $block_attributes['style']['typography']['textColumns'] )
[189] Fix | Delete
? $block_attributes['style']['typography']['textColumns']
[190] Fix | Delete
: null;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
if (
[194] Fix | Delete
$has_text_decoration_support &&
[195] Fix | Delete
! $should_skip_text_decoration &&
[196] Fix | Delete
isset( $block_attributes['style']['typography']['textDecoration'] )
[197] Fix | Delete
) {
[198] Fix | Delete
$typography_block_styles['textDecoration'] = wp_typography_get_preset_inline_style_value(
[199] Fix | Delete
$block_attributes['style']['typography']['textDecoration'],
[200] Fix | Delete
'text-decoration'
[201] Fix | Delete
);
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
if (
[205] Fix | Delete
$has_text_transform_support &&
[206] Fix | Delete
! $should_skip_text_transform &&
[207] Fix | Delete
isset( $block_attributes['style']['typography']['textTransform'] )
[208] Fix | Delete
) {
[209] Fix | Delete
$typography_block_styles['textTransform'] = wp_typography_get_preset_inline_style_value(
[210] Fix | Delete
$block_attributes['style']['typography']['textTransform'],
[211] Fix | Delete
'text-transform'
[212] Fix | Delete
);
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
if (
[216] Fix | Delete
$has_letter_spacing_support &&
[217] Fix | Delete
! $should_skip_letter_spacing &&
[218] Fix | Delete
isset( $block_attributes['style']['typography']['letterSpacing'] )
[219] Fix | Delete
) {
[220] Fix | Delete
$typography_block_styles['letterSpacing'] = wp_typography_get_preset_inline_style_value(
[221] Fix | Delete
$block_attributes['style']['typography']['letterSpacing'],
[222] Fix | Delete
'letter-spacing'
[223] Fix | Delete
);
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
if ( $has_writing_mode_support &&
[227] Fix | Delete
! $should_skip_writing_mode &&
[228] Fix | Delete
isset( $block_attributes['style']['typography']['writingMode'] )
[229] Fix | Delete
) {
[230] Fix | Delete
$typography_block_styles['writingMode'] = isset( $block_attributes['style']['typography']['writingMode'] )
[231] Fix | Delete
? $block_attributes['style']['typography']['writingMode']
[232] Fix | Delete
: null;
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
$attributes = array();
[236] Fix | Delete
$classnames = array();
[237] Fix | Delete
$styles = wp_style_engine_get_styles(
[238] Fix | Delete
array( 'typography' => $typography_block_styles ),
[239] Fix | Delete
array( 'convert_vars_to_classnames' => true )
[240] Fix | Delete
);
[241] Fix | Delete
[242] Fix | Delete
if ( ! empty( $styles['classnames'] ) ) {
[243] Fix | Delete
$classnames[] = $styles['classnames'];
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
if ( $has_text_align_support && ! $should_skip_text_align && isset( $block_attributes['style']['typography']['textAlign'] ) ) {
[247] Fix | Delete
$classnames[] = 'has-text-align-' . $block_attributes['style']['typography']['textAlign'];
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
if ( ! empty( $classnames ) ) {
[251] Fix | Delete
$attributes['class'] = implode( ' ', $classnames );
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
if ( ! empty( $styles['css'] ) ) {
[255] Fix | Delete
$attributes['style'] = $styles['css'];
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
return $attributes;
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
/**
[262] Fix | Delete
* Generates an inline style value for a typography feature e.g. text decoration,
[263] Fix | Delete
* text transform, and font style.
[264] Fix | Delete
*
[265] Fix | Delete
* Note: This function is for backwards compatibility.
[266] Fix | Delete
* * It is necessary to parse older blocks whose typography styles contain presets.
[267] Fix | Delete
* * It mostly replaces the deprecated `wp_typography_get_css_variable_inline_style()`,
[268] Fix | Delete
* but skips compiling a CSS declaration as the style engine takes over this role.
[269] Fix | Delete
* @link https://github.com/wordpress/gutenberg/pull/27555
[270] Fix | Delete
*
[271] Fix | Delete
* @since 6.1.0
[272] Fix | Delete
*
[273] Fix | Delete
* @param string $style_value A raw style value for a single typography feature from a block's style attribute.
[274] Fix | Delete
* @param string $css_property Slug for the CSS property the inline style sets.
[275] Fix | Delete
* @return string A CSS inline style value.
[276] Fix | Delete
*/
[277] Fix | Delete
function wp_typography_get_preset_inline_style_value( $style_value, $css_property ) {
[278] Fix | Delete
// If the style value is not a preset CSS variable go no further.
[279] Fix | Delete
if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
[280] Fix | Delete
return $style_value;
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/*
[284] Fix | Delete
* For backwards compatibility.
[285] Fix | Delete
* Presets were removed in WordPress/gutenberg#27555.
[286] Fix | Delete
* A preset CSS variable is the style.
[287] Fix | Delete
* Gets the style value from the string and return CSS style.
[288] Fix | Delete
*/
[289] Fix | Delete
$index_to_splice = strrpos( $style_value, '|' ) + 1;
[290] Fix | Delete
$slug = _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
[291] Fix | Delete
[292] Fix | Delete
// Return the actual CSS inline style value,
[293] Fix | Delete
// e.g. `var(--wp--preset--text-decoration--underline);`.
[294] Fix | Delete
return sprintf( 'var(--wp--preset--%s--%s);', $css_property, $slug );
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Renders typography styles/content to the block wrapper.
[299] Fix | Delete
*
[300] Fix | Delete
* @since 6.1.0
[301] Fix | Delete
*
[302] Fix | Delete
* @param string $block_content Rendered block content.
[303] Fix | Delete
* @param array $block Block object.
[304] Fix | Delete
* @return string Filtered block content.
[305] Fix | Delete
*/
[306] Fix | Delete
function wp_render_typography_support( $block_content, $block ) {
[307] Fix | Delete
if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) {
[308] Fix | Delete
return $block_content;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
$custom_font_size = $block['attrs']['style']['typography']['fontSize'];
[312] Fix | Delete
$fluid_font_size = wp_get_typography_font_size_value( array( 'size' => $custom_font_size ) );
[313] Fix | Delete
[314] Fix | Delete
/*
[315] Fix | Delete
* Checks that $fluid_font_size does not match $custom_font_size,
[316] Fix | Delete
* which means it's been mutated by the fluid font size functions.
[317] Fix | Delete
*/
[318] Fix | Delete
if ( ! empty( $fluid_font_size ) && $fluid_font_size !== $custom_font_size ) {
[319] Fix | Delete
// Replaces the first instance of `font-size:$custom_font_size` with `font-size:$fluid_font_size`.
[320] Fix | Delete
return preg_replace( '/font-size\s*:\s*' . preg_quote( $custom_font_size, '/' ) . '\s*;?/', 'font-size:' . esc_attr( $fluid_font_size ) . ';', $block_content, 1 );
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
return $block_content;
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Checks a string for a unit and value and returns an array
[328] Fix | Delete
* consisting of `'value'` and `'unit'`, e.g. array( '42', 'rem' ).
[329] Fix | Delete
*
[330] Fix | Delete
* @since 6.1.0
[331] Fix | Delete
*
[332] Fix | Delete
* @param string|int|float $raw_value Raw size value from theme.json.
[333] Fix | Delete
* @param array $options {
[334] Fix | Delete
* Optional. An associative array of options. Default is empty array.
[335] Fix | Delete
*
[336] Fix | Delete
* @type string $coerce_to Coerce the value to rem or px. Default `'rem'`.
[337] Fix | Delete
* @type int $root_size_value Value of root font size for rem|em <-> px conversion. Default `16`.
[338] Fix | Delete
* @type string[] $acceptable_units An array of font size units. Default `array( 'rem', 'px', 'em' )`;
[339] Fix | Delete
* }
[340] Fix | Delete
* @return array|null An array consisting of `'value'` and `'unit'` properties on success.
[341] Fix | Delete
* `null` on failure.
[342] Fix | Delete
*/
[343] Fix | Delete
function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
[344] Fix | Delete
if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) {
[345] Fix | Delete
_doing_it_wrong(
[346] Fix | Delete
__FUNCTION__,
[347] Fix | Delete
__( 'Raw size value must be a string, integer, or float.' ),
[348] Fix | Delete
'6.1.0'
[349] Fix | Delete
);
[350] Fix | Delete
return null;
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
if ( empty( $raw_value ) ) {
[354] Fix | Delete
return null;
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
// Converts numbers to pixel values by default.
[358] Fix | Delete
if ( is_numeric( $raw_value ) ) {
[359] Fix | Delete
$raw_value = $raw_value . 'px';
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
$defaults = array(
[363] Fix | Delete
'coerce_to' => '',
[364] Fix | Delete
'root_size_value' => 16,
[365] Fix | Delete
'acceptable_units' => array( 'rem', 'px', 'em' ),
[366] Fix | Delete
);
[367] Fix | Delete
[368] Fix | Delete
$options = wp_parse_args( $options, $defaults );
[369] Fix | Delete
[370] Fix | Delete
$acceptable_units_group = implode( '|', $options['acceptable_units'] );
[371] Fix | Delete
$pattern = '/^(\d*\.?\d+)(' . $acceptable_units_group . '){1,1}$/';
[372] Fix | Delete
[373] Fix | Delete
preg_match( $pattern, $raw_value, $matches );
[374] Fix | Delete
[375] Fix | Delete
// Bails out if not a number value and a px or rem unit.
[376] Fix | Delete
if ( ! isset( $matches[1] ) || ! isset( $matches[2] ) ) {
[377] Fix | Delete
return null;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
$value = $matches[1];
[381] Fix | Delete
$unit = $matches[2];
[382] Fix | Delete
[383] Fix | Delete
/*
[384] Fix | Delete
* Default browser font size. Later, possibly could inject some JS to
[385] Fix | Delete
* compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
[386] Fix | Delete
*/
[387] Fix | Delete
if ( 'px' === $options['coerce_to'] && ( 'em' === $unit || 'rem' === $unit ) ) {
[388] Fix | Delete
$value = $value * $options['root_size_value'];
[389] Fix | Delete
$unit = $options['coerce_to'];
[390] Fix | Delete
}
[391] Fix | Delete
[392] Fix | Delete
if ( 'px' === $unit && ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) ) {
[393] Fix | Delete
$value = $value / $options['root_size_value'];
[394] Fix | Delete
$unit = $options['coerce_to'];
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
/*
[398] Fix | Delete
* No calculation is required if swapping between em and rem yet,
[399] Fix | Delete
* since we assume a root size value. Later we might like to differentiate between
[400] Fix | Delete
* :root font size (rem) and parent element font size (em) relativity.
[401] Fix | Delete
*/
[402] Fix | Delete
if ( ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) && ( 'em' === $unit || 'rem' === $unit ) ) {
[403] Fix | Delete
$unit = $options['coerce_to'];
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
return array(
[407] Fix | Delete
'value' => round( $value, 3 ),
[408] Fix | Delete
'unit' => $unit,
[409] Fix | Delete
);
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
/**
[413] Fix | Delete
* Internal implementation of CSS clamp() based on available min/max viewport
[414] Fix | Delete
* width and min/max font sizes.
[415] Fix | Delete
*
[416] Fix | Delete
* @since 6.1.0
[417] Fix | Delete
* @since 6.3.0 Checks for unsupported min/max viewport values that cause invalid clamp values.
[418] Fix | Delete
* @since 6.5.0 Returns early when min and max viewport subtraction is zero to avoid division by zero.
[419] Fix | Delete
* @access private
[420] Fix | Delete
*
[421] Fix | Delete
* @param array $args {
[422] Fix | Delete
* Optional. An associative array of values to calculate a fluid formula
[423] Fix | Delete
* for font size. Default is empty array.
[424] Fix | Delete
*
[425] Fix | Delete
* @type string $maximum_viewport_width Maximum size up to which type will have fluidity.
[426] Fix | Delete
* @type string $minimum_viewport_width Minimum viewport size from which type will have fluidity.
[427] Fix | Delete
* @type string $maximum_font_size Maximum font size for any clamp() calculation.
[428] Fix | Delete
* @type string $minimum_font_size Minimum font size for any clamp() calculation.
[429] Fix | Delete
* @type int $scale_factor A scale factor to determine how fast a font scales within boundaries.
[430] Fix | Delete
* }
[431] Fix | Delete
* @return string|null A font-size value using clamp() on success, otherwise null.
[432] Fix | Delete
*/
[433] Fix | Delete
function wp_get_computed_fluid_typography_value( $args = array() ) {
[434] Fix | Delete
$maximum_viewport_width_raw = isset( $args['maximum_viewport_width'] ) ? $args['maximum_viewport_width'] : null;
[435] Fix | Delete
$minimum_viewport_width_raw = isset( $args['minimum_viewport_width'] ) ? $args['minimum_viewport_width'] : null;
[436] Fix | Delete
$maximum_font_size_raw = isset( $args['maximum_font_size'] ) ? $args['maximum_font_size'] : null;
[437] Fix | Delete
$minimum_font_size_raw = isset( $args['minimum_font_size'] ) ? $args['minimum_font_size'] : null;
[438] Fix | Delete
$scale_factor = isset( $args['scale_factor'] ) ? $args['scale_factor'] : null;
[439] Fix | Delete
[440] Fix | Delete
// Normalizes the minimum font size in order to use the value for calculations.
[441] Fix | Delete
$minimum_font_size = wp_get_typography_value_and_unit( $minimum_font_size_raw );
[442] Fix | Delete
[443] Fix | Delete
/*
[444] Fix | Delete
* We get a 'preferred' unit to keep units consistent when calculating,
[445] Fix | Delete
* otherwise the result will not be accurate.
[446] Fix | Delete
*/
[447] Fix | Delete
$font_size_unit = isset( $minimum_font_size['unit'] ) ? $minimum_font_size['unit'] : 'rem';
[448] Fix | Delete
[449] Fix | Delete
// Normalizes the maximum font size in order to use the value for calculations.
[450] Fix | Delete
$maximum_font_size = wp_get_typography_value_and_unit(
[451] Fix | Delete
$maximum_font_size_raw,
[452] Fix | Delete
array(
[453] Fix | Delete
'coerce_to' => $font_size_unit,
[454] Fix | Delete
)
[455] Fix | Delete
);
[456] Fix | Delete
[457] Fix | Delete
// Checks for mandatory min and max sizes, and protects against unsupported units.
[458] Fix | Delete
if ( ! $maximum_font_size || ! $minimum_font_size ) {
[459] Fix | Delete
return null;
[460] Fix | Delete
}
[461] Fix | Delete
[462] Fix | Delete
// Uses rem for accessible fluid target font scaling.
[463] Fix | Delete
$minimum_font_size_rem = wp_get_typography_value_and_unit(
[464] Fix | Delete
$minimum_font_size_raw,
[465] Fix | Delete
array(
[466] Fix | Delete
'coerce_to' => 'rem',
[467] Fix | Delete
)
[468] Fix | Delete
);
[469] Fix | Delete
[470] Fix | Delete
// Viewport widths defined for fluid typography. Normalize units.
[471] Fix | Delete
$maximum_viewport_width = wp_get_typography_value_and_unit(
[472] Fix | Delete
$maximum_viewport_width_raw,
[473] Fix | Delete
array(
[474] Fix | Delete
'coerce_to' => $font_size_unit,
[475] Fix | Delete
)
[476] Fix | Delete
);
[477] Fix | Delete
$minimum_viewport_width = wp_get_typography_value_and_unit(
[478] Fix | Delete
$minimum_viewport_width_raw,
[479] Fix | Delete
array(
[480] Fix | Delete
'coerce_to' => $font_size_unit,
[481] Fix | Delete
)
[482] Fix | Delete
);
[483] Fix | Delete
[484] Fix | Delete
// Protects against unsupported units in min and max viewport widths.
[485] Fix | Delete
if ( ! $minimum_viewport_width || ! $maximum_viewport_width ) {
[486] Fix | Delete
return null;
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
// Calculates the linear factor denominator. If it's 0, we cannot calculate a fluid value.
[490] Fix | Delete
$linear_factor_denominator = $maximum_viewport_width['value'] - $minimum_viewport_width['value'];
[491] Fix | Delete
if ( empty( $linear_factor_denominator ) ) {
[492] Fix | Delete
return null;
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
/*
[496] Fix | Delete
* Build CSS rule.
[497] Fix | Delete
* Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
[498] Fix | Delete
*/
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function