Skip to content

Commit

Permalink
fix: custom less feature not working on production
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Sep 24, 2023
1 parent 1010b44 commit 44a3bc5
Showing 1 changed file with 71 additions and 58 deletions.
129 changes: 71 additions & 58 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,13 @@ public function init()
$this->addHookBefore('TemplateFile::render', $this, "autoPrepend");
$this->addHookAfter("InputfieldForm::processInput", $this, "createWebfontsFile");
$this->addHookBefore("Inputfield::render", $this, "addFooterlinksNote");
$this->addHookAfter("Pages::saved", $this, "createAndSaveLESS");
$this->addHookAfter("Page::changed", $this, "resetCustomLess");
$this->addHookBefore("Page::render", $this, "createCustomLess");

// health checks
$this->checkHealth();
}

public function createAndSaveLESS(HookEvent $event): void
{
/** @var Page $page */
$page = $event->arguments(0);
$field = $page->fields->get(self::field_less);
if (!$field) return;
$code = $page->getUnformatted(self::field_less);

$path = $page->filesManager->path() . "custom.less";
if (!$code) $this->wire->files->unlink($path);
else $this->wire->files->filePutContents($path, $code);
}

public function ready()
{
$this->liveReload();
Expand Down Expand Up @@ -304,50 +292,6 @@ private function addRockFrontendJS(): void
} else $this->scripts('rockfrontend')->add($this->minifyFile($file), "defer");
}

/**
* Render a portion of HTML that needs consent from the user.
*
* This will replace all "src" attributes by "data-src" attributes.
* All scripts will therefore only be loaded when the user clicks on the
* consent button.
*
* Usage:
* $rockfrontend->consent(
* 'youtube',
* '<iframe src=...',
* '<a href=# rfc-allow=youtube>Allow YouTube-Player on this website</a>'
* );
*
* You can also render files instead of markup:
* $rockfrontend->consent(
* 'youtube'
* 'your youtube embed code',
* 'sections/youtube-consent.latte'
* );
*/
public function consent($name, $enabled, $disabled = null)
{
$enabled = str_replace(" src=", " rfconsent='$name' rfconsent-src=", $enabled);
if ($disabled) {
// we only add the wrapper if we have a disabled markup
// if we dont have a disabled markup that means we only have
// a script tag (like plausible analytics) so we don't need the
// wrapping div!
$enabled = "<div data-rfc-show='$name' hidden>$enabled</div>";
$file = $this->getFile($disabled);
if ($file) $disabled = $this->render($file);
$disabled = "<div data-rfc-hide='$name' hidden>$disabled</div>";
}
return $this->html($enabled . $disabled);
}

public function consentOptout($name, $script, $condition = true)
{
if (!$condition) return;
$enabled = str_replace(" src=", " rfconsent='$name' rfconsent-type=optout rfconsent-src=", $script);
return $this->html($enabled);
}

public function ___addAlfredStyles()
{
$this->styles('rockfrontend')->add($this->path . "Alfred.css", "", ['minify' => false]);
Expand Down Expand Up @@ -593,6 +537,50 @@ private function checkHealth()
// removed version healthcheck as of v2.0.0
}

/**
* Render a portion of HTML that needs consent from the user.
*
* This will replace all "src" attributes by "data-src" attributes.
* All scripts will therefore only be loaded when the user clicks on the
* consent button.
*
* Usage:
* $rockfrontend->consent(
* 'youtube',
* '<iframe src=...',
* '<a href=# rfc-allow=youtube>Allow YouTube-Player on this website</a>'
* );
*
* You can also render files instead of markup:
* $rockfrontend->consent(
* 'youtube'
* 'your youtube embed code',
* 'sections/youtube-consent.latte'
* );
*/
public function consent($name, $enabled, $disabled = null)
{
$enabled = str_replace(" src=", " rfconsent='$name' rfconsent-src=", $enabled);
if ($disabled) {
// we only add the wrapper if we have a disabled markup
// if we dont have a disabled markup that means we only have
// a script tag (like plausible analytics) so we don't need the
// wrapping div!
$enabled = "<div data-rfc-show='$name' hidden>$enabled</div>";
$file = $this->getFile($disabled);
if ($file) $disabled = $this->render($file);
$disabled = "<div data-rfc-hide='$name' hidden>$disabled</div>";
}
return $this->html($enabled . $disabled);
}

public function consentOptout($name, $script, $condition = true)
{
if (!$condition) return;
$enabled = str_replace(" src=", " rfconsent='$name' rfconsent-type=optout rfconsent-src=", $script);
return $this->html($enabled);
}

/**
* Create CSS from LESS file
* @return void
Expand All @@ -610,6 +598,14 @@ private function createCSS()
$this->message("Created $css from $lessFile");
}

public function createCustomLess(HookEvent $event): void
{
$file = $this->lessFilePath();
if (is_file($file)) return;
$less = $this->wire->pages->get(1)->getFormatted(self::field_less);
$this->wire->files->filePutContents($file, $less);
}

/**
* Create permission
* @return void
Expand Down Expand Up @@ -1224,6 +1220,11 @@ public function layoutSuggestions(HookEvent $event)
return $this->findSuggestFiles($event->q);
}

private function lessFilePath(): string
{
return $this->wire->config->paths->templates . "less/rf-custom.less";
}

/**
* Setup live reloading
*/
Expand Down Expand Up @@ -1887,6 +1888,18 @@ public function renderStaticFile(Page $page): string|false
return false;
}

public function resetCustomLess(HookEvent $event): void
{
/** @var Page $page */
$page = $event->object;
$field = $event->arguments(0);
if ($field != self::field_less) return;
$file = $this->lessFilePath();
$this->wire->files->unlink($file);
$newValue = $event->arguments(2);
$this->wire->files->filePutContents($file, $newValue);
}

public function rfGrow($_data, $shrink = false): string
{
if (is_string($_data)) {
Expand Down

0 comments on commit 44a3bc5

Please sign in to comment.