1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Features\TimberLoader; |
4
|
|
|
|
5
|
|
|
use Flynt\Utils\TwigExtensionFlynt; |
6
|
|
|
use Flynt; |
7
|
|
|
use Timber\Image; |
8
|
|
|
use Timber\Post; |
9
|
|
|
use Timber\Timber; |
10
|
|
|
use Twig_SimpleFunction; |
11
|
|
|
|
12
|
|
|
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); |
13
|
|
|
|
14
|
|
|
// Convert ACF Images to Timber Images |
15
|
|
|
add_filter('acf/format_value/type=image', NS . 'formatImage', 100); |
16
|
|
|
|
17
|
|
|
// Convert ACF Gallery Images to Timber Images |
18
|
|
|
add_filter('acf/format_value/type=gallery', NS . 'formatGallery', 100); |
19
|
|
|
|
20
|
|
|
// Convert ACF Field of type post_object to a Timber\Post and add all ACF Fields of that Post |
21
|
|
|
add_filter('acf/format_value/type=post_object', NS . 'formatPostObject', 100); |
22
|
|
|
|
23
|
|
|
// Convert ACF Field of type relationship to a Timber\Post and add all ACF Fields of that Post |
24
|
|
|
add_filter('acf/format_value/type=relationship', NS . 'formatPostObject', 100); |
25
|
|
|
add_filter('get_twig', function ($twig) { |
26
|
|
|
$twig->addExtension(new TwigExtensionFlynt()); |
27
|
|
|
return $twig; |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
function formatImage($value) |
31
|
|
|
{ |
32
|
|
|
if (!empty($value)) { |
33
|
|
|
$value = new Image($value); |
34
|
|
|
} |
35
|
|
|
return $value; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function formatGallery($value) |
39
|
|
|
{ |
40
|
|
|
if (!empty($value)) { |
41
|
|
|
$value = array_map(function ($image) { |
42
|
|
|
return new Image($image); |
43
|
|
|
}, $value); |
44
|
|
|
} |
45
|
|
|
return $value; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function formatPostObject($value) |
49
|
|
|
{ |
50
|
|
|
if (is_array($value)) { |
51
|
|
|
$value = array_map(NS . 'convertToTimberPost', $value); |
52
|
|
|
} else { |
53
|
|
|
$value = convertToTimberPost($value); |
54
|
|
|
} |
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function convertToTimberPost($value) |
59
|
|
|
{ |
60
|
|
|
if (!empty($value) && is_object($value) && get_class($value) === 'WP_Post') { |
61
|
|
|
$value = new Post($value); |
62
|
|
|
} |
63
|
|
|
return $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
add_action('timber/twig/filters', function ($twig) { |
67
|
|
|
$twig->addFunction(new \Twig_SimpleFunction('placeholderImage', function ($width, $height, $color = null) { |
68
|
|
|
$width = round($width); |
69
|
|
|
$height = round($height); |
70
|
|
|
$colorRect = $color ? "<rect width='{$width}' height='{$height}' style='fill:$color' />" : ''; |
71
|
|
|
$svg = "<svg width='{$width}' height='{$height}' xmlns='http://www.w3.org/2000/svg'>{$colorRect}</svg>"; |
72
|
|
|
return "data:image/svg+xml;base64," . base64_encode($svg); |
73
|
|
|
})); |
74
|
|
|
|
75
|
|
|
return $twig; |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
function generateToken($args) |
79
|
|
|
{ |
80
|
|
|
return crypt(serialize($args), NONCE_SALT); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
add_action('timber/twig/filters', function ($twig) { |
84
|
|
|
$twig->addFilter(new \Twig_SimpleFilter('resizeDynamic', function ($src, $w, $h = 0, $crop = 'default', $force = false) { |
85
|
|
|
$arguments = [ |
86
|
|
|
'src' => $src, |
87
|
|
|
'w' => $w, |
88
|
|
|
'h' => $h, |
89
|
|
|
'crop' => $crop, |
90
|
|
|
'force' => $force, |
91
|
|
|
]; |
92
|
|
|
$arguments['token'] = generateToken($arguments); |
93
|
|
|
return add_query_arg($arguments, home_url('dynamic-images')); |
94
|
|
|
})); |
95
|
|
|
return $twig; |
96
|
|
|
}); |
97
|
|
|
|