Completed
Pull Request — develop (#279)
by Doğa
06:42 queued 04:29
created

functions.php ➔ formatImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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