-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·271 lines (241 loc) · 7.91 KB
/
functions.php
File metadata and controls
executable file
·271 lines (241 loc) · 7.91 KB
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
<?php
/**
* Mill3 WP Theme Boilerplate
* https://github.com/Mill3/mill3-wp-theme-boilerplate
*
* @package Mill3Wp
* @subpackage Timber
*/
require __DIR__ . '/vendor/autoload.php';
// Initialize Timber.
Timber\Timber::init();
//
// Define various constants
//
defined('THEME_ENV') or define('THEME_ENV', 'production');
defined('VITE_DEV_SERVER') or define('VITE_DEV_SERVER', THEME_ENV === 'development');
defined('SENTRY_DSN_PHP') or define('SENTRY_DSN_PHP', null);
defined('SENTRY_DSN_JS') or define('SENTRY_DSN_JS', null);
defined('SENTRY_ENV') or define('SENTRY_ENV', null);
//
// Init Sentry.io
//
if (defined('SENTRY_DSN_PHP') && \function_exists('\Sentry\init')) {
\Sentry\init([
'dsn' => SENTRY_DSN_PHP,
'environment' => SENTRY_ENV ? SENTRY_ENV : 'production',
'error_types' => E_ERROR & ~E_WARNING & ~E_NOTICE
]);
}
//
// Add theme WP CLI commands.
// note: must be invoked before Timber plugin installation status
//
if (defined('WP_CLI') && WP_CLI) {
require_once __DIR__ . '/lib/cli.php';
$commands = new \Mill3WP\Cli\Commands();
\WP_CLI::add_command('mill3wp', $commands);
}
//
// Handling when Advanced Custom Fields is not installed
//
if (!\class_exists('ACF')) {
// add notice in admin
add_action('admin_notices', function () {
echo '<div class="error"><p>Advanced Custom Field not activated. Make sure you activate the plugin in <a href="' .
esc_url(admin_url('plugins.php#advanced-custom-fields-pro')) .
'">' .
esc_url(admin_url('plugins.php')) .
'</a></p></div>';
});
// fallback method avoiding 500 error when get_field is called from Twig function() method
if (!is_admin() && !\function_exists('get_field')) {
function get_field()
{
return "ACF not installed.";
}
}
}
/**
* Sets the directories (inside your theme) to find .twig files
*/
Timber::$dirname = array('templates');
// include theme custom classes
$includes = [
'lib/acf.php',
'lib/actions.php',
'lib/admin-status.php',
'lib/assets.php',
'lib/cache.php',
'lib/class-walker-nav-menu-edit.php',
'lib/editor.php',
'lib/filters.php',
'lib/gdpr.php',
'lib/gravity-form.php',
'lib/gutenberg.php',
'lib/media-gallery.php',
'lib/menu.php',
'lib/newsletter.php',
'lib/polylang.php',
'lib/post-functions.php',
'lib/post-queries.php',
'lib/post-type.php',
'lib/rankmath.php',
'lib/recaptcha.php',
'lib/tracking.php',
//'lib/rive.php',
'lib/search.php',
'lib/security.php',
'lib/sentry.php',
'lib/setup.php',
'lib/shortcodes.php',
'lib/taxonomies.php',
'lib/taxonomy-queries.php',
'lib/utils.php',
'lib/windmill-prefetch.php',
//'lib/window-messenger.php',
// Timber
'lib/timber/timber-acf-wp-blocks.php',
'lib/timber/Mill3Timber.php',
// twig
'lib/twig/extra-timber-filters.php',
'lib/twig/extra-timber-functions.php',
'lib/twig/file-filters.php',
'lib/twig/sharing-filters.php',
'lib/twig/splitting.php',
'lib/twig/title-highlights.php',
'lib/twig/title-replacements.php',
// custom field type
'lib/acf-fields/media/index.php',
'lib/acf-fields/spacer/index.php',
'lib/acf-fields/row-title/index.php',
// model class per post-type
'lib/models/abstract-post.php',
'lib/models/image.php',
'lib/models/page-section.php',
'lib/models/post.php',
'lib/models/post-class-map.php'
];
foreach ($includes as $file) {
if (!$filepath = locate_template($file)) {
trigger_error(
sprintf(__('Error locating %s for inclusion'), $file),
E_USER_ERROR
);
}
include_once $filepath;
}
unset($file, $filepath);
/**
* 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 Mill3WP extends Timber\Site
{
/* Add timber support. */
public function __construct()
{
add_action('init', array($this, 'register_taxonomies'));
add_action('init', array($this, 'register_post_types'));
add_filter('timber/context', array($this, 'add_to_context'));
add_filter('timber/twig/filters', array($this, 'add_filters'));
add_filter('timber/twig/functions', array($this, 'add_functions'));
parent::__construct();
}
/**
* Override Timber\Site\link method :
* When Polylang is installed, returns home_url() core function instead of $this->url
*
* @return string
*/
public function link(): string
{
if( function_exists('pll_current_language') ) {
return home_url() . "/";
} else {
return $this->url;
}
}
/**
* Overide Timber\Site url() method
* This is for handling polylang home_url() method when its activated
*
* @return string
*/
public function url(): string
{
if (function_exists('pll_home_url')) {
return pll_home_url();
} else {
return $this->url;
}
}
/**
* This is where you can register custom post types.
*/
public function register_post_types(): void
{
Theme_CustomPostTypes::instance()->run();
}
/**
* This is where you can register custom taxonomies.
*/
public function register_taxonomies(): void
{
Theme_CustomTaxonomies::instance()->run();
}
/** This is where you add some context
*
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context($context)
{
$context['site'] = $this;
$context['site_navigation'] = Timber::get_menu('site_navigation');
$context['header_navigation'] = Timber::get_menu('header_navigation');
$context['footer_navigation'] = Timber::get_menu('footer_navigation');
$context['social_links'] = Timber::get_menu('social_links');
$context['THEME_ENV'] = THEME_ENV;
$context['VITE_DEV_SERVER'] = VITE_DEV_SERVER;
$context['SENTRY_DSN_PHP'] = SENTRY_DSN_PHP;
$context['SENTRY_DSN_JS'] = SENTRY_DSN_JS;
$context['SENTRY_ENV'] = SENTRY_ENV;
if (\class_exists('ACF')) {
$context['options'] = get_fields('options');
}
return $context;
}
/**
* This is where you can add your custom functions to Twig.
*
* @param string $functions : Array of available functions in Twig.
*/
public function add_filters($filters)
{
// alias of sanitize
$filters['slugify'] = $filters['sanitize'];
$filters['embeded_settings'] = ['callable' => 'filter_embeded_settings'];
$filters['line_breaks_to'] = ['callable' => 'filter_line_breaks'];
$filters['line_breaks_to_array'] = ['callable' => 'filter_split_line_breaks'];
$filters['nl2br_aria'] = ['callable' => 'filter_line_breaks_aria'];
$filters['group_by_key'] = ['callable' => 'filter_group_by_key'];
$filters['add_scroll_to'] = ['callable' => 'filter_add_scroll_to'];
return $filters;
}
/**
* This is where you can add your custom functions to Twig.
*
* @param string $functions : Array of available functions in Twig.
*/
public function add_functions($functions)
{
$functions['get_context'] = ['callable' => function () { return Timber::context(); }];
$functions['get_options'] = ['callable' => function () { return get_fields('options'); }];
$functions['is_menu_item'] = ['callable' => function ($item) { return ($item instanceof Timber\MenuItem); }];
$functions['get_table_of_contents'] = ['callable' => 'Mill3\Twig\get_table_of_contents'];
$functions['get_term_by'] = ['callable' => [Timber::class, 'get_term_by']];
$functions['get_post_by'] = ['callable' => [Timber::class, 'get_post_by']];
return $functions;
}
}
new Mill3WP();