Skip to content

Commit

Permalink
Move Customizer controls for Header Brand Component (#641)
Browse files Browse the repository at this point in the history
* fix: move customizer controls for header brand

* fix: editor styles leaking into customizer panels

* feat: allow emblem as logo in header

* refactor: opening php tag
  • Loading branch information
thorbrink authored Aug 17, 2023
1 parent a618821 commit f474749
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 129 deletions.
7 changes: 6 additions & 1 deletion assets/source/3.0/sass/navigation/_header.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$brand-height: var(--c-brand-height, 48);
$brand-height: calc(var(--c-header-logotype-height, 6) * #{$base});
$brand-text-color: var(--c-header-brand-color, var(--color-black, #000));

//Spacing
.c-header .c-header__flex-content {
Expand Down Expand Up @@ -27,6 +28,10 @@ $brand-height: var(--c-brand-height, 48);
.c-header .c-brand {
line-height: 0; // Prevents space below svg.

&__text {
color: $brand-text-color;
}

&__viewbox {
height: $brand-height;

Expand Down
7 changes: 7 additions & 0 deletions library/Admin/UI/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function blockEditorScript()
*/
public function blockEditorStyle()
{
global $wp_customize;

if( isset($wp_customize) ) {
// If in customizer, do not enqueue styles which might affect editor appearance.
return;
}

wp_enqueue_style(
'block-editor-adjustments',
apply_filters(
Expand Down
37 changes: 37 additions & 0 deletions library/Controller/BaseController.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class BaseControllerTest extends \WP_UnitTestCase
{
public function testGetLogotypeReturnsString()
{
$baseController = new \Municipio\Controller\BaseController();
$this->assertIsString($baseController->getLogotype());
}

public function testGetLogotypeReturnsDefaultUrlIfNoOtherFound()
{
$baseController = new \Municipio\Controller\BaseController();
$defaultLogotype = $baseController->getDefaultLogotype();
$this->assertEquals($defaultLogotype, $baseController->getLogotype());
}

public function testGetLogotypeReturnsVariantUrlIfSet()
{
set_theme_mod('logotype_negative', 'bar');
$baseController = new \Municipio\Controller\BaseController();
$this->assertEquals('bar', $baseController->getLogotype('negative'));
}

public function testGetLogotypeDefaultsToStandard()
{
set_theme_mod('logotype', 'foo');
$baseController = new \Municipio\Controller\BaseController();
$this->assertEquals('foo', $baseController->getLogotype());
}

public function testGetLogotypeReturnsEmptyIfNotAvailable()
{
$baseController = new \Municipio\Controller\BaseController();
$this->assertEquals('', $baseController->getLogotype('foo'));
}
}
23 changes: 10 additions & 13 deletions library/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function __construct()
$this->data['emblem'] = $this->getEmblem();
$this->data['showEmblemInHero'] = $this->data['customizer']->showEmblemInHero ?? true;
$this->data['brandText'] = $this->getMultilineTextAsArray(get_option('brand_text', ''));
$this->data['headerBrandEnabled'] = $this->data['customizer']->headerBrandEnabled && !empty($this->data['brandText']);

// Footer
[$footerStyle, $footerColumns, $footerAreas] = $this->getFooterSettings();
Expand Down Expand Up @@ -743,32 +744,28 @@ public function getEmblem()
*/
public function getLogotype($variant = "standard"): string
{
//Cache, early bailout
if (isset($this->data['customizer']->logotype) && !empty($this->data['customizer']->logotype)) {
return $this->data['customizer']->logotype;
}

//Get fresh logotypes
$variantKey = "logotype";

//Builds acf-field name
if ($variant !== "standard" && !is_null($variant)) {
$variantKey = FormatObject::camelCaseString("${variantKey}_${variant}");
$variantKey = FormatObject::camelCaseString("{$variantKey}_{$variant}");
}

//Get the logo, ensure url is defined.
$logotypeUrl = isset($this->data['customizer']->$variantKey)
? $this->data['customizer']->{$variantKey}
: '';
? $this->data['customizer']->{$variantKey}
: '';

if (empty($logotypeUrl) && $variantKey == "logotype") {
$logotypeUrl = get_stylesheet_directory_uri() . '/assets/images/municipio.svg';
return $this->getDefaultLogotype();
}

//Return
return $logotypeUrl;
}

public function getDefaultLogotype(): string
{
return get_stylesheet_directory_uri() . '/assets/images/municipio.svg';
}

public function getMultilineTextAsArray(string $text)
{
$trimmed = trim($text);
Expand Down
6 changes: 0 additions & 6 deletions library/Customizer/PanelsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,6 @@ public static function registerComponentAppearancePanel()
->setTitle(esc_html__('Header', 'municipio'))
->setFieldsCallback(fn() => new \Municipio\Customizer\Sections\Header('municipio_customizer_section_header'))
)
->addSection(
KirkiPanelSection::create()
->setID('municipio_customizer_section_brand')
->setTitle(esc_html__('Brand', 'municipio'))
->setFieldsCallback(fn() => new \Municipio\Customizer\Sections\Brand('municipio_customizer_section_brand'))
)
->addSection(
KirkiPanelSection::create()
->setID('municipio_customizer_section_siteselector')
Expand Down
83 changes: 0 additions & 83 deletions library/Customizer/Sections/Brand.php

This file was deleted.

139 changes: 138 additions & 1 deletion library/Customizer/Sections/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function __construct(string $sectionID)
[
'element' => ':root',
'property' => '--c-header-logotype-height',
]
],
],
]);

Expand All @@ -195,9 +195,15 @@ public function __construct(string $sectionID)
'choices' => array(
'standard' => esc_html__('Primary', 'municipio'),
'negative' => esc_html__('Secondary', 'municipio'),
'emblem' => esc_html__('Emblem', 'municipio'),
),
'output' => [
['type' => 'controller']
]
]));

$this->addBrandFields($sectionID);

Kirki::add_field(\Municipio\Customizer::KIRKI_CONFIG, [
'type' => 'select',
'settings' => 'tabmenu_button_color',
Expand Down Expand Up @@ -283,4 +289,135 @@ public function __construct(string $sectionID)
],
]);
}

private function addBrandFields(string $sectionID) {

Kirki::add_field(new \Kirki\Pro\Field\Divider(
[
'settings' => 'header_brand_divider_top',
'section' => $sectionID,
'active_callback' => $this->getHeaderBrandEnabledActiveCallback(),
]
));

Kirki::add_field(new \Kirki\Pro\Field\HeadlineToggle(
[
'settings' => 'header_brand_enabled',
'label' => esc_html__('Header Logotype Text', 'municipio'),
'description' => esc_html__('Enables text to the right of the header logotype.', 'municipio'),
'section' => $sectionID,
'default' => false,
'output' => [
[
'type' => 'controller',
'as_object' => false,
]
]
]
));

Kirki::add_field(new \Kirki\Field\Textarea([
'settings' => 'brand_text',
'section' => $sectionID,
'label' => esc_html__('Header Logotype Text: Content', 'municipio'),
'option_type' => 'option',
'default' => '',
'active_callback' => $this->getHeaderBrandEnabledActiveCallback(),
'output' => [
[
'type' => 'controller',
'as_object' => false,
]
]
]));

Kirki::add_field(new \Kirki\Field\Color(
[
'settings' => 'header_brand_color',
'label' => __('Header LogoType Text: Color ', 'municipio'),
'section' => $sectionID,
'active_callback' => $this->getHeaderBrandEnabledActiveCallback(),
'default' => '#000000',
'output' => [
[
'element' => ':root',
'property' => '--c-header-brand-color',
],
]
]
));

Kirki::add_field(\Municipio\Customizer::KIRKI_CONFIG, [
'type' => 'typography',
'settings' => 'header_brand_font_settings',
'active_callback' => $this->getHeaderBrandEnabledActiveCallback(),
'label' => __('Header LogoType Text: Font Settings ', 'municipio'),
'section' => $sectionID,
'priority' => 10,
'choices' => [
'fonts' => [
'google' => ['popularity', 200],
],
],
'default' => [
'font-size' => '2.25rem',
'font-family' => 'Roboto',
'variant' => '400',
'line-height' => '1.2',
'letter-spacing' => '0',
'text-transform' => 'none',
],
'output' => [
[
'choice' => 'font-size',
'element' => ':root',
'property' => '--c-brand-font-size',
],
[
'choice' => 'font-family',
'element' => '.c-brand .c-brand__text',
'property' => 'font-family',
],
[
'choice' => 'variant',
'element' => '.c-brand .c-brand__text',
'property' => 'font-variant',
],
[
'choice' => 'line-height',
'element' => '.c-brand .c-brand__text',
'property' => 'line-height',
],
[
'choice' => 'letter-spacing',
'element' => '.c-brand .c-brand__text',
'property' => 'letter-spacing',
],
[
'choice' => 'text-transform',
'element' => '.c-brand .c-brand__text',
'property' => 'text-transform',
],
]
]);

Kirki::add_field(new \Kirki\Pro\Field\Divider(
[
'settings' => 'header_brand_divider_bottom',
'section' => $sectionID,
'active_callback' => $this->getHeaderBrandEnabledActiveCallback(),
]
));
}

private function getHeaderBrandEnabledActiveCallback(): array
{
return [
[
'setting' => 'header_brand_enabled',
'operator' => '==',
'value' => true,
]
];
}
}
Loading

0 comments on commit f474749

Please sign in to comment.