Skip to content

Commit 7970c2a

Browse files
authored
[4.3] Convert email cloack plugin to services (#38466)
1 parent 2423bad commit 7970c2a

File tree

3 files changed

+75
-32
lines changed

3 files changed

+75
-32
lines changed

plugins/content/emailcloak/emailcloak.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
<authorUrl>www.joomla.org</authorUrl>
1010
<version>3.0.0</version>
1111
<description>PLG_CONTENT_EMAILCLOAK_XML_DESCRIPTION</description>
12+
<namespace path="src">Joomla\Plugin\Content\EmailCloak</namespace>
1213
<files>
13-
<filename plugin="emailcloak">emailcloak.php</filename>
14+
<folder plugin="emailcloak">services</folder>
15+
<folder>src</folder>
1416
</files>
1517
<languages>
1618
<language tag="en-GB">language/en-GB/plg_content_emailcloak.ini</language>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Plugin
5+
* @subpackage Content.emailcloak
6+
*
7+
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\PluginInterface;
14+
use Joomla\CMS\Factory;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\DI\Container;
17+
use Joomla\DI\ServiceProviderInterface;
18+
use Joomla\Event\DispatcherInterface;
19+
use Joomla\Plugin\Content\EmailCloak\Extension\EmailCloak;
20+
21+
return new class implements ServiceProviderInterface
22+
{
23+
/**
24+
* Registers the service provider with a DI container.
25+
*
26+
* @param Container $container The DI container.
27+
*
28+
* @return void
29+
*
30+
* @since __DEPLOY_VERSION__
31+
*/
32+
public function register(Container $container)
33+
{
34+
$container->set(
35+
PluginInterface::class,
36+
function (Container $container) {
37+
$dispatcher = $container->get(DispatcherInterface::class);
38+
$plugin = new EmailCloak(
39+
$dispatcher,
40+
(array) PluginHelper::getPlugin('content', 'emailcloak')
41+
);
42+
$plugin->setApplication(Factory::getApplication());
43+
44+
return $plugin;
45+
}
46+
);
47+
}
48+
};

plugins/content/emailcloak/emailcloak.php renamed to plugins/content/emailcloak/src/Extension/EmailCloak.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
*
77
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
88
* @license GNU General Public License version 2 or later; see LICENSE.txt
9-
10-
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
119
*/
1210

11+
namespace Joomla\Plugin\Content\EmailCloak\Extension;
12+
1313
use Joomla\CMS\HTML\HTMLHelper;
1414
use Joomla\CMS\Plugin\CMSPlugin;
1515
use Joomla\String\StringHelper;
@@ -23,15 +23,8 @@
2323
*
2424
* @since 1.5
2525
*/
26-
class PlgContentEmailcloak extends CMSPlugin
26+
final class EmailCloak extends CMSPlugin
2727
{
28-
/**
29-
* @var \Joomla\CMS\Application\SiteApplication
30-
*
31-
* @since 3.9.0
32-
*/
33-
protected $app;
34-
3528
/**
3629
* Plugin that cloaks all emails in content from spambots via Javascript.
3730
*
@@ -46,16 +39,16 @@ public function onContentPrepare($context, &$row, &$params, $page = 0)
4639
{
4740
// Don't run if in the API Application
4841
// Don't run this plugin when the content is being indexed
49-
if ($this->app->isClient('api') || $context === 'com_finder.indexer') {
42+
if ($this->getApplication()->isClient('api') || $context === 'com_finder.indexer') {
5043
return;
5144
}
5245

53-
// If the row is not an object or does not have a text property there is nothign to do
46+
// If the row is not an object or does not have a text property there is nothing to do
5447
if (!is_object($row) || !property_exists($row, 'text')) {
5548
return;
5649
}
5750

58-
$this->_cloak($row->text, $params);
51+
$this->cloak($row->text, $params);
5952
}
6053

6154
/**
@@ -66,7 +59,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0)
6659
*
6760
* @return string A regular expression that matches a link containing the parameters.
6861
*/
69-
protected function _getPattern($link, $text)
62+
private function getPattern($link, $text)
7063
{
7164
$pattern = '~(?:<a ([^>]*)href\s*=\s*"mailto:' . $link . '"([^>]*))>' . $text . '</a>~i';
7265

@@ -82,7 +75,7 @@ protected function _getPattern($link, $text)
8275
*
8376
* @return void
8477
*/
85-
protected function _cloak(&$text, &$params)
78+
private function cloak(&$text, &$params)
8679
{
8780
/*
8881
* Check for presence of {emailcloak=off} which is explicits disables this
@@ -125,7 +118,7 @@ protected function _cloak(&$text, &$params)
125118
* >[email protected]</a>. This happens when inserting an email in TinyMCE, cancelling its suggestion to add
126119
* the mailto: prefix...
127120
*/
128-
$pattern = $this->_getPattern($searchEmail, $searchEmail);
121+
$pattern = $this->getPattern($searchEmail, $searchEmail);
129122
$pattern = str_replace('"mailto:', '"([\x20-\x7f][^<>]+/)', $pattern);
130123

131124
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
@@ -146,7 +139,7 @@ protected function _cloak(&$text, &$params)
146139
* >anytext</a>. This happens when inserting an email in TinyMCE, cancelling its suggestion to add
147140
* the mailto: prefix...
148141
*/
149-
$pattern = $this->_getPattern($searchEmail, $searchText);
142+
$pattern = $this->getPattern($searchEmail, $searchText);
150143
$pattern = str_replace('"mailto:', '"([\x20-\x7f][^<>]+/)', $pattern);
151144

152145
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
@@ -166,7 +159,7 @@ protected function _cloak(&$text, &$params)
166159
* Search for derivatives of link code <a href="mailto:[email protected]"
167160
168161
*/
169-
$pattern = $this->_getPattern($searchEmail, $searchEmail);
162+
$pattern = $this->getPattern($searchEmail, $searchEmail);
170163

171164
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
172165
$mail = $regs[2][0];
@@ -185,7 +178,7 @@ protected function _cloak(&$text, &$params)
185178
* Search for derivatives of link code <a href="mailto:[email protected]"
186179
* ><anyspan >[email protected]</anyspan></a>
187180
*/
188-
$pattern = $this->_getPattern($searchEmail, $searchEmailSpan);
181+
$pattern = $this->getPattern($searchEmail, $searchEmailSpan);
189182

190183
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
191184
$mail = $regs[2][0];
@@ -204,7 +197,7 @@ protected function _cloak(&$text, &$params)
204197
* Search for derivatives of link code <a href="mailto:[email protected]">
205198
* <anyspan >anytext</anyspan></a>
206199
*/
207-
$pattern = $this->_getPattern($searchEmail, $searchTextSpan);
200+
$pattern = $this->getPattern($searchEmail, $searchTextSpan);
208201

209202
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
210203
$mail = $regs[2][0];
@@ -222,7 +215,7 @@ protected function _cloak(&$text, &$params)
222215
* Search for derivatives of link code <a href="mailto:[email protected]">
223216
* anytext</a>
224217
*/
225-
$pattern = $this->_getPattern($searchEmail, $searchText);
218+
$pattern = $this->getPattern($searchEmail, $searchText);
226219

227220
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
228221
$mail = $regs[2][0];
@@ -240,7 +233,7 @@ protected function _cloak(&$text, &$params)
240233
* Search for derivatives of link code <a href="mailto:[email protected]">
241234
* <img anything></a>
242235
*/
243-
$pattern = $this->_getPattern($searchEmail, $searchImage);
236+
$pattern = $this->getPattern($searchEmail, $searchImage);
244237

245238
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
246239
$mail = $regs[2][0];
@@ -258,7 +251,7 @@ protected function _cloak(&$text, &$params)
258251
* Search for derivatives of link code <a href="mailto:[email protected]">
259252
* <img anything>[email protected]</a>
260253
*/
261-
$pattern = $this->_getPattern($searchEmail, $searchImage . $searchEmail);
254+
$pattern = $this->getPattern($searchEmail, $searchImage . $searchEmail);
262255

263256
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
264257
$mail = $regs[2][0];
@@ -276,7 +269,7 @@ protected function _cloak(&$text, &$params)
276269
* Search for derivatives of link code <a href="mailto:[email protected]">
277270
* <img anything>any text</a>
278271
*/
279-
$pattern = $this->_getPattern($searchEmail, $searchImage . $searchText);
272+
$pattern = $this->getPattern($searchEmail, $searchImage . $searchText);
280273

281274
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
282275
$mail = $regs[2][0];
@@ -294,7 +287,7 @@ protected function _cloak(&$text, &$params)
294287
* Search for derivatives of link code <a href="mailto:[email protected]?
295288
* subject=Text">[email protected]</a>
296289
*/
297-
$pattern = $this->_getPattern($searchEmailLink, $searchEmail);
290+
$pattern = $this->getPattern($searchEmailLink, $searchEmail);
298291

299292
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
300293
$mail = $regs[2][0] . $regs[3][0];
@@ -316,7 +309,7 @@ protected function _cloak(&$text, &$params)
316309
* Search for derivatives of link code <a href="mailto:[email protected]?
317310
* subject=Text">anytext</a>
318311
*/
319-
$pattern = $this->_getPattern($searchEmailLink, $searchText);
312+
$pattern = $this->getPattern($searchEmailLink, $searchText);
320313

321314
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
322315
$mail = $regs[2][0] . $regs[3][0];
@@ -337,7 +330,7 @@ protected function _cloak(&$text, &$params)
337330
* Search for derivatives of link code <a href="mailto:[email protected]?subject= Text"
338331
* ><anyspan >[email protected]</anyspan></a>
339332
*/
340-
$pattern = $this->_getPattern($searchEmailLink, $searchEmailSpan);
333+
$pattern = $this->getPattern($searchEmailLink, $searchEmailSpan);
341334

342335
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
343336
$mail = $regs[2][0] . $regs[3][0];
@@ -356,7 +349,7 @@ protected function _cloak(&$text, &$params)
356349
* Search for derivatives of link code <a href="mailto:[email protected]?subject= Text">
357350
* <anyspan >anytext</anyspan></a>
358351
*/
359-
$pattern = $this->_getPattern($searchEmailLink, $searchTextSpan);
352+
$pattern = $this->getPattern($searchEmailLink, $searchTextSpan);
360353

361354
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
362355
$mail = $regs[2][0] . $regs[3][0];
@@ -374,7 +367,7 @@ protected function _cloak(&$text, &$params)
374367
* Search for derivatives of link code
375368
* <a href="mailto:[email protected]?subject=Text"><img anything></a>
376369
*/
377-
$pattern = $this->_getPattern($searchEmailLink, $searchImage);
370+
$pattern = $this->getPattern($searchEmailLink, $searchImage);
378371

379372
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
380373
$mail = $regs[2][0] . $regs[3][0];
@@ -396,7 +389,7 @@ protected function _cloak(&$text, &$params)
396389
* Search for derivatives of link code
397390
* <a href="mailto:[email protected]?subject=Text"><img anything>[email protected]</a>
398391
*/
399-
$pattern = $this->_getPattern($searchEmailLink, $searchImage . $searchEmail);
392+
$pattern = $this->getPattern($searchEmailLink, $searchImage . $searchEmail);
400393

401394
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
402395
$mail = $regs[2][0] . $regs[3][0];
@@ -418,7 +411,7 @@ protected function _cloak(&$text, &$params)
418411
* Search for derivatives of link code
419412
* <a href="mailto:[email protected]?subject=Text"><img anything>any text</a>
420413
*/
421-
$pattern = $this->_getPattern($searchEmailLink, $searchImage . $searchText);
414+
$pattern = $this->getPattern($searchEmailLink, $searchImage . $searchText);
422415

423416
while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
424417
$mail = $regs[2][0] . $regs[3][0];

0 commit comments

Comments
 (0)