1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Undefined\Stash\ImageHelper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Check if Timber is activated |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
if (!class_exists('Timber')) { |
10
|
|
|
add_action('admin_notices', function () { |
11
|
|
|
echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url(admin_url('plugins.php#timber')) . '">' . esc_url(admin_url('plugins.php')) . '</a></p></div>'; |
12
|
|
|
}); |
13
|
|
|
|
14
|
|
|
return; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Timber |
19
|
|
|
*/ |
20
|
|
|
class TwigStashTheme extends TimberSite |
21
|
|
|
{ |
22
|
|
|
function __construct() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
add_filter('timber_context', array($this, 'add_to_context')); |
25
|
|
|
add_filter('get_twig', [$this, 'addToTwig']); |
26
|
|
|
|
27
|
|
|
parent::__construct(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function add_to_context($context) |
31
|
|
|
{ |
32
|
|
|
/* Menu */ |
33
|
|
|
$context['menu'] = new TimberMenu('primary_navigation'); |
34
|
|
|
|
35
|
|
|
/* Site info */ |
36
|
|
|
$context['site'] = $this; |
37
|
|
|
|
38
|
|
|
/* Boolean if is homepage */ |
39
|
|
|
$context['is_home'] = is_front_page() ? "true" : "false"; |
40
|
|
|
|
41
|
|
|
/* homepage url */ |
42
|
|
|
$context['home_url'] = get_home_url(); |
43
|
|
|
|
44
|
|
|
/* if multilang is enabled return current language */ |
45
|
|
|
if (function_exists("pll_current_language")) { |
46
|
|
|
$context['languages'] = pll_the_languages(['raw' => 1]); |
47
|
|
|
$context['current_lang'] = pll_current_language(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $context; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This is where you can add your own functions to twig |
55
|
|
|
* @param $twig |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function addToTwig($twig) |
59
|
|
|
{ |
60
|
|
|
$twig->addExtension(new Twig_Extension_StringLoader()); |
61
|
|
|
$twig->addGlobal('image', new ImageHelper()); |
62
|
|
|
|
63
|
|
|
if (function_exists('pll_register_string')) { |
64
|
|
|
$filter = new Twig_SimpleFilter("translate", function ($string) { |
65
|
|
|
pll_register_string($string, $string, "stash"); |
66
|
|
|
|
67
|
|
|
return pll__($string); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
$twig->addFilter($filter); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (function_exists('wpautop')) { |
74
|
|
|
$filter = new Twig_SimpleFilter("p", function ($object) { |
75
|
|
|
return wpautop($object); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
$twig->addFilter($filter); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $twig; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
new TwigStashTheme(); |
86
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.