| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles locating templates from either the theme or plugin, |
| 7 |
* injecting and extracting data and rendering them. |
| 8 |
* |
| 9 |
* Class View |
| 10 |
* @package Codelight\GDPR |
| 11 |
*/ |
| 12 |
class View |
| 13 |
{ |
| 14 |
var $dirs; |
| 15 |
|
| 16 |
/** |
| 17 |
* View constructor. |
| 18 |
*/ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->dirs = $this->getTemplateDirectories(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Render a given template. |
| 26 |
* |
| 27 |
* @param $template |
| 28 |
* @param array $data |
| 29 |
* @param null $templateDir |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public function render($template, $data = [], $templateDir = null) |
| 34 |
{ |
| 35 |
if (is_null($templateDir)) { |
| 36 |
foreach ($this->dirs as $dir) { |
| 37 |
if (file_exists($dir . $template . '.php')) { |
| 38 |
$templateDir = $dir; |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
extract($data); |
| 45 |
ob_start(); |
| 46 |
require $templateDir . $template . '.php'; |
| 47 |
|
| 48 |
return ob_get_clean(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get valid template directories and pass them through a filter |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
protected function getTemplateDirectories() |
| 57 |
{ |
| 58 |
global $gdpr; |
| 59 |
|
| 60 |
$directories = array_filter([ |
| 61 |
get_stylesheet_directory() . '/gdpr-framework/', |
| 62 |
get_template_directory() . '/gdpr-framework/', |
| 63 |
$gdpr->PluginTemplatePath, |
| 64 |
], 'is_dir'); |
| 65 |
|
| 66 |
return array_unique(apply_filters('gdpr/views', $directories)); |
| 67 |
} |
| 68 |
} |
| 69 |
|