Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Theme custom fonts
givanzc5667ffb04f6
diff --git a/admin/controller/theme/fonts.php b/admin/controller/theme/fonts.phpnew file mode 100644index 0000000..426f2ba--- /dev/null+++ b/admin/controller/theme/fonts.php@@ -0,0 +1,169 @@+<?php++/**+ * Vvveb+ *+ * Copyright (C) 2022 Ziadin Givan+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++namespace Vvveb\Controller\Theme;++use function Vvveb\__;+use Vvveb\Controller\Base;+use function Vvveb\humanReadable;+use function Vvveb\sanitizeFileName;+use Vvveb\System\Import\Theme;+use Vvveb\System\Media\Font;+use Vvveb\System\Sites;+use Vvveb\System\Traits\Media as MediaTrait;++class Fonts extends Base {+ use MediaTrait;++ protected $dirMedia = DIR_PUBLIC . 'fonts' . DS;++ protected $fontsPath = '../../../fonts/'; // /public/fonts relative to /themes/theme-slug/css folder++ private function getTheme() {+ return $theme = sanitizeFileName($this->request->get['theme'] ?? Sites::getTheme() ?? 'default');+ }++ function save() {+ $fonts = $this->request->post['font'] ?? [];++ if ($fonts) {+ $theme = $this->getTheme();+ $themeFolder = DIR_THEMES . $theme;+ $cssFile = DS . 'css' . DS . 'fonts.css';++ //try to create file if it doesn't exist+ if ($theme && ! file_exists($themeFolder . $cssFile) && ! touch($themeFolder . $cssFile)) {+ $this->view->errors[] = sprintf(__('Can not create file %s!'), $theme . $cssFile);++ return $this->index();+ }++ if ($theme && is_writable($themeFolder . $cssFile)) {+ //set font src relative to fonts folder path+ foreach ($fonts as &$font) {+ if (isset($font['src'])) {+ $font['src'] = $this->fontsPath . trim($font['src'], '/');+ }+ }++ $fontsCss = Font::arrayToFontFaces($fonts);++ if ($fontsCss) {+ if ($css = file_get_contents($themeFolder . $cssFile)) {+ $css = trim(Font::removeFontFaces($css));+ }++ $css .= "\n$fontsCss";++ if (! file_put_contents($themeFolder . $cssFile, $css)) {+ $this->view->errors[] = sprintf(__('%s is not writable!'), $theme . $cssFile);+ }+ }+ } else {+ $this->view->errors[] = sprintf(__('%s is not writable!'), $theme . $cssFile);+ }+ }++ return $this->index();+ }++ function index() {+ $theme = $this->getTheme();+ //var_dump(Font::themeFonts($theme));+ if ($theme && is_dir($themeCssDir = DIR_THEMES . $theme . DS . 'css' . DS)) {+ $styleCss = $themeCssDir . 'style.css';+ $fontsCss = $themeCssDir . 'fonts.css';++ $builtIn = [];+ $custom = [];++ if (file_exists($styleCss) && ($css = file_get_contents($styleCss))) {+ $builtIn = Font::parseFontFaces($css);+ }++ if (file_exists($fontsCss) && ($css = file_get_contents($fontsCss))) {+ $custom = Font::parseFontFaces($css, true, false);+ //set font src relative to fonts folder path+ foreach ($custom as &$font) {+ if (isset($font['src'])) {+ $font['src'] = str_replace($this->fontsPath, '/', $font['src']);+ }+ }+ }++ $this->view->builtin = $builtIn;+ $this->view->fonts = $custom;+ //$this->view->theme = $theme;+ $this->view->themename = humanReadable($theme);+ } else {+ $this->notFound(sprintf(__('Theme fonts not found for %s'), $theme));+ }++ $adminPath = \Vvveb\adminPath();++ $controllerPath = $adminPath . 'index.php?module=theme/fonts';+ $this->setMediaEndpoints($controllerPath);++ /*+ $this->view->fonts =+ [+ 0 => [+ 'font-family' => 'test1',+ 'font-weight' => 'normal',+ 'font-style' => 'normal',+ 'src' => 'asdasd',+ 'font-display' => 'auto',+ ],+ 3 => [+ 'font-family' => 'test 2',+ 'font-weight' => 'normal',+ 'font-style' => 'normal',+ 'src' => 'asdasd',+ 'font-display' => 'auto',+ ],+ 4 => [+ 'font-family' => 'test 3',+ 'font-weight' => 'normal',+ 'font-style' => 'normal',+ 'src' => 'asd',+ 'font-display' => 'auto',+ ],+ 5 => [+ 'font-family' => 'test 4',+ 'font-weight' => 'normal',+ 'font-style' => 'normal',+ 'src' => 'fef4f',+ 'font-display' => 'auto',+ ],+ 6 => [+ 'font-family' => 'test 5',+ 'font-weight' => 'normal',+ 'font-style' => 'normal',+ 'src' => 'fsdf',+ 'font-display' => 'auto',+ ],+ ];+ */+ //var_dump($builtIn);+ //var_dump($custom);+ }+}diff --git a/admin/template/theme/fonts.tpl b/admin/template/theme/fonts.tplnew file mode 100644index 0000000..8018235--- /dev/null+++ b/admin/template/theme/fonts.tpl@@ -0,0 +1,60 @@+import(common.tpl)+import(pagination.tpl)++@font = [data-v-builtin] [data-v-font]+@font|deleteAllButFirstChild++@variant = [data-v-builtin] [data-v-font] [data-v-variant]+@variant|deleteAllButFirstChild++@font|before = <?php+if(isset($this->builtin) && is_array($this->builtin)) {+ foreach ($this->builtin as $family => $font) {?>++ @font [data-v-font-family]|innerText = $family+ @font [data-v-font-*]|innerText = $font['@@__data-v-font-([-_\w]+)__@@']++ @variant|before = <?php+ if(is_array($font['variants'])) foreach ($font['variants'] as $variant) {?>++ @variant [data-v-variant-*]|innerText = $variant['@@__data-v-variant-([-_\w]+)__@@']++ @variant [data-v-values]|before = <?php+ if(is_array($variant)) foreach ($variant as $key => $value) {?>++ @variant [data-v-values] [data-v-values-key] = $key+ @variant [data-v-values] [data-v-values-value] = $value++ @variant [data-v-values]|after = <?php } ?>++ @variant|after = <?php } ?>++ @font|after = <?php+ }+}?>++@font = [data-v-fonts] [data-v-font]+@font|deleteAllButFirstChild++@variant = [data-v-fonts] [data-v-font] [data-v-variant]+@variant|deleteAllButFirstChild++@font|before = <?php+if(isset($this->fonts) && is_array($this->fonts)) {+ $i = 0;+ foreach ($this->fonts as $font) { $i++;?>++ @font [data-v-*]|innerText = $font['@@__data-v-([-_\w]+)__@@']+ @font [data-v-*]|name = <?php echo str_replace('[0]', "[$i]", '@@__name__@@');?>+ @font .accordion-button|data-bs-target = <?php echo '#font-' . $i?>;+ @font .accordion-collapse|id = <?php echo 'font-' . $i?>;++ @font [data-v-src]|id = <?php echo "font-$i-src"?>;+ @font [data-media-gallery]|data-target-input = <?php echo "#font-$i-src"?>;++ @font|after = <?php+ }+}?>+++[data-v-theme-name] = $this->themenamediff --git a/admin/template/theme/themes_list.tpl b/admin/template/theme/themes_list.tplindex 8810889..cf12201 100755--- a/admin/template/theme/themes_list.tpl+++ b/admin/template/theme/themes_list.tpl@@ -15,6 +15,7 @@ if(isset($this->themes) && is_array($this->themes)) {[data-v-themes] [data-v-theme] [data-v-theme-preview-url]|href = <?php echo Vvveb\url('index/index') . '?theme=' . $theme['folder'];?>[data-v-themes] [data-v-theme] [data-v-theme-edit-url]|href = <?php echo Vvveb\url(['module' => 'editor/editor']) . '&url=/&template=index.html&theme=' . $theme['folder'];?>[data-v-themes] [data-v-theme] [data-v-theme-code-url]|href = <?php echo Vvveb\url(['module' => 'editor/code']) . '&type=themes#%2F' . $theme['folder'];?>+ [data-v-themes] [data-v-theme] [data-v-theme-fonts-url]|href = <?php echo Vvveb\url(['module' => 'theme/fonts', 'theme' => $theme['folder']]);?>[data-v-themes] [data-v-theme]|after = <?php}}?>diff --git a/config/admin-menu.php b/config/admin-menu.phpindex 6ba4ca0..1e265fa 100755--- a/config/admin-menu.php+++ b/config/admin-menu.php@@ -27,6 +27,7 @@ if (APP == 'admin') {} else {$admin_path = \Vvveb\adminPath();}+$admin_path .= strpos($admin_path, 'index.php') === false ? 'index.php' : '';$homeUrl = Vvveb\Url('index/index');@@ -519,6 +520,20 @@ return'action' => 'index','icon' => 'la la-plus-circle',],+ 'fonts-heading' => [+ 'name' => __('Customize'),+ 'heading' => true,+ 'url' => 'javascript:void(0)',+ 'module' => 'theme/themes',+ 'action' => 'fonts',+ ],+ 'fonts' => [+ 'name' => __('Fonts'),+ 'url' => $admin_path . '?module=theme/fonts',+ 'module' => 'theme/themes',+ 'action' => 'fonts',+ 'icon' => 'la la-font',+ ],'editor' => ['name' => __('Code editor'),'url' => $admin_path . '?module=editor/code&type=themes',@@ -669,14 +684,6 @@ return'action' => 'index','icon' => 'la la-box',],-- 'fields' => [- 'name' => __('Fields'),- 'url' => $admin_path . '?module=field/field-group',- 'module' => 'field/field-group',- 'action' => 'index',- 'icon' => 'la la-stream',- ],],],'ecommerce' => [@@ -937,13 +944,6 @@ return'module' => 'settings/email','action' => 'index',],- 'rest' => [- 'name' => __('Rest Api'),- 'icon' => 'la la-database',- 'url' => $admin_path . '?module=settings/email',- 'module' => 'settings/email',- 'action' => 'index',- ],],],],diff --git a/system/media/font.php b/system/media/font.phpnew file mode 100644index 0000000..63efcfc--- /dev/null+++ b/system/media/font.php@@ -0,0 +1,158 @@+<?php++/**+ * Vvveb+ *+ * Copyright (C) 2022 Ziadin Givan+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++namespace Vvveb\System\Media;++class Font {+ const KEY_VALUE_REGEX = '/\s*(.+?)\s*:\s*([^;$]+)[;$]?/ms';++ const FONT_FACE_REGEX = '/@font-face\s*{([^}]+)}/ms';++ static function getParams($comments) {+ $results = [];++ if (preg_match_all(static :: KEY_VALUE_REGEX, $comments, $matches)) {+ $matches[1] = array_map(function ($key) {+ return trim($key);+ }, $matches[1]);++ $results = array_combine($matches[1], $matches[2]);+ }++ return $results;+ }++ static function arrayToFontFaces($fonts = []) {+ $css = '';++ if ($fonts) {+ foreach ($fonts as &$font) {+ //$font = array_filter($font, fn($var) => ($var !== NULL && $var !== FALSE && $var !== ''));+ //remove empty fields+ //$font = array_filter($font, 'strlen');++ if (isset($font['font-family'])) {+ $font['font-family'] = '"' . trim($font['font-family'], '\'"') . '"';+ }++ if (isset($font['src'])) {+ if (strncmp($font['src'], 'url(', 4) === 0) {+ $font['src']= substr($font['src'], 4);+ }++ $src = trim($font['src'], '\'")(');+ $font['src'] = 'url("' . $src . '")';+ }++ $properties = '';++ foreach ($font as $property => $value) {+ if ($value) {+ $properties .= "$property: $value;";+ }+ }++ if ($properties) {+ $css .= '@font-face {' . $properties . "}\n";+ }+ }+ }++ return $css;+ }++ static function removeFontFaces($css) {+ return preg_replace(static :: FONT_FACE_REGEX, '', $css) ?? $css;+ }++ static function parseFontFaces($css, $allFonts = true, $variants = true) {+ $fontFaceRegex = '/@font-face\s*{([^}]+)}/ms';+ //$css = file_get_contents($cssFile);+ $fonts = [];+ $matches = [];++ if (preg_match_all(static:: FONT_FACE_REGEX, $css, $matches)) {+ foreach ($matches[1] as $match) {+ //var_dump($match);+ $properties = $match;+ //var_dump($match);+ $props = self::getParams($properties);++ if (isset($props['font-family'])) {+ $props['font-family'] = trim($props['font-family'], '\'"');+ }++ if (isset($props['src'])) {+ if (strncmp($props['src'], 'url(', 4) === 0) {+ $props['src']= substr($props['src'], 4);+ }++ $props['src']= trim($props['src'], '\'")(');+ }++ $font = [];++ if ($allFonts) {+ $font = $props;+ } else {+ if ((! isset($props['font-weight']) || $props['font-weight'] == '400' || $props['font-weight'] == 'normal' || strpos($props['font-weight'], ' ') !== false)+ && (! isset($props['font-style']) || $props['font-style'] == 'normal')) {+ $font = $props;+ }+ }++ if ($variants) {+ $family = $props['font-family'];++ if ($family) {+ $fonts[$family]['variants'][] = $font;+ }+ } else {+ if ($font) {+ $fonts[] = $font;+ }+ }+ }+ }++ return $fonts;+ }++ static function themeFonts($theme, $variants = false) {+ $fonts = [];++ if ($theme && is_dir($themeCssDir = DIR_THEMES . $theme . DS . 'css' . DS)) {+ $styleCss = $themeCssDir . 'style.css';+ $fontsCss = $themeCssDir . 'fonts.css';++ if (file_exists($styleCss) && ($css = file_get_contents($styleCss))) {+ $fonts = Font::parseFontFaces($css, false, $variants);+ }++ if (file_exists($fontsCss) && ($css = file_get_contents($fontsCss))) {+ $fonts = array_merge($fonts, Font::parseFontFaces($css, false, $variants));+ }+ }++ return $fonts;+ }+}