Skip to content

Commit

Permalink
Updated comments
Browse files Browse the repository at this point in the history
Light refactoring
Added extra test to check for empty objects/arrays
  • Loading branch information
ramonjd committed Jan 30, 2024
1 parent c21c420 commit 80b31ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
15 changes: 7 additions & 8 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ function gutenberg_get_computed_fluid_typography_value( $args = array() ) {
* @since 6.2.0 Added 'settings.typography.fluid.minFontSize' support.
* @since 6.3.0 Using layout.wideSize as max viewport width, and logarithmic scale factor to calculate minimum font scale.
* @since 6.4.0 Added configurable min and max viewport width values to the typography.fluid theme.json schema.
* @since 6.5.0 Changing the type and name of the bool argument $should_use_fluid_typography.
* @since 6.5.0 Deprecated bool argument $should_use_fluid_typography.
*
* @param array $preset {
* Required. fontSizes preset value as seen in theme.json.
Expand All @@ -441,8 +441,7 @@ function gutenberg_get_computed_fluid_typography_value( $args = array() ) {
* @type string|int|float $size CSS font-size value, including units where applicable.
* }
* @param bool|array $settings Optional Theme JSON settings array that overrides any global theme settings.
* As a bool (deprecated), it acts as an override to switch fluid typography "on" (`true`) or "off" (`false`).
* Can be used for unit testing. Default is `array()`.
* Default is `array()`.
*
* @return string|null Font-size value or `null` if a size is not passed in $preset.
*/
Expand All @@ -461,7 +460,7 @@ function gutenberg_get_typography_font_size_value( $preset, $settings = array()

/*
* Backwards compatibility since 6.5.
* If the settings argument is a boolean, it's a fluid typography override.
* As a bool (deprecated since 6.5), $settings acts as an override to switch fluid typography "on" (`true`) or "off" (`false`).
*/
if ( is_bool( $settings ) ) {
_deprecated_argument( __FUNCTION__, '6.5.0', __( '`boolean` type for second argument `$settings` is deprecated. Use `array()` instead.', 'gutenberg' ) );
Expand All @@ -479,15 +478,15 @@ function gutenberg_get_typography_font_size_value( $preset, $settings = array()
$global_settings
);
$typography_settings = isset( $settings['typography'] ) ? $settings['typography'] : array();
$layout_settings = isset( $settings['layout'] ) ? $settings['layout'] : array();
$should_use_fluid_typography = isset( $typography_settings['fluid'] ) &&
( true === $typography_settings['fluid'] || is_array( $typography_settings['fluid'] ) );
$should_use_fluid_typography = isset( $typography_settings['fluid'] ) && ! empty( $typography_settings['fluid'] );

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

$fluid_settings = isset( $typography_settings['fluid'] ) && is_array( $typography_settings['fluid'] ) ? $typography_settings['fluid'] : array();
// $typography_settings['fluid'] can be a bool or an array. Normalize to array.
$fluid_settings = is_array( $typography_settings['fluid'] ) ? $typography_settings['fluid'] : array();
$layout_settings = isset( $settings['layout'] ) ? $settings['layout'] : array();

// Defaults.
$default_maximum_viewport_width = '1600px';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ describe( 'typography utils', () => {
expected: '28px',
},

{
message: 'should return value when fluid config is empty`',
preset: {
size: '28px',
},
settings: {
typography: {
fluid: {},
},
},
expected: '28px',
},

{
message:
'should return clamp value with `minViewportWidth` override',
Expand Down
14 changes: 13 additions & 1 deletion phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => null,
),

'returns value when fluid is `false`' => array(
'returns value when fluid config is empty`' => array(
'font_size' => array(
'size' => '28px',
'fluid' => false,
Expand All @@ -368,6 +368,18 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => '28px',
),

'returns value when fluid is empty array' => array(
'font_size' => array(
'size' => '28px',
),
'settings' => array(
'typography' => array(
'fluid' => array(),
),
),
'expected_output' => '28px',
),

'returns clamp value with minViewportWidth override' => array(
'font_size' => array(
'size' => '28px',
Expand Down

0 comments on commit 80b31ba

Please sign in to comment.