| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.editor |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
JLoader::import('adapter.editor.editor'); |
| 15 |
|
| 16 |
/** |
| 17 |
* Editor class to handle a Code Mirror editor. |
| 18 |
* |
| 19 |
* @since 10.0 |
| 20 |
*/ |
| 21 |
class JEditorCodeMirror extends JEditor |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @override |
| 25 |
* Renders the editor area. |
| 26 |
* |
| 27 |
* @param string $name The control name. |
| 28 |
* @param string $html The contents of the text area. |
| 29 |
* @param string $width The width of the text area (px or %). |
| 30 |
* @param string $height The height of the text area (px or %). |
| 31 |
* @param integer $col The number of columns for the textarea. |
| 32 |
* @param integer $row The number of rows for the textarea. |
| 33 |
* @param boolean $buttons True and the editor buttons will be displayed. |
| 34 |
* @param string $id An optional ID for the textarea (@since 10.1.20). |
| 35 |
* @param array $params Associative array of editor parameters (@since 10.1.35). |
| 36 |
* |
| 37 |
* @return string The editor. |
| 38 |
* |
| 39 |
* @link https://developer.wordpress.org/reference/functions/wp_enqueue_code_editor/ |
| 40 |
*/ |
| 41 |
protected function render($name, $html, $width, $height, $col, $row, $buttons, $id, $params) |
| 42 |
{ |
| 43 |
$options = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Check whether code mirror textarea should allow the code editing. |
| 47 |
* |
| 48 |
* @since 10.1.35 |
| 49 |
*/ |
| 50 |
$options['readOnly'] = !empty($params['readonly']) || !empty($params['disabled']) ? 'nocursor' : false; |
| 51 |
|
| 52 |
?> |
| 53 |
<textarea |
| 54 |
name="<?php echo esc_attr($name); ?>" |
| 55 |
id="<?php echo esc_attr($id); ?>" |
| 56 |
rows="<?php echo esc_attr($row); ?>" |
| 57 |
cols="<?php echo esc_attr($col); ?>" |
| 58 |
><?php echo htmlentities($html); ?></textarea> |
| 59 |
|
| 60 |
<?php |
| 61 |
|
| 62 |
// Make sure codemirror is supported by this version of WordPress (4.9.0 >). |
| 63 |
// If not, a plain textarea will be shown. |
| 64 |
if (function_exists('wp_enqueue_code_editor')) |
| 65 |
{ |
| 66 |
/** |
| 67 |
* Use the requested type of language, otherwise fallback to PHP if not supplied. |
| 68 |
* |
| 69 |
* @since 10.1.35 |
| 70 |
*/ |
| 71 |
$syntax = !empty($params['syntax']) ? $params['syntax'] : 'php'; |
| 72 |
|
| 73 |
/** |
| 74 |
* Adjust the syntax according to the WordPress needs. |
| 75 |
* |
| 76 |
* @since 10.1.48 |
| 77 |
*/ |
| 78 |
switch ($syntax) |
| 79 |
{ |
| 80 |
case 'js': |
| 81 |
$syntax = 'text/javascript'; |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
// enqueue code editor |
| 86 |
wp_enqueue_code_editor([ |
| 87 |
'type' => $syntax, |
| 88 |
'codemirror' => $options, |
| 89 |
]); |
| 90 |
|
| 91 |
/** |
| 92 |
* Inject CodeMirror instance within Joomla.editors pool. |
| 93 |
* |
| 94 |
* @since 10.1.17 |
| 95 |
*/ |
| 96 |
JFactory::getDocument()->addScriptDeclaration( |
| 97 |
<<<JS |
| 98 |
(function($) { |
| 99 |
'use strict'; |
| 100 |
|
| 101 |
$(function() { |
| 102 |
const editor = wp.codeEditor.initialize('$id'); |
| 103 |
|
| 104 |
Joomla.editors.instances['$id'] = { |
| 105 |
id: '$id', |
| 106 |
element: editor, |
| 107 |
getValue: function() { |
| 108 |
return this.element.codemirror.getValue(); |
| 109 |
}, |
| 110 |
setValue: function(text) { |
| 111 |
return this.element.codemirror.setValue(text); |
| 112 |
}, |
| 113 |
getSelection: function() { |
| 114 |
return this.element.codemirror.getSelection(); |
| 115 |
}, |
| 116 |
replaceSelection: function(text) { |
| 117 |
this.element.codemirror.replaceSelection(text); |
| 118 |
}, |
| 119 |
}; |
| 120 |
}); |
| 121 |
})(jQuery); |
| 122 |
JS |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|