PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 5.8.0
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v5.8.0
5.8.0 5.8.1 5.7.0 5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 All 41 releases
← All changes | core/TemplateHandler.class.php +115 -111 5.3.2 → 5.8.0 View file →
@@ -1,112 +1,116 @@
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 -
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 +
112 116 }