init_twig(); } private function init_twig() { $loader = new FilesystemLoader(WC_TPP_PLUGIN_DIR . 'templates'); $this->twig = new Environment($loader, array( 'cache' => WP_DEBUG ? false : WC_TPP_PLUGIN_DIR . 'templates/cache', 'auto_reload' => true, 'autoescape' => 'html', )); // Add WordPress translation filter $this->twig->addFilter(new TwigFilter('__', function ($string, $domain = 'wc-tier-package-prices') { return __($string, $domain); })); $this->twig->addFilter(new TwigFilter('_e', function ($string, $domain = 'wc-tier-package-prices') { _e($string, $domain); })); $this->twig->addFilter(new TwigFilter('esc_html', 'esc_html')); $this->twig->addFilter(new TwigFilter('esc_attr', 'esc_attr')); $this->twig->addFilter(new TwigFilter('esc_url', 'esc_url')); // Add WordPress functions $this->twig->addFunction(new TwigFunction('get_option', 'get_option')); $this->twig->addFunction(new TwigFunction('checked', function($checked, $current = true, $echo = false) { return checked($checked, $current, $echo); })); $this->twig->addFunction(new TwigFunction('selected', function($selected, $current = true, $echo = false) { return selected($selected, $current, $echo); })); $this->twig->addFunction(new TwigFunction('settings_fields', function($option_group) { settings_fields($option_group); })); $this->twig->addFunction(new TwigFunction('submit_button', function($text = null) { submit_button($text); })); $this->twig->addFunction(new TwigFunction('get_admin_page_title', 'get_admin_page_title')); $this->twig->addFunction(new TwigFunction('wc_price', 'wc_price')); } /** * Render a Twig template * * @param string $template Template file name (e.g., 'admin/settings-page.twig') * @param array $context Variables to pass to the template * @return string Rendered template */ public function render($template, $context = array()) { try { return $this->twig->render($template, $context); } catch (Exception $e) { if (WP_DEBUG) { return sprintf( '
Twig Error: %s