| 1 |
<?php |
| 2 |
namespace forge12\contactform7\CF7DoubleOptIn; |
| 3 |
|
| 4 |
if(!defined('ABSPATH')){ |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class TemplateHandler{ |
| 9 |
private static ?TemplateHandler $_instance = null; |
| 10 |
|
| 11 |
public static function getInstance() { |
| 12 |
if ( self::$_instance == null ) { |
| 13 |
self::$_instance = new self(); |
| 14 |
} |
| 15 |
|
| 16 |
return self::$_instance; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Get the content of the specified template with the given attributes. |
| 21 |
* |
| 22 |
* @param string $template_name The name of the template file. |
| 23 |
* @param array $atts The parameters sent to the template. |
| 24 |
* |
| 25 |
* @return string The content of the template. |
| 26 |
*/ |
| 27 |
public function getTemplate( $template_name, $atts = array() ) { |
| 28 |
$template_path = plugin_dir_path( dirname(__FILE__) ) . '/templates/' . $template_name . '.php'; |
| 29 |
|
| 30 |
if ( ! is_readable( $template_path ) ) { |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
extract( $atts ); |
| 35 |
|
| 36 |
ob_start(); |
| 37 |
require( $template_path ); |
| 38 |
|
| 39 |
return ob_get_clean(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Renders the specified template with the given attributes. |
| 44 |
* |
| 45 |
* @param string $template_name The name of the template file. |
| 46 |
* @param array $atts The parameters sent to the template. |
| 47 |
*/ |
| 48 |
public function renderTemplate( $template_name, $atts = array() ) { |
| 49 |
$template = $this->getTemplate( $template_name, $atts ); |
| 50 |
|
| 51 |
if ( empty( $template ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
echo $template; |
| 56 |
} |
| 57 |
|
| 58 |
} |