Skip to content

Commit 1dce49a

Browse files
fix: use SPARXSTAR_GLUON_COOKIE_TOKEN constant and integrate WP Consent API
- gluonRegisterCookies(): use self::SPARXSTAR_GLUON_COOKIE_TOKEN instead of hardcoded '__Host-SparxstarGluon-TOKEN' to avoid registration/removal drift when the scaffold is renamed. - Add gluonSetTokenCookie(): consent-gated cookie writer; returns false and writes nothing if the user has not granted functional consent or if headers are already sent. - Add gluonDeleteTokenCookie(): expires the cookie immediately and removes it from $_COOKIE so callers see the change in the same request. - Add gluonHandleConsentChange(): hooked on wp_set_consent; deletes the token cookie the moment functional consent is withdrawn. - Register wp_set_consent hook in gluonRegisterHooks(). Co-authored-by: MaximillianGroup <34328348+MaximillianGroup@users.noreply.github.com> Agent-Logs-Url: https://github.com/Starisian-Technologies/sparxstar-gluon/sessions/7b663b48-d6c7-4749-89fb-35939ca1fd1a
1 parent 2c5beab commit 1dce49a

1 file changed

Lines changed: 95 additions & 9 deletions

File tree

src/integrations/SparxstarGluonRules.php

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ public function __construct() {
104104
private function gluonRegisterHooks(): void {
105105
// Register hooks related to plugin rules here.
106106
\add_action( 'plugins_loaded', array( $this, 'gluonRegisterCookies' ) );
107-
// sets the consent type (optin, optout, default false)
107+
// Sets the consent type (optin, optout, default false).
108108
\add_filter( 'wp_get_consent_type', array( $this, 'gluonSetConsentType' ), 10, 1 );
109-
// Modify consent categories
109+
// Modify consent categories.
110110
\add_filter( 'wp_get_consent_categories', array( $this, 'gluonSetConsentCategories' ), 10, 1 );
111+
// React to consent changes — delete token cookie when functional consent is withdrawn.
112+
\add_action( 'wp_set_consent', array( $this, 'gluonHandleConsentChange' ), 10, 2 );
111113
}
112114

113115
/**
@@ -127,11 +129,11 @@ public function gluonComplyWithRules(): void {
127129
/**
128130
* Register cookies with the WordPress Consent API.
129131
*
130-
* Registers plugin cookies so they can be shown to users on the front-end
132+
* Registers the plugin token cookie so it can be shown to users on the front-end
131133
* via wp_get_cookie_info(). This is required for GDPR compliance.
132134
*
133135
* Cookie is registered with:
134-
* - Token: __Host-SparxstarGluon-TOKEN
136+
* - Token: {@see SPARXSTAR_GLUON_COOKIE_TOKEN}
135137
* - Purpose: Session management / functional
136138
* - Type: functional (required for site operation)
137139
*
@@ -140,11 +142,9 @@ public function gluonComplyWithRules(): void {
140142
* @return void
141143
*/
142144
public function gluonRegisterCookies(): void {
143-
// If the Consent API function exists
144-
if ( function_exists( 'wp_add_cookie_info' ) ) {
145-
// Register the plugin cookie with the Consent API
146-
wp_add_cookie_info(
147-
'__Host-SparxstarGluon-TOKEN',
145+
if ( \function_exists( 'wp_add_cookie_info' ) ) {
146+
\wp_add_cookie_info(
147+
self::SPARXSTAR_GLUON_COOKIE_TOKEN,
148148
'SparxstarGluon',
149149
'functional',
150150
__( 'Session', 'sparxstar-gluon' ),
@@ -170,6 +170,92 @@ public function gluonUnregisterCookies(): void {
170170
}
171171
}
172172

173+
/**
174+
* Set the plugin token cookie, gated on functional consent.
175+
*
176+
* Only writes the cookie when the user has granted functional consent via the
177+
* WordPress Consent API. If consent has not been given the request is silently
178+
* declined and the caller must handle the degraded state.
179+
*
180+
* The cookie uses the __Host- prefix which requires HTTPS, path="/", no Domain
181+
* attribute, and Secure flag — all enforced below.
182+
*
183+
* @since 1.0.0
184+
* @param string $token_value The token value to store in the cookie.
185+
* @param int $expires Unix timestamp for cookie expiry. 0 = session cookie.
186+
* @return bool True when the cookie was written, false when consent was denied
187+
* or headers have already been sent.
188+
*/
189+
public function gluonSetTokenCookie( string $token_value, int $expires = 0 ): bool {
190+
if ( ! $this->gluonIsConsentCategory( 'functional' ) ) {
191+
return false;
192+
}
193+
if ( headers_sent() ) {
194+
return false;
195+
}
196+
return setcookie(
197+
self::SPARXSTAR_GLUON_COOKIE_TOKEN,
198+
$token_value,
199+
array(
200+
'expires' => $expires,
201+
'path' => '/',
202+
'domain' => '',
203+
'secure' => true,
204+
'httponly' => true,
205+
'samesite' => 'Strict',
206+
)
207+
);
208+
}
209+
210+
/**
211+
* Delete the plugin token cookie.
212+
*
213+
* Expires the cookie immediately and removes it from the current request's
214+
* $_COOKIE superglobal so callers see the change without a page reload.
215+
* Safe to call even when the cookie is not present.
216+
*
217+
* @since 1.0.0
218+
* @return void
219+
*/
220+
public function gluonDeleteTokenCookie(): void {
221+
if ( ! isset( $_COOKIE[ self::SPARXSTAR_GLUON_COOKIE_TOKEN ] ) ) {
222+
return;
223+
}
224+
if ( ! headers_sent() ) {
225+
setcookie(
226+
self::SPARXSTAR_GLUON_COOKIE_TOKEN,
227+
'',
228+
array(
229+
'expires' => time() - HOUR_IN_SECONDS,
230+
'path' => '/',
231+
'domain' => '',
232+
'secure' => true,
233+
'httponly' => true,
234+
'samesite' => 'Strict',
235+
)
236+
);
237+
}
238+
unset( $_COOKIE[ self::SPARXSTAR_GLUON_COOKIE_TOKEN ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- key lookup only, no value used
239+
}
240+
241+
/**
242+
* React to WordPress Consent API consent changes.
243+
*
244+
* Fires on the `wp_set_consent` action. When functional consent is denied or
245+
* withdrawn the token cookie is immediately deleted so no functional cookie
246+
* persists without the user's agreement.
247+
*
248+
* @since 1.0.0
249+
* @param string $category The consent category that changed (e.g. 'functional').
250+
* @param string $value The new consent value ('allow' | 'deny').
251+
* @return void
252+
*/
253+
public function gluonHandleConsentChange( string $category, string $value ): void {
254+
if ( 'functional' === $category && 'allow' !== $value ) {
255+
$this->gluonDeleteTokenCookie();
256+
}
257+
}
258+
173259
/**
174260
* Check if user has given consent for a specific category.
175261
*

0 commit comments

Comments
 (0)