Completed
Push — componentlibrary ( c348c3...e8d0dd )
by
unknown
02:05
created

functions.php ➔ aspectToRatio()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 2
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
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
// Convert ACF Field of type relationship to a Timber\Post and add all ACF Fields of that Post
26
add_filter('acf/format_value/type=relationship', NS . 'formatPostObject', 100);
27
28
function renderTwigIndex($output, $componentName, $componentData, $areaHtml)
29
{
30
    // get index file
31
    $componentManager = Flynt\ComponentManager::getInstance();
32
    $templateFilename = apply_filters('Flynt/Features/TimberLoader/templateFilename', 'index.twig');
33
    $templateFilename = apply_filters("Flynt/Features/TimberLoader/templateFilename?name=${componentName}", $templateFilename);
34
    $filePath = $componentManager->getComponentFilePath($componentName, $templateFilename);
35
    $relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/');
36
37
    if (!is_file($filePath)) {
38
        trigger_error("Template not found: {$filePath}", E_USER_WARNING);
39
        return $output;
40
    }
41
42
    $addArea = function ($twig) use ($areaHtml) {
43
44
        $twig->addFunction(new Twig_SimpleFunction('area', function ($areaName) use ($areaHtml) {
45
            if (array_key_exists($areaName, $areaHtml)) {
46
                return $areaHtml[$areaName];
47
            }
48
        }));
49
50
        return $twig;
51
    };
52
53
    add_filter('get_twig', $addArea);
54
55
    $returnTimberPaths = function ($paths) use ($filePath) {
56
        array_unshift($paths, dirname($filePath));
57
        return $paths;
58
    };
59
60
    add_filter('timber/loader/paths', $returnTimberPaths);
61
62
    $output = Timber::fetch($relativeFilePath, $componentData);
63
64
    remove_filter('timber/loader/paths', $returnTimberPaths);
65
66
    remove_filter('get_twig', $addArea);
67
68
    return $output;
69
}
70
71
function formatImage($value)
72
{
73
    if (!empty($value)) {
74
        $value = new Image($value);
75
    }
76
    return $value;
77
}
78
79
function formatGallery($value)
80
{
81
    if (!empty($value)) {
82
        $value = array_map(function ($image) {
83
            return new Image($image);
84
        }, $value);
85
    }
86
    return $value;
87
}
88
89
function formatPostObject($value)
90
{
91
    if (is_array($value)) {
92
        $value = array_map(NS . 'convertToTimberPost', $value);
93
    } else {
94
        $value = convertToTimberPost($value);
95
    }
96
    return $value;
97
}
98
99
function convertToTimberPost($value)
100
{
101
    if (!empty($value) && is_object($value) && get_class($value) === 'WP_Post') {
102
        $value = new Post($value);
103
    }
104
    return $value;
105
}
106
107
add_action('timber/twig/filters', function ($twig) {
108
    $twig->addFunction(new \Twig_SimpleFunction('placeholderImage', function ($width, $height, $color = null) {
109
        $width = round($width);
110
        $height = round($height);
111
        $colorRect = $color ? "<rect width='{$width}' height='{$height}' style='fill:$color' />" : '';
112
        $svg = "<svg width='{$width}' height='{$height}' xmlns='http://www.w3.org/2000/svg'>{$colorRect}</svg>";
113
        return "data:image/svg+xml;base64," . base64_encode($svg);
114
    }));
115
116
    return $twig;
117
});
118
119
function generateToken($args)
120
{
121
    return crypt(serialize($args), NONCE_SALT);
122
}
123
124
add_action('timber/twig/filters', function ($twig) {
125
    $twig->addFilter(new \Twig_SimpleFilter('resizeDynamic', function ($src, $w, $h = 0, $crop = 'default', $force = false) {
126
        $arguments = [
127
            'src' => $src,
128
            'w' => $w,
129
            'h' => $h,
130
            'crop' => $crop,
131
            'force' => $force,
132
        ];
133
        $arguments['token'] = generateToken($arguments);
134
        return add_query_arg($arguments, home_url('dynamic-images'));
135
    }));
136
    return $twig;
137
});
138