-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
521 lines (445 loc) · 14.1 KB
/
functions.php
File metadata and controls
521 lines (445 loc) · 14.1 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
/**
* Config array
*/
$svg_url = get_template_directory_uri() . '/dist/svg/sprite.symbol.svg';
$svg_file_path = get_template_directory() . '/dist/svg/sprite.symbol.svg';
$theme_config = [
'svg' => ['sprite_file' => $svg_url . '?v=' . filemtime($svg_file_path)]
];
//fix header sent issue
ob_start();
/**
* Tofino includes
*
* The $tofino_includes array determines the code library included in your theme.
* Add or remove files to the array as needed.
*
* Missing files will produce a fatal error.
*
*/
$tofino_includes = [
"src/lib/nav-walker.php",
"src/lib/init.php",
"src/lib/assets.php",
"src/lib/helpers.php",
"src/lib/pagination.php",
"src/lib/initiatives.php",
"src/lib/hubs.php",
"src/lib/healthchecks.php",
"src/lib/permissions.php",
"src/lib/output-csv.php",
"src/shortcodes/copyright.php",
"src/shortcodes/social-icons.php",
"src/shortcodes/svg.php",
"src/shortcodes/theme-option.php",
"src/theme-options/admin.php",
"src/theme-options/advanced.php",
"src/theme-options/client-data.php",
"src/theme-options/footer.php",
"src/theme-options/google-analytics.php",
"src/theme-options/google-recaptcha.php",
"src/theme-options/init.php",
"src/theme-options/maintenance-mode.php",
"src/theme-options/menu.php",
"src/theme-options/notifications.php",
"src/theme-options/social-networks.php",
"src/theme-options/theme-tracker.php",
"src/theme-options/dashboard-widget.php",
"src/ajax/get-markers.php",
// "src/ajax/graph-requests.php",
"src/ajax/file-requests.php",
"src/custom/admin-tables.php",
"src/custom/acf-save.php",
"src/custom/helpers.php",
"src/custom/rank-math.php",
"src/custom/register-types-tax.php",
"src/custom/controller_trainers.php",
"src/custom/controller_groups.php",
"src/custom/login.php",
"src/custom/ma_co_authors.php",
"src/custom/generate_login_token.php",
"src/custom/api-endpoints/helpers.php",
"src/custom/api-endpoints/initiatives.php",
"src/custom/api-endpoints/initiatives_murmation.php",
"src/custom/api-endpoints/group_totals.php",
"src/custom/api-endpoints/trainers.php",
"src/custom/api-endpoints/hubs.php",
"src/custom/api-endpoints/post_to_murmuration.php",
"src/custom/api-endpoints/webhooks.php",
"src/custom/emails/emails.php",
"src/custom/emails/retention-email-content.php",
"src/custom/emails/cron.php",
"src/custom/vive-cron.php",
"src/custom/mailchimp-api.php",
"src/xinc-events/events.php",
"src/custom/dropbox/dropbox-upload.php"
// "src/dev-machines/export-author-emails-by-hub.php",
];
foreach ($tofino_includes as $file) {
if (!$filepath = locate_template($file)) {
trigger_error(sprintf(__('Error locating %s for inclusion', 'tofino-nt'), $file), E_USER_ERROR);
}
require_once $filepath;
}
unset($file, $filepath);
/**
* Composer dependencies
*
* External dependencies are defined in the composer.json and autoloaded.
* Use 'composer dump-autoload -o' after adding new files.
*
*/
if (file_exists(get_template_directory() . '/vendor/autoload.php')) { // Check composer autoload file exists. Result is cached by PHP.
require_once 'vendor/autoload.php';
} else {
if (is_admin()) {
add_action('admin_notices', 'composer_error_notice');
} else {
wp_die(composer_error_notice(), __('An error occured.', 'tofino-nt'));
}
}
// Check for missing dist directory. Result is cached by PHP.
if (!is_dir(get_template_directory() . '/dist')) {
if (is_admin()) {
add_action('admin_notices', 'missing_dist_error_notice');
} else {
wp_die(missing_dist_error_notice(), __('An error occured.', 'tofino-nt'));
}
}
// Admin notice for missing composer autoload.
function composer_error_notice()
{
?>
<div class="error notice">
<p>
<?php _e('Composer autoload file not found. Run composer install on the command line.', 'tofino-nt'); ?>
</p>
</div>
<?php
}
// Admin notice for missing dist directory.
function missing_dist_error_notice() {
?>
<div class="error notice">
<p>
<?php _e('/dist directory not found. You probably want to run npm install and gulp on the command line.', 'tofino-nt'); ?>
</p>
</div>
<?php
}
function diedump($string)
{
die(var_dump($string));
}
// Custom image sizes
add_image_size('event', 600, 400, true);
// disable for posts
add_filter('use_block_editor_for_post', '__return_false', 10);
// disable for post types
add_filter('use_block_editor_for_post_type', '__return_false', 10);
function custom_query_vars_filter($vars)
{
$vars[] = 'edit_post';
$vars[] = 'initiative_id';
$vars[] = 'error_code';
$vars[] = 'updated';
$vars[] = 'deleted';
$vars[] = 'promoted';
$vars[] = 'failed';
$vars[] = 'added';
$vars[] = 'hub_id';
$vars[] = 'edited_post';
$vars[] = 'added_post';
$vars[] = 'edited_note';
$vars[] = 'added_note';
$vars[] = 'source';
$vars[] = 'step';
$vars[] = 'hub_name';
$vars[] = 'type';
$vars[] = 'search';
$vars[] = 'country';
$vars[] = 'training';
$vars[] = 'topic';
$vars[] = 'show_recent';
$vars[] = 'last_mail_event';
$vars[] = 'sort_by';
//api endpoints
$vars[] = 'per_page';
$vars[] = 'page';
return $vars;
}
add_filter('query_vars', 'custom_query_vars_filter');
//Redirect after post deletion
function wpse132196_redirect_after_trashing($post_id) {
if(get_post_type($post_id) === 'files') {
wp_redirect(add_query_arg('tab', 'file', get_the_permalink(24)));
exit;
}
}
add_action('trashed_post', 'wpse132196_redirect_after_trashing', 10);
//Change label of Content Editor in acf_form()
function prepare_post_content($field)
{
$field['label'] = "Description";
return $field;
}
add_filter('acf/prepare_field/name=_post_content', 'prepare_post_content');
// Hide the greylist field on front-end group edit screen
add_filter('acf/prepare_field', 'my_acf_prepare_field', 10, 1);
function my_acf_prepare_field($field) {
if ( ! is_admin() && $field['key'] == 'field_695e763a53af4' && is_page('edit-group')) {
return false;
}
return $field;
}
function change_post_content_type( $field ) {
//disable wysiwyg fancies
if($field['type'] == 'wysiwyg') {
$field['tabs'] = 'visual';
$field['toolbar'] = 'basic';
$field['media_upload'] = 0;
}
return $field; }
add_filter( 'acf/get_valid_field', 'change_post_content_type');
//Logout link with nonce
function add_logout_link($nav, $args)
{
$logoutlink = '<li class="nav-item menu-logout"><a class="nav-link" href="' . wp_logout_url(home_url()) . '">Logout</a></li>';
if ($args->theme_location == 'primary_navigation_loggedin') {
return $nav . $logoutlink;
} else {
return $nav;
}
}
// disabled for now to keep nav tidy
// add_filter('wp_nav_menu_items', 'add_logout_link', 10, 2);
//Healthcheck
function get_latest_healthcheck($post = 0)
{
$post = get_post($post);
$args = array(
'post_type' => 'healthchecks',
'title' => $post->ID,
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts = get_posts($args);
if ($posts) {
return '<a href="' . get_permalink($posts[0]->ID) . '">' . date('j-M-Y H:i', strtotime($posts[0]->post_date)) . '</a>';
} else {
return 'Never';
}
}
function get_user_hub_slug($user_id) {
$hub_id = get_user_meta( $user_id, 'hub_user')[0];
$hub = get_term_by('id', $hub_id, 'hub');
return $hub->slug;
}
function get_hub_users($hub_slug)
{
$args = array(
'meta_query' => array(
array(
'key' => 'hub',
'value' => $hub_slug
)
)
);
$user_query = new WP_User_Query( $args );
return $user_query->results;
}
function get_hub_by_id($id)
{
var_dump($id);
return get_term_by('term_id', $id, 'hub')->name;
}
// Hook Gravity Forms user registration -> Map taxomomy
function map_taxonomy($user_id, $config, $entry, $user_pass)
{
global $wpdb;
// Get all taxonomies
$taxs = get_taxonomies();
// Get all user meta
$all_meta_for_user = get_user_meta($user_id);
// Loop through meta data and map to taxonomies with same name as user meta key
foreach ($all_meta_for_user as $taxonomy => $value) {
if (in_array($taxonomy, $taxs)) { // Check if there is a Taxonomy with the same name as the Custom user meta key
// Get term id
$term_id = get_user_meta($user_id, $taxonomy, true);
if (is_numeric($term_id)) { // Check if Custom user meta is an ID
$taxonomy . '=' . $term_id . '<br>';
// Add user to taxomomy term
$term = get_term($term_id, $taxonomy);
$termslug = $term->slug;
wp_set_object_terms($user_id, array($termslug), $taxonomy, false);
}
}
}
}
add_action("gform_user_registered", "map_taxonomy", 10, 4);
function generate_map($post_id)
{
$map = get_field('map', $post_id, false);
$map = maybe_unserialize($map);
if ($map['center_lat']) {
return '<li class="point" data-lat="' . htmlspecialchars($map['center_lat']) . '" data-lng="' . htmlspecialchars($map['center_lng']) . '" data-title="' . get_the_title($post_id) . '" data-link="' . get_the_permalink($post_id) . '" data-excerpt="' . get_the_excerpt($post_id) . '"></li>';
}
return false;
}
function wpse23007_redirect()
{
if (is_admin() && !defined('DOING_AJAX') && (current_user_can('initiative') || current_user_can('hub'))) {
wp_redirect(home_url());
exit;
}
}
add_action('init', 'wpse23007_redirect');
// set default hub value to no-hub when adding initiative
function set_tax_default($field) {
global $post;
if($post && $post->post_name == 'add-new-group') {
$field['value'] = 285;
}
return $field;
}
add_filter('acf/load_field/key=field_5c473dfca1fd3', 'set_tax_default');
function process_post_requests() {
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if(array_key_exists('accepted', $_POST)) {
if($_POST['accepted'] == 'true') {
update_user_meta(get_current_user_id(), '_gdpr_accepted', 'field_5c51aba1d7642');
update_user_meta(get_current_user_id(), 'gdpr_accepted', true);
wp_safe_redirect('account');
exit();
}
}
if(array_key_exists('update_group_author_id', $_POST)) {
$args = array(
'ID' => $_POST['post_id'],
'post_author' => $_POST['update_group_author_id']
);
wp_update_post($args);
wp_safe_redirect(add_query_arg('updated', 'author', get_the_permalink($_POST['post_id'])));
exit();
}
if(array_key_exists('unpublish', $_POST)) {
$args = array(
'ID' => $_POST['unpublish'],
'post_status' => 'pending'
);
wp_update_post($args);
}
if(array_key_exists('publish', $_POST)) {
$args = array(
'ID' => $_POST['publish'],
'post_status' => 'publish'
);
//keep record of who last published
add_post_meta( $_POST['publish'], 'last_published_by', get_current_user_id());
wp_update_post($args);
wp_safe_redirect(add_query_arg('updated', 'publish', home_url('account')));
exit;
}
if(array_key_exists('trash_group_id', $_POST)) {
$args = array(
'ID' => $_POST['trash_group_id'],
'post_status' => 'trash'
);
wp_update_post($args);
wp_safe_redirect(add_query_arg('updated', 'trash', home_url('account')));
exit;
}
//hub access
if(array_key_exists('request_access', $_POST)) {
request_hub_access((int)$_POST['request_access']);
}
if(array_key_exists('confirm_hub_admin', $_POST)) {
//TODO: email user and tell them
grant_hub_access($_POST['confirm_hub_admin'], $_POST['hub_id']);
}
if(array_key_exists('deny_hub_admin', $_POST)) {
//TODO: email user and tell them
delete_hub_access_request($_POST['deny_hub_admin'], $_POST['hub_id']);
}
//end hub access
//author access
if(array_key_exists('request_post_access', $_POST)) {
author_access_request($_POST['request_post_access']);
}
if(array_key_exists('deny_author_access', $_POST)) {
author_access_deny($_POST['post_id'], $_POST['deny_author_access']);
}
if(array_key_exists('confirm_author_access', $_POST)) {
author_access_grant($_POST['post_id'], $_POST['confirm_author_access']);
}
//end author access
if(array_key_exists('trainer_update', $_POST)) {
$args = array(
'ID' => $_POST['post_id'],
'post_status' => $_POST['trainer_update']
);
wp_update_post($args);
wp_safe_redirect(add_query_arg('updated', 'trainer', get_the_permalink(get_queried_object()->name)));
}
}
//$_GET REQUESTS - revert post back to unpublished if edited by non admin owner
global $post;
if($post && (get_post_type() === 'trainers') && get_query_var('updated') === 'trainer') {
if(get_the_author_meta('ID') === get_current_user_id()) {
$args = array(
'ID' => get_the_ID(),
'post_status' => $_POST['trainer_update']
);
wp_update_post($args);
wp_safe_redirect(add_query_arg('updated', 'trainer', get_the_permalink(get_queried_object()->name)));
}
}
}
if (!is_admin()) {
add_action('template_redirect', 'process_post_requests');
}
function get_environment() {
if((strpos(get_site_url(), 'transitiongroups.org') !== false) ||
(strpos(get_site_url(), 'maps.transitionnetwork.org') !== false)) {
return 'production';
}
if(strpos(get_site_url(), 'stage') !== false) {
return 'stage';
}
return 'dev';
}
function get_words($sentence, $count = 10) {
preg_match("/(?:[^\s,\.;\?\!]+(?:[\s,\.;\?\!]+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
add_action('init', 'customRSS');
function customRSS() {
add_feed('last-updated-initiatives', 'get_feed_updated_initiatives');
}
function get_feed_updated_initiatives() {
get_template_part('templates/rss/updated-initiatives');
}
function is_user_trainer_admin() {
if(is_user_role(array('administrator', 'trainer_admin'))) {
return true;
}
return false;
}
function adding_custom_meta_boxes( $post ) {
add_meta_box(
'trainer-info',
__( 'Trainer Created' ),
'render_trainer_meta_box',
'trainers',
'normal',
'default'
);
}
add_action( 'add_meta_boxes_trainers', 'adding_custom_meta_boxes' );
function render_trainer_meta_box($post) {
?>
<?php echo get_the_date(); ?>
<?php
}