- md5
- cbf1014c8588286d432cc8c20802d8cb
- sha1
- 3dc070fbfbe6dec698868537c51fbd6f041b2d55
- sha256
- e99ae1db925f088c48a79fc27075c910f04012bc981a8d4f99b0f2c7e6129839
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Highlight
*
* @copyright (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Finder\Administrator\Indexer\Result;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* System plugin to highlight terms.
*
* @since 2.5
*/
class PlgSystemHighlight extends CMSPlugin
{
/**
* Application object.
*
* @var \Joomla\CMS\Application\CMSApplication
* @since 3.7.0
*/
protected $app;
/**
* Method to catch the onAfterDispatch event.
*
* This is where we setup the click-through content highlighting for.
* The highlighting is done with JavaScript so we just
* need to check a few parameters and the JHtml behavior will do the rest.
*
* @return void
*
* @since 2.5
*/
public function onAfterDispatch()
{
// Check that we are in the site application.
if ($this->app->isClient('administrator')) {
return;
}
// Set the variables.
$input = $this->app->input;
$extension = $input->get('option', '', 'cmd');
// Check if the highlighter is enabled.
if (!ComponentHelper::getParams($extension)->get('highlight_terms', 1)) {
return;
}
// Check if the highlighter should be activated in this environment.
if ($input->get('tmpl', '', 'cmd') === 'component' || $this->app->getDocument()->getType() !== 'html') {
return;
}
// Get the terms to highlight from the request.
$terms = $input->request->get('highlight', null, 'base64');
$terms = $terms ? json_decode(base64_decode($terms)) : null;
// Check the terms.
if (empty($terms)) {
return;
}
// Clean the terms array.
$filter = InputFilter::getInstance();
$cleanTerms = array();
foreach ($terms as $term) {
$cleanTerms[] = htmlspecialchars($filter->clean($term, 'string'));
}
// Activate the highlighter.
if (!empty($cleanTerms)) {
$doc = Factory::getDocument();
$doc->getWebAssetManager()->useScript('highlight');
$doc->addScriptOptions(
'highlight',
[[
'class' => 'js-highlight',
'highLight' => $cleanTerms,
]]
);
}
// Adjust the component buffer.
/** @var \Joomla\CMS\Document\HtmlDocument $doc */
$doc = $this->app->getDocument();
$buf = $doc->getBuffer('component');
$buf = '<div class="js-highlight">' . $buf . '</div>';
$doc->setBuffer($buf, 'component');
}
/**
* Method to catch the onFinderResult event.
*
* @param Result $item The search result
* @param array $query The search query of this result
*
* @return void
*
* @since 4.0.0
*/
public function onFinderResult($item, $query)
{
static $params;
if (is_null($params)) {
$params = ComponentHelper::getParams('com_finder');
}
// Get the route with highlighting information.
if (
!empty($query->highlight)
&& empty($item->mime)
&& $params->get('highlight_terms', 1)
) {
$item->route .= '&highlight=' . base64_encode(json_encode($query->highlight));
}
}
}
The same content is in 9 releases, Joomla 4.2.1 to 4.2.6. See them all, or every version of this file.