1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Utils; |
4
|
|
|
|
5
|
|
|
use Flynt; |
6
|
|
|
use Twig_Environment; |
7
|
|
|
use Twig_Extension; |
8
|
|
|
use Twig_SimpleFunction; |
9
|
|
|
|
10
|
|
|
class TwigExtensionFlynt extends Twig_Extension { |
11
|
|
|
|
12
|
|
|
public function getName() { |
13
|
|
|
return 'twig_extension_flynt'; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function getFunctions() { |
17
|
|
|
return array( |
18
|
|
|
new Twig_SimpleFunction('renderComponent', [$this, 'renderComponent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))), |
19
|
|
|
new Twig_SimpleFunction('renderFlexibleContent', [$this, 'renderFlexibleContent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))), |
20
|
|
|
); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function renderFlexibleContent(Twig_Environment $env, $context, $fields, $withContext = true, $ignoreMissing = false, $sandboxed = false) |
24
|
|
|
{ |
25
|
|
|
$output = ''; |
26
|
|
|
foreach (($fields ?? []) as $field) { |
27
|
|
|
$output .= $this->renderComponent($env, $context, ucfirst($field['acf_fc_layout']), $field, $withContext, $ignoreMissing, $sandboxed); |
28
|
|
|
} |
29
|
|
|
return $output; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function renderComponent(Twig_Environment $env, $context, $componentName, $data = [], $withContext = true, $ignoreMissing = false, $sandboxed = false) |
33
|
|
|
{ |
34
|
|
|
$data = $this->getComponentData($data, $componentName); |
35
|
|
|
|
36
|
|
|
$componentManager = Flynt\ComponentManager::getInstance(); |
37
|
|
|
$templateFilename = apply_filters('Flynt/Features/TimberLoader/templateFilename', 'index.twig'); |
38
|
|
|
$templateFilename = apply_filters("Flynt/Features/TimberLoader/templateFilename?name=${componentName}", $templateFilename); |
39
|
|
|
$filePath = $componentManager->getComponentFilePath($componentName, $templateFilename); |
40
|
|
|
$relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/'); |
41
|
|
|
|
42
|
|
|
if (!is_file($filePath)) { |
43
|
|
|
trigger_error("Template not found: {$filePath}", E_USER_WARNING); |
44
|
|
|
return $output; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$loader = $env->getLoader(); |
48
|
|
|
|
49
|
|
|
$loaderPaths = $loader->getPaths(); |
50
|
|
|
|
51
|
|
|
$loader->addPath(dirname($filePath)); |
52
|
|
|
|
53
|
|
|
$output = twig_include($env, $context, $relativeFilePath, $data, $withContext, $ignoreMissing, $sandboxed); |
54
|
|
|
|
55
|
|
|
$loader->setPaths($loaderPaths); |
56
|
|
|
|
57
|
|
|
return $output; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getComponentData($data, $componentName) |
61
|
|
|
{ |
62
|
|
|
$data = apply_filters( |
63
|
|
|
'Flynt/addComponentData', |
64
|
|
|
$data, |
65
|
|
|
[], |
66
|
|
|
[ |
67
|
|
|
'name' => $componentName, |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
$data = apply_filters( |
71
|
|
|
"Flynt/addComponentData?name={$componentName}", |
72
|
|
|
$data, |
73
|
|
|
[], |
74
|
|
|
[ |
75
|
|
|
'name' => $componentName, |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $data; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.