| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template Trait |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Traits |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Traits; |
| 10 |
|
| 11 |
use EasyInvoice\Controllers\SettingsController; |
| 12 |
|
| 13 |
/** |
| 14 |
* TemplateTrait contains methods for template handling |
| 15 |
*/ |
| 16 |
trait TemplateTrait { |
| 17 |
/** |
| 18 |
* Get common template variables |
| 19 |
* |
| 20 |
* @return array Common template variables |
| 21 |
*/ |
| 22 |
protected function getCommonTemplateVars() { |
| 23 |
$settings_controller = new SettingsController(); |
| 24 |
$settings = $settings_controller->getSettings(); |
| 25 |
|
| 26 |
// Ensure settings is an array and has default values |
| 27 |
if (!is_array($settings)) { |
| 28 |
$settings = []; |
| 29 |
} |
| 30 |
|
| 31 |
return [ |
| 32 |
'company' => $settings['company'] ?? '', |
| 33 |
'currency_symbol' => isset($settings['currency']['symbol']) ? $settings['currency']['symbol'] : '$', |
| 34 |
'currency_position' => isset($settings['currency']['position']) ? $settings['currency']['position'] : 'left', |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Display a template with given variables |
| 40 |
* |
| 41 |
* @param string $template_path Path to the template file |
| 42 |
* @param array $vars Variables to extract for the template |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
protected function displayTemplate($template_path, $vars = []) { |
| 46 |
// Add common template variables if not already provided |
| 47 |
if (!isset($vars['template_vars'])) { |
| 48 |
$vars['template_vars'] = $this->getCommonTemplateVars(); |
| 49 |
} |
| 50 |
|
| 51 |
// Extract variables for use in the template |
| 52 |
extract($vars); |
| 53 |
|
| 54 |
// Include the template file |
| 55 |
if (file_exists($template_path)) { |
| 56 |
include $template_path; |
| 57 |
} else { |
| 58 |
/* translators: %s: file path. */ |
| 59 |
wp_die(esc_html(sprintf(__('Template file not found: %s', 'easy-invoice'), $template_path))); |
| 60 |
} |
| 61 |
} |
| 62 |
} |