-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.php
582 lines (493 loc) · 17.2 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
define( 'OPENLAB_VERSION', '1.6.0-1723126444908' );
if ( ! defined( 'CSS_DEBUG' ) ) {
define( 'CSS_DEBUG', false );
}
// Abort loading if commons-in-a-box is not active.
if ( ! function_exists( 'cbox_is_main_site' ) ) {
return;
}
// Register sidebars.
add_action( 'widgets_init', 'openlab_register_sidebars' );
// Force Legacy templates.
add_action(
'after_setup_theme',
function () {
add_theme_support( 'buddypress-use-legacy' );
}
);
// Install widgets.
function openlab_core_setup() {
add_theme_support( 'post-thumbnails' );
global $content_width;
register_nav_menus(
array(
'main' => __( 'Main Menu', 'openlab' ),
'aboutmenu' => __( 'About Menu', 'openlab' ),
'helpmenu' => __( 'Help Menu', 'openlab' ),
'helpmenusec' => __( 'Help Menu Secondary', 'openlab' ),
)
);
}
// test
add_action( 'after_setup_theme', 'openlab_core_setup' );
function openlab_create_default_nav_menus() {
// Main Menu.
$menu_name = wp_slash( __( 'Main Menu', 'commons-in-a-box' ) );
$menu_id = wp_create_nav_menu( $menu_name );
if ( is_wp_error( $menu_id ) ) {
return;
}
$brand_pages = cboxol_get_brand_pages();
if ( isset( $brand_pages['about'] ) ) {
wp_update_nav_menu_item(
$menu_id,
0,
array(
'menu-item-title' => $brand_pages['about']['title'],
'menu-item-classes' => 'about',
'menu-item-url' => $brand_pages['about']['preview_url'],
'menu-item-status' => 'publish',
)
);
}
wp_update_nav_menu_item(
$menu_id,
0,
array(
'menu-item-title' => bp_get_directory_title( 'members' ),
'menu-item-classes' => 'home',
'menu-item-url' => bp_get_members_directory_permalink(),
'menu-item-status' => 'publish',
)
);
$group_types = cboxol_get_group_types();
foreach ( $group_types as $group_type ) {
wp_update_nav_menu_item(
$menu_id,
0,
array(
'menu-item-title' => $group_type->get_label( 'plural' ),
'menu-item-classes' => 'group-type ' . $group_type->get_slug(),
'menu-item-url' => bp_get_group_type_directory_permalink( $group_type->get_slug() ),
'menu-item-status' => 'publish',
)
);
}
if ( isset( $brand_pages['help'] ) ) {
wp_update_nav_menu_item(
$menu_id,
0,
array(
'menu-item-title' => $brand_pages['help']['title'],
'menu-item-classes' => 'help',
'menu-item-url' => $brand_pages['help']['preview_url'],
'menu-item-status' => 'publish',
)
);
}
$locations = get_theme_mod( 'nav_menu_locations' );
$locations['main'] = $menu_id;
set_theme_mod( 'nav_menu_locations', $locations );
// About Menu.
if ( isset( $brand_pages['about'] ) ) {
$menu_name = wp_slash( __( 'About Menu', 'commons-in-a-box' ) );
$menu_id = wp_create_nav_menu( $menu_name );
if ( is_wp_error( $menu_id ) ) {
return;
}
wp_update_nav_menu_item(
$menu_id,
0,
array(
'menu-item-title' => $brand_pages['about']['title'],
'menu-item-classes' => 'about',
'menu-item-url' => $brand_pages['about']['preview_url'],
'menu-item-status' => 'publish',
)
);
$locations = get_theme_mod( 'nav_menu_locations' );
$locations['aboutmenu'] = $menu_id;
set_theme_mod( 'nav_menu_locations', $locations );
}
}
/*
* creating a library to organize functions* */
/* * core* */
require_once get_template_directory() . '/lib/core/page-control.php';
require_once get_template_directory() . '/lib/core/backend-admin.php';
require_once get_template_directory() . '/lib/course-clone.php';
require_once get_template_directory() . '/lib/header-funcs.php';
require_once get_template_directory() . '/lib/post-types.php';
require_once get_template_directory() . '/lib/menus.php';
require_once get_template_directory() . '/lib/content-processing.php';
require_once get_template_directory() . '/lib/nav.php';
require_once get_template_directory() . '/lib/breadcrumbs.php';
require_once get_template_directory() . '/lib/shortcodes.php';
require_once get_template_directory() . '/lib/media-funcs.php';
require_once get_template_directory() . '/lib/group-funcs.php';
require_once get_template_directory() . '/lib/ajax-funcs.php';
require_once get_template_directory() . '/lib/help-funcs.php';
require_once get_template_directory() . '/lib/member-funcs.php';
require_once get_template_directory() . '/lib/page-funcs.php';
require_once get_template_directory() . '/lib/sidebar-funcs.php';
require_once get_template_directory() . '/lib/plugin-hooks.php';
require_once get_template_directory() . '/lib/theme-hooks.php';
require_once get_template_directory() . '/lib/widgets.php';
require_once get_template_directory() . '/lib/customizer.php';
require_once get_template_directory() . '/lib/buddypress.php';
/**
* Gets a version string for assets.
*
* @return string
*/
function openlab_get_asset_version() {
return apply_filters( 'openlab_get_asset_version', OPENLAB_VERSION );
}
function openlab_load_scripts() {
$stylesheet_dir_uri = get_template_directory_uri();
$ver = openlab_get_asset_version();
/**
* scripts, additional functionality
*/
if ( ! is_admin() ) {
// google fonts
wp_register_style( 'google-open-sans', set_url_scheme( 'http://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600,600italic,700,700italic' ), array(), $ver, 'all' );
wp_enqueue_style( 'google-open-sans' );
wp_register_style( 'camera-js-styles', $stylesheet_dir_uri . '/css/camera.css', array(), $ver, 'all' );
wp_enqueue_style( 'camera-js-styles' );
// less compliation via js so we can check styles in firebug via fireless - local dev only
if ( CSS_DEBUG ) {
wp_register_script( 'less-config-js', $stylesheet_dir_uri . '/js/less.config.js', array( 'jquery' ), $ver, true );
wp_enqueue_script( 'less-config-js' );
wp_register_script( 'less-js', $stylesheet_dir_uri . '/js/less-1.7.4.js', array( 'jquery' ), $ver, true );
wp_enqueue_script( 'less-js' );
}
wp_register_script( 'vendor-js', $stylesheet_dir_uri . '/js/dist/vendor.js', array( 'jquery' ), $ver, true );
wp_enqueue_script( 'vendor-js' );
wp_register_script( 'select2', $stylesheet_dir_uri . '/js/select2.min.js', array( 'jquery' ), $ver, true );
$utility_deps = array( 'jquery', 'select2', 'hyphenator-js' );
wp_register_script( 'utility', $stylesheet_dir_uri . '/js/utility.js', $utility_deps, $ver, true );
wp_enqueue_script( 'utility' );
wp_localize_script(
'utility',
'localVars',
array(
'nonce' => wp_create_nonce( 'request-nonce' ),
'strings' => array(
'cancelFriendship' => esc_html__( 'Cancel Friendship', 'commons-in-a-box' ),
'deleteFolder' => esc_html__( 'Are you sure you wish to permanently delete this folder? The files associated with this folder will not be deleted.', 'commons-in-a-box' ),
'seeMore' => esc_html__( 'See More', 'commons-in-a-box' ),
),
)
);
wp_register_script( 'parsley', $stylesheet_dir_uri . '/js/parsley.min.js', array( 'jquery' ), $ver, true );
wp_register_script(
'openlab-group-documents',
$stylesheet_dir_uri . '/js/group-documents.js',
[ 'jquery' ],
$ver,
true
);
wp_register_script(
'openlab-avatar-privacy',
$stylesheet_dir_uri . '/js/avatar-privacy.js',
[ 'jquery' ],
$ver,
true
);
if ( bp_is_groups_directory() || openlab_is_search_results_page() || bp_is_members_directory() ) {
wp_enqueue_script( 'openlab-directory', $stylesheet_dir_uri . '/js/directory.js', [ 'jquery' ], $ver, true );
wp_localize_script(
'openlab-directory',
'OLDirectory',
[
'groupTypeDisabledFilters' => openlab_group_type_disabled_filters(),
]
);
}
if ( bp_is_group() ) {
wp_enqueue_script(
'openlab-group-membership',
$stylesheet_dir_uri . '/js/membership-privacy.js',
[ 'jquery' ],
$ver,
true
);
}
}
}
add_action( 'wp_enqueue_scripts', 'openlab_load_scripts' );
function openlab_admin_scripts() {
wp_register_script( 'utility-admin', get_template_directory_uri() . '/js/utility.admin.js', array( 'jquery', 'jquery-ui-autocomplete' ), openlab_get_asset_version(), true );
wp_enqueue_script( 'utility-admin' );
wp_localize_script(
'utility-admin',
'localVars',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'request-nonce' ),
)
);
}
add_action( 'admin_enqueue_scripts', 'openlab_admin_scripts' );
/**
* Giving the main stylesheet the highest priority among stylesheets to make sure it loads last
*/
function openlab_load_scripts_high_priority() {
$stylesheet_dir_uri = get_template_directory_uri();
global $post;
$color_scheme = openlab_get_color_scheme();
$ver = openlab_get_asset_version();
// less compliation via js so we can check styles in firebug via fireless - local dev only
// @to-do: way to enqueue as last item?
if ( CSS_DEBUG ) {
wp_register_style( 'main-styles', $stylesheet_dir_uri . '/style.less', array(), $ver, 'all' );
wp_enqueue_style( 'main-styles' );
} else {
wp_register_style( 'main-styles', $stylesheet_dir_uri . '/css/color-schemes/' . $color_scheme . '.css', array(), $ver, 'all' );
wp_enqueue_style( 'main-styles' );
}
if ( isset( $post->post_type ) && 'help' === $post->post_type ) {
wp_register_style( 'print-styles', $stylesheet_dir_uri . '/css/print.css', array(), $ver, 'print' );
wp_enqueue_style( 'print-styles' );
}
}
add_action( 'wp_enqueue_scripts', 'openlab_load_scripts_high_priority', 999 );
/**
* Custom image sizes
*/
// front page slider
add_image_size( 'front-page-slider', 735, 295, true );
/**
* Register sidebar widget areas.
*
* @since 1.0.0
*/
function openlab_register_sidebars() {
// Home sidebar.
register_sidebar(
array(
'name' => __( 'Home Sidebar', 'commons-in-a-box' ),
'description' => __( 'The sidebar at the left side of the home page.', 'commons-in-a-box' ),
'id' => 'home-sidebar',
'before_widget' => '<div id="%1$s" class="box-1 left-box widget %2$s">',
'after_widget' => '</div>',
)
);
// Home main (group type columns).
register_sidebar(
array(
'name' => __( 'Home Main', 'commons-in-a-box' ),
'description' => __( 'The main section of the home page. Generally used for group type widgets.', 'commons-in-a-box' ),
'id' => 'home-main',
)
);
}
/**
* Modify the body class
*
* Invite New Members and Your Email Options fall under "Settings", so need
* an appropriate body class.
*/
function openlab_group_admin_body_classes( $classes ) {
if ( ! bp_is_group() ) {
return $classes;
}
if ( in_array( bp_current_action(), array( 'invite-anyone', 'notifications' ), true ) ) {
$classes[] = 'group-admin';
}
return $classes;
}
add_filter( 'bp_get_the_body_class', 'openlab_group_admin_body_classes' );
// for less js - local dev only
function enqueue_less_styles( $tag, $handle ) {
global $wp_styles;
$match_pattern = '/\.less$/U';
if ( preg_match( $match_pattern, $wp_styles->registered[ $handle ]->src ) ) {
$handle = $wp_styles->registered[ $handle ]->handle;
$media = $wp_styles->registered[ $handle ]->args;
$href = $wp_styles->registered[ $handle ]->src;
$rel = isset( $wp_styles->registered[ $handle ]->extra['alt'] ) && $wp_styles->registered[ $handle ]->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
$title = isset( $wp_styles->registered[ $handle ]->extra['title'] ) ? "title='" . esc_attr( $wp_styles->registered[ $handle ]->extra['title'] ) . "'" : '';
// phpcs:disable
$tag = "<link rel='stylesheet/less' $title href='$href' type='text/css' media='$media' />";
// phpcs:enable
}
return $tag;
}
add_filter( 'style_loader_tag', 'enqueue_less_styles', 5, 2 );
/**
* Get content with formatting in place
*
* @param type $more_link_text
* @param type $stripteaser
* @param type $more_file
* @return type
*/
function get_the_content_with_formatting( $more_link_text = '(more...)', $stripteaser = 0, $more_file = '' ) {
$content = get_the_content( $more_link_text, $stripteaser, $more_file );
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
return $content;
}
/**
* Get a value from a failed POST request, especially during registration.
*/
function openlab_post_value( $key ) {
$value = '';
// phpcs:disable WordPress.Security.NonceVerification.Missing
if ( ! empty( $_POST[ $key ] ) ) {
$value = wp_unslash( $_POST[ $key ] );
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
return $value;
}
/**
* Disable the new avatar upload interface introduced in BP 2.3.
*/
add_filter( 'bp_avatar_is_front_edit', '__return_false' );
/**
* Generate data attributes for xprofile 'input' fields.
*
* Used for Parsely validation.
*/
function openlab_profile_field_input_attributes() {
$attributes = array();
switch ( bp_get_the_profile_field_name() ) {
case 'Name':
$attributes[] = 'data-parsley-required';
$attributes[] = 'data-parsley-required';
break;
case 'First Name':
$attributes[] = 'data-parsley-required';
break;
case 'Last Name':
$attributes[] = 'data-parsley-required';
break;
case 'Account Type':
$attributes[] = 'data-parsley-required';
break;
}
if ( $attributes ) {
return ' ' . implode( ' ', $attributes ) . ' ';
}
}
/**
* Fetch the sitewide footer markup, storing in a transient if necessary.
*/
function openlab_site_footer() {
$footer = get_site_transient( 'cboxol_network_footer' );
if ( $footer ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $footer;
return;
}
$left_heading = get_theme_mod( 'openlab_footer_left_heading' );
$left_content = get_theme_mod( 'openlab_footer_left_content' );
$middle_heading = get_theme_mod( 'openlab_footer_middle_heading' );
$middle_content = get_theme_mod( 'openlab_footer_middle_content' );
ob_start(); ?>
<div id="openlab-footer" class="oplb-bs page-table-row" role="complementary">
<div class="oplb-bs">
<div class="footer-wrapper">
<div class="container-fluid footer-desktop">
<div class="row row-footer">
<div id="footer-left" class="footer-left footer-section col-md-12">
<h2 id="footer-left-heading"><?php echo esc_html( $left_heading ); ?></h2>
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div id="footer-left-content"><?php echo $left_content; ?></div>
</div>
<div id="footer-middle" class="footer-middle footer-section col-md-8">
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<h2 id="footer-middle-heading"><?php echo esc_html( $middle_heading ); ?></h2>
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div id="footer-middle-content"><?php echo $middle_content; ?></div>
</div>
<div id="footer-right" class="footer-right footer-section col-md-4">
<p><?php esc_html_e( 'Powered by:', 'commons-in-a-box' ); ?></p>
<div class="cboxol-footer-logo">
<a href="https://commonsinabox.org/"><img src="<?php echo esc_attr( get_template_directory_uri() ); ?>/images/cboxol-logo-noicon.png" alt="<?php esc_attr_e( 'CBOX-OL Logo', 'commons-in-a-box' ); ?>" /></a>
</div>
</div>
</div>
</div>
</div>
</div>
<a class="visible-xs" id="go-to-top" href="#"><span class="fa fa-chevron-circle-up"></span><br /><?php esc_html_e( 'top', 'commons-in-a-box' ); ?></a>
</div>
<?php
$footer = ob_get_contents();
ob_end_clean();
set_site_transient( 'cboxol_network_footer', $footer, DAY_IN_SECONDS );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $footer;
}
/**
* Bust the footer markup transient when it's modified.
*/
add_action(
'update_option_theme_mods_' . get_option( 'stylesheet' ),
function () {
delete_site_transient( 'cboxol_network_footer' );
}
);
/**
* Bust the sitewide nav item transient when it's edited.
*/
function openlab_bust_sitewide_nav_menu_cache() {
delete_site_transient( 'cboxol_network_nav_items' );
}
add_action( 'wp_update_nav_menu', 'openlab_bust_sitewide_nav_menu_cache' );
add_action( 'wp_update_nav_menu_item', 'openlab_bust_sitewide_nav_menu_cache' );
/**
* Check if Braille is available and the Braille community features are activated.
*/
function openlab_braille_is_enabled() {
// @todo Add actual check for Braille server.
return function_exists( 'get_braille' ) && function_exists( '\HardG\BPBraille\braille_works' ) && \HardG\BPBraille\braille_works();
}
/**
* Is this the search results page?
*
* @since 1.2.0
*/
function openlab_is_search_results_page() {
return cboxol_is_brand_page( 'search-results' );
}
/**
* Gets the brand page ancestor of the given post.
*
* For brand pages themselves, the $post_id is returned.
*
* @since 1.5.0
*
* @param int $post_id Optional. Post ID. Defaults to current object.
* @return int
*/
function openlab_get_brand_page_ancestor( $post_id = null ) {
static $map;
if ( isset( $map[ $post_id ] ) ) {
return $map[ $post_id ];
}
if ( ! $post_id ) {
$post_id = get_queried_object_id();
}
if ( ! $post_id ) {
return 0;
}
$brand_pages = cboxol_get_brand_pages();
foreach ( $brand_pages as $brand_page ) {
if ( $post_id === $brand_page['id'] ) {
$map[ $post_id ] = $post_id;
return $map[ $post_id ];
}
}
$post = get_post( $post_id );
if ( ! $post || ! $post->post_parent ) {
$map[ $post_id ] = 0;
return $map[ $post_id ];
}
$map[ $post_id ] = openlab_get_brand_page_ancestor( $post->post_parent );
return $map[ $post_id ];
}