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();
}
}
+14 -6
View File
@@ -18,14 +18,10 @@ 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 = [
return apply_filters('ogre_consent_config', [
'elementID' => Plugin::get_id(),
'storageMethod' => 'localStorage',
'storageName' => Plugin::get_id(),
@@ -34,9 +30,21 @@ final class Config {
'mustConsent' => true,
'acceptAll' => true,
'hideLearnMore' => true,
'disablePoweredBy' => true,
'services' => Services::instance()->get_config(),
]);
}
public static function get_purposes(): array
{
return [
'advertising' => Plugin::__('Advertising'),
'analytics' => Plugin::__('Analytics'),
'livechat' => Plugin::__('Livechat'),
'marketing' => Plugin::__('Marketing'),
'security' => Plugin::__('Security'),
'styling' => Plugin::__('Styling'),
];
return apply_filters('ogre_consent_config', $config);
}
}
+30 -9
View File
@@ -24,38 +24,59 @@ final class Script {
protected function __construct() {
add_action('wp_enqueue_scripts', [$this, 'wp_enqueue_scripts']);
add_filter('script_loader_tag', [$this, 'script_loader_tag'], 10, 3);
add_filter('script_loader_tag', [$this, 'script_loader_tag'], 99, 3);
}
public function wp_enqueue_scripts(): void
{
// Check user role
if (is_user_logged_in() && empty(array_intersect((array) wp_get_current_user()->roles, Settings::instance()->get_enabled_user_roles()))) return;
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()
);
wp_enqueue_style(
Plugin::get_id(),
Plugin::get_url('assets/style.css'),
deps: [
self::HANDLE
],
ver: Plugin::get_version()
);
}
private function get_config_tag(): string
{
return sprintf(
'<script id="%s-config" type="application/javascript">window.%s = %s;</script>',
esc_attr(self::HANDLE),
esc_js(self::CONFIG),
wp_json_encode(Config::instance()->get_config())
);
}
public function script_loader_tag(string $tag, string $handle, string $src): string
{
if ($tag !== self::HANDLE) return $tag;
if ($handle !== self::HANDLE) return $tag;
$tags = new WP_HTML_Tag_Processor($tag);
if ($tags->next_tag('script')) {
$tags->set_attribute('data-config', self::CONFIG);
if ((bool) apply_filters('ogre_consent_async', true)) {
$tags->set_attribute('async', true);
}
}
return $tags->get_updated_html();
$tag = $tags->get_updated_html();
$tag = $this->get_config_tag() . $tag;
return $tag;
}
}
+20 -7
View File
@@ -10,9 +10,7 @@
namespace Ogre\Consent;
use Ogre\Consent as Plugin;
use Ogre\Singleton;
use WP_HTML_Tag_Processor;
defined('ABSPATH') || exit;
@@ -20,19 +18,34 @@ final class Services {
use Singleton;
protected array $services = [];
protected function __construct() {
}
public function register(Service $service): void
{
$this->services[] = $service;
}
public function get_services(): array
{
return $this->services;
}
public function get_services_by_purpose(string $purpose): array
{
return array_filter(
$this->services,
fn (Service $service): bool => $service->get_purpose() == $purpose
);
}
public function get_config(): array
{
return array_map(fn (Service $service): array => $service->get_config(), $this->services);
return array_map(
fn (Service $service): array => $service->get_config(),
array_values(array_filter(
$this->services,
fn (Service $service): bool => $service->is_enabled()
))
);
}
}
+58 -8
View File
@@ -11,8 +11,8 @@
namespace Ogre\Consent;
use Override;
use Ogre\Settings as AbstractSettings;
use Ogre\Consent as Plugin;
use Ogre\Settings as AbstractSettings;
defined('ABSPATH') || exit;
@@ -27,18 +27,68 @@ final class Settings extends AbstractSettings {
$this->add_section(
Plugin::__('General'),
Plugin::__('General settings for this plugin.')
sprintf(
Plugin::__('General settings for %s.'),
Plugin::get_title()
)
);
$this->add_field(
'text',
Plugin::__('Text'),
$this->add_checkbox_field(
'user_roles',
Plugin::__('User Roles'),
array_map(
fn (array $details): string => translate_user_role($details['name']),
array_reverse(get_editable_roles())
),
[
'placeholder' => Plugin::__('Insert some text here...'),
'description' => Plugin::__('An example field.'),
'description' => Plugin::__('The consent modal is disabled by default for all users. You can enable it for each user role using the settings below.'),
]
);
$this->add_section(
Plugin::__('Services'),
Plugin::__('You can control which services are included in user consent (opt-in) using the options below.'),
'services'
);
$purposes = Config::get_purposes();
foreach ($purposes as $name => $label) {
$services = [];
foreach (Services::instance()->get_services_by_purpose($name) as $service) {
$services[$service->get_name()] = $service->get_title();
}
if (empty($services)) continue;
$this->add_checkbox_field(
sprintf('services-%s', $name),
$label,
$services,
[
'name' => 'services',
'hide_value' => true,
],
'services'
);
}
}
public function is_service_enabled(Service|string $service): bool
{
if (is_a($service, Service::class)) {
$service = $service->get_name();
} else if (!is_string($service) || empty($service)) {
return false;
}
return $this->is_checked('services', $service);
}
public function get_enabled_user_roles(): array
{
if (!function_exists('get_editable_roles')) include_once(ABSPATH . 'wp-admin/includes/user.php');
return array_filter(
array_keys(get_editable_roles()),
fn (string $role): bool => $this->is_checked('user_roles', $role)
);
}
}
+203
View File
@@ -0,0 +1,203 @@
<?php
/**
* @package ogre-captcha
* @author cleverogre
* @copyright 2026 CleverOgre
* @license GLP-3.0-or-later
* @version 1.2.8
* @since 1.1.0
*/
namespace Ogre\Consent\Integrations;
use Ogre\Singleton;
use Ogre\Consent as Plugin;
use Ogre\Captcha\Settings;
use GFForms;
use GFAddOn;
use GFFormDisplay;
use GFCommon;
use GP_Nested_Forms;
defined('ABSPATH') || exit;
GFForms::include_addon_framework();
final class GravityForms extends GFAddOn {
use Singleton;
protected $_min_gravityforms_version = '2.6.3.2';
public static function get_instance(): GFAddOn {
return self::instance();
}
function __construct() {
$this->_version = Plugin::get_version();
$this->_slug = Plugin::get_id();
$this->_path = sprintf(
'%1$s/%1$s.php',
Plugin::get_id()
);
$this->_full_path = Plugin::get_file();
$this->_title = sprintf(
Plugin::__('%s Add-On'),
Plugin::get_title()
);
$this->_short_title = Plugin::get_title();
parent::__construct();
}
public function init() {
add_filter('gform_submit_button', [$this, 'submit_button'], 10, 2);
add_filter('gform_validation', [$this, 'validation'], 10, 2);
parent::init();
}
public function get_menu_icon() {
return 'gform-icon--lock';
}
public function plugin_settings_fields() {
return [
[
'title' => esc_html(sprintf(
Plugin::__('%s Settings'),
Plugin::get_title()
)),
'description' => sprintf(
Plugin::__('Looking to configure the rest of Ogre Consent? You can find the settings on <a href="%s">this page</a>.'),
esc_url(Settings::get_url())
),
'fields' => [
[
'label' => sprintf(
Plugin::esc_html__('Enable %s'),
Plugin::get_title()
),
'type' => 'checkbox',
'name' => 'enabled',
'choices' => [
[
'label' => Plugin::esc_html__('Enable consent on all forms by default.'),
'name' => 'enabled',
]
]
]
]
]
];
}
public function form_settings_fields($form) {
return [
[
'title' => esc_html(sprintf(
Plugin::__('%s Settings'),
Plugin::get_title()
)),
'fields' => [
[
'type' => 'checkbox',
'name' => 'disabled',
'choices' => [
[
'label' => Plugin::esc_html__('Disable consent for this form.'),
'name' => 'disabled',
]
]
]
]
]
];
}
public function is_enabled(array|null $form = null): bool {
// Check to make sure that we're enabled on the plugin level first
$plugin_enabled = $this->get_plugin_setting('enabled') === '1';
if (is_null($form) || !$plugin_enabled) return $plugin_enabled;
$form_id = !is_null($form) ? absint(rgar($form, 'id')) : 0;
// If Gravity Perks Nested Forms is active and the current form is nested, disable entirely (it's not compatible at the moment)
if (class_exists('GP_Nested_Forms')) {
$gp_nested_forms = GP_Nested_Forms::get_instance();
if ($gp_nested_forms->is_nested_form_submission() && $gp_nested_forms->get_parent_form_id() !== $form_id) return false;
}
// Now check to see if the opt-out is enabled within the form settings
$form_settings = $this->get_form_settings($form);
$form_disabled = (bool) rgar($form_settings, 'disabled');
return !$form_disabled;
}
public function set_enabled(bool $value): bool {
$settings = (array) $this->get_plugin_settings();
$settings['enabled'] = !!$value ? '1' : '0';
$this->update_plugin_settings($settings);
return true;
}
public function submit_button(string $button_input, array $form): string {
if (!Settings::is_connected() || !$this->is_enabled($form)) return $button_input;
if ((is_admin() && !defined('DOING_AJAX')) || GFCommon::is_form_editor()) return $button_input;
$id = sprintf(
'gform_cap_%d',
absint(rgar($form, 'id'))
);
ob_start();
Widget::render([
'id' => $id,
]);
$button_input = ob_get_clean() . $button_input;
if (Settings::is_widget_floating()) {
$button_input = Widget::set_button_floating(
$button_input,
[
'id' => $id,
]
);
}
return $button_input;
}
public function validation(array $validation_result, string $context): array {
if ($context != 'form-submit') return $validation_result;
$form = $validation_result['form'];
// Make sure that we are actually processing the end of the form
if (!GFFormDisplay::is_last_page($form) || rgpost('action') == 'heartbeat') return $validation_result;
// Make sure that we are connected and enabled
if (!Settings::is_connected() || !$this->is_enabled($form)) return $validation_result;
$errors = API::process(Widget::get_token());
if ($errors->has_errors()) {
$validation_result['is_valid'] = false;
}
// TODO: Mark as spam (optional)
return $validation_result;
}
public function plugin_settings_link( $links, $file ) {
// Remove plugin action link
return $links;
}
public function get_url(): string {
return add_query_arg(
[
'page' => 'gf_settings',
'subview' => $this->get_slug()
],
admin_url('admin.php')
);
}
}
+35
View File
@@ -0,0 +1,35 @@
<?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 as Plugin;
use Ogre\Consent\Service;
use Override;
defined('ABSPATH') || exit;
final class GoogleAdsense extends Service {
protected function __construct() {
parent::__construct('google-adsense', Plugin::__('Google Adsense'));
}
#[Override]
public function get_purposes(): array
{
return [
'advertising',
];
}
}
GoogleAdsense::instance();
+10 -1
View File
@@ -10,6 +10,7 @@
namespace Ogre\Consent\Services;
use Ogre\Consent as Plugin;
use Ogre\Consent\GoogleTagManagerService;
use Override;
@@ -18,7 +19,15 @@ defined('ABSPATH') || exit;
final class GoogleAnalytics extends GoogleTagManagerService {
protected function __construct() {
parent::__construct('google-analytics', consent_id: 'analytics_storage');
parent::__construct('google-analytics', consent_id: 'analytics_storage', title: Plugin::__('Google Analytics'));
}
#[Override]
public function get_purposes(): array
{
return [
'analytics',
];
}
#[Override]
+11 -3
View File
@@ -10,6 +10,7 @@
namespace Ogre\Consent\Services;
use Ogre\Consent as Plugin;
use Ogre\Consent\ScriptService;
use Override;
@@ -18,7 +19,15 @@ defined('ABSPATH') || exit;
final class GoogleTagManager extends ScriptService {
protected function __construct() {
parent::__construct('google-tag-manager');
parent::__construct('google-tag-manager', title: Plugin::__('Google Tag Manager'));
}
#[Override]
public function get_purposes(): array
{
return [
'advertising',
];
}
#[Override]
@@ -27,7 +36,6 @@ final class GoogleTagManager extends ScriptService {
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);",
]
@@ -36,4 +44,4 @@ final class GoogleTagManager extends ScriptService {
}
GoogleAnalytics::instance();
GoogleTagManager::instance();
+26
View File
@@ -0,0 +1,26 @@
<?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 as Plugin;
use Ogre\Consent\EmbedBlockService;
defined('ABSPATH') || exit;
final class Twitter extends EmbedBlockService {
protected function __construct() {
parent::__construct('twitter', Plugin::__('Twitter / X'));
}
}
Twitter::instance();
+4 -23
View File
@@ -10,34 +10,15 @@
namespace Ogre\Consent\Services;
use Ogre\Consent\BlockService;
use WP_Block;
use Override;
use WP_HTML_Tag_Processor;
use Ogre\Consent as Plugin;
use Ogre\Consent\EmbedBlockService;
defined('ABSPATH') || exit;
final class YouTube extends BlockService {
final class YouTube extends EmbedBlockService {
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();
parent::__construct('youtube', Plugin::__('YouTube'));
}
}