forked from hakkens/davehakkens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
639 lines (501 loc) · 19.4 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
include dirname( __FILE__ ) . '/includes/meta-boxes.php';
include_once dirname( __FILE__ ) . '/includes/pins.php';
function login_redirect_control( $redirect_to, $request, $user ) {
$urlParts = parse_url($request);
parse_str($urlParts['query'], $query);
if (isset($query['redirect_to'])) {
$redir = $query['redirect_to'];
if (substr($redir, 0, 4) == 'USER') {
return '/community/members/' . $user->user_nicename . substr($redir, 4);
}
return '/' . $redir;
}
return '/community/forums';
}
add_filter( 'login_redirect', 'login_redirect_control', 10 ,3);
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-formats', [ 'image', 'status', 'video', 'link' ] );
add_action( 'init', 'register_project_post_type' );
add_action( 'init', 'allow_origin' );
function allow_origin() {
header("Access-Control-Allow-Origin: *");
}
//Load php partials for particular parts of BP
function action_bp_before_register_page() {
include_once 'register-message.php';
}
add_action('bp_before_register_page', 'action_bp_before_register_page');
function action_bp_before_activation_page() {
include_once 'activate-message.php';
}
add_action('bp_before_activation_page', 'action_bp_before_activation_page');
//Change BP already registered redirect
function bp_loggedin_register_redirect( $redirect ) {
$user = get_userdata(bp_loggedin_user_id());
$redirect = isset($_REQUEST['add-pin'])
? '/community/members/' . $user->user_nicename . '/pins/'
: '/community/forums';
return $redirect;
}
add_filter( 'bp_loggedin_register_page_redirect_to', 'bp_loggedin_register_redirect' );
//add "sort-by-likes" endpoint for reply sorting purposes
function add_enpoint_for_reply_sorting() {
global $wp_rewrite;
add_rewrite_endpoint( 'sort-by-likes', EP_ALL );
}
add_action( 'init', 'add_enpoint_for_reply_sorting' );
//change the exipiration of the auth token
function my_expiration_filter($seconds, $user_id, $remember){
$expiration = 14*24*60*60; //2 weeks
return $expiration;
}
add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function get_vine_thumbnail( $id ) {
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="og:image" content="(.*?)"/', $vine, $matches);
return ( $matches[1] ) ? $matches[1] : false;
}
function dh_get_flag_by_location($country){
if($country <> '' && !empty($country)){
$country_filename = get_stylesheet_directory_uri() . '/img/flags/' . sanitize_file_name($country) . '.png';
$country_path = get_stylesheet_directory() . '/img/flags/' . sanitize_file_name($country). '.png';
if(file_exists($country_path)){
$html = '<img src="' . $country_filename . '"/>';
} else {
$html = $country;
echo '<!--' . get_stylesheet_directory_uri() . '/img/flags/' . sanitize_file_name($country) . '-->';
}
echo $html;
}
}
function register_project_post_type(){
register_post_type( 'projects', array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Project' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'project',
),
'supports' => array(
'title',
'author',
'excerpt',
'editor',
'thumbnail',
'revisions',
'custom-fields',
)
));
}
$args = [
'name' => __( 'Forum sidebar' ),
'id' => "forum-sidebar",
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>\n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>\n",
];
register_sidebar( $args );
add_post_type_support( 'forum', [ 'thumbnail' ] );
add_post_type_support( 'topic', [ 'thumbnail' ] );
function custom_bbp_show_lead_topic( $show_lead ) {
$show_lead[] = 'true';
return $show_lead;
}
add_filter( 'bbp_show_lead_topic', 'custom_bbp_show_lead_topic' );
add_action( 'wp_login_failed', 'my_front_end_login_fail' );
function my_front_end_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER'];
if ( !empty( $referrer ) && !strstr( $referrer, 'wp-login' ) && !strstr( $referrer, 'wp-admin' ) ) {
wp_redirect( $referrer . '?login=failed' );
exit;
}
}
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . get_the_content();
}
return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
register_nav_menus([
'grid_filter' => 'Filter for post grid',
]);
function dave_hakkens_scripts() {
wp_enqueue_script( 'jcrop', '/wp-includes/js/jcrop/jquery.Jcrop.min.js', array( 'jquery' ) );
wp_enqueue_script( 'fancybox', get_bloginfo( 'template_url' ) . '/js/vendor/fancybox/jquery.fancybox.pack.js', array( 'jquery' ) );
wp_enqueue_script( 'isotope', get_bloginfo( 'template_url' ) . '/js/vendor/isotope.pkgd.min.js', array( 'jquery' ) );
wp_enqueue_script( 'scroll_to', get_bloginfo( 'template_url' ) . '/js/vendor/jquery.scroll_to.js', array( 'jquery' ) );
wp_enqueue_script( 'mousewheel', get_bloginfo( 'template_url' ) . '/js/vendor/jquery.mousewheel.min.js', array( 'jquery' ) );
wp_enqueue_script( 'fullpage-js', get_bloginfo( 'template_url' ) . '/js/vendor/fullpage.min.js', array( 'jquery' ) );
wp_enqueue_script( 'dh_plugins', get_bloginfo( 'template_url' ) . '/js/plugins.js', array( 'jquery' ) );
wp_enqueue_script( 'dh_main_js', get_bloginfo( 'template_url' ) . '/js/main.js', array( 'jquery', 'fancybox', 'isotope', 'scroll_to', 'mousewheel', 'dh_plugins' ), rand(999,2500) );
}
add_action( 'wp_enqueue_scripts', 'dave_hakkens_scripts' );
//Remove "Billing Details" for all gateways give plugin
function give_remove_billing_fields(){
remove_action( 'give_after_cc_fields', 'give_default_cc_address_fields' );
}
add_action('init', 'give_remove_billing_fields');
//change more.. on homepage
function modify_read_more_link() {
return '<a class="more-link" href="' . get_permalink() . '">Read all..</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );
//set max topic title to 50
add_filter ('bbp_get_title_max_length','rkk_change_title') ;
Function rkk_change_title ($default) {
$default=50 ;
return $default ;
}
//Hide admin bar
add_filter('show_admin_bar', '__return_false');
//Remove user info
add_filter('user_contactmethods','hide_profile_fields',10,1);
function hide_profile_fields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}
//Remove color scheme admin panel
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
//Make frehness shorter in bbpress
function short_freshness_time( $output, $older_date, $newer_date ) {
$output = preg_replace( '/, .*[^ago]/', ' ', $output );
return $output;
}
add_filter( 'bbp_get_time_since', 'short_freshness_time' );
//Add extra class for topic lead
add_filter( 'bbp_show_lead_topic', '__return_true' );
//increae logout time
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
function keep_me_logged_in_for_1_year( $expirein ) {
return 604800; // 1 year in seconds
}
//Custom css moderator
add_filter('bbp_before_get_reply_author_role_parse_args', 'ntwb_bbpress_reply_css_role' );
function ntwb_bbpress_reply_css_role() {
$role = strtolower( bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) ) );
$args['class'] = 'bbp-author-role bbp-author-role-' . $role;
$args['before'] = '';
$args['after'] = '';
return $args;
}
add_filter('bbp_before_get_topic_author_role_parse_args', 'ntwb_bbpress_topic_css_role' );
function ntwb_bbpress_topic_css_role() {
$role = strtolower( bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id ) ) );
$args['class'] = 'bbp-author-role bbp-author-role-' . $role;
$args['before'] = '';
$args['after'] = '';
return $args;
}
//Redirect wp-login to community login
function redirect_login_page() {
$login_page = home_url( 'community/login/' );
$page_viewed = basename($_SERVER['REQUEST_URI']);
if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
wp_redirect($login_page);
exit;
}
}
add_action('init','redirect_login_page');
function logout_page() {
$login_page = home_url( 'community/login/' );
wp_redirect( $login_page . "?login=false" );
exit;
}
add_action('wp_logout','logout_page');
function login_failed() {
$login_page = home_url( '/login/' );
wp_redirect( $login_page . '?login=failed' );
exit;
}
add_action( 'wp_login_failed', 'login_failed' );
function verify_username_password( $user, $username, $password ) {
$login_page = home_url( 'community/login/' );
if( $username == "" || $password == "" ) {
wp_redirect( $login_page . "?login=empty" );
exit;
}
}
add_filter( 'authenticate', 'verify_username_password', 1, 3);
//Remove | (stripe) next to bbpress subscripe and favorites
function hide_before3 ($args = array() ) {
$args['before'] = '';
return $args;
}
add_filter ('bbp_before_get_user_subscribe_link_parse_args','hide_before3');
//remove the + from wp ulike
add_filter('wp_ulike_format_number','wp_ulike_new_format_number',10,3);
function wp_ulike_new_format_number($value, $num, $plus){
if ($num >= 1000 && get_option('wp_ulike_format_number') == '1'):
$value = round($num/1000, 2) . 'K';
else:
$value = $num;
endif;
return $value;
}
//change logo login
function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login.svg) !important; }
</style>';
}
add_action('login_head', 'custom_loginlogo');
//ad sidebar
if ( is_active_sidebar( 'primary' ) ) {
echo '<div id="primary" class="sidebar aside">';
dynamic_sidebar( 'primary' );
echo '</div>';
}
/*
* Let contributor manage users, and run this only once.
*/
function isa_contributor_manage_users() {
if ( get_option( 'isa_add_cap_contributor_once' ) != 'done' ) {
// let contributor manage users
$edit_contributor = get_role('contributor'); // Get the user role
$edit_contributor->add_cap('edit_users');
$edit_contributor->add_cap('list_users');
$edit_contributor->add_cap('promote_users');
$edit_contributor->add_cap('create_users');
$edit_contributor->add_cap('add_users');
$edit_contributor->add_cap('delete_users');
update_option( 'isa_add_cap_contributor_once', 'done' );
}
}
add_action( 'init', 'isa_contributor_manage_users' );
//prevent contributor from deleting, editing, or creating an administrator
// only needed if the contributor was given right to edit users
class ISA_User_Caps {
// Add our filters
function __construct() {
add_filter( 'editable_roles', array(&$this, 'editable_roles'));
add_filter( 'map_meta_cap', array(&$this, 'map_meta_cap'),10,4);
}
// Remove 'Administrator' from the list of roles if the current user is not an admin
function editable_roles( $roles ){
if( isset( $roles['administrator'] ) && !current_user_can('administrator') ){
unset( $roles['administrator']);
}
return $roles;
}
// If someone is trying to edit or delete an
// admin and that user isn't an admin, don't allow it
function map_meta_cap( $caps, $cap, $user_id, $args ){
switch( $cap ){
case 'edit_user':
case 'remove_user':
case 'promote_user':
if( isset($args[0]) && $args[0] == $user_id )
break;
elseif( !isset($args[0]) )
$caps[] = 'do_not_allow';
$other = new WP_User( absint($args[0]) );
if( $other->has_cap( 'administrator' ) ){
if(!current_user_can('administrator')){
$caps[] = 'do_not_allow';
}
}
break;
case 'delete_user':
case 'delete_users':
if( !isset($args[0]) )
break;
$other = new WP_User( absint($args[0]) );
if( $other->has_cap( 'administrator' ) ){
if(!current_user_can('administrator')){
$caps[] = 'do_not_allow';
}
}
break;
default:
break;
}
return $caps;
}
}
$isa_user_caps = new ISA_User_Caps();
function tinymce_other_css_for_content( $init ) {
$init['content_css'] = get_bloginfo('stylesheet_url');
return $init;
}
add_filter('tiny_mce_before_init', 'tinymce_other_css_for_content');
// ALTER TinyMCE INIT FOR VISUAL EDITOR ON FORUM TOPICS AND REPLIES
// SOURCE: https://bbpress.org/forums/topic/alter-mceinit-for-visual-editor-on-topics/
// Enable visual editor on tinymce in bbPress
function bbp_enable_visual_editor( $args = array() ) {
$args['tinymce'] = true;
$args['quicktags'] = false;
return $args;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor' );
// Enable TinyMCE paste plugin
function bbp_add_paste_plugin($args) {
$args[] = 'paste';
return $args;
}
add_filter( 'teeny_mce_plugins', 'bbp_add_paste_plugin');
// ADDS A JQUERY PASTE PREPROCESSOR TO REMOVE DISALLOWED TAGS WHILE PASTING
// SOURCE https://jonathannicol.com/blog/2015/02/19/clean-pasted-text-in-wordpress/
function configure_tinymce($in) {
$in['paste_preprocess'] = "function(plugin, args){
// Strip all HTML tags except those we have whitelisted
var whitelist = 'a,p,b,strong,i';
var stripped = jQuery('<div>' + args.content + '</div>');
var els = stripped.find('*').not(whitelist);
for (var i = els.length - 1; i >= 0; i--) {
var e = els[i];
jQuery(e).replaceWith(e.innerHTML);
}
// Strip all class and id attributes
stripped.find('*').removeAttr('id').removeAttr('class');
// Return the clean HTML
args.content = stripped.html();
}";
return $in;
}
add_filter('teeny_mce_before_init','configure_tinymce', 99999999999);
function custom_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read more...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
/* Modified from 'mycred_display_users_badges' to just display selected badges, TODO: pass argumment and merge upstream */
if ( ! function_exists( 'mycred_display_custom_users_badges' ) ) {
function mycred_display_custom_users_badges( $user_id = NULL, $width = MYCRED_BADGE_WIDTH, $height = MYCRED_BADGE_HEIGHT ) {
$user_id = absint( $user_id );
if ( $user_id === 0 ) return;
$valid_badges = array(4709, 4710, 4744);
$users_badges = mycred_get_users_badges( $user_id );
echo '<div class="row" id="mycred-users-badges"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
do_action( 'mycred_before_users_badges', $user_id, $users_badges );
if ( ! empty( $users_badges ) ) {
foreach ( $users_badges as $badge_id => $level ) {
if (!in_array($badge_id, $valid_badges))continue;
$badge = mycred_get_badge( $badge_id, $level );
if ( $badge === false ) continue;
$badge->image_width = $width;
$badge->image_height = $height;
if ( $badge->level_image !== false )
echo apply_filters( 'mycred_the_badge', $badge->get_image( $level ), $badge_id, $badge, $user_id );
}
}
do_action( 'mycred_after_users_badges', $user_id, $users_badges );
echo '</div></div>';
}
}
function davehakkens2_widgets_init() {
require get_template_directory() . '/includes/widgets.php';
register_widget( 'Latest_Community_Uploads' );
}
add_action( 'widgets_init', 'davehakkens2_widgets_init' );
/**
* Over write existing comment
*/
function davehakkens_theme_comment($comment, $args, $depth) {
if ( 'div' === $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}?>
<?php
$comment_data = get_comment( $comment->comment_ID, ARRAY_A );
$user_info = get_userdata($comment_data['user_id']);?>
<<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID() ?>">
<?php
if ( 'div' != $args['style'] ) { ?>
<div id="div-comment-<?php comment_ID() ?>" class="comment-body"><?php
} ?>
<div class="comment-author vcard"><?php
if ( $args['avatar_size'] != 0 ) {
echo '<a href="/community/members/'.$user_info->user_nicename.'">'.get_avatar( $comment, $args['avatar_size'] ).'</a>';
}
//printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() );
?>
</div><?php
if ( $comment->comment_approved == '0' ) { ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em><br/><?php
} ?>
<div class="comment-meta commentmetadata">
<a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>"><?php
/* translators: 1: date, 2: time */
printf(
__('%1$s at %2$s'),
get_comment_date(),
get_comment_time()
); ?>
</a><?php
edit_comment_link( __( '(Edit)' ), ' ', '' ); ?>
</div>
<div id="custom_comment">
<a href='/community/members/<?php echo $user_info->user_nicename; ?> '>
<?php
$country = xprofile_get_field_data( 42, $comment_data['user_id']);
dh_get_flag_by_location($country);
?>
</a>
</div>
<div class='comment_author_name'><a href='/community/members/<?php echo $user_info->user_nicename; ?> '><?php echo '<span class="post_author_name">'.$user_info->display_name.'</span>'; ?> </a>
<div class="date"> -
<?php
echo esc_html( human_time_diff( get_comment_date( 'U', $comment->comment_ID ), current_time('timestamp') ) ) . ' ago'; ?>
</div> </div>
<?php comment_text(); ?>
<div class="reply"><?php
comment_reply_link(
array_merge(
$args,
array(
'add_below' => $add_below,
'depth' => $depth,
'max_depth' => $args['max_depth']
)
)
); ?>
</div><?php
if ( 'div' != $args['style'] ) : ?>
</div><?php
endif;
}
/**
* Register our sidebars and widgetized areas.
*
*/
function community_widgets_init() {
register_sidebar( array(
'name' => 'Community info',
'id' => 'community_info',
) );
}
add_action( 'widgets_init', 'community_widgets_init' );
add_action('profile_pic_upload_button','show_profile_pic_button',10);
function show_profile_pic_button(){
$user_id = get_current_user_id();
global $bp;
if(!empty($user_id)):
$profile = bp_core_get_user_domain($user_id);
if( !bp_get_user_has_avatar()) : ?>
<div class="upload_profile">
<a href="<?php echo $profile.'/profile/change-avatar/#avatar-upload-form'; ?>">Upload your Profile pic</a>
</div>
<?php
endif;
endif;
} ?>