-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
324 lines (297 loc) · 8.71 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
// ACF configuration
function my_acf_init()
{
acf_update_setting(
'google_api_key',
get_field('api_google_maps', 'option')
);
}
add_action('acf/init', 'my_acf_init');
// This page is used by multiple of the requires below, so let's just stick it here.
if (function_exists('acf_add_options_page')) {
acf_add_options_page([
'page_title' => 'Website Settings',
'menu_title' => 'Website Settings',
'menu_slug' => 'website-settings',
'capability' => 'edit_posts',
]);
}
require get_template_directory() . '/inc/about.php';
require get_template_directory() . '/inc/apis.php';
require get_template_directory() . '/inc/blocks.php';
require get_template_directory() . '/inc/error-page.php';
require get_template_directory() . '/inc/footer.php';
require get_template_directory() . '/inc/header.php';
require get_template_directory() . '/inc/image-copyright.php';
require get_template_directory() . '/inc/images.php';
require get_template_directory() . '/inc/location.php';
require get_template_directory() . '/inc/logo.php';
require get_template_directory() . '/inc/meet.php';
require get_template_directory() . '/inc/meet-archive.php';
require get_template_directory() . '/inc/meet-runners.php';
require get_template_directory() . '/inc/navigations.php';
require get_template_directory() . '/inc/page-groups.php';
require get_template_directory() . '/inc/phase-banner.php';
require get_template_directory() . '/inc/search.php';
require get_template_directory() . '/inc/signposts.php';
require get_template_directory() . '/inc/site-description.php';
require get_template_directory() . '/inc/stream-team.php';
/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
if (!class_exists('Timber')) {
add_action('admin_notices', function () {
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>';
});
add_filter('template_include', function ($template) {
return get_stylesheet_directory() . '/static/no-timber.html';
});
return;
}
/**
* Sets the directories (inside your theme) to find .twig files
*/
Timber::$dirname = ['views'];
/**
* By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
* No prob! Just set this value to true
*/
Timber::$autoescape = false;
/**
* We're going to configure our theme inside of a subclass of Timber\Site
* You can move this to its own file and include here via php's include("MySite.php")
*/
class Plimsoll extends Timber\Site
{
/** Add timber support. */
public function __construct()
{
add_action('after_setup_theme', [$this, 'theme_supports']);
add_filter('timber/loader/loader', function ($loader) {
$loader->addPath(__DIR__ . "/components", "components");
return $loader;
});
add_filter('timber/context', [$this, 'add_to_context']);
add_filter('timber/twig', [$this, 'add_to_twig']);
add_action('init', [$this, 'register_post_types']);
add_action('init', [$this, 'register_taxonomies']);
parent::__construct();
}
/** This is where you can register custom post types. */
public function register_post_types()
{
}
/** This is where you can register custom taxonomies. */
public function register_taxonomies()
{
}
/** This is where you add some context
*
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context($context)
{
// API keys
$context['api_google_maps'] = get_field('api_google_maps', 'option');
// Header
$context['primary_navigation'] = new Timber\Menu('primary', [
'depth' => 1,
]);
$context['logo_color_1'] = get_field('logo_color_1', 'option');
$context['logo_color_2'] = get_field('logo_color_2', 'option');
// Phase banner
$context['phase_banner_visible'] = get_field(
'phase_banner_visible',
'option'
);
$context['phase_banner_title'] = get_field(
'phase_banner_title',
'option'
);
$context['phase_banner_subtitle'] = get_field(
'phase_banner_subtitle',
'option'
);
$context['phase_banner_description'] = get_field(
'phase_banner_description',
'option'
);
// Notification banners
$context['banners'] = get_field('banners', 'option');
// Footer
$context['footer_navigation'] = new Timber\Menu('footer');
$context['footer_legal_copy'] = get_field(
'footer_legal_copy',
'option'
);
// Footer - Newsletter sign up
$context['newsletter_show'] =
get_field('footer_newsletter_visible', 'option') == 'true'
? true
: false;
$context['newsletter_heading'] = get_field(
'footer_newsletter_heading',
'option'
);
$context['newsletter_description'] = get_field(
'footer_newsletter_description',
'option'
);
$context['newsletter_placeholder'] = get_field(
'footer_newsletter_placeholder',
'option'
);
// Asset cachebusting
$context['cachebust_css_version'] = filemtime(
get_stylesheet_directory() . '/dist/css/all.css'
);
$context['cachebust_js_version'] = filemtime(
get_stylesheet_directory() . '/dist/js/all.js'
);
// Everything else
$context['site'] = $this;
return $context;
}
public function theme_supports()
{
/**
* Turn stuff on
*/
// Enabled auto-generated <title> tag
add_theme_support('title-tag');
// Enable post thumbnails
add_theme_support('post-thumbnails');
// Enable CMS-manageable navigation
add_theme_support('menus');
/**
* Turn stuff off
*/
// Disable WP emoji
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
// Disable admin bar
add_filter("show_admin_bar", "__return_false");
// Disable oEmbed support
add_filter('embed_oembed_discover', '__return_false');
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
if (function_exists('disable_embeds_rewrites')) {
add_filter('rewrite_rules_array', 'disable_embeds_rewrites');
}
// Disable WP blocks CSS
add_action(
'wp_enqueue_scripts',
function () {
wp_dequeue_style('wp-block-library'); // WordPress core
wp_dequeue_style('wp-block-library-theme'); // WordPress core
},
100
);
// Disable taxonomy and users sitemaps introduced in WordPress 5.5
add_filter('wp_sitemaps_taxonomies', function ($taxonomies) {
unset($taxonomies['category']);
return $taxonomies;
});
add_filter(
'wp_sitemaps_add_provider',
function ($provider, $name) {
if ($name === 'users') {
return false;
}
return $provider;
},
10,
2
);
}
/**
* For some reason custom fields are janked, with some stored as strings
* and others as arrays. This is a temporary workaround For this issue.
*/
public function twig_normalise_custom_field_filter($obj)
{
if (is_array($obj)) {
return $obj[0];
}
return $obj;
}
/**
* Function for merging multiple arrays together (useful for setting
* default or overriding parameters).
*/
function twig_merge_function()
{
$get_args = func_get_args();
$return_array_data = [];
foreach ($get_args as $arg) {
$return_array_data = array_merge_recursive(
$return_array_data,
$arg
);
}
return $return_array_data;
}
/** Function for casting PHP objects to associative arrays, as Twig cannot
* iterate over objects. HT: https://stackoverflow.com/a/17306423
*/
function twig_cast_to_array($stdClassObject)
{
return (array) $stdClassObject;
}
/**
* See if a named cookie has been set and, if so, what the value is.
*/
function twig_cookie_value_function($cookieName)
{
if (isset($_COOKIE[$cookieName])) {
return $_COOKIE[$cookieName];
} else {
return false;
}
}
/**
* Real basic function to turn a string into a WordPress-like URL slug
*/
function twig_slug_filter($string)
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
}
/** This is where you can add your own functions to twig.
*
* @param string $twig get extension.
*/
public function add_to_twig($twig)
{
$twig->addExtension(new Twig\Extension\StringLoaderExtension());
$twig->addFilter(
new Twig\TwigFilter('normaliseCustomField', [
$this,
'twig_normalise_custom_field_filter',
])
);
$twig->addFilter(
new Twig\TwigFilter('castToArray', [$this, 'twig_cast_to_array'])
);
$twig->addFilter(
new Twig\TwigFilter('slug', [$this, 'twig_slug_filter'])
);
$twig->addFunction(
new Timber\Twig_Function('merge', [$this, 'twig_merge_function'])
);
$twig->addFunction(
new Timber\Twig_Function('getCookie', [
$this,
'twig_cookie_value_function',
])
);
return $twig;
}
}
new Plimsoll();