- * @package Hofff_facebook-pixel
+ * @property string $fb_pixel_opt_out_active_text
+ * @property string $fb_pixel_opt_out_inactive_text
+ * @psalm-suppress PropertyNotSetInConstructor
*/
class FacebookPixelModule extends Module
{
@@ -24,5 +20,6 @@ class FacebookPixelModule extends Module
*
* @var string
*/
+ // phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
protected $strTemplate = 'mod_hofff_facebook_pixel_optout';
}
diff --git a/src/Contao/FacebookPixelTrait.php b/src/Contao/FacebookPixelTrait.php
index 72402ca..758dd75 100644
--- a/src/Contao/FacebookPixelTrait.php
+++ b/src/Contao/FacebookPixelTrait.php
@@ -8,42 +8,46 @@
use Contao\Database;
use Contao\Input;
+use function defined;
+
trait FacebookPixelTrait
{
+ protected string $fb_pixel_id = '';
+
/**
- * Return if there are no files
- *
- * @return string
+ * @psalm-suppress MixedPropertyFetch
+ * @SuppressWarnings(PHPMD.Superglobals)
*/
public function generate(): string
{
- if (TL_MODE === 'BE') {
+ if (defined('TL_MODE') && TL_MODE === 'BE') {
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### Facebook Pixel OptOut ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
- $objTemplate->link = $this->name;
- $objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
+ $objTemplate->link = $this->name ?? null;
return $objTemplate->parse();
}
- $this->fb_pixel_id = Database::getInstance()
- ->prepare("SELECT * FROM tl_page WHERE id=?")
+ $this->fb_pixel_id = (string) Database::getInstance()
+ ->prepare('SELECT fb_pixel_id FROM tl_page WHERE id=?')
->limit(1)
->execute($GLOBALS['objPage']->rootId)
->fb_pixel_id;
// Return if there is no page id
- if (!$this->fb_pixel_id) {
- return '' . $GLOBALS['TL_LANG']['MSC']['fbPixelNoIdIsSet'] . '
';
+ if (! $this->fb_pixel_id) {
+ /** @psalm-suppress MixedArrayAccess */
+ return '' . (string) $GLOBALS['TL_LANG']['MSC']['fbPixelNoIdIsSet'] . '
';
}
return parent::generate();
}
/**
- * Compile the content element
+ * @psalm-suppress MixedArrayAccess
+ * @SuppressWarnings(PHPMD.Superglobals)
*/
protected function compile(): void
{
diff --git a/src/ContaoManager/Plugin.php b/src/ContaoManager/Plugin.php
index 334f301..bb819f4 100644
--- a/src/ContaoManager/Plugin.php
+++ b/src/ContaoManager/Plugin.php
@@ -12,12 +12,17 @@
final class Plugin implements BundlePluginInterface
{
+ /**
+ * {@inheritDoc}
+ *
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(HofffContaoFacebookPixelBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
- ->setReplace(['hofff_facebook-pixel'])
+ ->setReplace(['hofff_facebook-pixel']),
];
}
}
diff --git a/src/DependencyInjection/HofffContaoFacebookPixelExtension.php b/src/DependencyInjection/HofffContaoFacebookPixelExtension.php
index 449360d..60b7895 100644
--- a/src/DependencyInjection/HofffContaoFacebookPixelExtension.php
+++ b/src/DependencyInjection/HofffContaoFacebookPixelExtension.php
@@ -11,12 +11,14 @@
final class HofffContaoFacebookPixelExtension extends Extension
{
- public function load(array $configs, ContainerBuilder $container)
+ /**
+ * {@inheritDoc}
+ *
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
+ public function load(array $configs, ContainerBuilder $container): void
{
- $loader = new XmlFileLoader(
- $container,
- new FileLocator(__DIR__ . '/../Resources/config')
- );
+ $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}
diff --git a/src/EventListener/HookSubscriber.php b/src/EventListener/HookSubscriber.php
index d03e82f..7ce0750 100644
--- a/src/EventListener/HookSubscriber.php
+++ b/src/EventListener/HookSubscriber.php
@@ -11,63 +11,21 @@
use Hofff\Contao\Consent\Bridge\ConsentToolManager;
use Hofff\Contao\Consent\Bridge\Exception\InvalidArgumentException as InvalidConsentIdException;
-/**
- * Class FacebookPixel
- *
- * Front end content element "hofff_facebook-pixel".
- *
- * @copyright Hofff.com 2017
- * @author Mathias Arzberger
- * @package Hofff_facebook-pixel
- */
+use function explode;
+
final class HookSubscriber
{
- /**
- * Database connection.
- *
- * @var Connection
- */
- private $connection;
-
- /**
- * Consent tool manager.
- *
- * @var ConsentToolManager
- */
- private $consentToolManager;
-
- /**
- * Consent Id parser.
- *
- * @var ConsentIdParser
- */
- private $consentIdParser;
-
- /**
- * HookSubscriber constructor.
- *
- * @param Connection $connection Database connection.
- * @param ConsentToolManager $consentToolManager Consent tool manager.
- * @param ConsentIdParser $consentIdParser Consent Id parser.
- */
public function __construct(
- Connection $connection,
- ConsentToolManager $consentToolManager,
- ConsentIdParser $consentIdParser
- ){
- $this->connection = $connection;
- $this->consentToolManager = $consentToolManager;
- $this->consentIdParser = $consentIdParser;
+ private readonly Connection $connection,
+ private readonly ConsentToolManager $consentToolManager,
+ private readonly ConsentIdParser $consentIdParser,
+ ) {
}
/**
* Add the facebook pixel when parsing a frontend template.
*
- * @param string $content
- *
- * @return string
- *
- * @throws \Doctrine\DBAL\DBALException
+ * @SuppressWarnings(PHPMD.Superglobals)
*/
public function onParseFrontendTemplate(string $content): string
{
@@ -87,12 +45,14 @@ public function onParseFrontendTemplate(string $content): string
}
// parse template file
- $objTemplate = new FrontendTemplate('hofff_facebook_pixel');
+ $objTemplate = new FrontendTemplate('hofff_facebook_pixel');
+ /** @psalm-suppress InvalidPropertyAssignmentValue */
$objTemplate->id = $pixelConfig['fb_pixel_id'];
$parsed = $objTemplate->parse();
$parsed = $this->applyConsentTool($parsed, $pixelConfig['fb_pixel_consentId']);
+ /** @psalm-suppress MixedArrayAssignment */
$GLOBALS['TL_HEAD'][] = $parsed;
}
@@ -102,11 +62,10 @@ public function onParseFrontendTemplate(string $content): string
/**
* function to add facebook pixel privacy link with an insert tag using switch to add other functions later.
*
- * @param string $tag
- *
- * @return bool|string
+ * @psalm-suppress MixedArrayAccess
+ * @SuppressWarnings(PHPMD.Superglobals)
*/
- public function onReplaceInsertTag(string $tag)
+ public function onReplaceInsertTag(string $tag): string|false
{
$parts = explode('::', $tag);
@@ -119,8 +78,8 @@ public function onReplaceInsertTag(string $tag)
[
'optOutActiveText' => $parts[2] ?: $GLOBALS['TL_LANG']['MSC']['fbPixelOptOutActiveText'],
'optOutInActiveText' => $parts[3] ?: $GLOBALS['TL_LANG']['MSC']['fbPixelOptOutInActiveText'],
- 'optOutStatus' => (bool) Input::cookie('FB_PIXEL_OPTOUT')
- ]
+ 'optOutStatus' => (bool) Input::cookie('FB_PIXEL_OPTOUT'),
+ ],
);
return $template->parse();
@@ -129,42 +88,48 @@ public function onReplaceInsertTag(string $tag)
/**
* Get the pixel config from the root page.
*
- * @return array
+ * @return array
*
- * @throws \Doctrine\DBAL\DBALException
+ * @psalm-suppress MixedReturnTypeCoercion
+ * @psalm-suppress InvalidFalsableReturnType
+ * @psalm-suppress FalsableReturnStatement
+ * @SuppressWarnings(PHPMD.Superglobals)
*/
private function getPixelConfig(): array
{
$query = 'SELECT fb_pixel_id, fb_pixel_status, fb_pixel_consentId FROM tl_page WHERE id=:pageId';
$statement = $this->connection->prepare($query);
- $statement->bindValue('pageId', $GLOBALS['objPage']->rootId);
- if (!$statement->execute() || $statement->rowCount() === 0) {
+ /** @psalm-suppress MixedPropertyFetch */
+ $result = $statement->executeQuery(['pageId' => $GLOBALS['objPage']->rootId]);
+ if ($result->rowCount() === 0) {
return [
- 'fb_pixel_id' => null,
- 'fb_pixel_status' => null,
- 'fb_pixel_consentId' => null
+ 'fb_pixel_id' => null,
+ 'fb_pixel_status' => null,
+ 'fb_pixel_consentId' => null,
];
}
- return $statement->fetch(\PDO::FETCH_ASSOC);
+ return $result->fetchAssociative();
}
- private function applyConsentTool(string $buffer, ?string $rawConsentId): string
+ private function applyConsentTool(string $buffer, string|null $rawConsentId): string
{
- if (null === $rawConsentId) {
+ if ($rawConsentId === null) {
return $buffer;
}
$consentTool = $this->consentToolManager->activeConsentTool();
- if (null === $consentTool) {
+ if ($consentTool === null) {
return $buffer;
}
try {
$consentId = $this->consentIdParser->parse($rawConsentId);
$buffer = $consentTool->renderRaw($buffer, $consentId);
- } catch (InvalidConsentIdException $exception) {}
+ } catch (InvalidConsentIdException) {
+ // Invalid consent id given, ignore it
+ }
return $buffer;
}
diff --git a/src/Resources/contao/config/config.php b/src/Resources/contao/config/config.php
index 90300eb..fadf081 100644
--- a/src/Resources/contao/config/config.php
+++ b/src/Resources/contao/config/config.php
@@ -2,8 +2,8 @@
declare(strict_types=1);
-use Hofff\Contao\FacebookPixel\EventListener\HookSubscriber;
use Hofff\Contao\FacebookPixel\Contao\FacebookPixelElement;
+use Hofff\Contao\FacebookPixel\EventListener\HookSubscriber;
/**
* Content elements
@@ -20,7 +20,7 @@
'miscellaneous' => [
'hofff_facebook_pixel_optout' => FacebookPixelElement::class,
],
- ]
+ ],
);
/*
diff --git a/src/Resources/contao/dca/tl_page.php b/src/Resources/contao/dca/tl_page.php
index 2136383..e1c777a 100644
--- a/src/Resources/contao/dca/tl_page.php
+++ b/src/Resources/contao/dca/tl_page.php
@@ -2,21 +2,26 @@
declare(strict_types=1);
+use Contao\CoreBundle\DataContainer\PaletteManipulator;
+use Contao\CoreBundle\DataContainer\PaletteNotFoundException;
use Hofff\Contao\Consent\Bridge\EventListener\Dca\ConsentIdOptions;
-$GLOBALS['TL_DCA']['tl_page']['palettes']['root'] = str_replace(
- '{publish_legend}',
- '{facebook_pixel_legend},fb_pixel_id,fb_pixel_status,fb_pixel_consentId;{publish_legend}',
- $GLOBALS['TL_DCA']['tl_page']['palettes']['root']
-);
+(static function (): void {
+ $manipulator = PaletteManipulator::create()
+ ->addLegend('facebook_pixel_legend', 'publish_legend', PaletteManipulator::POSITION_BEFORE)
+ ->addFields(
+ ['fb_pixel_id', 'fb_pixel_status', 'fb_pixel_consentId'],
+ 'facebook_pixel_legend',
+ PaletteManipulator::POSITION_APPEND,
+ );
-if (isset($GLOBALS['TL_DCA']['tl_page']['palettes']['rootfallback'])) {
- $GLOBALS['TL_DCA']['tl_page']['palettes']['rootfallback'] = str_replace(
- '{publish_legend}',
- '{facebook_pixel_legend},fb_pixel_id,fb_pixel_status,fb_pixel_consentId;{publish_legend}',
- $GLOBALS['TL_DCA']['tl_page']['palettes']['rootfallback']
- );
-}
+ foreach (['root', 'rootfallback'] as $palette) {
+ try {
+ $manipulator->applyToPalette($palette, 'tl_page');
+ } catch (PaletteNotFoundException) {
+ }
+ }
+})();
$GLOBALS['TL_DCA']['tl_page']['fields']['fb_pixel_id'] = [
'label' => &$GLOBALS['TL_LANG']['tl_page']['fb_pixel_id'],
diff --git a/src/Resources/contao/languages/de/default.php b/src/Resources/contao/languages/de/default.php
index cfddda3..760dcb9 100644
--- a/src/Resources/contao/languages/de/default.php
+++ b/src/Resources/contao/languages/de/default.php
@@ -1,23 +1,12 @@