contextBuilder = new ContextBuilder(); add_action('template_redirect', [$this, 'render']); } /** * Render the appropriate Twig template for the current request. */ public function render(): void { // Skip admin, REST API, and AJAX requests. if (is_admin() || wp_doing_ajax()) { return; } if (defined('REST_REQUEST') && REST_REQUEST) { return; } $template = $this->resolveTemplate(); if (! $template) { return; } try { $context = $this->contextBuilder->build(); $twig = TwigService::getInstance(); echo $twig->render($template, $context); exit; } catch (\Throwable $e) { // Log the error and fall back to FSE rendering. error_log('WP Bootstrap Twig Error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine()); if (defined('WP_DEBUG') && WP_DEBUG) { wp_die( '
' . esc_html($e->getMessage()) . '
' . '' . esc_html($e->getFile()) . ':' . esc_html($e->getLine()) . '
' . '' . esc_html($e->getTraceAsString()) . '', 'Template Error', ['response' => 500] ); } } } /** * Determine which Twig template to render based on WordPress conditionals. */ private function resolveTemplate(): ?string { if (is_404()) { return 'pages/404.html.twig'; } if (is_search()) { return 'pages/search.html.twig'; } if (is_singular('post')) { return 'pages/single.html.twig'; } if (is_page()) { $slug = get_page_template_slug(); return match ($slug) { 'page-landing' => 'pages/landing.html.twig', 'page-full-width' => 'pages/full-width.html.twig', 'page-hero' => 'pages/hero.html.twig', 'page-sidebar' => 'pages/page-sidebar.html.twig', default => 'pages/page.html.twig', }; } if (is_archive()) { return 'pages/archive.html.twig'; } if (is_home()) { return 'pages/index.html.twig'; } // Fallback. return 'pages/index.html.twig'; } }