-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.php
537 lines (378 loc) · 13.6 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
<?php
load_theme_textdomain( 'siforum', get_template_directory() . '/lang' );
require_once get_stylesheet_directory() . '/class-siforumcustomizer.php';
// 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' );
*/
function siforum_get_header_logo_and_link() {
$logo = get_theme_mod( 'siforum_header_image' );
$link = get_theme_mod( 'siforum_header_link' );
if ( ! empty( $logo ) && ! empty( $link ) ) {
$wh = str_replace( get_home_url() . '/', '', $logo );
$whh = getimagesize( $wh );
$html = '<a href="' . $link . '" class="forum-header-menu-logo-customized"> </a>';
$html .= '<style>.forum-header-menu-logo-customized{width: ' . $whh[0] . 'px;height: ' . $whh[1] . 'px;float: left;box-sizing: border-box;outline: none;text-align: left;background: url(' . $logo . ') center no-repeat;margin:10px 15px 10px 13px;}</style>';
return $html;
} else {
return '<a href="https://www.atarikafa.com/" class="forum-header-menu-logo"> </a>';
}
}
add_action( 'wp_head', 'siforum_add_custom_color', 9998 );
function siforum_add_custom_color() {
$color = get_theme_mod( 'siforum_header_background_color' );
if ( ! empty( $color ) ) {
echo '<style>.header_custom_color{background-color:' . $color . '!important}</style>' . "\n";
}
}
function header_class() {
$color = get_theme_mod( 'siforum_header_background_color' );
if ( ! empty( $color ) ) {
return 'header_custom_color';
}
return '';
}
add_action( 'wp_head', 'check_fontawesome_and_install', 9999 );
function check_fontawesome_and_install() {
$fontawesome = get_theme_mod( 'siforum_fontawesome_select' );
if ( ! empty( $fontawesome ) && 'yes' === $fontawesome ) {
echo '<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css">' . "\n";
}
}
//
// 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 );
// CATEGORY ICONS and COLORS
function wcr_category_fields( $term ) {
if ( 'category_edit_form_fields' === current_filter() ) {
$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/ (If you enabled fontawesome. use fontawesome icon names. <code>fa-regular fa-person</code> like.)</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 ( 'category_add_form_fields' === current_filter() ) {
?>
<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( /* translators: $1 comment date and $2 comment time. */ __( '%1$s at %2$s', 'siforum' ), get_comment_date(), get_comment_time() );
?>
</span>
<?php
if ( '0' === $comment->comment_approved ) {
?>
<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' );
//phpcs:disable
echo human_time_diff( $t, current_time( 'U' ) ) . __( ' ago', 'siforum' );
//phpcs:enable
?>
</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 */
?>