64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?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();
|