diff --git a/lib/block-supports/typography.php b/lib/block-supports/typography.php index fb354c5c2a728..a7c695bc30bde 100644 --- a/lib/block-supports/typography.php +++ b/lib/block-supports/typography.php @@ -302,7 +302,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; @@ -421,7 +441,7 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty $maximum_font_size_raw = ( $preferred_size['value'] * $default_maximum_font_size_factor ) . $preferred_size['unit']; } - $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, diff --git a/phpunit/block-supports/typography-test.php b/phpunit/block-supports/typography-test.php index 0d2958dcf9330..e7a9adb9d35bf 100644 --- a/phpunit/block-supports/typography-test.php +++ b/phpunit/block-supports/typography-test.php @@ -366,4 +366,40 @@ public function data_generate_font_size_preset_fixtures() { ), ); } + + /** + * 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']})"; + } }