| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn; |
| 4 |
|
| 5 |
use Forge12\Shared\Logger; |
| 6 |
use Forge12\Shared\LoggerInterface; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class TemplateHandler { |
| 13 |
private static ?TemplateHandler $_instance = null; |
| 14 |
private LoggerInterface $logger; |
| 15 |
|
| 16 |
public static function getInstance() { |
| 17 |
if ( self::$_instance == null ) { |
| 18 |
self::$_instance = new self( Logger::getInstance() ); |
| 19 |
} |
| 20 |
|
| 21 |
return self::$_instance; |
| 22 |
} |
| 23 |
|
| 24 |
private function __construct( LoggerInterface $logger ) { |
| 25 |
$this->logger = $logger; |
| 26 |
$this->get_logger()->info( 'TemplateHandler class instance created.', [ |
| 27 |
'plugin' => 'double-opt-in', |
| 28 |
] ); |
| 29 |
} |
| 30 |
|
| 31 |
public function get_logger() { |
| 32 |
return $this->logger; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the content of the specified template with the given attributes. |
| 37 |
* |
| 38 |
* @param string $template_name The name of the template file. |
| 39 |
* @param array $atts The parameters sent to the template. |
| 40 |
* @param string $base_dir Directory holding the template (with trailing |
| 41 |
* slash). Add-ons pass their own; empty means |
| 42 |
* Core's templates/ directory. Since 5.8.0. |
| 43 |
* |
| 44 |
* @return string The content of the template. |
| 45 |
*/ |
| 46 |
public function getTemplate( $template_name, $atts = array(), $base_dir = '' ) { |
| 47 |
$this->get_logger()->info( 'Attempting to load a template file.', [ |
| 48 |
'plugin' => 'double-opt-in', |
| 49 |
'template_name' => $template_name, |
| 50 |
'attributes' => array_keys($atts), // Log only keys to avoid sensitive data |
| 51 |
] ); |
| 52 |
|
| 53 |
$directory = $base_dir !== '' ? trailingslashit( (string) $base_dir ) : plugin_dir_path( dirname( __FILE__ ) ) . 'templates/'; |
| 54 |
$template_path = $directory . sanitize_file_name( (string) $template_name ) . '.php'; |
| 55 |
|
| 56 |
if ( ! is_readable( $template_path ) ) { |
| 57 |
$this->get_logger()->error( 'Template file is not readable or does not exist.', [ |
| 58 |
'plugin' => 'double-opt-in', |
| 59 |
'template_name' => $template_name, |
| 60 |
'template_path' => $template_path, |
| 61 |
] ); |
| 62 |
return ''; |
| 63 |
} |
| 64 |
|
| 65 |
$this->get_logger()->debug( 'Template file found and is readable. Path: ' . $template_path, [ |
| 66 |
'plugin' => 'double-opt-in', |
| 67 |
] ); |
| 68 |
|
| 69 |
// Extract attributes for use in the template. |
| 70 |
extract( $atts ); |
| 71 |
|
| 72 |
ob_start(); |
| 73 |
require( $template_path ); |
| 74 |
$template_content = ob_get_clean(); |
| 75 |
|
| 76 |
$this->get_logger()->notice( 'Template content successfully loaded and captured.', [ |
| 77 |
'plugin' => 'double-opt-in', |
| 78 |
'template_name' => $template_name, |
| 79 |
'content_length' => strlen($template_content), |
| 80 |
] ); |
| 81 |
|
| 82 |
return $template_content; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Renders the specified template with the given attributes. |
| 87 |
* |
| 88 |
* @param string $template_name The name of the template file. |
| 89 |
* @param array $atts The parameters sent to the template. |
| 90 |
*/ |
| 91 |
public function renderTemplate( $template_name, $atts = array() ) { |
| 92 |
$this->get_logger()->info( 'Rendering a template to the output.', [ |
| 93 |
'plugin' => 'double-opt-in', |
| 94 |
'template_name' => $template_name, |
| 95 |
] ); |
| 96 |
|
| 97 |
$template = $this->getTemplate( $template_name, $atts ); |
| 98 |
|
| 99 |
if ( empty( $template ) ) { |
| 100 |
$this->get_logger()->warning( 'Template content is empty. Nothing to render.', [ |
| 101 |
'plugin' => 'double-opt-in', |
| 102 |
'template_name' => $template_name, |
| 103 |
] ); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
echo $template; |
| 108 |
|
| 109 |
$this->get_logger()->notice( 'Template content successfully rendered.', [ |
| 110 |
'plugin' => 'double-opt-in', |
| 111 |
'template_name' => $template_name, |
| 112 |
'content_length' => strlen($template), |
| 113 |
] ); |
| 114 |
} |
| 115 |
|
| 116 |
} |