Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
class: phpbb\viglink\event\listener
arguments:
- '@config'
- '@language'
- '@template'
tags:
- { name: event.listener }
Expand Down
54 changes: 48 additions & 6 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@ class listener implements EventSubscriberInterface
/** @var \phpbb\config\config $config Config object */
protected $config;

protected $language;

/** @var \phpbb\template\template $template Template object */
protected $template;

/** @var string|null Cached VigLink API key */
protected $viglink_key;

/**
* Constructor
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\language\language $language Language object
* @param \phpbb\template\template $template Template object
*/
public function __construct(\phpbb\config\config $config, \phpbb\template\template $template)
public function __construct(\phpbb\config\config $config, \phpbb\language\language $language, \phpbb\template\template $template)
{
$this->config = $config;
$this->language = $language;
$this->template = $template;
}

Expand All @@ -42,6 +49,7 @@ public static function getSubscribedEvents()
{
return array(
'core.viewtopic_post_row_after' => 'display_viglink',
'phpbb.consentmanager.collect_registrations' => 'register_viglink',
);
}

Expand All @@ -52,6 +60,44 @@ public static function getSubscribedEvents()
*/
public function display_viglink()
{
$viglink_key = $this->get_viglink_key();

$this->template->assign_vars(array(
'VIGLINK_ENABLED' => $this->config['viglink_enabled'] && $viglink_key,
'VIGLINK_API_KEY' => $viglink_key,
'VIGLINK_SUB_ID' => md5(urlencode($this->config['viglink_api_siteid']) . $this->config['questionnaire_unique_id']),
));
}

/**
* Register Viglink with Consent Manager when available.
*
* @param \phpbb\event\data|array $event The event object or event data
* @return void
*/
public function register_viglink($event)
{
if (!$this->config['viglink_enabled'] || empty($this->get_viglink_key()))
{
return;
}

$this->language->add_lang('viglink_common', 'phpbb/viglink');

$event['consent_manager']->register('phpbb.viglink', [
'label' => $this->language->lang('VIGLINK'),
'category' => 'marketing',
'description' => $this->language->lang('VIGLINK_DESCRIPTION'),
]);
}

protected function get_viglink_key()
{
if ($this->viglink_key !== null)
{
return $this->viglink_key;
}

$viglink_key = '';

if ($this->config['allow_viglink_phpbb'] && $this->config['phpbb_viglink_api_key'])
Expand All @@ -60,10 +106,6 @@ public function display_viglink()
$viglink_key = $this->config['phpbb_viglink_api_key'];
}

$this->template->assign_vars(array(
'VIGLINK_ENABLED' => $this->config['viglink_enabled'] && $viglink_key,
'VIGLINK_API_KEY' => $viglink_key,
'VIGLINK_SUB_ID' => md5(urlencode($this->config['viglink_api_siteid']) . $this->config['questionnaire_unique_id']),
));
return $this->viglink_key = $viglink_key;
}
}
43 changes: 43 additions & 0 deletions language/en/viglink_common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
*
* VigLink extension for the phpBB Forum Software package.
*
* @copyright (c) 2026 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

/**
* DO NOT CHANGE
*/
if (!defined('IN_PHPBB'))
{
exit;
}

if (empty($lang) || !is_array($lang))
{
$lang = array();
}

// DEVELOPERS PLEASE NOTE
//
// All language files should use UTF-8 as their encoding and the files must not contain a BOM.
//
// Placeholders can now contain order information, e.g. instead of
// 'Page %s of %s' you can (and should) write 'Page %1$s of %2$s', this allows
// translators to re-order the output of data while ensuring it remains correct
//
// You do not need this where single placeholders are used, e.g. 'Message %d' is fine
// equally where a string contains only two placeholders which are used to wrap text
// in a url you again do not need to specify an order e.g., 'Click %sHERE%s' is fine
//
// Some characters you may want to copy&paste:
// ’ » “ ” …
//

$lang = array_merge($lang, array(
'VIGLINK' => 'VigLink',
'VIGLINK_DESCRIPTION' => 'Uses third‑party cookies to personalise content and enable site monetisation.',
));
2 changes: 1 addition & 1 deletion styles/all/template/event/overall_footer_after.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if VIGLINK_ENABLED %}
<script>
<script{% if S_CONSENTMANAGER_MARKETING_ENABLED %} type="text/plain" data-consent-category="marketing"{% endif %}>
var vglnk = {
key: '{{ VIGLINK_API_KEY|e("js") }}',
sub_id: '{{ VIGLINK_SUB_ID|e("js") }}'
Expand Down
59 changes: 59 additions & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class listener_test extends \phpbb_test_case
/** @var \phpbb\config\config */
protected $config;

/** @var \phpbb\language\language */
protected $language;

/** @var \phpbb\viglink\event\listener */
protected $listener;

Expand All @@ -28,6 +31,8 @@ protected function setUp(): void
{
parent::setUp();

global $phpbb_root_path, $phpEx;

// Load/Mock classes required by the event listener class
$this->config = new \phpbb\config\config(array(
'viglink_enabled' => 1,
Expand All @@ -36,6 +41,9 @@ protected function setUp(): void
));
$this->template = $this->getMockBuilder('\phpbb\template\template')
->getMock();

$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
$this->language = new \phpbb\language\language($lang_loader);
}

/**
Expand All @@ -45,6 +53,7 @@ protected function set_listener()
{
$this->listener = new \phpbb\viglink\event\listener(
$this->config,
$this->language,
$this->template
);
}
Expand All @@ -65,6 +74,7 @@ public function test_getSubscribedEvents()
{
self::assertEquals(array(
'core.viewtopic_post_row_after',
'phpbb.consentmanager.collect_registrations',
), array_keys(\phpbb\viglink\event\listener::getSubscribedEvents()));
}

Expand Down Expand Up @@ -128,4 +138,53 @@ public function test_display_viglink($allow_viglink_phpbb, $phpbb_api_key, $expe
$dispatcher->addListener('core.viewtopic_post_row_after', array($this->listener, 'display_viglink'));
$dispatcher->trigger_event('core.viewtopic_post_row_after');
}

public function register_marketing_data()
{
return [
'enabled' => [true, 1],
'disabled' => [false, 0],
];
}

/**
* @dataProvider register_marketing_data
*/
public function test_register_analytics($enabled, $expected_calls)
{
$this->config['viglink_enabled'] = $this->config['allow_viglink_phpbb'] = $this->config['phpbb_viglink_api_key'] = $enabled;
$this->set_listener();

$consent_manager = new consent_manager_double();

$this->listener->register_viglink([
'consent_manager' => $consent_manager,
]);

self::assertCount($expected_calls, $consent_manager->registrations);

if ($expected_calls)
{
self::assertSame('phpbb.viglink', $consent_manager->registrations[0]['id']);
self::assertSame([
'label' => $this->language->lang('VIGLINK'),
'category' => 'marketing',
'description' => $this->language->lang('VIGLINK_DESCRIPTION'),
], $consent_manager->registrations[0]['definition']);
}
}
}

class consent_manager_double
{
/** @var array */
public $registrations = [];

public function register($id, array $definition)
{
$this->registrations[] = [
'id' => $id,
'definition' => $definition,
];
}
}