From c1f14ee2fb2d5e40cb73ae680c533fc6bcfc8dda Mon Sep 17 00:00:00 2001 From: Jan-CoreBunch Date: Mon, 24 Aug 2026 14:55:33 +0200 Subject: [PATCH] fix(wp): exclude disabled fonts from integrations --- packages/wp/Tests/EnabledFontsTest.php | 143 ++++++++++++++++++++++++ packages/wp/wp/App/Bricks/Bricks.php | 5 +- packages/wp/wp/App/Oxygen/Functions.php | 10 +- packages/wp/wp/Helper.php | 23 ++++ 4 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 packages/wp/Tests/EnabledFontsTest.php diff --git a/packages/wp/Tests/EnabledFontsTest.php b/packages/wp/Tests/EnabledFontsTest.php new file mode 100644 index 0000000..72aec25 --- /dev/null +++ b/packages/wp/Tests/EnabledFontsTest.php @@ -0,0 +1,143 @@ + json_encode( $GLOBALS['cf_test_preset'] ), + ); + } +} + +final class EnabledFontsTest extends TestCase { + protected function setUp(): void { + global $wpdb; + + $GLOBALS['cf_test_options'] = array( + 'core_framework_main' => array( + 'selected_id' => 'preset-id', + 'disable_fonts' => false, + ), + ); + $GLOBALS['cf_test_inline_styles'] = array(); + $GLOBALS['cf_test_preset'] = array( + 'modulesData' => array( + 'FONTS' => array( + 'fonts' => array( + array( + 'family' => 'Enabled Font', + 'enable' => true, + 'cssPreview' => '.enabled-font { font-family: "Enabled Font"; }', + ), + array( + 'family' => 'Disabled Font', + 'enable' => false, + 'cssPreview' => '.disabled-font { font-family: "Disabled Font"; }', + ), + ), + ), + ), + ); + $wpdb = new CoreFrameworkEnabledFontsTestDatabase(); + } + + public function testHelperReturnsOnlyEnabledFonts(): void { + $fonts = ( new Helper() )->getEnabledFonts(); + + $this->assertCount( 1, $fonts ); + $this->assertSame( 'Enabled Font', $fonts[0]['family'] ); + } + + public function testOxygenDropdownReceivesOnlyEnabledFontFamilies(): void { + $reflection = new ReflectionClass( OxygenFunctions::class ); + $functions = $reflection->newInstanceWithoutConstructor(); + + ob_start(); + $functions->elegant_custom_fonts(); + $output = html_entity_decode( ob_get_clean(), ENT_QUOTES ); + + $this->assertSame( 'elegantCustomFonts=["Enabled Font"];', $output ); + } + + public function testOxygenEnqueuesCssForEnabledFontsOnly(): void { + $reflection = new ReflectionClass( OxygenFunctions::class ); + $functions = $reflection->newInstanceWithoutConstructor(); + + $functions->add_corresponding_css(); + + $this->assertInlineCssContainsOnlyEnabledFont( 'core-framework-fonts-inline' ); + } + + public function testBricksEnqueuesCssForEnabledFontsOnly(): void { + $reflection = new ReflectionClass( Bricks::class ); + $bricks = $reflection->newInstanceWithoutConstructor(); + + $bricks->add_corresponding_css(); + + $this->assertInlineCssContainsOnlyEnabledFont( 'core-framework-inline' ); + } + + private function assertInlineCssContainsOnlyEnabledFont( string $handle ): void { + $css = $GLOBALS['cf_test_inline_styles'][ $handle ]; + + $this->assertStringContainsString( '.enabled-font', $css ); + $this->assertStringNotContainsString( '.disabled-font', $css ); + } +} diff --git a/packages/wp/wp/App/Bricks/Bricks.php b/packages/wp/wp/App/Bricks/Bricks.php index 1817e34..6487c71 100644 --- a/packages/wp/wp/App/Bricks/Bricks.php +++ b/packages/wp/wp/App/Bricks/Bricks.php @@ -94,10 +94,7 @@ public function add_corresponding_css() { return; } - $preset = $helper->loadPreset(); - $preset_fonts = isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FONTS'] ) - ? $preset['modulesData']['FONTS']['fonts'] - : array(); + $preset_fonts = $helper->getEnabledFonts(); $css = ''; foreach ( $preset_fonts as $font ) { diff --git a/packages/wp/wp/App/Oxygen/Functions.php b/packages/wp/wp/App/Oxygen/Functions.php index 9a515f7..c25a42b 100644 --- a/packages/wp/wp/App/Oxygen/Functions.php +++ b/packages/wp/wp/App/Oxygen/Functions.php @@ -186,10 +186,7 @@ public function elegant_custom_fonts() { return; } - $preset = $helper->loadPreset(); - $preset_fonts = isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FONTS'] ) - ? $preset['modulesData']['FONTS']['fonts'] - : array(); + $preset_fonts = $helper->getEnabledFonts(); $customCoreFontFamilies = array_column($preset_fonts, 'family'); $output = \wp_json_encode($customCoreFontFamilies); @@ -459,10 +456,7 @@ public function add_corresponding_css() { return; } - $preset = $helper->loadPreset(); - $preset_fonts = isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FONTS'] ) - ? $preset['modulesData']['FONTS']['fonts'] - : array(); + $preset_fonts = $helper->getEnabledFonts(); $css = ''; foreach ( $preset_fonts as $font ) { diff --git a/packages/wp/wp/Helper.php b/packages/wp/wp/Helper.php index 608e0fb..f3cdf5e 100644 --- a/packages/wp/wp/Helper.php +++ b/packages/wp/wp/Helper.php @@ -45,6 +45,29 @@ public function isFontsDisabled(): bool { return isset( $option['disable_fonts'] ) && $option['disable_fonts']; } + /** + * Get fonts that are enabled in the selected preset. + * + * @return array + */ + public function getEnabledFonts(): array { + $preset = $this->loadPreset(); + $fonts = isset( $preset['modulesData']['FONTS']['fonts'] ) + ? $preset['modulesData']['FONTS']['fonts'] + : array(); + + if ( ! is_array( $fonts ) ) { + return array(); + } + + return array_values( + array_filter( + $fonts, + static fn( $font ): bool => is_array( $font ) && true === ( $font['enable'] ?? false ) + ) + ); + } + public function setPresetId( string $preset_id ): void { $options = get_option( 'core_framework_main', array() ); $options['selected_id'] = $preset_id;