From ee109d6f6a8e87acacd07c9795aba4a5797cc9e4 Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Thu, 18 Dec 2025 18:21:55 +0530 Subject: [PATCH 1/4] Fix: Sync fluid typography size with fluid.max (#789) --- includes/class-create-block-theme-api.php | 1 + .../create-theme/theme-fluid-typography.php | 69 +++++++++ tests/test-fluid-typography.php | 133 ++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 includes/create-theme/theme-fluid-typography.php create mode 100644 tests/test-fluid-typography.php diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index a620fbb0..f7ff32fc 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -13,6 +13,7 @@ require_once __DIR__ . '/create-theme/theme-readme.php'; require_once __DIR__ . '/create-theme/theme-fonts.php'; require_once __DIR__ . '/create-theme/theme-create.php'; +require_once __DIR__ . '/create-theme/theme-fluid-typography.php'; /** * The api functionality of the plugin leveraged by the site editor UI. diff --git a/includes/create-theme/theme-fluid-typography.php b/includes/create-theme/theme-fluid-typography.php new file mode 100644 index 00000000..bc31f470 --- /dev/null +++ b/includes/create-theme/theme-fluid-typography.php @@ -0,0 +1,69 @@ + $font_size ) { + // Check if this font size has fluid settings + if ( isset( $font_size['fluid'] ) && is_array( $font_size['fluid'] ) ) { + // If fluid.max is set, sync it with size + if ( isset( $font_size['fluid']['max'] ) ) { + $font_sizes[ $key ]['size'] = $font_size['fluid']['max']; + } + } + } + + $data['settings']['typography']['fontSizes'] = $font_sizes; + + return $data; + } + + /** + * Hook into the global styles REST API to sync fluid typography sizes. + */ + public static function init() { + add_filter( 'rest_pre_insert_wp_global_styles', array( __CLASS__, 'sync_global_styles_fluid_typography' ), 10, 2 ); + } + + /** + * Sync fluid typography in global styles before saving. + * + * @param stdClass $prepared_post The prepared post object. + * @param WP_REST_Request $request The REST request object. + * @return stdClass The modified prepared post object. + */ + public static function sync_global_styles_fluid_typography( $prepared_post, $request ) { + if ( ! isset( $prepared_post->post_content ) ) { + return $prepared_post; + } + + $data = json_decode( $prepared_post->post_content, true ); + + if ( ! is_array( $data ) ) { + return $prepared_post; + } + + $data = self::sync_fluid_typography_sizes( $data ); + + $prepared_post->post_content = wp_json_encode( $data ); + + return $prepared_post; + } +} + +// Initialize the fluid typography sync on plugins_loaded +add_action( 'plugins_loaded', array( 'CBT_Theme_Fluid_Typography', 'init' ) ); diff --git a/tests/test-fluid-typography.php b/tests/test-fluid-typography.php new file mode 100644 index 00000000..e176282d --- /dev/null +++ b/tests/test-fluid-typography.php @@ -0,0 +1,133 @@ + array( + 'typography' => array( + 'fontSizes' => array( + array( + 'slug' => 'custom-fluid', + 'name' => 'Custom Fluid', + 'fluid' => array( + 'min' => '1.5rem', + 'max' => '5.63rem', + ), + 'size' => '3rem', // Old value, should be updated + ), + array( + 'slug' => 'normal', + 'name' => 'Normal', + 'size' => '1rem', // No fluid, should remain unchanged + ), + ), + ), + ), + ); + + $result = CBT_Theme_Fluid_Typography::sync_fluid_typography_sizes( $data ); + + // Verify the fluid size was synced + $this->assertEquals( + '5.63rem', + $result['settings']['typography']['fontSizes'][0]['size'], + 'Fluid typography size should be synced with fluid.max' + ); + + // Verify the non-fluid size remained unchanged + $this->assertEquals( + '1rem', + $result['settings']['typography']['fontSizes'][1]['size'], + 'Non-fluid typography size should remain unchanged' + ); + } + + /** + * Test that global styles filter works correctly + */ + public function test_sync_global_styles_fluid_typography() { + $prepared_post = new stdClass(); + $prepared_post->post_content = wp_json_encode( + array( + 'settings' => array( + 'typography' => array( + 'fontSizes' => array( + array( + 'slug' => 'custom-fluid', + 'name' => 'Custom Fluid', + 'fluid' => array( + 'min' => '1.5rem', + 'max' => '5.63rem', + ), + 'size' => '3rem', + ), + ), + ), + ), + ) + ); + + $request = new WP_REST_Request(); + + $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post, $request ); + + $data = json_decode( $result->post_content, true ); + + $this->assertEquals( + '5.63rem', + $data['settings']['typography']['fontSizes'][0]['size'], + 'Global styles should have synced fluid typography sizes' + ); + } + + /** + * Test that missing typography doesn't cause errors + */ + public function test_sync_with_no_typography() { + $data = array( + 'settings' => array(), + ); + + $result = CBT_Theme_Fluid_Typography::sync_fluid_typography_sizes( $data ); + + $this->assertArrayNotHasKey( 'typography', $result['settings'] ); + $this->assertEqualsCanonicalizing( array(), $result['settings'] ); + } + + /** + * Test that missing fontSizes doesn't cause errors + */ + public function test_sync_with_no_font_sizes() { + $data = array( + 'settings' => array( + 'typography' => array(), + ), + ); + + $result = CBT_Theme_Fluid_Typography::sync_fluid_typography_sizes( $data ); + + $this->assertArrayNotHasKey( 'fontSizes', $result['settings']['typography'] ); + } + + /** + * Test that invalid JSON in global styles filter is handled gracefully + */ + public function test_sync_global_styles_with_invalid_json() { + $prepared_post = new stdClass(); + $prepared_post->post_content = 'invalid json'; + + $request = new WP_REST_Request(); + + $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post, $request ); + + // Should return the prepared post unchanged + $this->assertEquals( 'invalid json', $result->post_content ); + } +} From 3b0bba0b7494cb6233533e5fe6ed0d81a78413c2 Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Thu, 18 Dec 2025 18:44:31 +0530 Subject: [PATCH 2/4] Fix: PHP linting issues - remove unused parameter and align equals signs --- includes/create-theme/theme-fluid-typography.php | 3 +-- tests/test-fluid-typography.php | 12 ++++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/includes/create-theme/theme-fluid-typography.php b/includes/create-theme/theme-fluid-typography.php index bc31f470..65fbe197 100644 --- a/includes/create-theme/theme-fluid-typography.php +++ b/includes/create-theme/theme-fluid-typography.php @@ -43,10 +43,9 @@ public static function init() { * Sync fluid typography in global styles before saving. * * @param stdClass $prepared_post The prepared post object. - * @param WP_REST_Request $request The REST request object. * @return stdClass The modified prepared post object. */ - public static function sync_global_styles_fluid_typography( $prepared_post, $request ) { + public static function sync_global_styles_fluid_typography( $prepared_post ) { if ( ! isset( $prepared_post->post_content ) ) { return $prepared_post; } diff --git a/tests/test-fluid-typography.php b/tests/test-fluid-typography.php index e176282d..781ef57e 100644 --- a/tests/test-fluid-typography.php +++ b/tests/test-fluid-typography.php @@ -53,7 +53,7 @@ public function test_sync_fluid_typography_sizes() { * Test that global styles filter works correctly */ public function test_sync_global_styles_fluid_typography() { - $prepared_post = new stdClass(); + $prepared_post = new stdClass(); $prepared_post->post_content = wp_json_encode( array( 'settings' => array( @@ -74,9 +74,7 @@ public function test_sync_global_styles_fluid_typography() { ) ); - $request = new WP_REST_Request(); - - $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post, $request ); + $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post ); $data = json_decode( $result->post_content, true ); @@ -120,12 +118,10 @@ public function test_sync_with_no_font_sizes() { * Test that invalid JSON in global styles filter is handled gracefully */ public function test_sync_global_styles_with_invalid_json() { - $prepared_post = new stdClass(); + $prepared_post = new stdClass(); $prepared_post->post_content = 'invalid json'; - $request = new WP_REST_Request(); - - $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post, $request ); + $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post ); // Should return the prepared post unchanged $this->assertEquals( 'invalid json', $result->post_content ); From 572dc0fee6d1663a99062a61b89dd7e86ca99b00 Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Thu, 18 Dec 2025 18:49:52 +0530 Subject: [PATCH 3/4] Fix: Align equals signs to 15 spaces per PHPCS standard --- tests/test-fluid-typography.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-fluid-typography.php b/tests/test-fluid-typography.php index 781ef57e..32858429 100644 --- a/tests/test-fluid-typography.php +++ b/tests/test-fluid-typography.php @@ -53,7 +53,7 @@ public function test_sync_fluid_typography_sizes() { * Test that global styles filter works correctly */ public function test_sync_global_styles_fluid_typography() { - $prepared_post = new stdClass(); + $prepared_post = new stdClass(); $prepared_post->post_content = wp_json_encode( array( 'settings' => array( @@ -118,7 +118,7 @@ public function test_sync_with_no_font_sizes() { * Test that invalid JSON in global styles filter is handled gracefully */ public function test_sync_global_styles_with_invalid_json() { - $prepared_post = new stdClass(); + $prepared_post = new stdClass(); $prepared_post->post_content = 'invalid json'; $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post ); From 31462dbb39b7529b5c402264a64d366511ae9f88 Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Thu, 18 Dec 2025 18:53:09 +0530 Subject: [PATCH 4/4] Fix: Correct equals sign alignment to exactly 15 spaces --- tests/test-fluid-typography.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-fluid-typography.php b/tests/test-fluid-typography.php index 32858429..b8ebb0ce 100644 --- a/tests/test-fluid-typography.php +++ b/tests/test-fluid-typography.php @@ -53,7 +53,7 @@ public function test_sync_fluid_typography_sizes() { * Test that global styles filter works correctly */ public function test_sync_global_styles_fluid_typography() { - $prepared_post = new stdClass(); + $prepared_post = new stdClass(); $prepared_post->post_content = wp_json_encode( array( 'settings' => array( @@ -118,7 +118,7 @@ public function test_sync_with_no_font_sizes() { * Test that invalid JSON in global styles filter is handled gracefully */ public function test_sync_global_styles_with_invalid_json() { - $prepared_post = new stdClass(); + $prepared_post = new stdClass(); $prepared_post->post_content = 'invalid json'; $result = CBT_Theme_Fluid_Typography::sync_global_styles_fluid_typography( $prepared_post );