More work on this, incomplete!
Validate Build / validate-build (push) Canceled after 0s

This commit is contained in:
Cooper Dalrymple
2026-10-06 17:00:20 -05:00
parent 5f702470cc
commit b8902b4afa
23 changed files with 2138 additions and 175 deletions
+65 -12
View File
@@ -21,28 +21,48 @@ abstract class Service {
use Singleton;
protected string $name;
protected string $title;
protected bool $contextualConsentOnly = false;
protected function __construct(string $name) {
protected function __construct(string $name, string $title = '') {
$this->name = $name;
$this->title = !empty($title) ? $title : $name;
Services::instance()->register($this);
}
public function get_name(): string
{
return $this->name;
}
public function get_title(): string
{
return $this->title;
}
abstract public function get_purposes(): array;
public function get_purpose(): string
{
$purposes = $this->get_purposes();
if (empty($purposes)) return 'analytics';
return array_values($purposes)[0];
}
public function get_config(): array
{
$config = [
'name' => $this->name,
'title' => $this->title,
'purposes' => $this->get_purposes(),
];
if ($this->contextualConsentOnly) $config['contextualConsentOnly'] = true;
return $config;
}
public function get_purposes(): array
public function is_enabled(): bool
{
return [
'marketing'
];
return Settings::instance()->is_service_enabled($this);
}
}
@@ -50,9 +70,9 @@ abstract class Service {
abstract class ScriptService extends Service {
protected string $handle;
protected function __construct(string $handle, string $name = '') {
protected function __construct(string $handle, string $name = '', string $title = '') {
$this->handle = $handle;
parent::__construct($name ?? $handle);
parent::__construct(!empty($name) ? $name : $handle, $title);
add_filter('script_loader_tag', [$this, 'script_loader_tag'], 10, 3);
}
@@ -77,9 +97,9 @@ abstract class BlockService extends Service {
protected string $blockName;
protected function __construct(string $blockName, string $name = '') {
protected function __construct(string $blockName, string $name = '', string $title = '') {
$this->blockName = $blockName;
parent::__construct($name ?? $blockName);
parent::__construct(!empty($name) ? $name : $blockName, $title);
add_filter("render_block_{$blockName}", [$this, 'render_block'], 10, 3);
}
@@ -91,9 +111,9 @@ 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);
protected function __construct(string $handle, string $name = '', string $consent_id = '', string $title = '') {
$this->consent_id = !empty($consent_id) ? $consent_id : (!empty($name) ? $name : $handle);
parent::__construct($handle, $name, $title);
}
#[Override]
@@ -118,3 +138,36 @@ abstract class GoogleTagManagerService extends ScriptService {
}
}
abstract class EmbedBlockService extends BlockService {
protected function __construct(string $name, string $title = '') {
parent::__construct('core/embed', $name, $title);
}
#[Override]
public function get_purposes(): array
{
return [
'marketing'
];
}
#[Override]
public function render_block(string $block_content, array $block, WP_Block $instance): string
{
if ($block['name'] !== $this->name) return $block_content;
$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();
}
}