[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/system/highlight/ -> highlight.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  System.Highlight
   6   *
   7   * @copyright   (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9  
  10   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  11   */
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Filter\InputFilter;
  16  use Joomla\CMS\Plugin\CMSPlugin;
  17  use Joomla\Component\Finder\Administrator\Indexer\Result;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * System plugin to highlight terms.
  25   *
  26   * @since  2.5
  27   */
  28  class PlgSystemHighlight extends CMSPlugin
  29  {
  30      /**
  31       * Application object.
  32       *
  33       * @var    \Joomla\CMS\Application\CMSApplication
  34       * @since  3.7.0
  35       */
  36      protected $app;
  37  
  38      /**
  39       * Method to catch the onAfterDispatch event.
  40       *
  41       * This is where we setup the click-through content highlighting for.
  42       * The highlighting is done with JavaScript so we just
  43       * need to check a few parameters and the JHtml behavior will do the rest.
  44       *
  45       * @return  void
  46       *
  47       * @since   2.5
  48       */
  49      public function onAfterDispatch()
  50      {
  51          // Check that we are in the site application.
  52          if ($this->app->isClient('administrator')) {
  53              return;
  54          }
  55  
  56          // Set the variables.
  57          $input     = $this->app->input;
  58          $extension = $input->get('option', '', 'cmd');
  59  
  60          // Check if the highlighter is enabled.
  61          if (!ComponentHelper::getParams($extension)->get('highlight_terms', 1)) {
  62              return;
  63          }
  64  
  65          // Check if the highlighter should be activated in this environment.
  66          if ($input->get('tmpl', '', 'cmd') === 'component' || $this->app->getDocument()->getType() !== 'html') {
  67              return;
  68          }
  69  
  70          // Get the terms to highlight from the request.
  71          $terms = $input->request->get('highlight', null, 'base64');
  72          $terms = $terms ? json_decode(base64_decode($terms)) : null;
  73  
  74          // Check the terms.
  75          if (empty($terms)) {
  76              return;
  77          }
  78  
  79          // Clean the terms array.
  80          $filter     = InputFilter::getInstance();
  81  
  82          $cleanTerms = array();
  83  
  84          foreach ($terms as $term) {
  85              $cleanTerms[] = htmlspecialchars($filter->clean($term, 'string'));
  86          }
  87  
  88          // Activate the highlighter.
  89          if (!empty($cleanTerms)) {
  90              $doc = Factory::getDocument();
  91  
  92              $doc->getWebAssetManager()->useScript('highlight');
  93              $doc->addScriptOptions(
  94                  'highlight',
  95                  [[
  96                      'class'      => 'js-highlight',
  97                      'highLight'  => $cleanTerms,
  98                  ]]
  99              );
 100          }
 101  
 102          // Adjust the component buffer.
 103          /** @var \Joomla\CMS\Document\HtmlDocument $doc */
 104          $doc = $this->app->getDocument();
 105          $buf = $doc->getBuffer('component');
 106          $buf = '<div class="js-highlight">' . $buf . '</div>';
 107          $doc->setBuffer($buf, 'component');
 108      }
 109  
 110      /**
 111       * Method to catch the onFinderResult event.
 112       *
 113       * @param   Result  $item   The search result
 114       * @param   array   $query  The search query of this result
 115       *
 116       * @return  void
 117       *
 118       * @since   4.0.0
 119       */
 120      public function onFinderResult($item, $query)
 121      {
 122          static $params;
 123  
 124          if (is_null($params)) {
 125              $params = ComponentHelper::getParams('com_finder');
 126          }
 127  
 128          // Get the route with highlighting information.
 129          if (
 130              !empty($query->highlight)
 131              && empty($item->mime)
 132              && $params->get('highlight_terms', 1)
 133          ) {
 134              $item->route .= '&highlight=' . base64_encode(json_encode($query->highlight));
 135          }
 136      }
 137  }


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