Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions packages/wp/Tests/EnabledFontsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

use CoreFramework\App\Bricks\Bricks;
use CoreFramework\App\Oxygen\Functions as OxygenFunctions;
use CoreFramework\Helper;
use PHPUnit\Framework\TestCase;

if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}

if ( ! defined( 'CORE_FRAMEWORK_VERSION' ) ) {
define( 'CORE_FRAMEWORK_VERSION', 'test' );
}

if ( ! function_exists( 'get_option' ) ) {
function get_option( $option, $default = false ) {
return array_key_exists( $option, $GLOBALS['cf_test_options'] )
? $GLOBALS['cf_test_options'][ $option ]
: $default;
}
}

if ( ! function_exists( 'esc_sql' ) ) {
function esc_sql( $value ) {
return $value;
}
}

if ( ! function_exists( 'wp_json_encode' ) ) {
function wp_json_encode( $value, $flags = 0, $depth = 512 ) {
return json_encode( $value, $flags, $depth );
}
}

if ( ! function_exists( 'wp_register_style' ) ) {
function wp_register_style() {
return true;
}
}

if ( ! function_exists( 'wp_enqueue_style' ) ) {
function wp_enqueue_style() {
return true;
}
}

if ( ! function_exists( 'wp_add_inline_style' ) ) {
function wp_add_inline_style( $handle, $css ) {
$GLOBALS['cf_test_inline_styles'][ $handle ] = $css;
return true;
}
}

final class CoreFrameworkEnabledFontsTestDatabase {
public $prefix = 'wp_';

public function prepare( $query ) {
return $query;
}

public function get_row() {
return (object) array(
'data' => 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 );
}
}
5 changes: 1 addition & 4 deletions packages/wp/wp/App/Bricks/Bricks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
10 changes: 2 additions & 8 deletions packages/wp/wp/App/Oxygen/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 ) {
Expand Down
23 changes: 23 additions & 0 deletions packages/wp/wp/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading