This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent;
|
||||
|
||||
use Override;
|
||||
use Ogre\Singleton;
|
||||
use WP_Block;
|
||||
use WP_HTML_Tag_Processor;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
abstract class Service {
|
||||
use Singleton;
|
||||
|
||||
protected string $name;
|
||||
protected bool $contextualConsentOnly = false;
|
||||
|
||||
protected function __construct(string $name) {
|
||||
$this->name = $name;
|
||||
Services::instance()->register($this);
|
||||
}
|
||||
|
||||
public function get_config(): array
|
||||
{
|
||||
$config = [
|
||||
'name' => $this->name,
|
||||
'purposes' => $this->get_purposes(),
|
||||
];
|
||||
if ($this->contextualConsentOnly) $config['contextualConsentOnly'] = true;
|
||||
return $config;
|
||||
}
|
||||
|
||||
public function get_purposes(): array
|
||||
{
|
||||
return [
|
||||
'marketing'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract class ScriptService extends Service {
|
||||
protected string $handle;
|
||||
|
||||
protected function __construct(string $handle, string $name = '') {
|
||||
$this->handle = $handle;
|
||||
parent::__construct($name ?? $handle);
|
||||
add_filter('script_loader_tag', [$this, 'script_loader_tag'], 10, 3);
|
||||
}
|
||||
|
||||
public function script_loader_tag(string $tag, string $handle, string $src): string
|
||||
{
|
||||
if ($handle !== $this->handle) return $tag;
|
||||
$tags = new WP_HTML_Tag_Processor($tag);
|
||||
if ($tags->next_tag('script')) {
|
||||
$tags->set_attribute('type', 'text/plain');
|
||||
$tags->set_attribute('data-name', $this->name);
|
||||
if (!str_starts_with(($src = $tags->get_attribute('src')), site_url())) {
|
||||
$tags->remove_attribute('src');
|
||||
$tags->set_attribute('data-src', $src);
|
||||
}
|
||||
}
|
||||
return $tag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract class BlockService extends Service {
|
||||
|
||||
protected string $blockName;
|
||||
|
||||
protected function __construct(string $blockName, string $name = '') {
|
||||
$this->blockName = $blockName;
|
||||
parent::__construct($name ?? $blockName);
|
||||
add_filter("render_block_{$blockName}", [$this, 'render_block'], 10, 3);
|
||||
}
|
||||
|
||||
abstract public function render_block(string $block_content, array $block, WP_Block $instance): string;
|
||||
|
||||
}
|
||||
|
||||
abstract class GoogleTagManagerService extends ScriptService {
|
||||
|
||||
protected string $consent_id;
|
||||
|
||||
protected function __construct(string $handle, string $name = '', string $consent_id = '') {
|
||||
$this->consent_id = $consent_id ?? ($name ?? $handle);
|
||||
parent::__construct($handle, $name);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get_config(): array
|
||||
{
|
||||
return array_merge(
|
||||
parent::get_config(),
|
||||
[
|
||||
'cookies' => [
|
||||
'/^_ga(_.*)?/'
|
||||
],
|
||||
'onAccept' => sprintf(
|
||||
"gtag('consent', 'update', {'%s': 'granted'})",
|
||||
esc_js($this->consent_id)
|
||||
),
|
||||
'onDecline' => sprintf(
|
||||
"gtag('consent', 'update', {'%s': 'denied'})",
|
||||
esc_js($this->consent_id)
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent;
|
||||
|
||||
use Ogre\Consent as Plugin;
|
||||
use Ogre\Singleton;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Config {
|
||||
use Singleton;
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
}
|
||||
|
||||
public function get_config(): array
|
||||
{
|
||||
// See https://klaro.org/docs/integration/annotated-configuration
|
||||
$config = [
|
||||
'elementID' => Plugin::get_id(),
|
||||
'storageMethod' => 'localStorage',
|
||||
'storageName' => Plugin::get_id(),
|
||||
'cookieDomain' => parse_url(site_url(), PHP_URL_HOST),
|
||||
'default' => true,
|
||||
'mustConsent' => true,
|
||||
'acceptAll' => true,
|
||||
'hideLearnMore' => true,
|
||||
'services' => Services::instance()->get_config(),
|
||||
];
|
||||
return apply_filters('ogre_consent_config', $config);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Config::instance();
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent;
|
||||
|
||||
use Ogre\Consent as Plugin;
|
||||
use Ogre\Singleton;
|
||||
use WP_HTML_Tag_Processor;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Script {
|
||||
use Singleton;
|
||||
|
||||
private const HANDLE = 'klaro';
|
||||
private const CONFIG = 'klaroConfig';
|
||||
|
||||
protected function __construct() {
|
||||
add_action('wp_enqueue_scripts', [$this, 'wp_enqueue_scripts']);
|
||||
add_filter('script_loader_tag', [$this, 'script_loader_tag'], 10, 3);
|
||||
}
|
||||
|
||||
public function wp_enqueue_scripts(): void
|
||||
{
|
||||
wp_enqueue_script(
|
||||
self::HANDLE,
|
||||
Plugin::get_url('assets/klaro.js'),
|
||||
ver: Plugin::get_version()
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
self::HANDLE,
|
||||
self::CONFIG,
|
||||
Config::instance()->get_config()
|
||||
);
|
||||
|
||||
wp_enqueue_style(
|
||||
self::HANDLE,
|
||||
Plugin::get_url('assets/klaro.min.css'),
|
||||
ver: Plugin::get_version()
|
||||
);
|
||||
}
|
||||
|
||||
public function script_loader_tag(string $tag, string $handle, string $src): string
|
||||
{
|
||||
if ($tag !== self::HANDLE) return $tag;
|
||||
$tags = new WP_HTML_Tag_Processor($tag);
|
||||
if ($tags->next_tag('script')) {
|
||||
$tags->set_attribute('data-config', self::CONFIG);
|
||||
}
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Script::instance();
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent;
|
||||
|
||||
use Ogre\Consent as Plugin;
|
||||
use Ogre\Singleton;
|
||||
use WP_HTML_Tag_Processor;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Services {
|
||||
use Singleton;
|
||||
|
||||
protected array $services = [];
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
}
|
||||
|
||||
public function register(Service $service): void
|
||||
{
|
||||
$this->services[] = $service;
|
||||
}
|
||||
|
||||
public function get_config(): array
|
||||
{
|
||||
return array_map(fn (Service $service): array => $service->get_config(), $this->services);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Services::instance();
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* @package plugin
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
@@ -8,11 +8,11 @@
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Example;
|
||||
namespace Ogre\Consent;
|
||||
|
||||
use Override;
|
||||
use Ogre\Settings as AbstractSettings;
|
||||
use Ogre\Example as Plugin;
|
||||
use Ogre\Consent as Plugin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent\Services;
|
||||
|
||||
use Ogre\Consent\GoogleTagManagerService;
|
||||
use Override;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class GoogleAnalytics extends GoogleTagManagerService {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct('google-analytics', consent_id: 'analytics_storage');
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get_config(): array
|
||||
{
|
||||
return array_merge(
|
||||
parent::get_config(),
|
||||
[
|
||||
'cookies' => [
|
||||
'/^_ga(_.*)?/'
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
GoogleAnalytics::instance();
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent\Services;
|
||||
|
||||
use Ogre\Consent\ScriptService;
|
||||
use Override;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class GoogleTagManager extends ScriptService {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct('google-tag-manager');
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get_config(): array
|
||||
{
|
||||
return array_merge(
|
||||
parent::get_config(),
|
||||
[
|
||||
'required' => true,
|
||||
'onAccept' => "for (let k of Object.keys(opts.consents)) { if (opts.consents[k]) { dataLayer.push({'event': 'ogre-consent-'+k+'-accepted'}) } }",
|
||||
'onInit' => "window.dataLayer = window.dataLayer || []; window.gtag = function() { dataLayer.push(arguments) }; gtag('consent', 'default', {'ad_storage': 'denied', 'analytics_storage': 'denied', 'ad_user_data': 'denied', 'ad_personalization': 'denied'}); gtag('set', 'ads_data_redaction', true);",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
GoogleAnalytics::instance();
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-consent
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 0.0.0
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent\Services;
|
||||
|
||||
use Ogre\Consent\BlockService;
|
||||
use WP_Block;
|
||||
use Override;
|
||||
use WP_HTML_Tag_Processor;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class YouTube extends BlockService {
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct('core/iframe', 'youtube');
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function render_block(string $block_content, array $block, WP_Block $instance): string
|
||||
{
|
||||
// TODO: Check block iframe type
|
||||
|
||||
$tags = new WP_HTML_Tag_Processor($block_content);
|
||||
|
||||
if ($tags->next_tag('iframe')) {
|
||||
$tags->set_attribute('data-name', $this->name);
|
||||
$src = $tags->get_attribute('src');
|
||||
$tags->remove_attribute('src');
|
||||
$tags->set_attribute('data-src', $src);
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
YouTube::instance();
|
||||
Reference in New Issue
Block a user