diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index 87da31e..20da3ef 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -961,6 +961,11 @@ public function sanitize_settings( $value ) { return get_option( self::OPTION_NAME, array() ); } + // The GDPR Cookie Compliance importer saves values it already sanitized. + if ( GDPR_Cookie_Compliance_Migration::is_saving() ) { + return $value; + } + $current_options = get_option( self::OPTION_NAME, array() ); $sanitized = $current_options; diff --git a/includes/GDPR_Cookie_Compliance_Migration.php b/includes/GDPR_Cookie_Compliance_Migration.php index 4a243d2..0f5fbed 100644 --- a/includes/GDPR_Cookie_Compliance_Migration.php +++ b/includes/GDPR_Cookie_Compliance_Migration.php @@ -62,6 +62,17 @@ class GDPR_Cookie_Compliance_Migration { 'gtmc2' => 'gtm', ); + /** + * Cookie categories the source plugin assigns to an integration, mapped + * to FrontConsent's consent categories. Other values (e.g. 1) are ignored. + * + * @var string[] + */ + const SOURCE_CATEGORIES = array( + '2' => 'analytics', + '3' => 'marketing', + ); + /** * Source integrations FrontConsent has no equivalent for, with a label. * @@ -77,6 +88,23 @@ public static function get_unsupported_integrations() { ); } + /** + * Whether run() is saving the already sanitized settings right now. + * + * @var bool + */ + private static $saving = false; + + /** + * Whether the importer is saving FrontConsent's settings at this moment, + * so the settings form sanitizer can let them through unchanged. + * + * @return bool + */ + public static function is_saving() { + return self::$saving; + } + /** * Whether the source plugin's settings exist on this site. * @@ -103,7 +131,15 @@ public static function run() { $gtm4wp = get_option( 'gtm4wp-options', array() ); $result = self::build( $source, $current, is_array( $gtm4wp ) ? $gtm4wp : array() ); - update_option( 'frontconsent_settings', $result['settings'] ); + // The settings form sanitizer only keeps the tracking integrations it + // receives through its own form fields, so it would discard the ones + // built here. The values are already sanitized by build(). + self::$saving = true; + try { + update_option( 'frontconsent_settings', $result['settings'] ); + } finally { + self::$saving = false; + } // Leave the import available for a retry when the source data could // not be read, so the admin can repair it and run it again. @@ -161,7 +197,10 @@ public static function build( array $source, array $current, array $gtm4wp = arr private static function map_integrations( array $source, array &$current, array &$report, array $gtm4wp = array() ) { $values = array(); $readable = true; - if ( isset( $source['gdin_values'] ) && is_string( $source['gdin_values'] ) ) { + if ( isset( $source['gdin_values'] ) && is_array( $source['gdin_values'] ) ) { + // Already decoded (e.g. filtered by another plugin): use it as is. + $values = $source['gdin_values']; + } elseif ( isset( $source['gdin_values'] ) && is_string( $source['gdin_values'] ) ) { $decoded = json_decode( $source['gdin_values'], true ); $values = is_array( $decoded ) ? $decoded : array(); @@ -215,8 +254,9 @@ private static function map_integrations( array $source, array &$current, array continue; } - $id = strtoupper( trim( sanitize_text_field( (string) ( $values[ $slug . '_id' ] ?? '' ) ) ) ); - $type = self::INTEGRATION_MAP[ $slug ]; + $raw_id = $values[ $slug . '_id' ] ?? ''; + $id = is_scalar( $raw_id ) ? strtoupper( trim( sanitize_text_field( (string) $raw_id ) ) ) : ''; + $type = self::INTEGRATION_MAP[ $slug ]; // The source plugin accepts GA4 measurement IDs in its GTM field. if ( 'gtm' === $type && 0 === strpos( $id, 'G-' ) ) { @@ -259,15 +299,29 @@ private static function map_integrations( array $source, array &$current, array 'id' => $id, ); - $note = ''; + $notes = array(); if ( 'gtmc2' === $slug ) { - $note = __( 'Imported as Google Tag Manager. Review your Consent Mode v2 setup.', 'frontconsent' ); + $notes[] = __( 'Imported as Google Tag Manager. Review your Consent Mode v2 setup.', 'frontconsent' ); + } + + // FrontConsent derives the consent category from the integration + // type, so a category chosen in the source plugin cannot be kept + // when it differs: flag it instead of changing it silently. + $source_category = is_scalar( $value ) ? ( self::SOURCE_CATEGORIES[ (string) $value ] ?? '' ) : ''; + $default = Frontend\CookieNotice::get_integration_default_category( $type ); + if ( '' !== $source_category && $source_category !== $default ) { + $notes[] = sprintf( + /* translators: 1: consent category in GDPR Cookie Compliance, 2: consent category in FrontConsent. */ + __( 'It was in the "%1$s" category in GDPR Cookie Compliance, but FrontConsent treats it as "%2$s". Review the consent category if you use per-category consent.', 'frontconsent' ), + $source_category, + $default + ); } $report[] = array( 'item' => $label . ' (' . $id . ')', - 'status' => 'gtmc2' === $slug ? self::STATUS_REVIEW : self::STATUS_MIGRATED, - 'note' => $note, + 'status' => $notes ? self::STATUS_REVIEW : self::STATUS_MIGRATED, + 'note' => implode( ' ', $notes ), ); } @@ -332,9 +386,16 @@ private static function map_banner_settings( array $source, array &$current, arr } if ( isset( $source['moove_gdpr_consent_expiration'] ) ) { - $days = absint( $source['moove_gdpr_consent_expiration'] ); - if ( $days > 0 ) { - $candidates['cookie_notice_expiration_days'] = min( $days, 730 ); + $raw_days = $source['moove_gdpr_consent_expiration']; + if ( is_numeric( $raw_days ) && $raw_days > 0 ) { + $candidates['cookie_notice_expiration_days'] = min( (int) $raw_days, 730 ); + } elseif ( is_numeric( $raw_days ) ? $raw_days < 0 : ( is_string( $raw_days ) && '' !== trim( $raw_days ) ) ) { + // absint() would turn a negative value into a valid duration. + $report[] = array( + 'item' => __( 'Consent expiration', 'frontconsent' ), + 'status' => self::STATUS_REVIEW, + 'note' => __( 'The consent expiration of GDPR Cookie Compliance is not a valid number of days, so it was not imported. Set it manually in FrontConsent.', 'frontconsent' ), + ); } } @@ -366,7 +427,9 @@ private static function map_banner_settings( array $source, array &$current, arr $migrated = 0; $existing = 0; foreach ( $candidates as $key => $value ) { - if ( array_key_exists( $key, $current ) ) { + // A blank saved value (empty text, no policy page) is only the + // form default, not a choice, so it is safe to fill. + if ( array_key_exists( $key, $current ) && ! in_array( $current[ $key ], array( '', 0, null ), true ) ) { ++$existing; continue; } diff --git a/tests/Unit/GDPRCookieComplianceMigrationTest.php b/tests/Unit/GDPRCookieComplianceMigrationTest.php index f5174f6..e7039ae 100644 --- a/tests/Unit/GDPRCookieComplianceMigrationTest.php +++ b/tests/Unit/GDPRCookieComplianceMigrationTest.php @@ -31,7 +31,7 @@ public function test_ga4_and_gtm_are_migrated() { array( 'ga4' => 2, 'ga4_id' => 'G-ABC123', - 'gtm' => 3, + 'gtm' => 2, 'gtm_id' => 'GTM-ABCD12', ) ), @@ -380,4 +380,137 @@ public function test_position_is_not_mixed_with_a_partially_configured_layout() $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ) ); } } + + public function test_integration_in_a_different_category_is_flagged_for_review() { + $result = Importer::build( + $this->source( + array( + 'gtm' => 3, + 'gtm_id' => 'GTM-ABCD12', + 'ga4' => 3, + 'ga4_id' => 'G-ABC123', + ) + ), + array() + ); + + // Imported, but the category FrontConsent derives from the type differs. + $this->assertCount( 2, $result['settings']['cookie_notice_tracking_integrations'] ); + $this->assertSame( array( 'needs_review', 'needs_review' ), $this->statuses( $result['report'] ) ); + $this->assertStringContainsString( 'marketing', $result['report'][0]['note'] ); + $this->assertStringContainsString( 'analytics', $result['report'][0]['note'] ); + } + + public function test_matching_category_is_not_flagged() { + $result = Importer::build( $this->source( array( 'ga4' => 2, 'ga4_id' => 'G-ABC123' ) ), array() ); + + $this->assertSame( array( 'migrated' ), $this->statuses( $result['report'] ) ); + $this->assertSame( '', $result['report'][0]['note'] ); + } + + public function test_non_scalar_tracking_id_is_flagged_without_warnings() { + $result = Importer::build( $this->source( array( 'ga4' => 2, 'ga4_id' => array( 'x' ) ) ), array() ); + + $this->assertArrayNotHasKey( 'cookie_notice_tracking_integrations', $result['settings'] ); + $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ) ); + } + + public function test_already_decoded_gdin_values_is_imported() { + $result = Importer::build( + array( + 'gdin_values' => array( + 'ga4' => 2, + 'ga4_id' => 'G-ABC123', + ), + ), + array() + ); + + $this->assertSame( + array( + array( + 'type' => 'ga4', + 'id' => 'G-ABC123', + ), + ), + $result['settings']['cookie_notice_tracking_integrations'] + ); + $this->assertTrue( $result['complete'] ); + } + + public function test_negative_or_invalid_expiration_is_reported_not_imported() { + foreach ( array( '-5', 'abc' ) as $value ) { + $result = Importer::build( array( 'moove_gdpr_consent_expiration' => $value ), array() ); + + $this->assertArrayNotHasKey( 'cookie_notice_expiration_days', $result['settings'], $value ); + $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ), $value ); + } + } + + public function test_valid_expiration_is_imported_and_capped() { + $result = Importer::build( array( 'moove_gdpr_consent_expiration' => '30' ), array() ); + $this->assertSame( 30, $result['settings']['cookie_notice_expiration_days'] ); + + $result = Importer::build( array( 'moove_gdpr_consent_expiration' => '99999' ), array() ); + $this->assertSame( 730, $result['settings']['cookie_notice_expiration_days'] ); + } + + /** + * With the settings page registered (as on a real admin request), saving + * must go through the option sanitizer without losing the imported data. + */ + public function test_run_persists_integrations_through_the_settings_sanitizer() { + ( new \FrontConsent\Admin\Settings() )->register_settings(); + + update_option( + 'frontconsent_settings', + array( + 'enable_cookie_notice' => true, + 'cookie_notice_message' => '', + 'cookie_notice_accept_label' => '', + 'cookie_notice_reject_label' => '', + 'cookie_notice_policy_page_id' => 0, + 'cookie_notice_tracking_integrations' => array(), + ) + ); + update_option( + Importer::SOURCE_OPTION, + $this->source( + array( + 'ga4' => 2, + 'ga4_id' => 'G-ABC123', + 'gtm' => 2, + 'gtm_id' => 'GTM-ABCD12', + ), + array( + 'moove_gdpr_info_bar_content' => '
Hello cookies
', + 'moove_gdpr_infobar_accept_button_label' => 'Accept all', + ) + ) + ); + + Importer::run(); + $saved = get_option( 'frontconsent_settings' ); + + $this->assertSame( + array( + array( + 'type' => 'ga4', + 'id' => 'G-ABC123', + ), + array( + 'type' => 'gtm', + 'id' => 'GTM-ABCD12', + ), + ), + $saved['cookie_notice_tracking_integrations'] + ); + // Blank defaults are not a configured value, so the texts are imported. + $this->assertSame( 'Hello cookies', $saved['cookie_notice_message'] ); + $this->assertSame( 'Accept all', $saved['cookie_notice_accept_label'] ); + + // A second run neither duplicates nor changes anything. + Importer::run(); + $this->assertSame( $saved, get_option( 'frontconsent_settings' ) ); + } }