| 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 |
* |
| 41 |
* @return string The content of the template. |
| 42 |
*/ |
| 43 |
public function getTemplate( $template_name, $atts = array() ) { |
| 44 |
$this->get_logger()->info( 'Attempting to load a template file.', [ |
| 45 |
'plugin' => 'double-opt-in', |
| 46 |
'template_name' => $template_name, |
| 47 |
'attributes' => array_keys($atts), // Log only keys to avoid sensitive data |
| 48 |
] ); |
| 49 |
|
| 50 |
$template_path = plugin_dir_path( dirname( __FILE__ ) ) . 'templates/' . $template_name . '.php'; |
| 51 |
|
| 52 |
if ( ! is_readable( $template_path ) ) { |
| 53 |
$this->get_logger()->error( 'Template file is not readable or does not exist.', [ |
| 54 |
'plugin' => 'double-opt-in', |
| 55 |
'template_name' => $template_name, |
| 56 |
'template_path' => $template_path, |
| 57 |
] ); |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
$this->get_logger()->debug( 'Template file found and is readable. Path: ' . $template_path, [ |
| 62 |
'plugin' => 'double-opt-in', |
| 63 |
] ); |
| 64 |
|
| 65 |
// Extract attributes for use in the template. |
| 66 |
extract( $atts ); |
| 67 |
|
| 68 |
ob_start(); |
| 69 |
require( $template_path ); |
| 70 |
$template_content = ob_get_clean(); |
| 71 |
|
| 72 |
$this->get_logger()->notice( 'Template content successfully loaded and captured.', [ |
| 73 |
'plugin' => 'double-opt-in', |
| 74 |
'template_name' => $template_name, |
| 75 |
'content_length' => strlen($template_content), |
| 76 |
] ); |
| 77 |
|
| 78 |
return $template_content; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Renders the specified template with the given attributes. |
| 83 |
* |
| 84 |
* @param string $template_name The name of the template file. |
| 85 |
* @param array $atts The parameters sent to the template. |
| 86 |
*/ |
| 87 |
public function renderTemplate( $template_name, $atts = array() ) { |
| 88 |
$this->get_logger()->info( 'Rendering a template to the output.', [ |
| 89 |
'plugin' => 'double-opt-in', |
| 90 |
'template_name' => $template_name, |
| 91 |
] ); |
| 92 |
|
| 93 |
$template = $this->getTemplate( $template_name, $atts ); |
| 94 |
|
| 95 |
if ( empty( $template ) ) { |
| 96 |
$this->get_logger()->warning( 'Template content is empty. Nothing to render.', [ |
| 97 |
'plugin' => 'double-opt-in', |
| 98 |
'template_name' => $template_name, |
| 99 |
] ); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
echo $template; |
| 104 |
|
| 105 |
$this->get_logger()->notice( 'Template content successfully rendered.', [ |
| 106 |
'plugin' => 'double-opt-in', |
| 107 |
'template_name' => $template_name, |
| 108 |
'content_length' => strlen($template), |
| 109 |
] ); |
| 110 |
} |
| 111 |
|
| 112 |
} |