diff --git a/.gitignore b/.gitignore index e795694..b23f2fe 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ src/db/config.php src/db/config-* src/config.php src/includes/vendor/ +src/content/theme/custom.css tests/ diff --git a/src/ext/CustomCSS/.htaccess b/src/ext/CustomCSS/.htaccess new file mode 100644 index 0000000..8d2f256 --- /dev/null +++ b/src/ext/CustomCSS/.htaccess @@ -0,0 +1 @@ +deny from all diff --git a/src/ext/CustomCSS/extension.json b/src/ext/CustomCSS/extension.json new file mode 100644 index 0000000..64081e2 --- /dev/null +++ b/src/ext/CustomCSS/extension.json @@ -0,0 +1,6 @@ +{ + "bundleId": "CustomCSS", + "name": "Custom CSS", + "version": "1.0", + "description": "Add you own css rules" +} diff --git a/src/ext/CustomCSS/lang/en.json b/src/ext/CustomCSS/lang/en.json new file mode 100644 index 0000000..07adf87 --- /dev/null +++ b/src/ext/CustomCSS/lang/en.json @@ -0,0 +1,7 @@ +{ + "ext.CustomCSS.name": "Custom CSS", + "customcss.h_css": "CSS", + "customcss.d_css": "Write you own CSS rules", + "customcss.not_writable": "CSS file is not writable, check permissions for custom.css", + "customcss.saved": "Saved" +} diff --git a/src/ext/CustomCSS/lang/ru.json b/src/ext/CustomCSS/lang/ru.json new file mode 100644 index 0000000..90da2db --- /dev/null +++ b/src/ext/CustomCSS/lang/ru.json @@ -0,0 +1,7 @@ +{ + "ext.CustomCSS.name": "Дополнительные стили", + "customcss.h_css": "CSS", + "customcss.d_css": "Добавляйте собственные стили CSS", + "customcss.not_writable": "Ошибка записи в файл, проверьте права доступа к custom.css", + "customcss.saved": "Сохранено" +} diff --git a/src/ext/CustomCSS/loader.php b/src/ext/CustomCSS/loader.php new file mode 100644 index 0000000..fa35437 --- /dev/null +++ b/src/ext/CustomCSS/loader.php @@ -0,0 +1,88 @@ + + Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. +*/ + +if (!defined('MTTPATH')) { + die("Unexpected usage."); +} + +function mtt_ext_customcss_instance(): MTTExtension +{ + return new CustomCssExtension(); +} + +class CustomCssExtension extends MTTExtension implements MTTExtensionSettingsInterface +{ + //the same as dir name + const bundleId = 'CustomCSS'; + + // settings domain + const domain = "ext.customcss.json"; + + const cssFilename = 'custom.css'; + + function init() + { + $prefs = self::preferences(); + if (isset($prefs['css'])) { + $href = htmlspecialchars( get_unsafe_mttinfo('theme_url'). self::cssFilename. '?v='. ($prefs['edited'] ?? 0) ); + $cb = function() use ($href) { + print "\n"; + }; + add_action('theme_head_end', $cb); + } + } + + function settingsPage(): string + { + $e = function($s) { return __($s, true); }; + $prefs = self::preferences(); + $css = htmlspecialchars($prefs['css'] ?? ''); + + return +<< + {$e('customcss.h_css')} + {$e('customcss.d_css')} + + $css + +EOD; + } + + function saveSettings(array $params, ?string &$outMessage): bool + { + if (defined('MTT_DEMO')) { + $outMessage = "Demo"; + return true; + } + $css = $params['css'] ?? ''; + + $cssFilename = MTT_THEME_PATH. self::cssFilename; + if (!file_exists($cssFilename)) { + @touch($cssFilename); + } + if (!is_writable($cssFilename)) { + $outMessage = __('customcss.not_writable'); + return false; + } + @file_put_contents($cssFilename, $css); + $outMessage = __('customcss.saved'); + return true; + } + + static function preferences(): array + { + $prefs['cssFilename'] = $cssFilename = MTT_THEME_PATH. self::cssFilename; + if (file_exists($prefs['cssFilename'])) { + $prefs['edited'] = filemtime($cssFilename) ?? 0; + $prefs['css'] = @file_get_contents($cssFilename) ?? ''; + } + return $prefs; + } + +}