wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_typography_font_size_value › WordPress Function

Since6.1.0
Deprecatedn/a
wp_get_typography_font_size_value ( $preset, $should_use_fluid_typography = false )
Parameters: (2)
  • (array) $preset { Required. fontSizes preset value as seen in theme.json. @type string $name Name of the font size preset. @type string $slug Kebab-case unique identifier for the font size preset. @type string $size CSS font-size value, including units where applicable. }
    Required: Yes
  • (bool) $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing. Default is `false`.
    Required: No
    Default: false
Returns:
  • (string) Font-size value.
Defined at:
Codex:

Returns a font-size value based on a given font-size preset.

Takes into account fluid typography parameters and attempts to return a CSS formula depending on available, valid values.


Source

function wp_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) {
	// Checks if fluid font sizes are activated.
	$typography_settings         = wp_get_global_settings( array( 'typography' ) );
	$should_use_fluid_typography = isset( $typography_settings['fluid'] ) && true === $typography_settings['fluid'] ? true : $should_use_fluid_typography;

	if ( ! $should_use_fluid_typography ) {
		return $preset['size'];
	}

	// Defaults.
	$default_maximum_viewport_width   = '1600px';
	$default_minimum_viewport_width   = '768px';
	$default_minimum_font_size_factor = 0.75;
	$default_maximum_font_size_factor = 1.5;
	$default_scale_factor             = 1;

	// Font sizes.
	$fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null;

	// A font size has explicitly bypassed fluid calculations.
	if ( false === $fluid_font_size_settings ) {
		return $preset['size'];
	}

	// Try to grab explicit min and max fluid font sizes.
	$minimum_font_size_raw = isset( $fluid_font_size_settings['min'] ) ? $fluid_font_size_settings['min'] : null;
	$maximum_font_size_raw = isset( $fluid_font_size_settings['max'] ) ? $fluid_font_size_settings['max'] : null;

	// Font sizes.
	$preferred_size = wp_get_typography_value_and_unit( $preset['size'] );

	// Protect against unsupported units.
	if ( empty( $preferred_size['unit'] ) ) {
		return $preset['size'];
	}

	// If no fluid min or max font sizes are available, create some using min/max font size factors.
	if ( ! $minimum_font_size_raw ) {
		$minimum_font_size_raw = ( $preferred_size['value'] * $default_minimum_font_size_factor ) . $preferred_size['unit'];
	}

	if ( ! $maximum_font_size_raw ) {
		$maximum_font_size_raw = ( $preferred_size['value'] * $default_maximum_font_size_factor ) . $preferred_size['unit'];
	}

	$fluid_font_size_value = wp_get_computed_fluid_typography_value(
		array(
			'minimum_viewport_width' => $default_minimum_viewport_width,
			'maximum_viewport_width' => $default_maximum_viewport_width,
			'minimum_font_size'      => $minimum_font_size_raw,
			'maximum_font_size'      => $maximum_font_size_raw,
			'scale_factor'           => $default_scale_factor,
		)
	);

	if ( ! empty( $fluid_font_size_value ) ) {
		return $fluid_font_size_value;
	}

	return $preset['size'];
}