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)
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user