Skip to content

Fix GDPR Cookie Compliance importer: integrations were not saved on a real site - #19

Merged
davidperezgar merged 2 commits into
mainfrom
feature/gdpr-cookie-compliance-migrator
Nov 13, 2026
Merged

davidperezgar merged 2 commits into
mainfrom
feature/gdpr-cookie-compliance-migrator

Conversation

@Castellon-ACM

Copy link
Copy Markdown
Contributor

Follow-up to #17 (issue #14, already merged). Found while verifying the importer on main (v1.0.2), first with the test suite and then on a real Local site.

The main bug

Imported tracking integrations were never saved. The import report said "Imported: GA4 / GTM", but cookie_notice_tracking_integrations stayed empty and the frontend loaded nothing.

  • Cause: the settings form sanitizer, registered with register_setting(), runs on every update_option() call. It only keeps cookie_notice_tracking_integrations when it arrives through the form's own fields (cookie_notice_tracking_integration_code / cookie_notice_tracking_remove). The importer sends the key directly, so the sanitizer discarded it and kept the old value.
  • Why the tests missed it: that sanitizer is only registered on admin_init, which the unit tests never run.
  • Fix: run() now saves through a guard (GDPR_Cookie_Compliance_Migration::is_saving()) that Settings::sanitize_settings() honours. This is safe because build() already sanitizes every value.
  • Regression test: test_run_persists_integrations_through_the_settings_sanitizer registers the setting as admin_init does, runs the import twice, and checks that nothing is lost or duplicated.

Other fixes

  1. Source category (2 = analytics, 3 = marketing) was ignored. FrontConsent derives the consent category from the integration type (GTM and GA4 are always analytics), so a GTM that was 3 became analytics silently. It is still imported, but it is now reported as needs review with a note. Storing a category per integration would change the data model and the PRO add-on, so that is out of scope here.
  2. Blank saved values counted as "already configured". A site that had saved the settings form once has empty texts and no policy page stored, so banner texts were never imported. Blank defaults ('', 0, null) are now treated as unset and filled. Real choices are still never overwritten.
  3. Non-scalar {slug}_id (corrupted data) no longer triggers an "Array to string conversion" warning. It is treated as a missing ID and flagged for review.
  4. gdin_values already decoded as an array is now imported instead of being ignored without a report.
  5. Negative or non-numeric moove_gdpr_consent_expiration is reported and not imported. Before, absint( -5 ) imported 5 days.

Tests

  • 7 new tests: the regression test above, category mismatch flagged, matching category not flagged, non-scalar ID, array gdin_values, negative/invalid expiration, and valid/capped expiration.
  • The fixture of test_ga4_and_gtm_are_migrated now uses GTM category 2, because category 3 is now flagged on purpose.

Verification

Run locally on PHP 8.2:

  • composer lint: pass
  • composer phpstan: no errors
  • composer test: 115 tests, all passing (108 before + 7 new)

Also tested end to end on a Local site that already had FrontConsent configured, using real admin HTTP requests with a temporary admin session:

  • The import notice and button appear; the button works with a valid nonce.
  • GA4 and GTM are stored; banner texts and labels are imported.
  • The report shows the category warning for the GTM that was in marketing, and lists Google Ads, Meta Pixel, Clarity and reCAPTCHA as "Not supported".
  • With consent accepted, the config endpoint serves the imported GTM and GA4 IDs. With consent rejected it serves nothing, and no Google <script> tag is printed before consent.
  • A second import leaves the settings byte-identical.
  • A bad nonce returns 403, a request without a session is rejected, and the source option is left untouched.

The site was restored to its previous state afterwards.

Not covered

  • Not tested with the real GDPR Cookie Compliance plugin installed. A seeded option with the same data shape was used.
  • Not tested by clicking through a browser.
  • Banner position and consent expiration are often reported as "already configured", because the settings form always stores defaults (365 days, bottom-right bar). This is a design limitation and is unchanged.
  • Google Ads, Meta Pixel, Clarity and UET still have no native integration type. They are only reported, as in Add settings importer from GDPR Cookie Compliance (Integrations tab) #17.

🤖 Generated with Claude Code

Castellon-ACM and others added 2 commits November 3, 2026 12:43
…iration

- Flag integrations whose source category (2 analytics / 3 marketing)
  differs from the category FrontConsent derives from the type, instead of
  changing it silently.
- Treat a non-scalar `{slug}_id` as a missing ID (no "Array to string" warning).
- Accept `gdin_values` already decoded as an array.
- Report a negative or non-numeric consent expiration instead of importing
  absint() of it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The settings form sanitizer registered via register_setting() runs on every
update_option() and only keeps `cookie_notice_tracking_integrations` when it
arrives through its own form fields, so the integrations built by the
importer were silently discarded on a real site (the report said "Imported"
but nothing was stored). run() now saves through a guard the sanitizer
honours, since build() already sanitized every value.

Also treat blank saved values (empty texts, no policy page) as unset, so a
site that already saved the settings form once still gets banner texts
imported.

Adds a regression test that registers the setting like admin_init does.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@davidperezgar
davidperezgar merged commit c0c9aaf into main Nov 13, 2026
7 checks passed
@davidperezgar
davidperezgar deleted the feature/gdpr-cookie-compliance-migrator branch November 13, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants