Skip to content

Commit

Permalink
Add a new addon - SafeLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Dec 10, 2024
1 parent 9a691b0 commit d56951e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Sources/Optimus/Addons/SafeLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

/**
* @package SafeLinks (Optimus)
* @link https://custom.simplemachines.org/mods/index.php?mod=2659
* @author Bugo https://dragomano.ru/mods/optimus
* @copyright 2010-2024 Bugo
* @license https://opensource.org/licenses/artistic-license-2.0 Artistic-2.0
*
* @category addon
* @version 09.12.24
*/

namespace Bugo\Optimus\Addons;

use Bugo\Compat\IntegrationHook;
use Bugo\Optimus\Events\AddonEvent;

if (! defined('SMF'))
die('No direct access...');

final class SafeLinks extends AbstractAddon
{
public const PACKAGE_ID = 'Optimus:SafeLinks';

public static array $events = [
self::HOOK_EVENT,
];

public function __invoke(AddonEvent $event): void
{
if ($event->eventName() !== self::HOOK_EVENT)
return;

IntegrationHook::add(
'integrate_bbc_codes', self::class . '::changeAttributesForLinks#', false, __FILE__
);
}

public function changeAttributesForLinks(array &$codes): void
{
foreach ($codes as &$code) {
if ($code['tag'] !== 'url') continue;

if ($code['type'] === 'unparsed_content') {
$code['content'] = $this->replaceRel($code['content']);
}

if ($code['type'] === 'unparsed_equals') {
$code['before'] = $this->replaceRel($code['before']);
}
}

unset($code);
}

private function replaceRel(string $link): string
{
return str_replace('rel="noopener"', 'rel="noopener noreferrer nofollow"', $link);
}
}

0 comments on commit d56951e

Please sign in to comment.