Skip to content

Commit

Permalink
Adding a filter pre-calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Oct 27, 2022
1 parent 8fe67cc commit 8f789fc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,27 @@ function gutenberg_get_typography_value_and_unit( $raw_value, $options = array()
* }
* @return string|null A font-size value using clamp().
*/
function gutenberg_get_computed_fluid_typography_value( $args = array() ) {
function gutenberg_get_computed_fluid_font_size_value( $args = array() ) {
/**
* Filters the fluid font size arguments array before the internal calculations take place.
*
* Return a non-null value to bypass the WordPress internal calculations.
*
* @param string|null $fluid_typography_value Return a font-size value using the passed arguments to short-circuit the default calculations,
* or null to allow WordPress to calculate the font size value.
* @param array $args array(
* 'maximum_viewport_width' => (string) Maximum size up to which type will have fluidity.
* 'minimum_viewport_width' => (string) Minimum viewport size from which type will have fluidity.
* 'maximum_font_size' => (string) Maximum font size.
* 'minimum_font_size' => (string) Minimum font size.
* 'scale_factor' => (number) A scale factor to determine how fast a font scales within boundaries.
* );.
*/
$fluid_typography_value = apply_filters( 'pre_get_computed_fluid_font_size_value', null, $args );
if ( ! is_null( $fluid_typography_value ) ) {
return $fluid_typography_value;
}

$maximum_viewport_width_raw = isset( $args['maximum_viewport_width'] ) ? $args['maximum_viewport_width'] : null;
$minimum_viewport_width_raw = isset( $args['minimum_viewport_width'] ) ? $args['minimum_viewport_width'] : null;
$maximum_font_size_raw = isset( $args['maximum_font_size'] ) ? $args['maximum_font_size'] : null;
Expand Down Expand Up @@ -529,7 +549,7 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty
}
}

$fluid_font_size_value = gutenberg_get_computed_fluid_typography_value(
$fluid_font_size_value = gutenberg_get_computed_fluid_font_size_value(
array(
'minimum_viewport_width' => $default_minimum_viewport_width,
'maximum_viewport_width' => $default_maximum_viewport_width,
Expand Down
36 changes: 36 additions & 0 deletions phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,4 +833,40 @@ public function data_invalid_size_wp_get_typography_value_and_unit() {
'size: array' => array( array( '10' ) ),
);
}

/**
* Tests bypassing WordPress font size calculations using the `pre_get_computed_fluid_typography_value` filter.
*/
public function test_gutenberg_get_computed_fluid_typography_value_filter() {
add_filter( 'pre_get_computed_fluid_font_size_value', array( $this, 'filter_fluid_font_size' ), 10, 2 );
$filtered_fluid_font_size_value = gutenberg_get_computed_fluid_font_size_value(
array(
'minimum_viewport_width' => '100px',
'maximum_viewport_width' => '200px',
'minimum_font_size' => '1em',
'maximum_font_size' => '2em',
'scale_factor' => '.9',
)
);
remove_filter( 'pre_get_computed_fluid_font_size_value', array( $this, 'filter_fluid_font_size' ) );
$this->assertSame( 'clamp(1em, 0.818rem + .9vw, 2em)', $filtered_fluid_font_size_value );
}

/**
* Filters the fluid font size arguments array before the internal calculations take place.
*
* @param string|null $fluid_typography_value Return a font-size value using the passed arguments to short-circuit the default calculations,
* or null to allow WordPress to calculate the font size value.
* @param array $args array(
* 'maximum_viewport_width' => (string) Maximum size up to which type will have fluidity.
* 'minimum_viewport_width' => (string) Minimum viewport size from which type will have fluidity.
* 'maximum_font_size' => (string) Maximum font size.
* 'minimum_font_size' => (string) Minimum font size.
* 'scale_factor' => (number) A scale factor to determine how fast a font scales within boundaries.
* );.
* @return string A font-size value
*/
public function filter_fluid_font_size( $fluid_typography_value, $args ) {
return "clamp({$args['minimum_font_size']}, 0.818rem + {$args['scale_factor']}vw, {$args['maximum_font_size']})";
}
}

0 comments on commit 8f789fc

Please sign in to comment.