This repository was archived by the owner on Jul 13, 2024. It is now read-only.
forked from KindnessNetwork/brony-kindness-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
588 lines (535 loc) · 22.1 KB
/
functions.php
File metadata and controls
588 lines (535 loc) · 22.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
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
583
584
585
586
587
<?php
define('BKN_THEME_VER', '1.5.13');
$bkn_fallback_google_fonts_url = 'https://fonts.googleapis.com/css2?family=Didact+Gothic&family=Poppins:ital,wght@0,400;0,700;1,400;1,700&display=swap';
$bkn_fallback_fontawesome_url = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js';
add_action('after_setup_theme', 'bkn_setup');
function bkn_setup() {
load_theme_textdomain('bkn', get_template_directory() . '/languages');
add_theme_support('title-tag');
add_theme_support('custom-logo', array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
'unlink-homepage-logo' => false,
));
add_theme_support('automatic-feed-links');
add_theme_support('post-thumbnails');
add_theme_support('html5', array('search-form'));
add_theme_support('woocommerce');
global $content_width;
if (!isset($content_width)) {
$content_width = 1920;
}
register_nav_menus(array('main-menu' => esc_html__('Main Menu', 'bkn')));
register_nav_menus(array('footer-menu' => esc_html__('Footer Menu', 'bkn')));
}
add_action('wp_enqueue_scripts', 'bkn_load_scripts');
function bkn_load_scripts() {
$google_fonts = get_option('google_fonts_resource_url');
if (empty($google_fonts)){
global $bkn_fallback_google_fonts_url;
$google_fonts = $bkn_fallback_google_fonts_url;
}
wp_enqueue_style('google-fonts', $google_fonts, [], null);
wp_enqueue_style('bkn-style', get_stylesheet_uri(), [], BKN_THEME_VER);
wp_deregister_script('jquery');
wp_deregister_script('jquery-core');
wp_register_script('jquery', get_template_directory_uri() . '/js/jquery.min.js', [], null);
wp_register_script('jquery-core', get_template_directory_uri() . '/js/dummy.js', [], null);
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-core');
$fontawesome = get_option('fontawesome_resource_url');
if (empty($fontawesome)){
global $bkn_fallback_fontawesome_url;
$fontawesome = $bkn_fallback_fontawesome_url;
}
wp_register_script('fontawesome', $fontawesome, ['jquery']);
wp_enqueue_script('fontawesome');
wp_deregister_style('vc_font_awesome_5'); // We have our own. Let's replace it.
wp_register_style('vc_font_awesome_5', get_template_directory_uri() . '/css/dummy.css', [], null);
wp_register_script('bkn-scripts', get_template_directory_uri() . '/js/global.js', ['jquery'], BKN_THEME_VER);
wp_enqueue_script('bkn-scripts');
}
add_action('wp_footer', 'bkn_footer_scripts');
function bkn_footer_scripts() {
?>
<script>
jQuery(document).ready(function ($) {
let deviceAgent = navigator.userAgent.toLowerCase();
if (deviceAgent.match(/(iphone|ipod|ipad)/)) {
$("html").addClass(["ios", "mobile"]);
}
if (navigator.userAgent.search("MSIE") >= 0) {
$("html").addClass("ie");
} else if (navigator.userAgent.search("Chrome") >= 0) {
$("html").addClass("chrome");
} else if (navigator.userAgent.search("Firefox") >= 0) {
$("html").addClass("firefox");
} else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
$("html").addClass("safari");
} else if (navigator.userAgent.search("Opera") >= 0) {
$("html").addClass("opera");
}
});
</script>
<?php
}
add_filter('document_title_separator', 'bkn_document_title_separator');
function bkn_document_title_separator($sep) {
$sep = '|';
return $sep;
}
add_filter('the_title', 'bkn_title');
function bkn_title($title) {
if ($title == '') {
return '...';
} else {
return $title;
}
}
add_filter('the_content_more_link', 'bkn_read_more_link');
function bkn_read_more_link() {
if (!is_admin()) {
global $post;
if($post->post_type != 'event') {
return '… <a href="' . esc_url(get_permalink()) . '" class="more-link">Read More <span class="far fa-chevron-right" aria-hidden="true"></span></a>';
} else {
return '…';
}
}
}
add_filter('excerpt_more', 'bkn_excerpt_read_more_link');
function bkn_excerpt_read_more_link($more) {
if (!is_admin()) {
global $post;
if($post->post_type != 'event') {
return '… <a href="' . esc_url(get_permalink($post->ID)) . '" class="more-link">Read More <span class="far fa-chevron-right" aria-hidden="true"></span></a>';
} else {
return '…';
}
}
}
add_filter('intermediate_image_sizes_advanced', 'bkn_image_insert_override');
function bkn_image_insert_override($sizes) {
unset($sizes['medium_large']);
return $sizes;
}
add_action('widgets_init', 'bkn_widgets_init');
function bkn_widgets_init() {
register_sidebar(array(
'name' => 'Footer Widget Area 1',
'id' => 'footer-widget-area-1',
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
register_sidebar(array(
'name' => 'Footer Widget Area 2',
'id' => 'footer-widget-area-2',
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
register_sidebar(array(
'name' => 'Footer Widget Area 3',
'id' => 'footer-widget-area-3',
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
register_sidebar(array(
'name' => 'Products Sidebar',
'id' => 'product-sidebar',
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
add_action('wp_head', 'bkn_pingback_header');
function bkn_pingback_header() {
if (is_singular() && pings_open()) {
printf('<link rel="pingback" href="%s" />' . "\n", esc_url(get_bloginfo('pingback_url')));
}
}
add_action('comment_form_before', 'bkn_enqueue_comment_reply_script');
function bkn_enqueue_comment_reply_script() {
if (get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
}
function bkn_custom_pings($comment) {
?><li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?></li><?php
}
add_filter('get_comments_number', 'bkn_comment_count', 0);
function bkn_comment_count($count) {
if (!is_admin()) {
global $id;
$get_comments = get_comments('status=approve&post_id=' . $id);
$comments_by_type = separate_comments($get_comments);
return count($comments_by_type['comment']);
} else {
return $count;
}
}
add_filter('edit_post_link', 'bkn_edit_post_link', 10, 3);
function bkn_edit_post_link($link, $post_id, $text) {
return '';
// if (strlen($link) <= 0) return $link;
// return
// '<a target="_blank" href="' . get_edit_post_link($post_id) . '">
// <span class="screen-reader-text">(Edit this post)</span>
// <span class="fa-stack" aria-hidden="true">
// <span class="fas fa-circle fa-stack-2x"></span>
// <span class="fas fa-pencil fa-stack-1x fa-inverse"></span>
// </span>
// </a>';
}
add_filter('comments_open', 'bkn_filter_media_comment_status', 10, 2);
function bkn_filter_media_comment_status($open, $post_id) {
$post = get_post($post_id);
if ($post->post_type == 'attachment') {
return false;
}
return $open;
}
function bkn_button($button_url = '', $button_text = 'button', $button_class = 'primary') {
echo bkn_get_button($button_url, $button_text, $button_class);
}
function bkn_get_button($button_url = '', $button_text = 'button', $button_class = 'primary'){
if (empty($button_text)) return '';
$button_attr = (!empty($button_url))? 'class="button ' . $button_class . '" href="' . $button_url . '"' : 'class="button disabled"';
return '<a ' . $button_attr . ((strpos($button_url, 'http') !== false)? ' target="_blank" rel="noopener noreferrer"' : '') . '>' . $button_text . '</a>';
}
function bkn_social_button($button_url = '', $button_text = 'button', $button_type = 'none') {
echo bkn_get_social_button($button_url, $button_text, $button_type);
}
function bkn_get_social_button($button_url = '', $button_text = 'button', $button_type = 'none'){
switch($button_type) {
case 'twitter':
if ($button_text == null) $button_text = "Twitter";
$button_text = '<span class="fab fa-twitter" aria-hidden="true"></span> ' . $button_text;
break;
case 'facebook':
if ($button_text == null) $button_text = "Facebook";
$button_text = '<span class="fab fa-facebook-square" aria-hidden="true"></span> ' . $button_text;
break;
case 'discord':
if ($button_text == null) $button_text = "Discord";
$button_text = '<span class="fab fa-discord" aria-hidden="true"></span> ' . $button_text;
break;
case 'telegram':
if ($button_text == null) $button_text = "Telegram";
$button_text = '<span class="fab fa-telegram" aria-hidden="true"></span> ' . $button_text;
break;
case 'youtube':
if ($button_text == null) $button_text = "YouTube";
$button_text = '<span class="fab fa-youtube" aria-hidden="true"></span> ' . $button_text;
break;
case 'soundcloud':
if ($button_text == null) $button_text = "Soundcloud";
$button_text = '<span class="fab fa-soundcloud" aria-hidden="true"></span> ' . $button_text;
break;
case 'mixcloud':
if ($button_text == null) $button_text = "Mixcloud";
$button_text = '<span class="fab fa-mixcloud" aria-hidden="true"></span> ' . $button_text;
break;
case 'bandcamp':
if ($button_text == null) $button_text = "Bandcamp";
$button_text = '<span class="fab fa-bandcamp" aria-hidden="true"></span> ' . $button_text;
break;
case 'website':
if ($button_text == null) $button_text = "Website";
$button_text = '<span class="far fa-globe" aria-hidden="true"></span> ' . $button_text;
break;
}
return bkn_get_button($button_url, $button_text, $button_type);
}
function bkn_first_paragraph() {
$first_paragraph_str = wpautop(get_the_content());
$first_paragraph_str = substr($first_paragraph_str, 0, strpos($first_paragraph_str, '<!-- /wp:paragraph -->') + 4);
$first_paragraph_str = strip_tags($first_paragraph_str, '<i><b><strong><em>');
return '<p>' . $first_paragraph_str . '</p>';
}
function woocommerce_template_loop_product_thumbnail() {
echo '<div class="product-loop-image-wrapper">' . woocommerce_get_product_thumbnail() . '</div>';
}
// Remove company name from checkout
add_filter('woocommerce_checkout_fields', 'bkn_remove_company_name');
function bkn_remove_company_name($fields) {
unset($fields['billing']['billing_company']);
return $fields;
}
add_filter('embed_oembed_html', 'wrap_embed_with_div', 10, 3);
function wrap_embed_with_div($html, $url, $attr) {
return '<div class="video-container">' . $html . '</div>';
}
//add_action('woocommerce_before_customer_login_form', 'bkn_login_message');
//function bkn_login_message() {
// if (get_option('woocommerce_enable_myaccount_registration') == 'yes') {
//
// }
//}
// set billing phone optional
//add_filter('woocommerce_billing_fields', 'bkn_remove_billing_phone_field', 20, 1);
//function bkn_remove_billing_phone_field($fields) {
// $fields ['billing_phone']['required'] = false;
//
//// $fields['billing_email']['class'] = array('form-row-wide');
//// unset($fields ['billing_phone']);
// return $fields;
//}
// Set shipping phone optional
//add_filter('woocommerce_shipping_fields', 'bkn_remove_shipping_phone_field', 20, 1);
//function bkn_remove_shipping_phone_field($fields) {
// $fields ['shipping_phone']['required'] = false;
//
//// unset($fields ['shipping_phone']);
// return $fields;
//}
// Check if WooCommerce is activated
if (!function_exists('is_woocommerce_activated')) {
function is_woocommerce_activated() {
return class_exists('woocommerce');
}
}
// Show shipping info
//add_action('woocommerce_admin_order_data_after_order_details', 'bkn_editable_order_meta_general');
//function bkn_editable_order_meta_general($order) {
//// ini_set('display_startup_errors', 1);
//// ini_set('display_errors', 1);
//// error_reporting(-1);
// $html = '<br><hr><h2>Shipment Info</h2>';
// try {
// /** @var WC_Order $order */
// $shipping_methods = $order->get_shipping_methods();
// if(count($shipping_methods) <= 0) return;
// $shipping_metas = array_values($shipping_methods)[0]->get_meta_data();
// /** @var WC_Meta_Data $shipping_meta */
// $html .= '<ul>';
// foreach ($shipping_metas as $shipping_meta) {
// $data = $shipping_meta->get_data();
// $html .= "<li>" . $data['key'] . ': ';
// if (gettype($data['value']) == "array") {
// $html .= '<pre>' . print_r($data['value'], true) . '</pre>';
// } else {
// $html .= $data['value'];
// }
// $html .= "</li>";
// }
// $html .= '</ul>';
// } catch (Exception $e) {
// $html .= 'An exception occurred. Please report this to LinuxPony.';
// }
// echo $html;
//}
// Kill redirect
function kill_404_redirect() {
if (is_404()) {
add_action('redirect_canonical','__return_false');
}
}
add_action('template_redirect','kill_404_redirect',1);
function bkn_add_custom_box() {
$screens = ['post', 'page', 'product'];
foreach ($screens as $screen) {
add_meta_box(
'bkn_theme_options', // Unique ID
'Theme Options', // Box title
'bkn_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action( 'add_meta_boxes', 'bkn_add_custom_box' );
function bkn_custom_box_html($post) {
$bkn_header_type = get_post_meta($post->ID, '_bkn_header_type', true) ?? '';
$bkn_sr = get_post_meta($post->ID, '_bkn_sr', true) ?? '';
$bkn_image = get_post_meta($post->ID, '_bkn_image', true) ?? '';
?>
<label for="bkn_header_type">Post Header Type:</label>
<br>
<select name="bkn_header_type" id="bkn_header_type" class="postbox">
<option value=""<?php selected($bkn_header_type, '', true); ?>>None</option>
<?php if(class_exists("RevSlider")){ ?>
<option value="sr"<?php selected($bkn_header_type, 'sr', true); ?>>Slider Revolution</option>
<?php } ?>
<option value="image"<?php selected($bkn_header_type, 'image', true); ?>>Image</option>
</select>
<div id="bkn-sr-wrapper">
<?php
if(class_exists("RevSlider")){
$slider = new RevSlider();
$objSliders = $slider->get_sliders();
// CREATE OPTIONS FOR SLIDER SELECTBOX
?>
<label for="bkn_sr">Slider Selection:</label>
<br>
<select name="bkn_sr" id="bkn_sr" class="postbox">
<option value=""<?php selected($bkn_sr, '', true); ?>>(None)</option>
<?php
foreach($objSliders as $slider){ ?>
<option value="<?php echo $slider->alias; ?>"
<?php selected($bkn_sr, $slider->alias, true)?>>
<?php echo $slider->title; ?>
</option>
<?php }
} ?></select>
</div>
<div id="bkn-picture-wrapper">
<label for="bkn_image">Image Selection:</label>
<br>
<input type="text" name="bkn_image" id="bkn_image" class="postbox" readonly="readonly" value="<?php echo $bkn_image ?>">
<img id="bkn_image_preview" src="" height="25" style="width: auto;">
<button id="bkn_image_picker">Choose...</button>
</div>
<script>
let file_frame;
jQuery(function ($){
function bknMediaPicker(e) {
e.preventDefault();
let field = $('#bkn_image');
// Uploading files
let wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
let set_to_post_id = field.val();
// If the media frame already exists, reopen it.
if (file_frame) {
// Set the post ID to what we want
file_frame.uploader.uploader.param('post_id', set_to_post_id);
// Open frame
file_frame.open();
return;
} else {
// Set the wp.media post id so the uploader grabs the ID we want when initialised
wp.media.model.settings.post.id = set_to_post_id;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: 'Select a image to upload',
button: {
text: 'Use this image',
},
multiple: false // Set to true to allow multiple files to be selected
});
// When an image is selected, run a callback.
file_frame.on('select', function() {
// We set multiple to false so only get one image from the uploader
let attachment = file_frame.state().get('selection').first().toJSON();
// Do something with attachment.id and/or attachment.url here
$('#bkn_image_preview').attr('src', attachment.url);
field.val( attachment.id );
// Restore the main post ID
wp.media.model.settings.post.id = wp_media_post_id;
});
// Finally, open the modal
file_frame.open();
}
function bknAdjustView(e) {
if(e) e.preventDefault();
$('#bkn-sr-wrapper, #bkn-picture-wrapper').hide();
switch ($('#bkn_header_type').val()) {
case "sr":
$('#bkn-sr-wrapper').show();
break;
case "image":
$('#bkn-picture-wrapper').show();
break;
case "":
default:
break;
}
}
function bknLoadImageAsync(attachment, target){
if(!attachment || !target) return;
if (!wp.media.attachment(attachment).get('url')) {
wp.media.attachment(attachment).fetch().then(function () {
$(target).attr('src', wp.media.attachment(attachment).get('url')).css( 'width', 'auto' );
});
} else {
$(target).attr('src', wp.media.attachment(attachment).get('url')).css( 'width', 'auto' );
}
}
$('#bkn_image_picker').on('click', bknMediaPicker);
$('#bkn_header_type').on('change', bknAdjustView);
bknLoadImageAsync($("#bkn_image").val(),"#bkn_image_preview");
bknAdjustView();
});
</script>
<?php
}
function bkn_save_postdata($post_id) {
if (array_key_exists('bkn_header_type', $_POST)) {
update_post_meta(
$post_id,
'_bkn_header_type',
$_POST['bkn_header_type']
);
} else {
delete_post_meta($post_id, '_bkn_header_type');
}
if (array_key_exists('bkn_sr', $_POST)) {
update_post_meta(
$post_id,
'_bkn_sr',
$_POST['bkn_sr']
);
} else {
delete_post_meta($post_id, '_bkn_sr');
}
if (array_key_exists('bkn_image', $_POST)) {
update_post_meta(
$post_id,
'_bkn_image',
$_POST['bkn_image']
);
} else {
delete_post_meta($post_id, '_bkn_image');
}
}
add_action('save_post', 'bkn_save_postdata');
function bkn_do_slider_or_image($id = null) {
$id = $id ?? get_post()->ID;
$bkn_header_type = get_post_meta($id, '_bkn_header_type', true) ?? '';
if($bkn_header_type == 'sr'){
$bkn_sr = get_post_meta($id, '_bkn_sr', true) ?? '';
echo do_shortcode('[rev_slider alias="' . $bkn_sr . '"][/rev_slider]');
} else if ($bkn_header_type == 'image'){
$bkn_image = get_post_meta($id, '_bkn_image', true) ?? '';
echo '<img class="cover-image" src="' . wp_get_attachment_image_url($bkn_image, 'full', false) . '">';
echo '<div class="cover-image-spacer"></div>';
} else {
echo '<div class="no-content-spacer"></div>';
}
}
function bkn_has_webp_support() {
return strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false;
}
function bkn_maybe_replace_webp($url) {
if(bkn_has_webp_support()){
return str_replace(['.jpg', '.jpeg', '.png', '.gif'], '.webp', $url);
}
return $url;
}
/**
* Renders the social nav. Should be called from headers.php
*/
function bkn_do_social_menu() {
global $bkn_theme_social_settings_fields;
$to_echo = '<div class="social"><ul class="menu">';
$fields_populated = 0;
foreach ($bkn_theme_social_settings_fields as $setting) {
$value = get_option($setting['id']);
if(!empty($value)){
$fields_populated++;
$to_echo .= '<li class="menu-item"><a href="' . $value . '" rel="noopener noreferrer" target="_blank"><i class="' . $setting['icon_class'] . '" aria-hidden="true"></i><span class="screen-reader-text">' . $setting['title'] . '</span></a></li>';
}
}
$to_echo .= '</ul></div>';
if($fields_populated > 0) echo $to_echo;
}
require_once 'functions-admin-page.php';
require_once 'current-post-type-widget.php';