[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Editor/ -> Editor.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Editor;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Filter\InputFilter;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Log\Log;
  16  use Joomla\CMS\Plugin\PluginHelper;
  17  use Joomla\Event\AbstractEvent;
  18  use Joomla\Event\DispatcherAwareInterface;
  19  use Joomla\Event\DispatcherAwareTrait;
  20  use Joomla\Event\DispatcherInterface;
  21  use Joomla\Registry\Registry;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('JPATH_PLATFORM') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Editor class to handle WYSIWYG editors
  29   *
  30   * @since  1.5
  31   */
  32  class Editor implements DispatcherAwareInterface
  33  {
  34      use DispatcherAwareTrait;
  35  
  36      /**
  37       * Editor Plugin object
  38       *
  39       * @var    object
  40       * @since  1.5
  41       */
  42      protected $_editor = null;
  43  
  44      /**
  45       * Editor Plugin name
  46       *
  47       * @var    string
  48       * @since  1.5
  49       */
  50      protected $_name = null;
  51  
  52      /**
  53       * Object asset
  54       *
  55       * @var    string
  56       * @since  1.6
  57       */
  58      protected $asset = null;
  59  
  60      /**
  61       * Object author
  62       *
  63       * @var    string
  64       * @since  1.6
  65       */
  66      protected $author = null;
  67  
  68      /**
  69       * Editor instances container.
  70       *
  71       * @var    Editor[]
  72       * @since  2.5
  73       */
  74      protected static $instances = array();
  75  
  76      /**
  77       * Constructor
  78       *
  79       * @param   string               $editor      The editor name
  80       * @param   DispatcherInterface  $dispatcher  The event dispatcher we're going to use
  81       */
  82      public function __construct($editor = 'none', DispatcherInterface $dispatcher = null)
  83      {
  84          $this->_name = $editor;
  85  
  86          // Set the dispatcher
  87          if (!\is_object($dispatcher)) {
  88              $dispatcher = Factory::getContainer()->get('dispatcher');
  89          }
  90  
  91          $this->setDispatcher($dispatcher);
  92  
  93          // Register the getButtons event
  94          $this->getDispatcher()->addListener(
  95              'getButtons',
  96              function (AbstractEvent $event) {
  97                  $editor = $event->getArgument('editor', null);
  98                  $buttons = $event->getArgument('buttons', null);
  99                  $result = $event->getArgument('result', []);
 100                  $newResult = $this->getButtons($editor, $buttons);
 101                  $newResult = (array) $newResult;
 102                  $event['result'] = array_merge($result, $newResult);
 103              }
 104          );
 105      }
 106  
 107      /**
 108       * Returns the global Editor object, only creating it
 109       * if it doesn't already exist.
 110       *
 111       * @param   string  $editor  The editor to use.
 112       *
 113       * @return  Editor The Editor object.
 114       *
 115       * @since   1.5
 116       */
 117      public static function getInstance($editor = 'none')
 118      {
 119          $signature = serialize($editor);
 120  
 121          if (empty(self::$instances[$signature])) {
 122              self::$instances[$signature] = new static($editor);
 123          }
 124  
 125          return self::$instances[$signature];
 126      }
 127  
 128      /**
 129       * Initialise the editor
 130       *
 131       * @return  void
 132       *
 133       * @since   1.5
 134       */
 135      public function initialise()
 136      {
 137          // Check if editor is already loaded
 138          if ($this->_editor === null) {
 139              return;
 140          }
 141  
 142          if (method_exists($this->_editor, 'onInit')) {
 143              \call_user_func(array($this->_editor, 'onInit'));
 144          }
 145      }
 146  
 147      /**
 148       * Display the editor area.
 149       *
 150       * @param   string   $name     The control name.
 151       * @param   string   $html     The contents of the text area.
 152       * @param   string   $width    The width of the text area (px or %).
 153       * @param   string   $height   The height of the text area (px or %).
 154       * @param   integer  $col      The number of columns for the textarea.
 155       * @param   integer  $row      The number of rows for the textarea.
 156       * @param   boolean  $buttons  True and the editor buttons will be displayed.
 157       * @param   string   $id       An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
 158       * @param   string   $asset    The object asset
 159       * @param   object   $author   The author.
 160       * @param   array    $params   Associative array of editor parameters.
 161       *
 162       * @return  string
 163       *
 164       * @since   1.5
 165       */
 166      public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
 167      {
 168          $this->asset = $asset;
 169          $this->author = $author;
 170          $this->_loadEditor($params);
 171  
 172          // Check whether editor is already loaded
 173          if ($this->_editor === null) {
 174              Factory::getApplication()->enqueueMessage(Text::_('JLIB_NO_EDITOR_PLUGIN_PUBLISHED'), 'danger');
 175  
 176              return;
 177          }
 178  
 179          // Backwards compatibility. Width and height should be passed without a semicolon from now on.
 180          // If editor plugins need a unit like "px" for CSS styling, they need to take care of that
 181          $width = str_replace(';', '', $width);
 182          $height = str_replace(';', '', $height);
 183  
 184          $args['name'] = $name;
 185          $args['content'] = $html;
 186          $args['width'] = $width;
 187          $args['height'] = $height;
 188          $args['col'] = $col;
 189          $args['row'] = $row;
 190          $args['buttons'] = $buttons;
 191          $args['id'] = $id ?: $name;
 192          $args['asset'] = $asset;
 193          $args['author'] = $author;
 194          $args['params'] = $params;
 195  
 196          return \call_user_func_array(array($this->_editor, 'onDisplay'), $args);
 197      }
 198  
 199      /**
 200       * Get the editor extended buttons (usually from plugins)
 201       *
 202       * @param   string  $editor   The name of the editor.
 203       * @param   mixed   $buttons  Can be boolean or array, if boolean defines if the buttons are
 204       *                            displayed, if array defines a list of buttons not to show.
 205       *
 206       * @return  array
 207       *
 208       * @since   1.5
 209       */
 210      public function getButtons($editor, $buttons = true)
 211      {
 212          $result = array();
 213  
 214          if (\is_bool($buttons) && !$buttons) {
 215              return $result;
 216          }
 217  
 218          // Get plugins
 219          $plugins = PluginHelper::getPlugin('editors-xtd');
 220  
 221          foreach ($plugins as $plugin) {
 222              if (\is_array($buttons) && \in_array($plugin->name, $buttons)) {
 223                  continue;
 224              }
 225  
 226              PluginHelper::importPlugin('editors-xtd', $plugin->name, false);
 227              $className = 'PlgEditorsXtd' . $plugin->name;
 228  
 229              if (!class_exists($className)) {
 230                  $className = 'PlgButton' . $plugin->name;
 231              }
 232  
 233              if (class_exists($className)) {
 234                  $dispatcher = $this->getDispatcher();
 235                  $plugin = new $className($dispatcher, (array) $plugin);
 236              }
 237  
 238              // Try to authenticate
 239              if (!method_exists($plugin, 'onDisplay')) {
 240                  continue;
 241              }
 242  
 243              $button = $plugin->onDisplay($editor, $this->asset, $this->author);
 244  
 245              if (empty($button)) {
 246                  continue;
 247              }
 248  
 249              if (\is_array($button)) {
 250                  $result = array_merge($result, $button);
 251                  continue;
 252              }
 253  
 254              $result[] = $button;
 255          }
 256  
 257          return $result;
 258      }
 259  
 260      /**
 261       * Load the editor
 262       *
 263       * @param   array  $config  Associative array of editor config parameters
 264       *
 265       * @return  mixed
 266       *
 267       * @since   1.5
 268       */
 269      protected function _loadEditor($config = array())
 270      {
 271          // Check whether editor is already loaded
 272          if ($this->_editor !== null) {
 273              return false;
 274          }
 275  
 276          // Build the path to the needed editor plugin
 277          $name = InputFilter::getInstance()->clean($this->_name, 'cmd');
 278          $path = JPATH_PLUGINS . '/editors/' . $name . '/' . $name . '.php';
 279  
 280          if (!is_file($path)) {
 281              Log::add(Text::_('JLIB_HTML_EDITOR_CANNOT_LOAD'), Log::WARNING, 'jerror');
 282  
 283              return false;
 284          }
 285  
 286          // Require plugin file
 287          require_once $path;
 288  
 289          // Get the plugin
 290          $plugin = PluginHelper::getPlugin('editors', $this->_name);
 291  
 292          // If no plugin is published we get an empty array and there not so much to do with it
 293          if (empty($plugin)) {
 294              return false;
 295          }
 296  
 297          $params = new Registry($plugin->params);
 298          $params->loadArray($config);
 299          $plugin->params = $params;
 300  
 301          // Build editor plugin classname
 302          $name = 'PlgEditor' . $this->_name;
 303  
 304          $dispatcher = $this->getDispatcher();
 305  
 306          if ($this->_editor = new $name($dispatcher, (array) $plugin)) {
 307              // Load plugin parameters
 308              $this->initialise();
 309              PluginHelper::importPlugin('editors-xtd');
 310          }
 311      }
 312  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer