Completed
Push — constructionplanless ( cb84a3...5132cc )
by Dominik
01:29
created

functions.php ➔ renderTwigIndex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 4
dl 0
loc 42
rs 9.248
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A functions.php ➔ formatImage() 0 7 2
A functions.php ➔ formatGallery() 0 9 2
A functions.php ➔ formatPostObject() 0 9 2
A functions.php ➔ convertToTimberPost() 0 7 4
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
add_filter('get_twig', function ($twig) {
24
    $twig->addExtension(new TwigExtensionFlynt());
25
    return $twig;
26
});
27
28
function formatImage($value)
29
{
30
    if (!empty($value)) {
31
        $value = new Image($value);
32
    }
33
    return $value;
34
}
35
36
function formatGallery($value)
37
{
38
    if (!empty($value)) {
39
        $value = array_map(function ($image) {
40
            return new Image($image);
41
        }, $value);
42
    }
43
    return $value;
44
}
45
46
function formatPostObject($value)
47
{
48
    if (is_array($value)) {
49
        $value = array_map(NS . 'convertToTimberPost', $value);
50
    } else {
51
        $value = convertToTimberPost($value);
52
    }
53
    return $value;
54
}
55
56
function convertToTimberPost($value)
57
{
58
    if (!empty($value) && is_object($value) && get_class($value) === 'WP_Post') {
59
        $value = new Post($value);
60
    }
61
    return $value;
62
}
63
64
add_action('timber/twig/filters', function ($twig) {
65
    $twig->addFunction(new \Twig_SimpleFunction('transparentPng', function ($aspect, $r = 0, $g = 0, $b = 0, $a = 127) {
66
        $ratio = aspectToRatio($aspect);
67
        //create image with specified sizes
68
        $image = imagecreatetruecolor($ratio['width'], $ratio['height']);
69
        //saving all full alpha channel information
70
        imagesavealpha($image, true);
71
        //setting completely transparent color
72
        $transparent = imagecolorallocatealpha($image, $r, $g, $b, $a);
73
        //filling created image with transparent color
74
        imagefill($image, 0, 0, $transparent);
75
        ob_start(); // Let's start output buffering.
76
        imagepng($image); //This will normally output the image, but because of ob_start(), it won't.
77
        $contents = ob_get_contents(); //Instead, output above is saved to $contents
78
        ob_end_clean(); //End the output buffer.
79
80
        return "data:image/png;base64," . base64_encode($contents);
81
    }));
82
83
    return $twig;
84
});
85
86
function aspectToRatio($n, $tolerance = 1.e-3)
87
{
88
    $h1 = 1;
89
    $h2 = 0;
90
    $k1 = 0;
91
    $k2 = 1;
92
    $b = 1 / $n;
93
    do {
94
        $b = 1/$b;
95
        $a = floor($b);
96
        $aux = $h1;
97
        $h1 = $a * $h1 + $h2;
98
        $h2 = $aux;
99
        $aux = $k1;
100
        $k1 = $a * $k1 + $k2;
101
        $k2 = $aux;
102
        $b = $b - $a;
103
    } while (abs($n - $h1 / $k1) > $n * $tolerance);
104
105
    return [
106
        'width' => $h1,
107
        'height' => $k1
108
    ];
109
}
110
111
function generateToken($args)
112
{
113
    return crypt(serialize($args), NONCE_SALT);
114
}
115
116
add_action('timber/twig/filters', function ($twig) {
117
    $twig->addFilter(new \Twig_SimpleFilter('resizeDynamic', function ($src, $w, $h = 0, $crop = 'default', $force = false) {
118
        $arguments = [
119
            'src' => $src,
120
            'w' => $w,
121
            'h' => $h,
122
            'crop' => $crop,
123
            'force' => $force,
124
        ];
125
        $arguments['token'] = generateToken($arguments);
126
        return add_query_arg($arguments, home_url('dynamic-images'));
127
    }));
128
    return $twig;
129
});
130