} */ public static function compute( $discovered, $discovered_cookies, $banner_enabled, $company, $policy_page_state, $legal_notice_page_state ) { $breakdown = array(); $breakdown['banner'] = $banner_enabled ? self::WEIGHT_BANNER : 0; $items = array_merge( array_values( (array) $discovered ), array_values( (array) $discovered_cookies ) ); $total = count( $items ); if ( 0 === $total ) { // Nothing detected yet to categorize — full credit rather // than penalizing a brand-new install that hasn't had a // visitor (or a scan) yet. $breakdown['categorization'] = self::WEIGHT_CATEGORIZATION; } else { $categorized = 0; foreach ( $items as $entry ) { if ( '' !== self::effective_category( is_array( $entry ) ? $entry : array() ) ) { ++$categorized; } } $breakdown['categorization'] = self::WEIGHT_CATEGORIZATION * ( $categorized / $total ); } $company = is_array( $company ) ? $company : array(); $filled = 0; foreach ( self::COMPANY_FIELDS as $field ) { if ( isset( $company[ $field ] ) && '' !== trim( (string) $company[ $field ] ) ) { ++$filled; } } $breakdown['company_info'] = self::WEIGHT_COMPANY_INFO * ( $filled / count( self::COMPANY_FIELDS ) ); $breakdown['policy_page'] = self::page_state_points( $policy_page_state ); $breakdown['legal_notice_page'] = self::page_state_points( $legal_notice_page_state ); $score = (int) round( array_sum( $breakdown ) ); $score = max( 0, min( 100, $score ) ); $band = self::band( $score ); return array( 'score' => $score, 'band' => $band, 'label' => self::label( $band ), 'breakdown' => $breakdown, ); } /** * Gathers real state from WordPress storage. The only non-pure * entry point — kept this thin on purpose. */ public static function compute_for_site() { $discovered = get_option( AlpineConsent_Blocker::OPTION_NAME, array() ); $discovered_cookies = get_option( AlpineConsent_Admin::COOKIE_OPTION_NAME, array() ); $settings = AlpineConsent_General_Settings::get(); return self::compute( $discovered, $discovered_cookies, ! empty( $settings['show_banner'] ), $settings, self::page_state( AlpineConsent_Policy_Page::managed_page() ), self::page_state( AlpineConsent_Legal_Notice_Page::managed_page() ) ); } /** * Same effective-category resolution as AlpineConsent_Admin's own * (and AlpineConsent_Privacy_Section's) — a script/cookie counts as * categorized once it has EITHER an explicit admin-picked category * OR an auto-suggested one, matching every other place in this * plugin that already treats those two the same way (e.g. what * actually gets blocked). Kept as its own small copy rather than a * shared call — this plugin already duplicates this exact 4-line * resolution in two other places for the same reason: it's pure, * tiny, and each caller owning its own copy avoids a needless * cross-class dependency for one `?:`-shaped fallback. */ private static function effective_category( $entry ) { if ( ! empty( $entry['category'] ) ) { return $entry['category']; } return empty( $entry['suggested_category'] ) ? '' : $entry['suggested_category']; } private static function page_state_points( $state ) { if ( 'published' === $state ) { return self::WEIGHT_PER_LEGAL_PAGE; } if ( 'draft' === $state ) { return self::WEIGHT_PER_LEGAL_PAGE / 2; } return 0; } private static function page_state( $post ) { if ( ! $post ) { return 'none'; } return 'publish' === $post->post_status ? 'published' : 'draft'; } private static function band( $score ) { if ( $score >= 80 ) { return 'good'; } if ( $score >= 50 ) { return 'getting-there'; } return 'needs-attention'; } /** * Deliberately never says "compliant"/"compliance" — see this * file's own top docblock and readme.txt's FAQ entry for why. */ private static function label( $band ) { switch ( $band ) { case 'good': return __( 'Good', 'alpineconsent' ); case 'getting-there': return __( 'Getting there', 'alpineconsent' ); default: return __( 'Needs attention', 'alpineconsent' ); } } }