|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flynt; |
|
4
|
|
|
|
|
5
|
|
|
use Flynt\ComponentManager; |
|
6
|
|
|
use Flynt\Helpers; |
|
7
|
|
|
|
|
8
|
|
|
class Defaults |
|
9
|
|
|
{ |
|
10
|
|
|
const CONFIG_DIR = 'config'; |
|
11
|
|
|
const COMPONENT_DIR = 'Components'; |
|
12
|
|
|
|
|
13
|
|
|
public static function init() |
|
14
|
|
|
{ |
|
15
|
|
|
add_filter('Flynt/configPath', ['Flynt\Defaults', 'setConfigPath'], 999, 2); |
|
16
|
|
|
add_filter('Flynt/configFileLoader', ['Flynt\Defaults', 'loadConfigFile'], 999, 3); |
|
17
|
|
|
add_filter('Flynt/renderComponent', ['Flynt\Defaults', 'renderComponent'], 999, 4); |
|
18
|
|
|
add_filter('Flynt/componentPath', ['Flynt\Defaults', 'setComponentPath'], 999, 2); |
|
19
|
|
|
add_action('Flynt/registerComponent', ['Flynt\Defaults', 'loadFunctionsFile']); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function setConfigPath($configPath, $configFileName) |
|
23
|
|
|
{ |
|
24
|
|
|
if (is_null($configPath)) { |
|
25
|
|
|
$configPath = get_template_directory() . '/' . self::CONFIG_DIR . '/' . $configFileName; |
|
26
|
|
|
} |
|
27
|
|
|
return $configPath; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function loadConfigFile($config, $configName, $configPath) |
|
31
|
|
|
{ |
|
32
|
|
|
if (is_null($config)) { |
|
33
|
|
|
$config = json_decode(file_get_contents($configPath), true); |
|
34
|
|
|
} |
|
35
|
|
|
return $config; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function renderComponent($output, $componentName, $componentData, $areaHtml) |
|
39
|
|
|
{ |
|
40
|
|
|
if (is_null($output)) { |
|
41
|
|
|
$componentManager = ComponentManager::getInstance(); |
|
42
|
|
|
$filePath = $componentManager->getComponentFilePath($componentName); |
|
43
|
|
|
$output = self::renderFile($componentData, $areaHtml, $filePath); |
|
44
|
|
|
} |
|
45
|
|
|
return $output; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function setComponentPath($componentPath, $componentName) |
|
49
|
|
|
{ |
|
50
|
|
|
if (is_null($componentPath)) { |
|
51
|
|
|
$componentPath = self::getComponentsDirectory() . '/' . $componentName; |
|
52
|
|
|
} |
|
53
|
|
|
return $componentPath; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function getComponentsDirectory() |
|
57
|
|
|
{ |
|
58
|
|
|
return get_template_directory() . '/' . self::COMPONENT_DIR; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// this action needs to be removed by the user if they want to overwrite this functionality |
|
62
|
|
|
public static function loadFunctionsFile($componentName) |
|
63
|
|
|
{ |
|
64
|
|
|
$componentManager = ComponentManager::getInstance(); |
|
65
|
|
|
$filePath = $componentManager->getComponentFilePath($componentName, 'functions.php'); |
|
66
|
|
|
if (false !== $filePath) { |
|
67
|
|
|
require_once $filePath; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected static function renderFile($componentData, $areaHtml, $filePath) |
|
72
|
|
|
{ |
|
73
|
|
|
if (!is_file($filePath)) { |
|
74
|
|
|
trigger_error("Template not found: {$filePath}", E_USER_WARNING); |
|
75
|
|
|
return ''; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$area = function ($areaName) use ($areaHtml) { |
|
79
|
|
|
if (array_key_exists($areaName, $areaHtml)) { |
|
80
|
|
|
return $areaHtml[$areaName]; |
|
81
|
|
|
} |
|
82
|
|
|
}; |
|
83
|
|
|
|
|
84
|
|
|
$data = function () use ($componentData) { |
|
85
|
|
|
$args = func_get_args(); |
|
86
|
|
|
array_unshift($args, $componentData); |
|
87
|
|
|
return Helpers::extractNestedDataFromArray($args); |
|
88
|
|
|
}; |
|
89
|
|
|
|
|
90
|
|
|
ob_start(); |
|
91
|
|
|
require $filePath; |
|
92
|
|
|
$output = ob_get_contents(); |
|
93
|
|
|
ob_get_clean(); |
|
94
|
|
|
|
|
95
|
|
|
return $output; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|