'functional', 'Necessary' => 'functional', 'Security' => 'functional', 'Marketing' => 'marketing', 'Analytics' => 'statistics', 'Personalization' => 'preferences', ); private static $exact = null; private static $wildcards = null; private static $by_platform = null; /** * @param string $name A cookie's name, as observed on a Set-Cookie header. * @return array|null { platform, category, db_category, description, retention_period, data_controller, privacy_link } or null if unrecognized. */ public static function lookup( $name ) { self::maybe_load(); $lower = strtolower( (string) $name ); if ( '' === $lower ) { return null; } if ( isset( self::$exact[ $lower ] ) ) { return self::$exact[ $lower ]; } foreach ( self::$wildcards as $wildcard ) { if ( 0 === strpos( $lower, $wildcard['prefix'] ) ) { return $wildcard['record']; } } return null; } /** * AlpineConsent#291/#292 follow-up — the same "known cookies for a * recognized provider" signal backend/src/utils/cookieDatabase.js's * own lookupPlatformCookies() already gives the website's scanner, * ported here so the plugin's own Cookies tab (and Cookie Policy * generator) can show it too. Real gap raised directly by the * user: this plugin only ever discovers a cookie via a Set-Cookie * HTTP response header (see this class's own top comment) — most * analytics/marketing cookies are set by client-side JavaScript * instead, which a plain wp_remote_get() can never see, so a site * whose tracking is all JS-driven shows an empty Cookies tab no * matter how many times it's scanned. This gives real, * informational value even then: for every SCRIPT the plugin has * already identified a provider for (AlpineConsent_Suggestions::describe()), * list the cookies that provider is publicly documented to use — * clearly labeled as "known for this provider", never claimed as * actually observed. * * Wildcard-prefix rows are excluded — a prefix isn't a real, * displayable cookie name to predict as one (same exclusion * lookupPlatformCookies() makes). * * @param string $platform A script's own describe()d provider name, matched case-insensitively. * @return array[] Each: { cookie, platform, category, description, retention_period, data_controller, privacy_link }. */ public static function lookup_by_platform( $platform ) { self::maybe_load(); $lower = strtolower( trim( (string) $platform ) ); if ( '' === $lower ) { return array(); } return isset( self::$by_platform[ $lower ] ) ? self::$by_platform[ $lower ] : array(); } /** * Shared by both class-admin.php's Cookies tab AND * class-policy-page.php's build_html() — factored out here rather * than duplicated in each (class-admin.php's own version used to be * an instance method there for its established public-instance- * method-for-testability convention; it now just delegates to this * static implementation, callable from build_html()'s own static * context too). * * @param array $discovered AlpineConsent_Blocker::OPTION_NAME shape. * @param array $discovered_cookies AlpineConsent_Admin::COOKIE_OPTION_NAME shape — already-observed names are excluded. * @return array[] Each: { cookie, platform, category, description, retention_period, data_controller, privacy_link, predicted_for }. */ public static function predicted_cookies_for_scripts( $discovered, $discovered_cookies = array() ) { $observed_names = array(); foreach ( array_keys( (array) $discovered_cookies ) as $name ) { $observed_names[ strtolower( (string) $name ) ] = true; } $seen_providers = array(); $predicted = array(); foreach ( (array) $discovered as $handle => $entry ) { $src = isset( $entry['src'] ) ? $entry['src'] : ''; $described = class_exists( 'AlpineConsent_Suggestions' ) ? AlpineConsent_Suggestions::describe( $handle, $src ) : null; $provider = $described && ! empty( $described['provider'] ) ? $described['provider'] : ''; if ( '' === $provider ) { continue; } $provider_key = strtolower( $provider ); if ( isset( $seen_providers[ $provider_key ] ) ) { continue; } $seen_providers[ $provider_key ] = true; foreach ( self::lookup_by_platform( $provider ) as $record ) { if ( isset( $observed_names[ strtolower( $record['cookie'] ) ] ) ) { continue; } $predicted[] = array_merge( $record, array( 'predicted_for' => $provider ) ); } } return $predicted; } /** * Loaded once per request (static, like AlpineConsent_Strings' * DICTIONARY constant is available immediately — the difference * here is this data is too large to hardcode as a PHP array * literal, so it's read from the vendored JSON file instead, but * only on first actual use within a request, not on every plugin * bootstrap). */ private static function maybe_load() { if ( null !== self::$exact ) { return; } self::$exact = array(); self::$wildcards = array(); self::$by_platform = array(); $path = ALPINECONSENT_PLUGIN_DIR . 'data/open-cookie-database.json'; if ( ! file_exists( $path ) ) { return; // Never fatal — an admin scan just finds nothing this way, same as no plugin at all. } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local file this plugin ships (see bin/sync-cookie-database.sh), not a user-facing or remote read; WP_Filesystem is for operations a site owner's file-permission setup should govern, not internal vendored-asset loading (same reasoning as class-strings.php's own bundled dictionary, just too large to be a PHP literal). $raw = file_get_contents( $path ); $data = json_decode( $raw, true ); if ( ! is_array( $data ) ) { return; } foreach ( $data as $platform => $entries ) { if ( ! is_array( $entries ) ) { continue; } foreach ( $entries as $entry ) { $cookie_name = isset( $entry['cookie'] ) ? strtolower( trim( (string) $entry['cookie'] ) ) : ''; if ( '' === $cookie_name ) { continue; } $db_category = isset( $entry['category'] ) ? $entry['category'] : ''; $record = array( 'platform' => $platform, 'category' => isset( self::CATEGORY_MAP[ $db_category ] ) ? self::CATEGORY_MAP[ $db_category ] : null, 'db_category' => $db_category, 'description' => isset( $entry['description'] ) ? $entry['description'] : '', 'retention_period' => isset( $entry['retentionPeriod'] ) ? $entry['retentionPeriod'] : '', 'data_controller' => isset( $entry['dataController'] ) ? $entry['dataController'] : '', 'privacy_link' => isset( $entry['privacyLink'] ) ? $entry['privacyLink'] : '', ); $is_wildcard = isset( $entry['wildcardMatch'] ) && '1' === (string) $entry['wildcardMatch']; if ( $is_wildcard ) { self::$wildcards[] = array( 'prefix' => $cookie_name, 'record' => $record, ); } else { if ( ! isset( self::$exact[ $cookie_name ] ) ) { // A handful of names repeat across platforms — first // entry in file order wins, silently. Not worth a // "which is more authoritative" policy for ~1% of rows. self::$exact[ $cookie_name ] = $record; } // Platform grouping (lookup_by_platform()) is a // separate index from the exact-name dedup above — // every non-wildcard row for a platform is kept here, // original casing preserved for display (unlike // $cookie_name, lowercased for indexing above). $platform_key = strtolower( $platform ); if ( ! isset( self::$by_platform[ $platform_key ] ) ) { self::$by_platform[ $platform_key ] = array(); } self::$by_platform[ $platform_key ][] = array_merge( array( 'cookie' => isset( $entry['cookie'] ) ? $entry['cookie'] : '', 'domain' => isset( $entry['domain'] ) ? $entry['domain'] : '', ), $record ); } } } // Longest prefix first, so a more specific wildcard (rare, but // possible) wins over a shorter/broader one. usort( self::$wildcards, function ( $a, $b ) { return strlen( $b['prefix'] ) - strlen( $a['prefix'] ); } ); } }