From 17cb4e47dfec18793a0848c66e9c9602e1a96394 Mon Sep 17 00:00:00 2001 From: Lester Caine Date: Sat, 16 May 2026 14:08:36 +0100 Subject: Add filter to display {code} block is ckeditor Caution, the {code} element is replaced by
' wrapper in souce view

---
 includes/bit_setup_inc.php   |  4 +++
 includes/code_blocks_inc.php | 62 ++++++++++++++++++++++++++++++++++++++++++++
 templates/footer_inc.tpl     |  1 +
 3 files changed, 67 insertions(+)
 create mode 100644 includes/code_blocks_inc.php

diff --git a/includes/bit_setup_inc.php b/includes/bit_setup_inc.php
index 7448d4c..008ba90 100755
--- a/includes/bit_setup_inc.php
+++ b/includes/bit_setup_inc.php
@@ -16,6 +16,10 @@ define( "CKEDITOR_PKG_ADMIN_PATH", BIT_ROOT_PATH . basename( $pRegisterHash["pac
 
 $gBitSystem->registerPackage( $pRegisterHash );
 
+if( $gBitSystem->isPackageActive( 'ckeditor' ) ) {
+	require_once CKEDITOR_PKG_INCLUDE_PATH . 'code_blocks_inc.php';
+}
+
 if( $gBitSystem->isPackageActive( "ckeditor" ) && $gBitUser->isRegistered() && $gBitUser->hasPermission( "p_liberty_enter_html" ) ){
 	if( defined( "IS_LIVE" ) && IS_LIVE ) {
 		$gBitThemes->loadJavascript( CKEDITOR_PKG_PATH."ckeditor.js", false, 600, false );
diff --git a/includes/code_blocks_inc.php b/includes/code_blocks_inc.php
new file mode 100644
index 0000000..ec50fce
--- /dev/null
+++ b/includes/code_blocks_inc.php
@@ -0,0 +1,62 @@
+registerPlugin( PLUGIN_GUID_CK_CODEBLOCKS, [
+	'title'             => 'CKEditor Code Block Conversion',
+	'description'       => 'Converts {code} wiki blocks to 
 for CKEditor editing, and back on save.',
+	'auto_activate'     => true,
+	'plugin_type'       => FILTER_PLUGIN,
+	'prestore_function' => 'ckeditor_code_pre_to_wiki_filter',
+]);
+
+/**
+ * Convert {code params}...{/code} wiki markup to 
 for CKEditor.
+ * HTML-encodes the content so CKEditor preserves it as text (protects ' . $code . '
'; + }, + $content + ); +} + +/** + * Reverse of ckeditor_code_wiki_to_pre — called by the prestore filter. + * Converts
...
back to {code ...}...{/code}. + */ +function ckeditor_code_pre_to_wiki( string $content ): string { + return preg_replace_callback( + '/]*?class="[^"]*\bbwcode\b[^"]*"[^>]*)>([\s\S]*?)<\/pre>/i', + function( array $m ): string { + $attrStr = $m[1]; + // Strip any trailing
CKEditor appends inside
+			$raw  = preg_replace( '/\s*$/i', '', $m[2] );
+			$code = html_entity_decode( $raw, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
+			$params = '';
+			if( preg_match( '/data-bwcode-params="([^"]*)"/', $attrStr, $pm ) ) {
+				$params = html_entity_decode( $pm[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' );
+			}
+			$open = $params !== '' ? "{code $params}" : '{code}';
+			return $open . $code . '{/code}';
+		},
+		$content
+	);
+}
+
+function ckeditor_code_pre_to_wiki_filter( &$pData, &$pFilterHash, $pObject = null ): void {
+	if( !empty( $pData ) ) {
+		$pData = ckeditor_code_pre_to_wiki( $pData );
+	}
+}
diff --git a/templates/footer_inc.tpl b/templates/footer_inc.tpl
index feb79bd..9a801d6 100755
--- a/templates/footer_inc.tpl
+++ b/templates/footer_inc.tpl
@@ -2,6 +2,7 @@
 {if $gBitUser->isRegistered() }