isJavascriptFirstModeEnabled()) { return; } // Solo en frontend, no en admin if (is_admin()) { return; } $scriptPath = $this->getScriptPath(); // Verificar que el archivo existe if (!file_exists($scriptPath)) { return; } $scriptUrl = $this->getScriptUrl(); wp_enqueue_script( self::SCRIPT_HANDLE, $scriptUrl, [], // Sin dependencias self::SCRIPT_VERSION, true // En footer ); // Pasar configuracion al script wp_localize_script(self::SCRIPT_HANDLE, 'roiAdsenseConfig', $this->getScriptConfig()); } /** * Verifica si el modo JavaScript-First esta activo. */ private function isJavascriptFirstModeEnabled(): bool { $settings = $this->settingsRepository->getComponentSettings(self::COMPONENT_NAME); $isEnabled = (bool) ($settings['visibility']['is_enabled'] ?? false); $jsFirstMode = (bool) ($settings['behavior']['javascript_first_mode'] ?? false); return $isEnabled && $jsFirstMode; } /** * Obtiene la ruta fisica del script. */ private function getScriptPath(): string { return get_template_directory() . '/Public/AdsensePlacement/Infrastructure/Ui/Assets/adsense-visibility.js'; } /** * Obtiene la URL del script. */ private function getScriptUrl(): string { return get_template_directory_uri() . '/Public/AdsensePlacement/Infrastructure/Ui/Assets/adsense-visibility.js'; } /** * Obtiene la configuracion para pasar al script. * * @return array */ private function getScriptConfig(): array { $postId = $this->getCurrentPostId(); $settings = $this->settingsRepository->getComponentSettings(self::COMPONENT_NAME); return [ 'endpoint' => rest_url('roi-theme/v1/adsense-placement/visibility'), 'postId' => $postId, 'nonce' => wp_create_nonce(AdsenseVisibilityController::getNonceAction()), 'settingsVersion' => $settings['_meta']['version'] ?? '1.0.0', 'debug' => defined('WP_DEBUG') && WP_DEBUG, 'featureEnabled' => true, 'fallbackStrategy' => 'cached-or-show', // cached-or-show | cached-or-hide | always-show ]; } /** * Obtiene el ID del post actual (0 si no es un post singular). */ private function getCurrentPostId(): int { if (is_singular()) { return get_the_ID() ?: 0; } return 0; } }