Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Jul 1, 2024
2 parents d537db8 + 0a741da commit a35efe1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 54 deletions.
4 changes: 4 additions & 0 deletions Alfred.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@
// add the class .no-alfred to prevent double click alfred, eg in forms
if (e.target.closest(".no-alfred")) return;

// dont trigger modal on links and buttons
if (e.target.closest("button")) return;
if (e.target.closest("a")) return;

let $alfred = $(e.target).closest(".alfred");
// console.log($alfred);
// if we are currently inline-editing somthing in this block
Expand Down
2 changes: 1 addition & 1 deletion Alfred.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 70 additions & 53 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class RockFrontend extends WireData implements Module, ConfigurableModule

private $addMarkup;

/**
* Property that is true for ajax requests.
* Compared to $config->ajax this will also account for HTMX etc.
* @var false
*/
public $ajax = false;

private $ajaxFolders = [];

/** @var WireData */
Expand Down Expand Up @@ -205,6 +212,10 @@ public function init()
$this->autoloadStyles = new WireArray();
$this->alfredCache = $this->wire(new WireData());

// set ajax flag
$htmx = isset($_SERVER['HTTP_HX_REQUEST']) && $_SERVER['HTTP_HX_REQUEST'];
$this->ajax = $this->wire->config->ajax || $htmx;

// JS defaults
// set the remBase either from config setting or use 16 as fallback
$this->remBase = $this->remBase ?: 16;
Expand Down Expand Up @@ -317,7 +328,11 @@ function (HookEvent $event) {
$this->injectJavascriptSettings($html);
$this->injectAssets($html);
$event->return = $html;
}
},
// other modules also hooking to page::render can define load order
// via hook priority, for example RockCommerce uses 200 to make sure
// all RockCommerce releated stuff is loaded after default RF assets
['priority' => 100]
);
}

Expand Down Expand Up @@ -486,30 +501,21 @@ private function adjustBrightness($hexCode, $adjustPercent)
protected function ajaxAddEndpoints(): void
{
foreach ($this->ajaxEndpoints() as $url => $endpoint) {
$this->wire->addHook($url, function (HookEvent $event) use ($endpoint, $url) {
$isAJAX = false;
if ($this->wire->config->ajax) $isAJAX = true;
if (!$isAJAX && $_SERVER['REQUEST_METHOD'] !== 'GET') $isAJAX = true;
if (
!$isAJAX &&
isset($_SERVER['HTTP_HX_REQUEST']) &&
$_SERVER['HTTP_HX_REQUEST']
) $isAJAX = true;

// render public endpoint (ajax response)
if ($isAJAX) return $this->ajaxPublic($endpoint);

// show debug screen for pw superusers
if ($this->wire->user->isSuperuser()) {
return $this->ajaxDebug($endpoint, $url);
} else {
// guest and no ajax: no access!
if ($this->wire->modules->isInstalled('TracyDebugger')) {
Debugger::$showBar = false;
}
http_response_code(403);
return "Access Denied";
wire()->addHook($url, function (HookEvent $event) use ($endpoint, $url) {
$GET = $this->wire->input->requestMethod() === 'GET';

// ajax requests always return the public endpoint
if ($this->ajax || !$GET) return $this->ajaxPublic($endpoint);

// non-ajax request show the debug screen for superusers
if (wire()->user->isSuperuser()) return $this->ajaxDebug($endpoint, $url);

// guest and no ajax: no access!
if (wire()->modules->isInstalled('TracyDebugger')) {
Debugger::$showBar = false;
}
http_response_code(403);
return "Access Denied";
});
}
}
Expand Down Expand Up @@ -1351,27 +1357,31 @@ public function getTranslation($key)

/**
* Get uikit versions from github
* @param bool $noCache load from cache?
* @return array
*/
public function getUikitVersions($noCache = false)
{
$expire = 60 * 5;
if ($noCache) $expire = WireCache::expireNow;
$versions = $this->wire->cache->get(self::cache, $expire, function () {
$http = new WireHttp();
$json = $http->get('https://api.github.com/repos/uikit/uikit/git/refs/tags');
$refs = json_decode($json);
$versions = [];
foreach ($refs as $ref) {
$version = str_replace("refs/tags/", "", $ref->ref);
$v = $version;
if (strpos($version, "v.") === 0) continue;
if (strpos($version, "v") !== 0) continue;
$versions[$v] = $version;
$refs = $http->getJSON('https://api.github.com/repos/uikit/uikit/git/refs/tags', $assoc = false);
if ($refs) {
$versions = [];
foreach ($refs as $ref) {
$version = str_replace("refs/tags/", "", $ref->ref);
$v = $version;
if (strpos($version, "v.") === 0) continue;
if (strpos($version, "v") !== 0) continue;
$versions[$v] = $version;
}
uasort($versions, "version_compare");
return array_reverse($versions);
}
uasort($versions, "version_compare");
return array_reverse($versions);
return [];
});
if ($versions) return $versions;
if (!empty($versions)) return $versions;
if (!$noCache) return $this->getUikitVersions(true);
return [];
}
Expand Down Expand Up @@ -1805,6 +1815,20 @@ function (\Latte\Runtime\Template $template) {
}
}

public function ___loadTwig()
{
try {
$debug = !!$this->wire->config->debugTwig;
require_once $this->wire->config->paths->root . 'vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader($this->wire->config->paths->root);
$twig = new \Twig\Environment($loader, ['debug' => $debug]);
if ($debug) $twig->addExtension(new \Twig\Extension\DebugExtension());
return $twig;
} catch (\Throwable $th) {
return false;
}
}

/**
* Create a site webmanifest in PW root
* @return Manifest
Expand Down Expand Up @@ -2120,24 +2144,17 @@ protected function renderFileSvg($file)
*/
protected function renderFileTwig($file, $vars)
{
try {
require_once $this->wire->config->paths->root . 'vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader($this->wire->config->paths->root);
$twig = new \Twig\Environment($loader, [
'debug' => true,
]);
$twig->addExtension(new \Twig\Extension\DebugExtension());
$relativePath = str_replace(
$this->wire->config->paths->root,
$this->wire->config->urls->root,
$file
);
$vars = array_merge((array)$this->wire('all'), $vars);
return $twig->render($relativePath, $vars);
} catch (\Throwable $th) {
return $th->getMessage() .
'<br><br>Use composer require "twig/twig:^3.0" in PW root';
$twig = $this->loadTwig();
if ($twig === false) {
return 'Error loading Twig. Use composer require "twig/twig:^3.0" in PW root.';
}
$relativePath = str_replace(
$this->wire->config->paths->root,
$this->wire->config->urls->root,
$file
);
$vars = array_merge((array)$this->wire('all'), $vars);
return $twig->render($relativePath, $vars);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions docs/asset-tools/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Asset Tools

<div class='uk-alert uk-alert-warning'>NOTE: RockFrontend's asset tools may cause problems when using template cache! See https://processwire.com/talk/topic/30145-- for details. ProCache works fine!</div>

Working with site assets can be tedious.

In your HTML markup the browser expects file urls whereas PHP expects system paths for several file operations. Converting from one to another can quickly lead to errors like forgetting to add/remove a slash somewhere. Or you forget to add a cache busting mechanism and waste time fixing bugs that do actually not exist just because your browser is serving an outdated version...
Expand Down

0 comments on commit a35efe1

Please sign in to comment.