-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
699 lines (540 loc) · 17.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<?php
load_theme_textdomain( 'siforum', get_template_directory() . '/lang' );
// Add Menu
add_theme_support( 'menus' );
register_nav_menus(
array(
'header_menu' => 'Header Menu',
)
);
// Add Widgets
/*
register_sidebars( 1, array( 'name' => 'Sidebar_Single' ) );
register_sidebars( 1, array( 'name' => 'Sidebar_Index' ) );
*/
// Login - Register Logo Change
function siforum_logo_change() { ?>
<style type="text/css">
body{
background:#14191f !important;
font-size:20px !important;
}
.login form{
border-radius:10px;
}
#login h1 a, .login h1 a {
background: url(<?php echo get_stylesheet_directory_uri(); ?>/img/logo.png) center no-repeat red;
height:105px;
width:320px;
padding-bottom: 30px ;
border-radius:10px;
}
</style>
<?php
}
add_action( 'login_enqueue_scripts', 'siforum_logo_change' );
// LOGIN and REGISTER LOGO URL
/*
function custom_loginlogo_url($url) {
return bloginfo('url');
}
add_filter( 'login_headerurl', 'custom_loginlogo_url' );
*/
// Allowing html tags like the_content output
function filter_html_usage() {
global $allowedtags;
$allowedtags['code'] = array( 'class' => array() );
$allowedtags['pre'] = array( 'class' => array() );
$allowedtags['p'] = array( 'class' => array() );
$allowedtags['strong'] = array( 'class' => array() );
$allowedtags['href'] = array( 'class' => array() );
$allowedtags['a'] = array( 'class' => array() );
$allowedtags['ul'] = array( 'class' => array() );
$allowedtags['li'] = array( 'class' => array() );
$allowedtags['i'] = array( 'class' => array() );
$allowedtags['h1'] = array( 'class' => array() );
$allowedtags['h2'] = array( 'class' => array() );
$allowedtags['h3'] = array( 'class' => array() );
$allowedtags['b'] = array( 'class' => array() );
$allowedtags['abbr'] = array( 'class' => array() );
$allowedtags['acronym'] = array( 'class' => array() );
$allowedtags['blockquote'] = array( 'class' => array() );
$allowedtags['cite'] = array( 'class' => array() );
$allowedtags['em'] = array( 'class' => array() );
}
add_action( 'init', 'filter_html_usage', 11 );
// Theme Settings
/*
function siforum_customize_register( $wp_customize ) {
$wp_customize->add_section(
'themename_color_scheme',
array(
'title' => __( 'Color Scheme', 'themename' ),
'description' => '',
'priority' => 120,
)
);
// =============================
// = Text Input =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[text_test]',
array(
'default' => 'value_xyz',
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
'themename_text_test',
array(
'label' => __( 'Text Test', 'themename' ),
'section' => 'themename_color_scheme',
'settings' => 'siforum_theme_options[text_test]',
)
);
// =============================
// = Radio Input =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[color_scheme]',
array(
'default' => 'value2',
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
'themename_color_scheme',
array(
'label' => __( 'Color Scheme', 'themename' ),
'section' => 'themename_color_scheme',
'settings' => 'siforum_theme_options[color_scheme]',
'type' => 'radio',
'choices' => array(
'value1' => 'Choice 1',
'value2' => 'Choice 2',
'value3' => 'Choice 3',
),
)
);
// =============================
// = Checkbox =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[checkbox_test]',
array(
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
'display_header_text',
array(
'settings' => 'siforum_theme_options[checkbox_test]',
'label' => __( 'Display Header Text' ),
'section' => 'themename_color_scheme',
'type' => 'checkbox',
)
);
// =============================
// = Select Box =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[header_select]',
array(
'default' => 'value2',
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
'example_select_box',
array(
'settings' => 'siforum_theme_options[header_select]',
'label' => 'Select Something:',
'section' => 'themename_color_scheme',
'type' => 'select',
'choices' => array(
'value1' => 'Choice 1',
'value2' => 'Choice 2',
'value3' => 'Choice 3',
),
)
);
// =============================
// = Image Upload =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[image_upload_test]',
array(
'default' => 'image.jpg',
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'image_upload_test',
array(
'label' => __( 'Image Upload Test', 'themename' ),
'section' => 'themename_color_scheme',
'settings' => 'siforum_theme_options[image_upload_test]',
)
)
);
// =============================
// = File Upload =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[upload_test]',
array(
'default' => 'uploadtest',
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
new WP_Customize_Upload_Control(
$wp_customize,
'upload_test',
array(
'label' => __( 'Upload Test', 'themename' ),
'section' => 'themename_color_scheme',
'settings' => 'siforum_theme_options[upload_test]',
)
)
);
// =============================
// = Color Picker =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[link_color]',
array(
'default' => '#000',
'sanitize_callback' => 'sanitize_hex_color',
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'link_color',
array(
'label' => __( 'Link Color', 'themename' ),
'section' => 'themename_color_scheme',
'settings' => 'siforum_theme_options[link_color]',
)
)
);
// =============================
// = Page Dropdown =
// =============================
$wp_customize->add_setting(
'siforum_theme_options[page_test]',
array(
'capability' => 'edit_theme_options',
'type' => 'option',
)
);
$wp_customize->add_control(
'themename_page_test',
array(
'label' => __( 'Page Test', 'themename' ),
'section' => 'themename_color_scheme',
'type' => 'dropdown-pages',
'settings' => 'siforum_theme_options[page_test]',
)
);
// =====================
// = Category Dropdown =
// =====================
$categories = get_categories();
$cats = array();
$i = 0;
foreach ( $categories as $category ) {
if ( $i == 0 ) {
$default = $category->slug;
$i++;
}
$cats[ $category->slug ] = $category->name;
}
$wp_customize->add_setting(
'_s_f_slide_cat',
array(
'default' => $default,
)
);
$wp_customize->add_control(
'cat_select_box',
array(
'settings' => '_s_f_slide_cat',
'label' => 'Select Category:',
'section' => '_s_f_home_slider',
'type' => 'select',
'choices' => $cats,
)
);
}
add_action( 'customize_register', 'siforum_customize_register' );
*/
// CATEGORY ICONS and COLORS
function wcr_category_fields( $term ) {
if ( current_filter() == 'category_edit_form_fields' ) {
$icon_slug = get_term_meta( $term->term_id, 'icon_slug', true );
$color_code = get_term_meta( $term->term_id, 'color_code', true );
?>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[icon_slug]"><?php _e( 'Dash Icon Slug-Name' ); ?></label></th>
<td>
<input type="text" size="40" value="<?php echo esc_attr( $icon_slug ); ?>" id="term_fields[icon_slug]" name="term_fields[icon_slug]"><br/>
<span class="description"><?php _e( 'Dash Icon Slug-Name' ); ?> - https://developer.wordpress.org/resource/dashicons/</span>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[color_code]"><?php _e( 'Color code' ); ?></label></th>
<td>
<input type="text" size="40" value="<?php echo esc_attr( $color_code ); ?>" id="term_fields[color_code]" name="term_fields[color_code]"><br/>
<span class="description"><?php _e( 'Please enter color hex code' ); ?> - https://www.w3.org/wiki/CSS/Properties/color/keywords</span>
</td>
</tr>
<?php
} elseif ( current_filter() == 'category_add_form_fields' ) {
?>
<div class="form-field">
<label for="term_fields[icon_slug]"><?php _e( 'Dash-Icon-Name-Slug' ); ?></label>
<textarea cols="40" rows="1" id="term_fields[icon_slug]" name="term_fields[icon_slug]"></textarea>
<p class="description">enter dashicon name - https://developer.wordpress.org/resource/dashicons/</p>
</div>
<div class="form-field">
<label for="term_fields[color_code]"><?php _e( 'Color code' ); ?></label>
<input type="text" size="40" value="" id="term_fields[color_code]" name="term_fields[color_code]">
<p class="description">enter color code/name - https://www.w3.org/wiki/CSS/Properties/color/keywords</p>
</div>
<?php
}
}
add_action( 'category_add_form_fields', 'wcr_category_fields', 10, 2 );
add_action( 'category_edit_form_fields', 'wcr_category_fields', 10, 2 );
function wcr_save_category_fields( $term_id ) {
if ( ! isset( $_POST['term_fields'] ) ) {
return;
}
foreach ( $_POST['term_fields'] as $key => $value ) {
update_term_meta( $term_id, $key, sanitize_text_field( $value ) );
}
}
add_action( 'edited_category', 'wcr_save_category_fields', 10, 2 );
add_action( 'create_category', 'wcr_save_category_fields', 10, 2 );
// SiForum Comments Custom Callback
function atarikafa_comments( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
<div class="comment-wrap">
<div class="comment-img">
<a href="<?php echo get_comment_author_url(); ?>">
<?php echo get_avatar( $comment, $args['avatar_size'], null, null, array( 'class' => array( 'img-responsive', 'img-circle' ) ) ); ?>
</a>
</div>
<div class="comment-body">
<span class="comment-author-name"><?php echo get_comment_author_link(); ?></span>
<span class="comment-date"><?php printf( __( '%1$s at %2$s', 'siforum' ), get_comment_date(), get_comment_time() ); ?></span>
<?php
if ( $comment->comment_approved == '0' ) {
?>
<em><i class="fa fa-spinner fa-spin" aria-hidden="true"></i> <?php _e( 'Comment awaiting approval', 'siforum' ); ?></em><br /><?php } ?>
<?php comment_text(); ?>
<span class="comment-reply">
<?php
comment_reply_link(
array_merge(
$args,
array(
'reply_text' => __( 'Reply', 'siforum' ),
'depth' => $depth,
'max_depth' => $args['max_depth'],
)
),
$comment->comment_ID
);
?>
</span>
</div>
</div>
<?php
}
// Security - Removing some Actions for normal users
add_action( 'bulk_actions-edit-comments', 'author_remove_comments_actions' );
add_action( 'comment_row_actions', 'author_remove_comments_actions' );
function author_remove_comments_actions( $actions ) {
if ( ! current_user_can( 'moderate_comments' ) ) {
if ( isset( $actions['delete'] ) ) {
unset( $actions['delete'] );
}
if ( isset( $actions['trash'] ) ) {
unset( $actions['trash'] );
}
if ( isset( $actions['spam'] ) ) {
unset( $actions['spam'] );
}
if ( isset( $actions['edit'] ) ) {
unset( $actions['edit'] );
}
if ( isset( $actions['quickedit'] ) ) {
unset( $actions['quickedit'] );
}
if ( isset( $actions['approve'] ) ) {
unset( $actions['approve'] );
}
if ( isset( $actions['unapprove'] ) ) {
unset( $actions['unapprove'] );
}
}
return $actions;
}
// Security - Hiding comment IPs for normal users
function lp_remove_IP_for_user( $comment_author_IP, $comment_ID, $comment ) {
if ( ! is_admin() ) {
return $comment_author_IP; // only do this on admin, though with this particular function, it probably only applies to admin at all
}
if ( ! current_user_can( 'manage_options' ) ) {
return '';
}
return $comment_author_IP;
}
add_filter( 'get_comment_author_IP', 'lp_remove_IP_for_user', 10, 3 );
// Security - Hiding others posts for user to see
function only_show_own_author_posts( $wp_query ) {
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-admin/edit.php' ) !== false ) {
if ( ! current_user_can( 'activate_plugins' ) ) {
add_action( 'views_edit-post', 'remove_items_from_edit' );
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
add_filter( 'parse_query', 'only_show_own_author_posts' );
function remove_items_from_edit( $views ) {
unset( $views['all'] );
unset( $views['publish'] );
unset( $views['trash'] );
unset( $views['draft'] );
unset( $views['pending'] );
return $views;
}
// Load More Posts
function misha_my_load_more_scripts() {
global $wp_query;
wp_enqueue_script( 'jquery' );
wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/assets/js/siforum.js', array( 'jquery' ) , 15 , true );
wp_localize_script(
'my_loadmore',
'misha_loadmore_params',
array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
'current_page' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
'max_page' => $wp_query->max_num_pages,
)
);
wp_enqueue_script( 'my_loadmore' );
}
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );
function misha_loadmore_ajax_handler() {
// prepare our arguments for the query
$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
$args['post_status'] = 'publish';
// it is always better to use WP_Query but not here
query_posts( $args );
if ( have_posts() ) :
// run the loop
while ( have_posts() ) :
the_post();
?>
<a href="<?php the_permalink(); ?>" class="forum-post-index">
<span class="forum-post-index-comment-count">
<span class="dashicons dashicons-welcome-comments"></span><?php echo get_comments_number( $post->ID ); ?>
</span>
<span class="forum-post-index-category">
<?php
$categories = get_the_terms( $post->ID, 'category' );
$i = 1;
foreach ( $categories as $c ) {
$termid = $c->term_id;
$color_code = get_term_meta( $termid, 'color_code', true );
echo '<span style="background:' . $color_code . '">' . $c->name . '</span>';
if ( ++$i > 3 ) {
break;
}
}
?>
</span>
<div class="forum-post-index-avatar"><?php echo get_avatar( get_the_author_meta( 'ID' ), 50 ); ?></div>
<div href="<?php the_permalink(); ?>" class="forum-post-index-title"><?php the_title(); ?> </div>
<span class="forum-post-index-author"><b><?php the_author(); ?></b>
<?php
$t = get_the_time( 'U' );
echo human_time_diff( $t, current_time( 'U' ) ) . __( ' ago','siforum' );
?>
</span>
</a>
<?php
endwhile;
endif;
die;
}
// Some Ajax for Image Upload, Reply and New Post
add_action( 'wp_ajax_loadmore', 'misha_loadmore_ajax_handler' ); // wp_ajax_{action}
add_action( 'wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler' ); // wp_ajax_nopriv_{action}
/** Ajax Image Upload */
add_action( 'wp_ajax_sicomment_file_upload', 'si_comment_file_upload' );
function si_comment_file_upload() {
check_ajax_referer( 'file_upload', 'security' );
$arr_img_ext = array( 'image/png', 'image/jpeg', 'image/jpg', 'image/gif' );
if ( $_FILES ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
if ( in_array( $_FILES['file']['type'], $arr_img_ext, true ) ) {
$upload = media_handle_upload( 'file', 0 );
if ( $upload && ! is_wp_error( $upload ) ) {
wp_send_json_success( si_get_image_for_return( $upload ), 200 );
} else {
wp_send_json_error( 'Error', 403, );
}
}
wp_die();
}
function si_get_image_for_return( $id, $size = 'full' ) {
return wp_json_encode( wp_get_attachment_image_src( $id, $size )[0] );
}
/** Ajax Image Upload */
/** Ajax New Post */
add_action( 'wp_ajax_create_new_from_from_index', 'create_new_from_from_index' );
function create_new_from_from_index(){
check_ajax_referer( 'new_post_sec', 'security' );
$the_content_text = $_POST['text'];
$tidy = new tidy();
$clean_text = $tidy->repairString($the_content_text);
$args = array(
'post_title' => wp_strip_all_tags( $_POST['title'] ),
'post_content' => $_POST['text'],
'post_status' => 'publish',
'post_author' => $_POST['author'],
'post_category' => array( $_POST['cat'] )
);
$post_id = wp_insert_post($args);
if(!is_wp_error($post_id)){
wp_send_json_success( get_the_permalink( $post_id ), 200 );
}else{
wp_send_json_error( $post_id->get_error_message(), 403, );
}
wp_die();
}
/** Ajax New Post */
?>