editor.php
145 lines
| 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 | /** |
| 15 | * Editor class to handle WYSIWYG editors. |
| 16 | * |
| 17 | * @since 10.0 |
| 18 | */ |
| 19 | abstract class JEditor |
| 20 | { |
| 21 | /** |
| 22 | * Editor instances container. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | protected static $instances = array(); |
| 27 | |
| 28 | /** |
| 29 | * The editor type name. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $_name; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * @param string $editor The editor name. |
| 39 | */ |
| 40 | public function __construct($editor = 'none') |
| 41 | { |
| 42 | $this->_name = !is_null($editor) ? $editor : 'none'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the global Editor object, only creating it |
| 47 | * if it doesn't already exist. |
| 48 | * |
| 49 | * @param string $editor The editor to use. |
| 50 | * |
| 51 | * @return self The Editor object. |
| 52 | */ |
| 53 | public static function getInstance($editor = 'none') |
| 54 | { |
| 55 | if (empty(static::$instances[$editor])) |
| 56 | { |
| 57 | if (!JLoader::import('adapter.editor.classes.' . $editor)) |
| 58 | { |
| 59 | $editor = 'none'; |
| 60 | JLoader::import('adapter.editor.classes.' . $editor); |
| 61 | } |
| 62 | |
| 63 | $classname = 'JEditor' . ucwords($editor); |
| 64 | |
| 65 | if (!class_exists($classname)) |
| 66 | { |
| 67 | throw new Exception("The editor [$classname] does not exist.", 404); |
| 68 | } |
| 69 | |
| 70 | $instance = new $classname($editor); |
| 71 | |
| 72 | if (!$instance instanceof JEditor) |
| 73 | { |
| 74 | throw new Exception("The editor [$classname] must be an instance of JEditor.", 500); |
| 75 | } |
| 76 | |
| 77 | static::$instances[$editor] = $instance; |
| 78 | } |
| 79 | |
| 80 | return static::$instances[$editor]; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Displays the editor area. |
| 85 | * |
| 86 | * @param string $name The control name. |
| 87 | * @param string $html The contents of the text area. |
| 88 | * @param string $width The width of the text area (px or %). |
| 89 | * @param string $height The height of the text area (px or %). |
| 90 | * @param integer $col The number of columns for the textarea. |
| 91 | * @param integer $row The number of rows for the textarea. |
| 92 | * @param boolean $buttons True and the editor buttons will be displayed. |
| 93 | * @param string $id An optional ID for the textarea (@since 10.1.20). If not supplied the name is used. |
| 94 | * @param array $params Associative array of editor parameters (@since 10.1.35). |
| 95 | * |
| 96 | * @return string The editor. |
| 97 | * |
| 98 | * @link https://codex.wordpress.org/Function_Reference/wp_editor |
| 99 | */ |
| 100 | public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $params = array()) |
| 101 | { |
| 102 | /** |
| 103 | * Since the Joomla `JEditor` interface supports other 2 arguments between $id and $params ($asset and $author), |
| 104 | * we should assume that the $params array has been provided has last element whenever the total number of |
| 105 | * received arguments is equals or higher than 9. |
| 106 | * |
| 107 | * @since 10.1.48 |
| 108 | */ |
| 109 | $args = func_get_args(); |
| 110 | |
| 111 | if (count($args) >= 9) |
| 112 | { |
| 113 | $params = end($args); |
| 114 | } |
| 115 | |
| 116 | $id = preg_replace("/[^a-zA-Z0-9_]+/", '_', $id ? $id : $name); |
| 117 | |
| 118 | ob_start(); |
| 119 | |
| 120 | $this->render($name, (string) $html, $width, $height, $col, $row, $buttons, $id, $params); |
| 121 | |
| 122 | $output = ob_get_contents(); |
| 123 | ob_end_clean(); |
| 124 | |
| 125 | return $output; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Renders the editor area. |
| 130 | * |
| 131 | * @param string $name The control name. |
| 132 | * @param string $html The contents of the text area. |
| 133 | * @param string $width The width of the text area (px or %). |
| 134 | * @param string $height The height of the text area (px or %). |
| 135 | * @param integer $col The number of columns for the textarea. |
| 136 | * @param integer $row The number of rows for the textarea. |
| 137 | * @param boolean $buttons True and the editor buttons will be displayed. |
| 138 | * @param string $id An optional ID for the textarea (@since 10.1.20). |
| 139 | * @param array $params Associative array of editor parameters (@since 10.1.35). |
| 140 | * |
| 141 | * @return string The editor. |
| 142 | */ |
| 143 | abstract protected function render($name, $html, $width, $height, $col, $row, $buttons, $id, $params); |
| 144 | } |
| 145 |