static function updateAssetsUrls($content, $ajaxMode) { $serverUrl = \BearCMS\Internal\Options::$serverUrl; if ($ajaxMode) { $hasChange = false; $contentData = json_decode($content, true); if (isset($contentData['jsFiles'])) { $newJsFiles = []; foreach ($contentData['jsFiles'] as $src) { if (isset($src[0]) && strpos($src, $serverUrl) === 0) { $hasChange = true; $scriptBundle[] = $src; } else { $newJsFiles[] = $src; } } if (!empty($scriptBundle)) { $newJsFiles[] = self::getAssetsUrl(array_unique($scriptBundle)); } $contentData['jsFiles'] = $newJsFiles; } if ($hasChange) { return json_encode($contentData); } } else { $hasChange = false; $dom = new \IvoPetkov\HTML5DOMDocument(); $dom->loadHTML($content); $scripts = $dom->querySelectorAll('script'); $scriptBundle = []; $scriptsToRemove = []; foreach ($scripts as $script) { $src = (string) $script->getAttribute('src'); if (isset($src[0]) && strpos($src, $serverUrl) === 0) { $hasChange = true; if ($script->getAttribute('async') === 'async') { $scriptsToRemove[] = $script; $scriptBundle[] = $src; } else { $script->setAttribute('src', self::getAssetsUrl([$src])); } } } foreach ($scriptsToRemove as $script) { $script->parentNode->removeChild($script); } if (!empty($scriptBundle)) { $script = $dom->createElement('script'); $script->setAttribute('async', 'async'); $script->setAttribute('src', self::getAssetsUrl(array_unique($scriptBundle))); $dom->querySelector('body')->appendChild($script); } if ($hasChange) { return $dom->saveHTML(); } } return $content; }
if ($component->lazyLoadImages === 'true') { $lazyLoadImages = true; } $galleryID = 'imggallery' . uniqid(); $containerAttributes = ''; if ($onClick === 'fullscreen') { $hasLightbox = true; $jsData = ['galleryID' => $galleryID, 'lightboxData' => ['images' => []], 'images' => []]; $index = 0; foreach ($files as $file) { $filename = $file->getAttribute('filename'); $imageContainerID = $galleryID . 'img' . $index; $html = $app->components->process('<div id="' . $imageContainerID . '"><component style="background-color:#000;" src="lazy-image" filename="' . $filename . '"/></div>'); $imageDomDocument = new IvoPetkov\HTML5DOMDocument(); $imageDomDocument->loadHTML($html); $imageHTMLBody = $imageDomDocument->querySelector('body'); $imageHTML = $imageHTMLBody->innerHTML; $imageHTMLBody->parentNode->removeChild($imageHTMLBody); $jsData['lightboxData']['images'][] = ['html' => $imageHTML, 'onBeforeShow' => 'window.' . $galleryID . 'ig.onBeforeShow(' . $index . ');', 'onShow' => 'window.' . $galleryID . 'ig.onShow(' . $index . ');']; list($imageWidth, $imageHeight) = $app->images->getSize($filename); $jsData['images'][] = [$imageWidth, $imageHeight, $imageDomDocument->saveHTML()]; $index++; } } $getColumnsStyle = function ($columnsCount, $attributeSelector = '') use($galleryID, $spacing) { $result = '#' . $galleryID . $attributeSelector . '>div{vertical-align:top;display:inline-block;width:calc((100% - ' . $spacing . '*' . ($columnsCount - 1) . ')/' . $columnsCount . ');margin-right:' . $spacing . ';margin-top:' . $spacing . ';}'; $result .= '#' . $galleryID . $attributeSelector . '>div:nth-child(' . $columnsCount . 'n){margin-right:0;}'; for ($i = 1; $i <= $columnsCount; $i++) { $result .= '#' . $galleryID . $attributeSelector . '>div:nth-child(' . $i . '){margin-top:0;}'; } return $result;
$ulElement->setAttribute('class', trim($ulElement->getAttribute('class') . ' ' . ($index === 0 ? 'bearcms-navigation-element' : 'bearcms-navigation-element-item-children'))); } $liElements = $domDocument->querySelectorAll('li'); foreach ($liElements as $index => $liElement) { $liClasssName = 'bearcms-navigation-element-item'; if ($liElement->firstChild) { $liPath = str_replace($app->request->base, '', $liElement->firstChild->getAttribute('href')); if ($liPath === $selectedPath) { $liClasssName .= ' bearcms-navigation-element-item-selected'; } elseif ($liPath !== '/' && strpos($selectedPath, $liPath) === 0) { $liClasssName .= ' bearcms-navigation-element-item-in-path'; } } $liElement->setAttribute('class', trim($liElement->getAttribute('class') . ' ' . $liClasssName)); } $rootULElement = $domDocument->querySelector('ul'); if ($rootULElement) { $itemsHtml = $rootULElement->outerHTML; } } else { if ($pages === null || $pages->length === 0) { $itemsHtml = ''; } else { $itemsHtml = $buildTree($pages, $type === 'tree'); } } $content = ''; if (isset($itemsHtml[0])) { $content = '<component src="navigation-menu"' . $attributes . '>' . $itemsHtml . '</component>'; } $content = \BearCMS\Internal\ElementsHelper::getElementComponentContent($component, 'navigation', $content);
static function replaceContent($data, $response) { $app = App::$instance; $body = $response['body']; $content = $app->components->process($data['content']); $domDocument = new \IvoPetkov\HTML5DOMDocument(); $domDocument->loadHTML($content); $bodyElement = $domDocument->querySelector('body'); $content = $bodyElement->innerHTML; $bodyElement->parentNode->removeChild($bodyElement); $allButBody = $domDocument->saveHTML(); $startPosition = strpos($body, '{bearcms-replace-content-' . $data['id'] . '-'); if ($startPosition === false) { return; } $endPosition = strpos($body, '}', $startPosition); $modificationsString = substr($body, $startPosition + 58, $endPosition - $startPosition - 58); $parts = explode('\'', $modificationsString); $singleQuoteSlashesCount = strlen($parts[0]); $doubleQuoteSlashesCount = strlen($parts[1]) - 1; for ($i = 0; $i < $doubleQuoteSlashesCount; $i += 2) { $content = substr(json_encode($content), 1, -1); } for ($i = 0; $i < $singleQuoteSlashesCount; $i += 2) { $content = addslashes($content); } $body = str_replace(substr($body, $startPosition, $endPosition - $startPosition + 1), $content, $body); //todo optimize $response1 = ['js' => 'html5DOMDocument.insert(' . json_encode($allButBody, true) . ');']; $response2 = json_decode($body, true); $response['body'] = json_encode(Server::mergeAjaxResponses($response1, $response2)); }