settingsRepository->getComponentSettings(self::COMPONENT_NAME); $settings = AdsenseSettings::fromArray($rawSettings); // Evaluar reglas de visibilidad $reasons = []; // 1. AdSense desactivado globalmente if (!$settings->isEnabled()) { return VisibilityDecision::hide(['adsense_disabled'], self::CACHE_SECONDS_HIDE); } // 2. JavaScript-First Mode desactivado (usar PHP legacy) if (!$settings->isJavascriptFirstMode()) { return VisibilityDecision::hide(['javascript_first_disabled'], self::CACHE_SECONDS_HIDE); } // 3. Ocultar para usuarios logueados if ($settings->hideForLoggedIn() && $userContext->isLoggedIn()) { $reasons[] = 'user_logged_in'; } // 4. Verificar dispositivo if ($userContext->isMobile() && !$settings->showOnMobile()) { $reasons[] = 'mobile_disabled'; } if ($userContext->isDesktop() && !$settings->showOnDesktop()) { $reasons[] = 'desktop_disabled'; } // 5. Verificar exclusiones de post (solo si postId > 0) if ($postId > 0) { if ($settings->isPostExcluded($postId)) { $reasons[] = 'post_excluded'; } // Verificar categorias del post $postCategories = $this->getPostCategoryIds($postId); foreach ($postCategories as $catId) { if ($settings->isCategoryExcluded($catId)) { $reasons[] = 'category_excluded'; break; } } // Verificar post type $postType = $this->getPostType($postId); if ($postType !== '' && $settings->isPostTypeExcluded($postType)) { $reasons[] = 'post_type_excluded'; } } // Decision final if (count($reasons) > 0) { return VisibilityDecision::hide($reasons, self::CACHE_SECONDS_HIDE); } return VisibilityDecision::show(self::CACHE_SECONDS_SHOW); } /** * Obtiene IDs de categorias de un post. * * @return array */ private function getPostCategoryIds(int $postId): array { $categories = get_the_category($postId); if (!is_array($categories)) { return []; } return array_map( static fn($cat): int => (int) $cat->term_id, $categories ); } /** * Obtiene el post type de un post. */ private function getPostType(int $postId): string { $postType = get_post_type($postId); return $postType !== false ? $postType : ''; } }