1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt; |
4
|
|
|
|
5
|
|
|
use Flynt\ComponentManager; |
6
|
|
|
use Dflydev\DotAccessData\Data; |
7
|
|
|
|
8
|
|
|
class Api |
9
|
|
|
{ |
10
|
|
|
public static function registerComponent($componentName, $componentPath = null) |
11
|
|
|
{ |
12
|
|
|
$componentManager = ComponentManager::getInstance(); |
13
|
|
|
$componentManager->registerComponent($componentName, $componentPath); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function registerComponentsFromPath($componentBasePath) |
17
|
|
|
{ |
18
|
|
|
foreach (glob("{$componentBasePath}/*", GLOB_ONLYDIR) as $componentPath) { |
19
|
|
|
$componentName = basename($componentPath); |
20
|
|
|
self::registerComponent($componentName, $componentPath); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function renderComponent($componentName, $data) |
25
|
|
|
{ |
26
|
|
|
$data = apply_filters( |
|
|
|
|
27
|
|
|
'Flynt/addComponentData', |
28
|
|
|
$data, |
29
|
|
|
$componentName |
30
|
|
|
); |
31
|
|
|
$output = apply_filters( |
32
|
|
|
'Flynt/renderComponent', |
33
|
|
|
null, |
34
|
|
|
$componentName, |
35
|
|
|
$data |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
return is_null($output) ? '' : $output; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function registerFields($scope, $fields, $fieldsId = null) |
42
|
|
|
{ |
43
|
|
|
global $flyntFields; |
44
|
|
|
$flyntFields = $flyntFields ?? []; |
45
|
|
|
if (empty($fieldsId)) { |
46
|
|
|
$flyntFields[$scope] = $fields; |
47
|
|
|
} else { |
48
|
|
|
$flyntFields[$scope] = $flyntFields[$scope] ?? []; |
49
|
|
|
$flyntFields[$scope][$fieldsId] = $fields; |
50
|
|
|
} |
51
|
|
|
return $fields; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function loadFields($scope, $fieldPath = null) |
55
|
|
|
{ |
56
|
|
|
global $flyntFields; |
57
|
|
|
$flyntFields = $flyntFields ?? []; |
58
|
|
|
if (empty($fieldPath)) { |
59
|
|
|
return $flyntFields[$scope]; |
60
|
|
|
} else { |
61
|
|
|
$data = new Data($flyntFields[$scope]); |
62
|
|
|
return $data->get($fieldPath); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function registerHooks() |
67
|
|
|
{ |
68
|
|
|
add_filter('Flynt/renderComponent', function ($output, $componentName, $data) { |
|
|
|
|
69
|
|
|
return apply_filters( |
|
|
|
|
70
|
|
|
"Flynt/renderComponent?name={$componentName}", |
71
|
|
|
$output, |
72
|
|
|
$componentName, |
73
|
|
|
$data |
74
|
|
|
); |
75
|
|
|
}, 10, 3); |
76
|
|
|
|
77
|
|
|
add_filter('Flynt/addComponentData', function ($data, $componentName) { |
78
|
|
|
return apply_filters( |
|
|
|
|
79
|
|
|
"Flynt/addComponentData?name={$componentName}", |
80
|
|
|
$data, |
81
|
|
|
$componentName |
82
|
|
|
); |
83
|
|
|
}, 10, 2); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|