2026-02-28 09:42:35 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
2026-02-28 11:15:59 +01:00
|
|
|
* Template Override.
|
2026-02-28 09:42:35 +01:00
|
|
|
*
|
2026-02-28 11:15:59 +01:00
|
|
|
* Intercepts WooCommerce's PHP template rendering and replaces it
|
|
|
|
|
* with Twig template output from the child theme's templates/ directory.
|
|
|
|
|
*
|
|
|
|
|
* Uses woocommerce_before_template_part / woocommerce_after_template_part
|
|
|
|
|
* hooks to render the Twig version and discard the PHP include output.
|
2026-02-28 09:42:35 +01:00
|
|
|
*
|
|
|
|
|
* @package WcBootstrap
|
|
|
|
|
* @since 0.1.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WcBootstrap;
|
|
|
|
|
|
2026-02-28 11:15:59 +01:00
|
|
|
use WPBootstrap\Twig\TwigService;
|
2026-02-28 09:42:35 +01:00
|
|
|
|
|
|
|
|
class TemplateOverride {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Path to the child theme's templates directory.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2026-02-28 11:15:59 +01:00
|
|
|
private string $templatePath;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stack of template names with active output buffers.
|
|
|
|
|
*
|
|
|
|
|
* Handles nested template calls where one WC template triggers
|
|
|
|
|
* another via wc_get_template() from within action hooks.
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
|
|
|
|
private array $bufferStack = [];
|
2026-02-28 09:42:35 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct() {
|
2026-02-28 11:15:59 +01:00
|
|
|
$this->templatePath = WC_BOOTSTRAP_PATH . 'templates/';
|
2026-02-28 09:42:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-28 11:15:59 +01:00
|
|
|
* Register the template override hooks.
|
2026-02-28 09:42:35 +01:00
|
|
|
*
|
2026-02-28 11:15:59 +01:00
|
|
|
* Only registers if the parent theme's TwigService is available.
|
2026-02-28 09:42:35 +01:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function register(): void {
|
2026-02-28 11:15:59 +01:00
|
|
|
if ( ! class_exists( TwigService::class ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add_action( 'woocommerce_before_template_part', [ $this, 'beforeTemplatePart' ], 10, 4 );
|
|
|
|
|
add_action( 'woocommerce_after_template_part', [ $this, 'afterTemplatePart' ], 10, 4 );
|
2026-02-28 09:42:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-28 11:15:59 +01:00
|
|
|
* Before WooCommerce includes a PHP template.
|
2026-02-28 09:42:35 +01:00
|
|
|
*
|
2026-02-28 11:15:59 +01:00
|
|
|
* If a matching Twig template exists, renders it and starts output
|
|
|
|
|
* buffering to capture (and later discard) the PHP template output.
|
2026-02-28 09:42:35 +01:00
|
|
|
*
|
2026-02-28 11:15:59 +01:00
|
|
|
* @param string $templateName Template name (e.g., 'cart/cart.php').
|
|
|
|
|
* @param string $templatePath Template path override.
|
|
|
|
|
* @param string $located Full path to the located PHP template.
|
|
|
|
|
* @param array $args Template context variables.
|
2026-02-28 09:42:35 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-02-28 11:15:59 +01:00
|
|
|
public function beforeTemplatePart( string $templateName, string $templatePath, string $located, array $args ): void {
|
|
|
|
|
$twigTemplate = $this->resolveTwigTemplate( $templateName );
|
2026-02-28 09:42:35 +01:00
|
|
|
|
2026-02-28 11:15:59 +01:00
|
|
|
if ( null === $twigTemplate ) {
|
|
|
|
|
return; // No Twig override — let PHP render normally.
|
2026-02-28 09:42:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-02-28 11:15:59 +01:00
|
|
|
$twig = TwigService::getInstance();
|
|
|
|
|
echo $twig->render( $twigTemplate, $args );
|
2026-02-28 09:42:35 +01:00
|
|
|
|
2026-02-28 11:15:59 +01:00
|
|
|
// Buffer the upcoming PHP include so we can discard it.
|
|
|
|
|
ob_start();
|
|
|
|
|
$this->bufferStack[] = $templateName;
|
|
|
|
|
} catch ( \Throwable $e ) {
|
|
|
|
|
// Twig render failed — let PHP render as fallback.
|
2026-02-28 09:42:35 +01:00
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
2026-02-28 11:15:59 +01:00
|
|
|
error_log( sprintf(
|
|
|
|
|
'WC Bootstrap: Twig render failed for %s — %s',
|
|
|
|
|
$templateName,
|
|
|
|
|
$e->getMessage()
|
|
|
|
|
) );
|
2026-02-28 09:42:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-28 11:15:59 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* After WooCommerce includes a PHP template.
|
|
|
|
|
*
|
|
|
|
|
* If we started buffering for this template, discards the PHP output.
|
|
|
|
|
*
|
|
|
|
|
* @param string $templateName Template name.
|
|
|
|
|
* @param string $templatePath Template path override.
|
|
|
|
|
* @param string $located Full path to the located PHP template.
|
|
|
|
|
* @param array $args Template context variables.
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function afterTemplatePart( string $templateName, string $templatePath, string $located, array $args ): void {
|
|
|
|
|
if ( ! empty( $this->bufferStack ) && end( $this->bufferStack ) === $templateName ) {
|
|
|
|
|
ob_end_clean(); // Discard PHP template output.
|
|
|
|
|
array_pop( $this->bufferStack );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve a WooCommerce template name to a Twig template path.
|
|
|
|
|
*
|
|
|
|
|
* Maps 'cart/cart.php' to 'cart/cart.html.twig' and checks that
|
|
|
|
|
* the file exists in the child theme's templates/ directory.
|
|
|
|
|
*
|
|
|
|
|
* @param string $templateName WooCommerce template name.
|
|
|
|
|
* @return string|null Twig template path relative to templates/, or null if not found.
|
|
|
|
|
*/
|
|
|
|
|
private function resolveTwigTemplate( string $templateName ): ?string {
|
|
|
|
|
$twigName = preg_replace( '/\.php$/', '.html.twig', $templateName );
|
|
|
|
|
$fullPath = $this->templatePath . $twigName;
|
|
|
|
|
|
|
|
|
|
if ( file_exists( $fullPath ) ) {
|
|
|
|
|
return $twigName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-02-28 09:42:35 +01:00
|
|
|
}
|