GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: render Google Consent Mode default synchronously in <head> · axeptio/wordpress-plugin@c24c711

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • includes/classes/frontend
    • class-axeptio-sdk.php
  • templates/frontend
    • google-consent-mode.php
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎includes/classes/frontend/class-axeptio-sdk.php‎

Copy file name to clipboardExpand all lines: includes/classes/frontend/class-axeptio-sdk.php
+44-13Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class Axeptio_Sdk extends Module {
2828

2929
const OPTION_JSON_COOKIE_NAME = 'axeptio_cookies';
3030

31+
/**
32+
* Memoized SDK settings for the current request (null until resolved).
33+
*
34+
* @var array|false|null
35+
*/
36+
private $sdk_settings = null;
37+
3138
/**
3239
* Module can run within the current context.
3340
*
@@ -44,6 +51,30 @@ public function can_register() {
4451
*/
4552
public function register() {
4653
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
54+
55+
// Negative priority so the Consent Mode default fires before any other wp_head tag.
56+
add_action( 'wp_head', array( $this, 'render_google_consent_mode' ), -1 );
57+
}
58+
59+
/**
60+
* Render the Google Consent Mode default snippet in <head>.
61+
*
62+
* @return void
63+
*/
64+
public function render_google_consent_mode() {
65+
$settings = $this->get_sdk_settings();
66+
67+
if ( ! $settings ) {
68+
return;
69+
}
70+
71+
\Axeptio\Plugin\get_template_part(
72+
'frontend/google-consent-mode',
73+
array(
74+
'active_google_consent_mode' => (bool) $settings['enableGoogleConsentMode'],
75+
'google_consent_mode_params' => $settings['googleConsentMode']['default'],
76+
)
77+
);
4778
}
4879

4980
/**
@@ -121,19 +152,6 @@ function ( $plugin_configuration ) use ( $cookies_version ) {
121152
$sdk_script = \Axeptio\Plugin\get_template_part( 'frontend/sdk', array(), false );
122153
preg_match( '/<script[^>]*>(.*?)<\/script>/is', $sdk_script, $matches );
123154
wp_add_inline_script( 'axeptio/sdk-script', $matches[1] ?? '' );
124-
125-
add_action(
126-
'wp_head',
127-
function () use ( $settings ) {
128-
\Axeptio\Plugin\get_template_part(
129-
'frontend/google-consent-mode',
130-
array(
131-
'active_google_consent_mode' => (bool) $settings['enableGoogleConsentMode'],
132-
'google_consent_mode_params' => $settings['googleConsentMode'],
133-
)
134-
);
135-
}
136-
);
137155
}
138156

139157
/**
@@ -165,6 +183,19 @@ function ( $value ) {
165183
* @return array|false Settings of the SDK.
166184
*/
167185
private function get_sdk_settings() {
186+
if ( null === $this->sdk_settings ) {
187+
$this->sdk_settings = $this->resolve_sdk_settings();
188+
}
189+
190+
return $this->sdk_settings;
191+
}
192+
193+
/**
194+
* Resolve the SDK settings from the stored options.
195+
*
196+
* @return array|false Settings of the SDK.
197+
*/
198+
private function resolve_sdk_settings() {
168199
$sdk_active = Sdk::is_active();
169200
$disable_send_datas = (bool) Settings::get_option( 'disable_send_datas', false );
170201

Collapse file

‎templates/frontend/google-consent-mode.php‎

Copy file name to clipboard
+20-1Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
<?php defined( 'ABSPATH' ) || exit; ?>
12
<?php
2-
defined( 'ABSPATH' ) || exit;
33
/**
44
* Google Consent Mode template.
55
*
66
* @package Axeptio
77
*/
8+
?>
9+
<?php if ( $data->active_google_consent_mode ) : ?>
10+
<script>
11+
window.dataLayer = window.dataLayer || [];
12+
window.gtag = window.gtag || function () {
13+
window.dataLayer.push( arguments );
14+
};
15+
gtag( 'set', 'developer_id.dNGFkYj', true );
16+
gtag( 'consent', 'default', {
17+
analytics_storage: "<?php echo esc_js( $data->google_consent_mode_params['analytics_storage'] ); ?>",
18+
ad_storage: "<?php echo esc_js( $data->google_consent_mode_params['ad_storage'] ); ?>",
19+
ad_user_data: "<?php echo esc_js( $data->google_consent_mode_params['ad_user_data'] ); ?>",
20+
ad_personalization: "<?php echo esc_js( $data->google_consent_mode_params['ad_personalization'] ); ?>",
21+
functionality_storage: "<?php echo esc_js( $data->google_consent_mode_params['functionality_storage'] ); ?>",
22+
personalization_storage: "<?php echo esc_js( $data->google_consent_mode_params['personalization_storage'] ); ?>",
23+
security_storage: "<?php echo esc_js( $data->google_consent_mode_params['security_storage'] ); ?>",
24+
} );
25+
</script>
26+
<?php endif; ?>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.