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
|
|
|
|
104
|
|
|
add_action('timber/twig/filters', function ($twig) { |
105
|
|
|
$twig->addFunction(new \Twig_SimpleFunction('transparentPng', function ($aspect, $r = 0, $g = 0, $b = 0, $a = 127) { |
106
|
|
|
$ratio = aspectToRatio($aspect); |
107
|
|
|
//create image with specified sizes |
108
|
|
|
$image = imagecreatetruecolor($ratio['width'], $ratio['height']); |
109
|
|
|
//saving all full alpha channel information |
110
|
|
|
imagesavealpha($image, true); |
111
|
|
|
//setting completely transparent color |
112
|
|
|
$transparent = imagecolorallocatealpha($image, $r, $g, $b, $a); |
113
|
|
|
//filling created image with transparent color |
114
|
|
|
imagefill($image, 0, 0, $transparent); |
115
|
|
|
ob_start(); // Let's start output buffering. |
116
|
|
|
imagepng($image); //This will normally output the image, but because of ob_start(), it won't. |
117
|
|
|
$contents = ob_get_contents(); //Instead, output above is saved to $contents |
118
|
|
|
ob_end_clean(); //End the output buffer. |
119
|
|
|
|
120
|
|
|
return "data:image/png;base64," . base64_encode($contents); |
121
|
|
|
})); |
122
|
|
|
|
123
|
|
|
return $twig; |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
function aspectToRatio($n, $tolerance = 1.e-6) |
127
|
|
|
{ |
128
|
|
|
$h1 = 1; |
129
|
|
|
$h2 = 0; |
130
|
|
|
$k1 = 0; |
131
|
|
|
$k2 = 1; |
132
|
|
|
$b = 1 / $n; |
133
|
|
|
do { |
134
|
|
|
$b = 1/$b; |
135
|
|
|
$a = floor($b); |
136
|
|
|
$aux = $h1; |
137
|
|
|
$h1 = $a * $h1 + $h2; |
138
|
|
|
$h2 = $aux; |
139
|
|
|
$aux = $k1; |
140
|
|
|
$k1 = $a * $k1 + $k2; |
141
|
|
|
$k2 = $aux; |
142
|
|
|
$b = $b - $a; |
143
|
|
|
} while (abs($n - $h1 / $k1) > $n * $tolerance); |
144
|
|
|
|
145
|
|
|
return [ |
146
|
|
|
'width' => $h1, |
147
|
|
|
'height' => $k1 |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|