. * * The textarea stays the source of truth: CodeMirror mirrors its content back * on every edit, so any form serialization that reads the textarea's value * keeps working unchanged. When the user has turned syntax highlighting off in * their profile, this degrades to the plain textarea. */ class RsvCodeEditor { /** * Renders a code-editor-backed '; $settings = wp_enqueue_code_editor(['type' => $mode]); // Syntax highlighting disabled in the user's profile — keep it plain. if ($settings === false) { return $textarea; } self::schedule_init($id, $settings); return $textarea; } /** * Initializes CodeMirror on $id after the code editor script loads, and * keeps the underlying textarea in sync — including dispatching `input` so * listeners (live previews, change tracking) still fire while typing. * * @param array $settings As returned by wp_enqueue_code_editor. */ private static function schedule_init(string $id, array $settings): void { $id_json = wp_json_encode($id); $settings_json = wp_json_encode($settings); if ($id_json === false || $settings_json === false) { return; } $script = '(function(){' . 'var ta=document.getElementById(' . $id_json . ');' . 'if(!ta||!window.wp||!wp.codeEditor)return;' . 'var ed=wp.codeEditor.initialize(ta,' . $settings_json . ');' . 'ed.codemirror.on("change",function(cm){' . 'cm.save();' . 'ta.dispatchEvent(new Event("input",{bubbles:true}));' . '});' . '})();'; wp_add_inline_script('code-editor', $script); } }