1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Features\TimberLoader; |
4
|
|
|
|
5
|
|
|
use Flynt; |
6
|
|
|
use Timber\Image; |
7
|
|
|
use Timber\Post; |
8
|
|
|
use Timber\Timber; |
9
|
|
|
use Twig_SimpleFunction; |
10
|
|
|
|
11
|
|
|
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); |
12
|
|
|
|
13
|
|
|
// Render Component with Timber (Twig) |
14
|
|
|
add_filter('Flynt/renderComponent', NS . 'renderTwigIndex', 10, 4); |
15
|
|
|
|
16
|
|
|
// Convert ACF Images to Timber Images |
17
|
|
|
add_filter('acf/format_value/type=image', NS . 'formatImage', 100); |
18
|
|
|
|
19
|
|
|
// Convert ACF Gallery Images to Timber Images |
20
|
|
|
add_filter('acf/format_value/type=gallery', NS . 'formatGallery', 100); |
21
|
|
|
|
22
|
|
|
// Convert ACF Field of type post_object to a Timber\Post and add all ACF Fields of that Post |
23
|
|
|
add_filter('acf/format_value/type=post_object', NS . 'formatPostObject', 100); |
24
|
|
|
|
25
|
|
|
function renderTwigIndex($output, $componentName, $componentData, $areaHtml) |
26
|
|
|
{ |
27
|
|
|
// get index file |
28
|
|
|
$componentManager = Flynt\ComponentManager::getInstance(); |
29
|
|
|
$templateFilename = apply_filters('Flynt/Features/TimberLoader/templateFilename', 'index.twig'); |
30
|
|
|
$templateFilename = apply_filters("Flynt/Features/TimberLoader/templateFilename?name=${componentName}", $templateFilename); |
31
|
|
|
$filePath = $componentManager->getComponentFilePath($componentName, $templateFilename); |
32
|
|
|
$relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/'); |
33
|
|
|
|
34
|
|
|
if (!is_file($filePath)) { |
35
|
|
|
trigger_error("Template not found: {$filePath}", E_USER_WARNING); |
36
|
|
|
return $output; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$addArea = function ($twig) use ($areaHtml) { |
40
|
|
|
|
41
|
|
|
$twig->addFunction(new Twig_SimpleFunction('area', function ($areaName) use ($areaHtml) { |
42
|
|
|
if (array_key_exists($areaName, $areaHtml)) { |
43
|
|
|
return $areaHtml[$areaName]; |
44
|
|
|
} |
45
|
|
|
})); |
46
|
|
|
|
47
|
|
|
return $twig; |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
add_filter('get_twig', $addArea); |
51
|
|
|
|
52
|
|
|
$returnTimberPaths = function ($paths) use ($filePath) { |
53
|
|
|
array_unshift($paths, dirname($filePath)); |
54
|
|
|
return $paths; |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
add_filter('timber/loader/paths', $returnTimberPaths); |
58
|
|
|
|
59
|
|
|
$output = Timber::fetch($relativeFilePath, $componentData); |
60
|
|
|
|
61
|
|
|
remove_filter('timber/loader/paths', $returnTimberPaths); |
62
|
|
|
|
63
|
|
|
remove_filter('get_twig', $addArea); |
64
|
|
|
|
65
|
|
|
return $output; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function formatImage($value) |
69
|
|
|
{ |
70
|
|
|
if (!empty($value)) { |
71
|
|
|
$value = new Image($value); |
72
|
|
|
} |
73
|
|
|
return $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
function formatGallery($value) |
77
|
|
|
{ |
78
|
|
|
if (!empty($value)) { |
79
|
|
|
$value = array_map(function ($image) { |
80
|
|
|
return new Image($image); |
81
|
|
|
}, $value); |
82
|
|
|
} |
83
|
|
|
return $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function formatPostObject($value) |
87
|
|
|
{ |
88
|
|
|
if (is_array($value)) { |
89
|
|
|
$value = array_map(NS . 'convertToTimberPost', $value); |
90
|
|
|
} else { |
91
|
|
|
$value = convertToTimberPost($value); |
92
|
|
|
} |
93
|
|
|
return $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function convertToTimberPost($value) |
97
|
|
|
{ |
98
|
|
|
if (!empty($value) && is_object($value) && get_class($value) === 'WP_Post') { |
99
|
|
|
$value = new Post($value); |
100
|
|
|
} |
101
|
|
|
return $value; |
102
|
|
|
} |
103
|
|
|
|