From 992a9d6ca186bb4399143dd5ee72a27a3b4909aa Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 26 Oct 2020 18:03:48 +0100 Subject: Initial commit --- src/MetaBox.php | 81 +++++++++++++++++++++++++ src/Settings.php | 97 ++++++++++++++++++++++++++++++ src/ThemeEditor.php | 73 +++++++++++++++++++++++ src/TimberEditor.php | 148 ++++++++++++++++++++++++++++++++++++++++++++++ src/codemirror-themes.php | 67 +++++++++++++++++++++ 5 files changed, 466 insertions(+) create mode 100644 src/MetaBox.php create mode 100644 src/Settings.php create mode 100644 src/ThemeEditor.php create mode 100644 src/TimberEditor.php create mode 100644 src/codemirror-themes.php (limited to 'src') diff --git a/src/MetaBox.php b/src/MetaBox.php new file mode 100644 index 0000000..d11336f --- /dev/null +++ b/src/MetaBox.php @@ -0,0 +1,81 @@ + + + $file, + 'codemirror' => [ + 'theme' => Settings::getCodeMirrorTheme(), + ], + ]); + wp_add_inline_script('code-editor', sprintf('jQuery( function() { wp.codeEditor.initialize( "timber-editor_content", %s ); } );', wp_json_encode($settings))); + } +} diff --git a/src/Settings.php b/src/Settings.php new file mode 100644 index 0000000..d9947f8 --- /dev/null +++ b/src/Settings.php @@ -0,0 +1,97 @@ + +
+

+
+ +
+
+ + + 'timber-editor_general_supported-post-types']); + + register_setting('timber-editor', 'timber-editor_codemirror_theme'); + add_settings_section('timber-editor_codemirror', 'CodeMirror', function () {}, 'timber-editor'); + add_settings_field('timber-editor_codemirror_theme', 'Theme', function () { + $theme = self::getCodeMirrorTheme(); + $themes = include_once 'codemirror-themes.php'; + ?> + +

+ + + +

+ 'timber-editor_codemirror_theme']); + } + + /** + * @return string[] + */ + public static function getGeneralSupportedPostTypes() + { + return get_option('timber-editor_general_supported-post-types', ['page']) ?: []; + } + + /** + * @return string + */ + public static function getCodeMirrorTheme() + { + return get_option('timber-editor_codemirror_theme', 'default'); + } +} diff --git a/src/ThemeEditor.php b/src/ThemeEditor.php new file mode 100644 index 0000000..0b62264 --- /dev/null +++ b/src/ThemeEditor.php @@ -0,0 +1,73 @@ + 'twig', 'base' => 'text/html']; + } + + $settings['codemirror']['theme'] = Settings::getCodeMirrorTheme(); + + return $settings; + } + + /** + * wp_enqueue_code_editor action callback + * Adds twig and custom mode support + * Adds selected theme css + * + * @param $settings + */ + public function enqueueCodeEditor($settings) { + if (isset($settings['codemirror']['mode']['name']) && $settings['codemirror']['mode']['name'] == 'twig') { + wp_add_inline_script( # fix as described here: https://make.wordpress.org/core/2017/10/22/code-editing-improvements-in-wordpress-4-9/ + 'wp-codemirror', + 'window.CodeMirror = wp.CodeMirror;' + ); + wp_enqueue_script('mode-twig', 'https://unpkg.com/codemirror@5/mode/twig/twig.js', ['wp-codemirror']); + } + + $theme = $settings['codemirror']['theme']; + if ($theme != 'default') { + wp_enqueue_style('codemirror-theme', "https://unpkg.com/codemirror@5/theme/$theme.css", ['wp-codemirror']); + } + } +} diff --git a/src/TimberEditor.php b/src/TimberEditor.php new file mode 100644 index 0000000..2eb3b36 --- /dev/null +++ b/src/TimberEditor.php @@ -0,0 +1,148 @@ +run(); + } + + /** + * admin_notices action callback for missing Timber Library + */ + public function adminNoticeTimberLibraryMissing() + { + ?> +
+

+ Timber + (install) + needs to be installed and active. +

+
+ +
+

+ Classic Editor + (install) + should be installed and active, because the Gutenberg Editor doesn't play well with CodeMirror currently. +

+
+