first commit
This commit is contained in:
21
plugins/system/highlight/highlight.xml
Normal file
21
plugins/system/highlight/highlight.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="system" method="upgrade">
|
||||
<name>plg_system_highlight</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2011-08</creationDate>
|
||||
<copyright>(C) 2011 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>3.0.0</version>
|
||||
<description>PLG_SYSTEM_HIGHLIGHT_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\System\Highlight</namespace>
|
||||
<files>
|
||||
<folder plugin="highlight">services</folder>
|
||||
<folder>src</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_system_highlight.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_system_highlight.sys.ini</language>
|
||||
</languages>
|
||||
</extension>
|
||||
46
plugins/system/highlight/services/provider.php
Normal file
46
plugins/system/highlight/services/provider.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage System.highlight
|
||||
*
|
||||
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\PluginInterface;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use Joomla\Event\DispatcherInterface;
|
||||
use Joomla\Plugin\System\Highlight\Extension\Highlight;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
public function register(Container $container): void
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
function (Container $container) {
|
||||
$plugin = new Highlight(
|
||||
$container->get(DispatcherInterface::class),
|
||||
(array) PluginHelper::getPlugin('system', 'highlight')
|
||||
);
|
||||
$plugin->setApplication(Factory::getApplication());
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
127
plugins/system/highlight/src/Extension/Highlight.php
Normal file
127
plugins/system/highlight/src/Extension/Highlight.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
namespace Joomla\Plugin\System\Highlight\Extension;
|
||||
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
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
|
||||
*/
|
||||
final class Highlight extends CMSPlugin
|
||||
{
|
||||
/**
|
||||
* 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->getApplication()->isClient('site')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the variables.
|
||||
$input = $this->getApplication()->getInput();
|
||||
$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->getApplication()->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 = [];
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$cleanTerms[] = htmlspecialchars($filter->clean($term, 'string'));
|
||||
}
|
||||
|
||||
/** @var \Joomla\CMS\Document\HtmlDocument $doc */
|
||||
$doc = $this->getApplication()->getDocument();
|
||||
|
||||
// Activate the highlighter.
|
||||
if (!empty($cleanTerms)) {
|
||||
$doc->getWebAssetManager()->useScript('highlight');
|
||||
$doc->addScriptOptions(
|
||||
'highlight',
|
||||
[[
|
||||
'class' => 'js-highlight',
|
||||
'highLight' => $cleanTerms,
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
// Adjust the component buffer.
|
||||
$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 object $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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user