-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
521 lines (426 loc) · 16.1 KB
/
functions.php
File metadata and controls
521 lines (426 loc) · 16.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
if ( ! isset( $content_width ) ) {
$content_width = 700;
}
/*********************/
/* FRONT-END ACTIONS */
/*********************/
// scipt loader tag callback function for adding script type="module" to firebird script tag
function add_type_attribute($tag, $handle, $src) {
if ( 'firebird-scripts' === $handle ) {
$tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
// } else if ( 'site-scripts' === $handle ) {
// $tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
}
return $tag;
}
/**
* Enqueues the global styles and scripts.
* Generally, use minimized styles/scripts in prod and unminimized files for local development.
*/
function pg_enqueue_site_files() {
$BABEL_ROOT = str_replace('www', 'babel', $_SERVER['DOCUMENT_ROOT']);
$manifest = json_decode(
file_get_contents($BABEL_ROOT . '/firebird-common/dist/manifest.json'),
true
);
$base_path = ('local' === wp_get_environment_type())
? '/firebird-common/dist/'
: '/common/firebird/dist/';
$firebird_manifest = [
'stylesheet' => $base_path . $manifest['index.css']['file'],
'script' => $base_path . $manifest['index.html']['file'],
];
/*
STYLES
*/
/* fonts, firebird, theme styles */
wp_enqueue_style( 'site-fonts', get_template_directory_uri() . '/fonts.min.css', NULL, filemtime( get_template_directory() . '/fonts.css' ) );
wp_enqueue_style( 'firebird-styles', $firebird_manifest['stylesheet']);
if ( 'local' === wp_get_environment_type() ) {
wp_enqueue_style( 'site-styles', get_template_directory_uri() . '/src/css/style.css', array( 'site-fonts', 'firebird-styles' ), filemtime( get_template_directory() . '/src/css/style.css' ) );
} else {
wp_enqueue_style( 'site-styles', get_template_directory_uri() . '/dist/css/style.min.css', array( 'site-fonts', 'firebird-styles' ), filemtime( get_template_directory() . '/dist/css/style.min.css' ) );
}
/* homepage styles */
if ( is_front_page() && 'local' === wp_get_environment_type() ) {
wp_enqueue_style( 'home-styles', get_template_directory_uri() . '/src/css/home.css', array( 'site-fonts', 'site-styles', 'firebird-styles'), filemtime( get_template_directory() . '/src/css/home.css' ) );
} else if ( is_front_page() ) {
wp_enqueue_style( 'home-styles', get_template_directory_uri() . '/dist/css/home.min.css', array( 'site-fonts', 'site-styles', 'firebird-styles' ), filemtime( get_template_directory() . '/dist/css/home.min.css' ) );
}
/*
SCRIPTS
*/
/* theme and firebird scripts */
wp_enqueue_script('firebird-scripts', $firebird_manifest['script'], array(), false, false);
if ( 'local' === wp_get_environment_type() ) {
// if you need your local firebird
// wp_enqueue_script( 'firebird-scripts', '//localhost:5173/js/main.js', array(), false, false);
wp_enqueue_script( 'site-scripts', get_template_directory_uri() . '/src/js/scripts.js', array('firebird-scripts'), filemtime( get_template_directory() . '/src/js/scripts.js' ), TRUE );
wp_enqueue_script( 'cookie-scripts', get_template_directory_uri() . '/src/js/cookies.js', array('firebird-scripts'), filemtime( get_template_directory() . '/src/js/cookies.js' ), TRUE );
wp_enqueue_script( 'highlight-scripts', get_template_directory_uri() . '/src/js/highlight.js', array('firebird-scripts'), filemtime( get_template_directory() . '/src/js/highlight.js' ), TRUE );
} else {
wp_enqueue_script( 'site-scripts', get_template_directory_uri() . '/dist/js/scripts.min.js', array('firebird-scripts'), filemtime( get_template_directory() . '/dist/js/scripts.min.js' ), TRUE );
wp_enqueue_script( 'cookie-scripts', get_template_directory_uri() . '/dist/js/cookies.min.js', array('firebird-scripts'), filemtime( get_template_directory() . '/dist/js/cookies.min.js' ), TRUE );
wp_enqueue_script( 'highlight-scripts', get_template_directory_uri() . '/dist/js/highlight.min.js', array('firebird-scripts'), filemtime( get_template_directory() . '/dist/js/highlight.min.js' ), TRUE );
// hotjar script is added through firebird-common + ping
}
/* matomo */
if ( 'development' === wp_get_environment_type() || 'local' === wp_get_environment_type() || 'staging' === wp_get_environment_type() ) {
wp_enqueue_script( 'testing-matomo-script', get_template_directory_uri() . '/src/js/testing-matomo.js', array('firebird-scripts'), filemtime( get_template_directory() . '/src/js/testing-matomo.js' ), TRUE);
}
if ( 'production' === wp_get_environment_type() ) {
wp_enqueue_script( 'matomo-script', get_template_directory_uri() . '/src/js/matomo.js', array('firebird-scripts'), filemtime( get_template_directory() . '/src/js/matomo.js' ), TRUE);
}
}
add_action( 'wp_enqueue_scripts', 'pg_enqueue_site_files' );
add_filter( 'script_loader_tag', 'add_type_attribute', 10, 3);
/**
* Customize login page
*/
function custom_login_page() { ?>
<style type="text/css">
body.login form#loginform {
border-radius: 0.375rem;
}
body.login #loginform .row a {
display:inline-flex;
width:100%;
}
body.login #loginform .row .mo_oauth_login_button {
padding: 0;
border-width: 0;
cursor: pointer;
display: inline-flex;
align-items: center;
padding: 0.5rem 1rem;
border: 1px solid;
border-radius: 0.375rem;
font-weight: 800;
line-height: 1.31;
text-decoration: none;
color:white;
box-shadow: none;
}
</style>
<?php }
add_action( 'login_enqueue_scripts', 'custom_login_page' );
/**
* Activate extended theme features.
*/
function pg_custom_theme_setup() {
add_editor_style( 'fonts.min.css' );
add_editor_style( '/dist/css/editor-style.min.css' );
add_image_size( 'person', 100, 100, TRUE );
add_theme_support( 'html5', array( 'caption', 'script', 'style' ) );
add_theme_support( 'menus' );
add_theme_support( 'title-tag' );
add_post_type_support( 'page', 'excerpt' );
}
add_action( 'after_setup_theme', 'pg_custom_theme_setup' );
/**
* Register locations for navigation. @TODO: This can be removed after header/footer integration.
*/
function pg_register_menus() {
register_nav_menus( array(
'temp' => 'Homepage Temporary'
) );
}
add_action( 'init', 'pg_register_menus' );
/**
* Modifies the default number of search results per page.
*/
function pg_increase_search_results_count( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
$query->set( 'posts_per_page', 20 );
}
}
}
add_action( 'pre_get_posts', 'pg_increase_search_results_count' );
/*********************/
/* BACK-END ACTIONS */
/*********************/
/**
* Disable unused default WordPress features.
*/
function pg_remove_comments() {
remove_post_type_support( 'post', 'comments' );
remove_post_type_support( 'page', 'comments' );
}
add_action( 'init', 'pg_remove_comments' );
/**
* Suppresses unused default WordPress pages from the admin menu.
*/
function pg_remove_menus() {
global $submenu;
remove_menu_page( 'edit.php' );
remove_menu_page( 'edit-comments.php' );
}
add_action( 'admin_menu', 'pg_remove_menus' );
/**
* Suppresses unused default WordPress pages from the admin bar.
*/
function pg_remove_nodes( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-post' );
$wp_admin_bar->remove_node( 'comments' );
}
add_action( 'admin_bar_menu', 'pg_remove_nodes', 999 );
/*********************/
/* FRONT-END FILTERS */
/*********************/
/**
* Adds additional legal tags to wp_kses().
*/
function pg_add_additional_tags_to_wpkses( $tags, $context ) {
if ( 'post' === $context ) {
$tags['iframe'] = array(
'allow' => TRUE,
'allowfullscreen' => TRUE,
'frameborder' => TRUE,
'height' => TRUE,
'loading' => TRUE,
'src' => TRUE,
'width' => TRUE,
);
}
return $tags;
}
add_filter( 'wp_kses_allowed_html', 'pg_add_additional_tags_to_wpkses', 10, 2 );
/********************/
/* BACK-END FILTERS */
/********************/
/**
* Suppresses unused default WordPress post list columns.
*/
function pg_set_edit_columns( $columns ) {
unset( $columns['author'] );
unset( $columns['comments'] );
unset( $columns['posts'] );
return $columns;
}
add_filter( 'manage_media_columns', 'pg_set_edit_columns' );
add_filter( 'manage_pages_columns', 'pg_set_edit_columns' );
add_filter( 'manage_users_columns', 'pg_set_edit_columns' );
/**
* Remove unused buttons from TinyMCE row 1.
*/
function pg_tinymce1( $buttons ) {
return array_diff( $buttons, array( 'aligncenter', 'alignleft', 'alignright', 'blockquote', 'wp_more' ) );
}
add_filter( 'mce_buttons', 'pg_tinymce1' );
/**
* Remove unused buttons from TinyMCE row 2.
*/
function pg_tinymce2( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return array_diff( $buttons, array( 'forecolor', 'outdent', 'indent' ) );
}
add_filter( 'mce_buttons_2', 'pg_tinymce2' );
/**
* TinyMCE configuration.
*/
function pg_editor_styles( $settings ) {
// updates the block formats dropdown
$settings['block_formats'] = 'Paragraph=p;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6';
// populates formats dropdown with theme-specific values
$style_formats = array(
array( 'title' => 'Button (Primary)', 'selector' => 'a', 'classes' => 'btn btn-primary' ),
array( 'title' => 'Button (Secondary)', 'selector' => 'a', 'classes' => 'btn btn-secondary' ),
array( 'title' => 'Button (Tertiary)', 'selector' => 'a', 'classes' => 'btn btn-tertiary' ),
array( 'title' => 'Button with Arrow', 'selector' => 'a', 'classes' => 'btn-arrow' ),
array( 'title' => 'Button with Info', 'selector' => 'a', 'classes' => 'btn-info' ),
array( 'title' => 'Button with PDF', 'selector' => 'a', 'classes' => 'btn-pdf' ),
array( 'title' => 'Button with Quote', 'selector' => 'a', 'classes' => 'btn-quote' ),
array( 'title' => 'Link with Arrow', 'selector' => 'a', 'classes' => 'link-arrow' ),
array( 'title' => 'Spaced List', 'selector' => 'ol,ul', 'classes' => 'spacedlist' )
);
$settings['style_formats'] = json_encode( $style_formats );
// cache invalidation for included stylesheets
$settings['cache_suffix'] = '?v=' . max( array( filemtime( get_template_directory() . '/dist/css/editor-style.min.css' ), filemtime( get_template_directory() . '/fonts.min.css' ) ) );
return $settings;
}
add_filter( 'tiny_mce_before_init', 'pg_editor_styles' );
/********************/
/* PLUGIN SETTINGS */
/*******************/
/**
* Hides the ACF "Custom Fields" menu (/wp-admin/edit.php?post_type=acf-field-group).
*/
#add_filter( 'acf/settings/show_admin', '__return_false' );
/**
* Registers the parent ACF options page.
*/
function pg_acf_options_page() {
acf_add_options_page( array( 'page_title' => 'Site Settings', 'redirect' => FALSE ) );
}
add_action( 'acf/init', 'pg_acf_options_page', 97 );
/**
* Populate ACF "Navigation Menus" select field with Menus.
*/
function pg_acf_populate_select_with_menus( $field ) {
$menus = get_terms( array(
'hide_empty' => FALSE,
'taxonomy' => 'nav_menu'
) );
if ( $menus ) {
// clear out existing values if there are menus saved
$field['choices'] = [];
foreach ( $menus as $menuObj ) {
$field['choices'][ $menuObj->slug ] = $menuObj->name;
}
}
return $field;
}
add_filter( 'acf/load_field/name=menu', 'pg_acf_populate_select_with_menus' );
/***************************/
/* CUSTOM POSTS/TAXONOMIES */
/***************************/
/**
* Set up custom post types.
*/
function pg_custom_post_types() {
register_post_type( 'blogs',
array(
'label' => 'Blog',
'menu_position' => 32,
'menu_icon' => 'dashicons-admin-post',
'public' => TRUE,
'supports' => array( 'title', 'editor', 'excerpt' )
)
);
register_post_type( 'events',
array(
'label' => 'Events',
'menu_position' => 32,
'menu_icon' => 'dashicons-admin-post',
'public' => TRUE,
'rewrite' => array(
'slug' => 'event-post'
),
'supports' => array( 'title', 'editor', 'excerpt' )
)
);
register_post_type( 'press',
array(
'label' => 'Press',
'menu_position' => 32,
'menu_icon' => 'dashicons-admin-post',
'public' => TRUE,
'rewrite' => array(
'slug' => 'press-post'
),
'supports' => array( 'title', 'editor', 'excerpt' )
)
);
register_post_type( 'newsletters',
array(
'label' => 'Newsletters',
'menu_position' => 32,
'menu_icon' => 'dashicons-admin-post',
'public' => TRUE,
'rewrite' => array(
'slug' => 'newsletter-post'
),
'supports' => array( 'title', 'excerpt' )
)
);
}
add_action( 'init', 'pg_custom_post_types' );
/**
* Set up custom taxonomies.
*/
function pg_custom_taxonomies() {
register_taxonomy( 'categories',
array( 'blogs' ),
array(
'label' => 'Blog Categories',
'meta_box_cb' => FALSE,
'public' => FALSE,
'publicly_queryable' => TRUE,
'show_admin_column' => TRUE,
'show_in_menu' => TRUE,
'show_ui' => TRUE
)
);
register_taxonomy( 'tags',
array( 'blogs' ),
array(
'label' => 'Blog Tags',
'meta_box_cb' => FALSE,
'public' => FALSE,
'publicly_queryable' => TRUE,
'show_admin_column' => TRUE,
'show_in_menu' => TRUE,
'show_ui' => TRUE
)
);
}
add_action( 'init', 'pg_custom_taxonomies' );
/********************/
/* THEME SHORTCODES */
/********************/
/**
* Creates a [year] shortcode.
*
* @return string Current year.
*/
function pg_year_shortcode() {
return date( 'Y' );
}
add_shortcode( 'year', 'pg_year_shortcode' );
/*****************/
/* HT DEV CUSTOM */
/*****************/
//format bytes to mb/kb
function formatBytes($size, $precision = 2) {
$base = log($size, 1024);
$suffixes = array('B', 'kB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}
// my attempt to write a directory listing with php
// TODO: use HT.www_domain to set preview vs prod
function file_list($directory, $linkpath) {
// $directory = "/htapps/test.www/files/hathifiles";
$files = new \FileSystemIterator($directory);
$html = [];
class HTfile {
public $fileName;
// public $filePath;
public $lastModified;
public $fileSize;
// public $fileInfo;
}
foreach ($files as $file) {
$hathifile = new HTfile();
//if ($files->isDir() ) {
// $href = add_query_arg(rawurlencode(base64_encode($directory . '/' . $file)));
//}
$hathifile->fileName = $files->getFileName();
// $hathifile->filePath = $files->getPathName();
$hathifile->lastModified = $files->getMTime();
$hathifile->fileSize = formatBytes($files->getSize());
// $hathifile->fileInfo = $files->getFileInfo();
$html[] = $hathifile;
}
//sort array by last modified, starting with newest
usort($html, function($a, $b) {
return ($b->lastModified) - ($a->lastModified);
});
foreach ($html as $key => $value) {
// echo "<tr><td><a href='https://www.hathitrust.org/sites/www.hathitrust.org/files/hathifiles/".$value->fileName."'>".$value->fileName."</a></td><td>".date('M d, Y', $value->lastModified)."</td><td>".$value->fileSize."</td></tr>";
echo "<tr><td><a href='".$linkpath.$value->fileName."'>".$value->fileName."</a></td><td>".date('M d, Y', $value->lastModified)."</td><td>".$value->fileSize."</td></tr>";
}
}
//truncate text, default character limit is 250
//used in search.php for ACF content excerpts
function truncate($text, $length = 250) {
$text = strip_tags($text);
if (strlen($text) <= $length) {
return $text;
}
$text = substr($text, 0, $length);
$text = substr($text, 0, strrpos($text, " "));
$text .= "...";
return $text;
}