You've already forked wc-bootstrap
77 lines
1.9 KiB
PHP
77 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Template Override
|
||
|
|
*
|
||
|
|
* Hooks into the plugin's Twig FilesystemLoader to prepend
|
||
|
|
* the child theme's templates/ directory, allowing template overrides.
|
||
|
|
*
|
||
|
|
* @package WcBootstrap
|
||
|
|
* @since 0.1.0
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace WcBootstrap;
|
||
|
|
|
||
|
|
// IMPORTANT: Update these imports to match your plugin's actual class names.
|
||
|
|
use Magdev\Woocommerce\Frontend\Template;
|
||
|
|
use Twig\Loader\FilesystemLoader;
|
||
|
|
|
||
|
|
class TemplateOverride {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Path to the child theme's templates directory.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
private string $template_path;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Constructor.
|
||
|
|
*/
|
||
|
|
public function __construct() {
|
||
|
|
$this->template_path = WC_BOOTSTRAP_PATH . 'templates';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Register the template override with WordPress hooks.
|
||
|
|
*
|
||
|
|
* Must be called after the plugin's Template singleton is initialized
|
||
|
|
* (plugin inits at 'init' priority 0).
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function register(): void {
|
||
|
|
add_action( 'init', [ $this, 'override_template_paths' ], 20 );
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Prepend the child theme's templates directory to the Twig loader.
|
||
|
|
*
|
||
|
|
* This makes Twig look in the child theme's templates/ first,
|
||
|
|
* falling back to the plugin's templates/ if not found.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function override_template_paths(): void {
|
||
|
|
if ( ! class_exists( Template::class ) ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( ! is_dir( $this->template_path ) ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$twig = Template::get_instance()->get_twig();
|
||
|
|
$loader = $twig->getLoader();
|
||
|
|
|
||
|
|
if ( $loader instanceof FilesystemLoader ) {
|
||
|
|
$loader->prependPath( $this->template_path );
|
||
|
|
}
|
||
|
|
} catch ( \Exception $e ) {
|
||
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||
|
|
error_log( 'WooCommerce Bootstrap: Failed to register template override - ' . $e->getMessage() );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|