Completed
Push — timber-loader-update ( a5fa4a )
by Doğa
02:25 queued 11s
created

functions.php ➔ formatImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 10
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
function renderTwigIndex($output, $componentName, $componentData, $areaHtml) {
26
    // get index file
27
    $componentManager = Flynt\ComponentManager::getInstance();
28
    $templateFilename = apply_filters('Flynt/Features/TimberLoader/templateFilename', 'index.twig');
29
    $templateFilename = apply_filters("Flynt/Features/TimberLoader/templateFilename?name=${componentName}", $templateFilename);
30
    $filePath = $componentManager->getComponentFilePath($componentName, $templateFilename);
31
32
    if (!is_file($filePath)) {
33
        trigger_error("Template not found: {$filePath}", E_USER_WARNING);
34
        return $output;
35
    }
36
37
    $addArea = function ($twig) use ($areaHtml) {
38
39
        $twig->addFunction(new Twig_SimpleFunction('area', function ($areaName) use ($areaHtml) {
40
            if (array_key_exists($areaName, $areaHtml)) {
41
                return $areaHtml[$areaName];
42
            }
43
        }));
44
45
        return $twig;
46
    };
47
48
    add_filter('get_twig', $addArea);
49
50
    $returnTimberPaths = function ($paths) use ($filePath) {
51
        array_unshift($paths, dirname($filePath));
52
        return $paths;
53
    };
54
55
    add_filter('timber/loader/paths', $returnTimberPaths);
56
57
    $output = Timber::fetch($filePath, $componentData);
58
59
    remove_filter('timber/loader/paths', $returnTimberPaths);
60
61
    remove_filter('get_twig', $addArea);
62
63
    return $output;
64
}
65
66
function formatImage($value) {
67
    if (!empty($value)) {
68
        $value = new Image($value);
69
    }
70
    return $value;
71
}
72
73
function formatGallery($value) {
74
    if (!empty($value)) {
75
        $value = array_map(function ($image) {
76
            return new Image($image);
77
        }, $value);
78
    }
79
    return $value;
80
}
81
82
function formatPostObject($value) {
83
    if (is_array($value)) {
84
        $value = array_map(NS . 'convertToTimberPost', $value);
85
    } else {
86
        $value = convertToTimberPost($value);
87
    }
88
    return $value;
89
}
90
91
function convertToTimberPost($value)
92
{
93
    if (!empty($value) && is_object($value) && get_class($value) === 'WP_Post') {
94
        $value = new Post($value);
95
    }
96
    return $value;
97
}
98