This repository was archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
157 lines (129 loc) · 3.88 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
if (!class_exists('Timber')) {
add_action('admin_notices', function() {
include get_stylesheet_directory() . '/src/messages/no-timber-admin.php';
});
add_filter('template_include', function($template) {
return get_stylesheet_directory() . '/static/no-timber.html';
});
return;
}
/*
Disable some WPML clutter
*/
define('ICL_DONT_LOAD_NAVIGATION_CSS', true);
define('ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS', true);
define('ICL_DONT_LOAD_LANGUAGES_JS', true);
use Timber\Site;
use Timber\Menu;
use Timber\Twig_Function;
require get_template_directory() . '/src/AdminHelper.php';
require get_template_directory() . '/src/AssetHelper.php';
require get_template_directory() . '/src/ImageHelper.php';
require get_template_directory() . '/src/ThemeHelper.php';
require get_template_directory() . '/src/ACFHelper.php';
/* The folder(s) containing Twig templates. */
Timber::$dirname = array('templates');
/*
Twig template cache, commented out for now
because it caches things weirdly
for logged-in users in the Dashboard
when WP_DEBUG is off. Must investigate.
*/
// Timber::$cache = defined('WP_DEBUG') ? WP_DEBUG === false : false;
class LatheSite extends Site {
function __construct() {
add_action('after_setup_theme', function() {
AssetHelper::init('/build/manifest.json');
ThemeHelper::init();
AdminHelper::init();
/* The set of image sizes used for the `size()` Twig filter */
ImageHelper::init(array(
'thumbnail' => [800, 600],
'full' => [1920, 1280, 'center']
));
ACFHelper::init();
});
/* Custom menu locations */
$menus = array(
'main-menu' => __('Main Menu', 'lathe'),
'footer-menu' => __('Footer Menu', 'lathe'),
'social-menu' => __('Social Menu', 'lathe')
);
/* Register menu locations */
add_action('init', function() use ($menus) {
register_nav_menus($menus);
});
/*
Add things to the Timber context
--------------------------------
These values can be read directly
in all Twig templates, e.g.:
{% if theme_mods.is_coming_soon_enabled %}
Website coming soon!
{% endif %}
Note that more useful things get added
to the Timber context depending on the
type of the currently displayed page
in the `src/context/` PHP files.
*/
add_filter('timber/context', function($context) use ($menus) {
/*
Site Menus
----------
*/
$context['menus'] = [];
foreach(array_keys($menus) as $key) {
$context['menus'][$key] = new Menu($key);
}
/* Make the main menu accessible under `menu` directly */
$context['menu'] = $context['menus']['main-menu'];
/*
Theme Mods
----------
*/
$context['theme_mods'] = get_theme_mods();
/*
WPML languages
--------------
*/
if (function_exists('icl_get_languages')) {
/* All languages */
$context['languages'] = icl_get_languages('skip_missing=0');
/* Current language */
if (defined('ICL_LANGUAGE_CODE')) {
$context['language'] = strtolower(ICL_LANGUAGE_CODE);
}
}
return $context;
});
/*
Add Twig functions and filters
------------------------------
*/
add_filter('timber/twig', function($twig) {
/* Twig Functions */
$twig->addFunction(new Twig_Function('asset', array($this, 'asset')));
/* Twig Filters */
// TODO: This will need to be changed to Timber\Twig_Filter soon.
$twig->addFilter(new Twig_SimpleFilter('size', array($this, 'size')));
$twig->addFilter(new Twig_SimpleFilter('asset', array($this, 'asset')));
$twig->addFilter(new Twig_SimpleFilter('hostname', function($url) {
return preg_replace(
'/[^\da-z]+/i',
'-',
str_ireplace('www.', '', parse_url($url, PHP_URL_HOST))
);
}));
return $twig;
});
parent::__construct();
}
function asset($handle, $enqueue = false) {
return AssetHelper::asset($handle, $enqueue);
}
function size($src, $size) {
return ImageHelper::size($src, $size);
}
}
new LatheSite();