| @@ -1,22 +1,38 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace forge12\contactform7\CF7DoubleOptIn; |
| 3 | 4 | |
| 4 | -if(!defined('ABSPATH')){ | |
| 5 | +use Forge12\Shared\Logger; | |
| 6 | +use Forge12\Shared\LoggerInterface; | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | 9 | exit; |
| 6 | 10 | } |
| 7 | 11 | |
| 8 | -class TemplateHandler{ | |
| 12 | +class TemplateHandler { | |
| 9 | 13 | private static ?TemplateHandler $_instance = null; |
| 14 | + private LoggerInterface $logger; | |
| 10 | 15 | |
| 11 | 16 | public static function getInstance() { |
| 12 | 17 | if ( self::$_instance == null ) { |
| 13 | - self::$_instance = new self(); | |
| 18 | + self::$_instance = new self( Logger::getInstance() ); | |
| 14 | 19 | } |
| 15 | 20 | |
| 16 | 21 | return self::$_instance; |
| 17 | 22 | } |
| 18 | 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 | + | |
| 19 | 35 | /** |
| 20 | 36 | * Get the content of the specified template with the given attributes. |
| 21 | 37 | * |
| 22 | 38 | * @param string $template_name The name of the template file. |
| @@ -24,20 +40,43 @@ | ||
| 24 | 40 | * |
| 25 | 41 | * @return string The content of the template. |
| 26 | 42 | */ |
| 27 | 43 | public function getTemplate( $template_name, $atts = array() ) { |
| 28 | - $template_path = plugin_dir_path( dirname(__FILE__) ) . '/templates/' . $template_name . '.php'; | |
| 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 | + ] ); | |
| 29 | 49 | |
| 50 | + $template_path = plugin_dir_path( dirname( __FILE__ ) ) . 'templates/' . $template_name . '.php'; | |
| 51 | + | |
| 30 | 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 | + ] ); | |
| 31 | 58 | return ''; |
| 32 | 59 | } |
| 33 | 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. | |
| 34 | 66 | extract( $atts ); |
| 35 | 67 | |
| 36 | 68 | ob_start(); |
| 37 | 69 | require( $template_path ); |
| 70 | + $template_content = ob_get_clean(); | |
| 38 | 71 | |
| 39 | - return ob_get_clean(); | |
| 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; | |
| 40 | 79 | } |
| 41 | 80 | |
| 42 | 81 | /** |
| 43 | 82 | * Renders the specified template with the given attributes. |
| @@ -45,14 +84,29 @@ | ||
| 45 | 84 | * @param string $template_name The name of the template file. |
| 46 | 85 | * @param array $atts The parameters sent to the template. |
| 47 | 86 | */ |
| 48 | 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 | + | |
| 49 | 93 | $template = $this->getTemplate( $template_name, $atts ); |
| 50 | 94 | |
| 51 | 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 | + ] ); | |
| 52 | 100 | return; |
| 53 | 101 | } |
| 54 | 102 | |
| 55 | 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 | + ] ); | |
| 56 | 110 | } |
| 57 | 111 | |
| 58 | 112 | } |