-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
468 lines (439 loc) · 11.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
<?php
/**
* Global functions
*
* @package hamethread
*/
use Hametuha\Thread\Hooks\BestAnswer;
/**
* Get file path.
*
* @param string $name
* @param string $slug
*
* @return string
*/
function hamethread_file_path( $name, $slug = '' ) {
$existing_path = '';
$files = [ $name . '.php' ];
if ( $slug ) {
$files[] = "{$name}-{$slug}.php";
}
foreach ( $files as $file ) {
foreach ( [
__DIR__ . '/template-parts',
get_template_directory() . '/template-parts/hamethread',
get_stylesheet_directory() . '/template-parts/hamethread',
] as $dir ) {
$path = $dir . '/' . $file;
if ( file_exists( $path ) ) {
$existing_path = $path;
}
}
}
$existing_path = apply_filters( 'hamethread_template', $existing_path, $name, $slug );
return $existing_path;
}
/**
* Load template.
*
* @param string $name
* @param string $slug
* @param bool $echo
* @param array $args
* @param array $default
* @return string
*/
function hamethread_template( $name, $slug = '', $echo = true, $args = [], $default = [] ) {
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
$existing_path = hamethread_file_path( $name, $slug );
if ( ! file_exists( $existing_path ) ) {
return '';
}
// Extract args.
$args = array_merge( $default, $args );
if ( $args ) {
extract( $args );
}
if ( ! $echo ) {
ob_start();
include $existing_path;
$content = ob_get_contents();
ob_end_clean();
return $content;
} else {
include $existing_path;
}
}
/**
* Get post type
*
* @return string
*/
function hamethread_post_type() {
return \Hametuha\Thread\Hooks\PostType::get_instance()->post_type;
}
/**
* Is comment recently editted.
*
* @param int $offset Initial value 7
* @param object $post
* @return boolean
*/
function hamethread_recently_commented( $offset = 7, $post = null ) {
$latest_date = hamethread_get_latest_comment_date( $post );
if ( !$latest_date ) {
return false;
}
return (boolean) ( ( time() - strtotime( $latest_date ) ) < 60 * 60 * 24 * $offset );
}
/**
* Get thread count.
*
* @global wpdb $wpdb
* @param int $user_id
* @return int
*/
function hamethread_get_author_thread_count( $user_id ) {
global $wpdb;
$sql = <<<EOS
SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_author = %d AND post_type = %s AND post_status = 'publish'
EOS;
return (int) $wpdb->get_var( $wpdb->prepare( $sql, $user_id, hamethread_post_type() ) );
}
/**
* Get latest comment date.
*
* @global wpdb $wpdb
* @param object $post
* @return string
*/
function hamethread_get_latest_comment_date( $post = null ) {
global $wpdb;
$post = get_post( $post );
$sql = <<<EOS
SELECT comment_date FROM {$wpdb->comments}
WHERE comment_post_ID = %d
LIMIT 1
EOS;
return $wpdb->get_var( $wpdb->prepare( $sql, $post->ID ) );
}
/**
* Get user responded thread count.
*
* @global wpdb $wpdb
* @param int $user_id
* @return int
*/
function hamethread_get_author_response_count( $user_id ) {
global $wpdb;
$sql = <<<EOS
SELECT COUNT(comment_ID) FROM {$wpdb->comments} AS c
INNER JOIN {$wpdb->posts} AS p
ON c.comment_post_ID = p.ID
WHERE p.post_type = 'thread' AND c.user_id = %d
EOS;
return (int) $wpdb->get_var( $wpdb->prepare( $sql, $user_id ) );
}
/**
* Is topic forced?
*
* @param int $user_id
* @return bool
*/
function hamethread_topic_forced( $user_id ) {
$force = true;
return apply_filters( 'hamethread_force_topic', $user_id );
}
/**
* Get topic list.
*
* @return array
*/
function hamethread_topics() {
$topics = get_terms( [
'hide_empty' => false,
'taxonomy' => \Hametuha\Thread\Hooks\PostType::get_instance()->taxonomy,
] );
return is_wp_error( $topics ) ? [] : (array) $topics;
}
/**
* Load button
*
* @param int $parent Post parent ID.
* @param string $label Button label.
* @param array $attr Attributes for templates.
*/
function hamethread_button( $parent = 0, $label = '', $attr = [] ) {
wp_enqueue_script( 'hamethread-thread' );
wp_enqueue_style( 'hamethread' );
$label = $label ?: __( 'Start new thread', 'hamethread' );
hamethread_template( 'button-thread', '', true, [
'parent' => $parent,
'label' => $label,
'private' => \Hametuha\Thread\Model\ThreadModel::can_start_private( get_current_user_id(), $parent ),
'attr' => $attr,
] );
}
/**
* Load button
*/
function hamethread_reply_button() {
wp_enqueue_script( 'hamethread' );
wp_enqueue_style( 'hamethread' );
hamethread_template( 'button-comment' );
}
/**
* Detect if user can start.
*
* @param int|null $user_id If null, means current user.
* @return bool
*/
function hamethread_user_can_start( $user_id = null ) {
return \Hametuha\Thread\Model\ThreadModel::can_post( $user_id );
}
/**
* Detect if user can comment.
*
* @param null|int|WP_Post $post
* @param null|null $user_id
*
* @return bool
*/
function hamethread_user_can_comment( $post = null, $user_id = null ) {
return \Hametuha\Thread\Model\CommentModel::can_comment( $post, $user_id );
}
/**
* Detect if thread is resolved.
*
* @param null|int|WP_Post $post
*
* @return bool
*/
function hamethread_is_resolved( $post = null ) {
$post = get_post( $post );
return (bool) get_post_meta( $post->ID, '_thread_resolved', true );
}
/**
* Get home URL.
*
* @param string $context
* @return string
*/
function hamethread_home( $context = '' ) {
return apply_filters( 'hamethread_home_url', get_post_type_archive_link( \Hametuha\Thread\Hooks\PostType::get_instance()->post_type ), $context );
}
/**
* Get log
*
* @param null|int|WP_Post $post
*
* @return array
*/
function hamethread_edit_logs( $post = null ) {
$post = get_post( $post );
$logs = get_post_meta( $post->ID, '_hamethread_thread_log' );
rsort( $logs );
return $logs;
}
/**
* Detect if user can start.
*
* @param int|null|WP_Post $post
* @return bool
*/
function hamethread_current_user_can_comment( $post = null ) {
$can = is_user_logged_in();
return apply_filters( 'hamethread_user_can_comment', $can, $post );
}
/**
* Get comment reactions.
*
* @param WP_Comment $comment
* @return array
*/
function hamethread_comment_actions( $comment ) {
return apply_filters( 'hamethread_comment_actions', [
'reply' => sprintf(
'<button class="hamethread-reply" data-path="comment/%d/new" data-reply-to="%d"><i class="fa fa-reply"></i> <span class="hamethread-comment-actions-label">%s</spanc></button>',
$comment->comment_post_ID,
$comment->comment_ID,
esc_html__( 'Reply', 'hamethread' )
),
'upvote' => sprintf(
'<button class="hamethread-upvote%s" data-path="vote/%d"><i class="fa fa-thumbs-up"></i> <span class="hamethread-comment-actions-label">%s</span></button>',
\Hametuha\Thread\Rest\RestVote::get_instance()->is_voted( $comment->comment_ID, get_current_user_id() ) ? ' active' : '' ,
$comment->comment_ID,
esc_html__( 'Upvote', 'hamethread' )
),
], $comment );
}
/**
* Check if comment is updated.
*
* @param WP_Comment $comment
* @return bool
*/
function hamethread_is_edited( $comment ) {
$comment = get_comment( $comment );
$metas = get_comment_meta( $comment->comment_ID, '_comment_diff' );
usort( $metas, function( $a, $b ) {
if ( $a['updated'] === $b['updated'] ) {
return 0;
} else {
return $a['updated'] < $b['updated'];
}
} );
return ! empty( $metas );
}
/**
* Get user roles.
*
* @param WP_Comment $comment
*
* @return string
*/
function hamethread_commentor_label( $comment ) {
static $loaded = false;
$user = get_userdata( $comment->user_id );
$label = '';
if ( $user ) {
global $wp_roles;
$role = current( $user->roles );
if ( isset( $wp_roles->role_names[ $role ] ) ) {
if ( ! $loaded ) {
load_textdomain( 'default', WP_LANG_DIR . sprintf( '/admin-%s.mo', get_user_locale() ) );
$loaded = true;
}
$label = translate_user_role( $wp_roles->role_names[ $role ] );
}
}
if ( ! $label ) {
$label = __( 'Guest', 'hamethread' );
}
$label = sprintf( '<span class="hamethread-comment-label">%s</span>', trim( wp_kses_post( $label ) ) );
return apply_filters( 'hamethread_commentor_label', $label, $comment, $user );
}
/**
* Get comment label class
*
* @param WP_Comment $comment
* @param array|string $classes
* @return string
*/
function hamethread_commentor_label_class( $comment, $classes ) {
$classes = apply_filters( 'hamethread_commentor_label_class', (array) $classes, $comment );
return esc_attr( implode( ' ', $classes ) );
}
/**
* Get last comment updated.
*
* @param string $no_comment Default '---'
* @param null|int|WP_Post $post
* @param string $format Default WordPress setting.
*
* @return bool|int|string
*/
function hamethread_last_commented( $no_comment = '---', $post = null, $format = '' ) {
$post = get_post( $post );
if ( ! $format ) {
$format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
}
$comments = get_comments( [
'post_id' => $post->ID,
'number' => 1,
] );
if ( ! $comments ) {
return $no_comment;
}
foreach ( $comments as $comment ) {
/** @var WP_Comment $comment */
return mysql2date( $format, $comment->comment_date );
}
}
/**
* Display comment links
*
* @return string
*/
function hamethread_comment_links() {
$link = paginate_comments_links( [
'echo' => false,
'type' => 'array',
] );
if ( $link ) {
$link = array_map( function( $l ) {
$classes = [ 'page-item' ];
if ( false !== strpos( $l, 'current' ) ) {
$classes[] = 'active';
} elseif ( false !== strpos( $l, 'dot' ) ) {
$classes[] = 'disabled';
}
return sprintf( '<li class="%s">%s</li>', implode( ' ', $classes ), $l );
}, $link );
array_unshift( $link, '<ul class="pagination">' );
array_unshift( $link, sprintf( '<nav aria-label="%s" class="text-center">', esc_html__( 'Comments Pagination', 'hamethread' ) ) );
array_push( $link, '</ul>' );
array_push( $link, '</nav>' );
$link = implode( "\n", $link );
}
return apply_filters( 'hamethread_comment_links', $link );
}
/**
* Get upvoted count.
*
* @param null|int|WP_Post $post
* @return int
*/
function hamethread_upvote_count( $post = null ) {
global $wpdb;
$post = get_post( $post );
if ( ! $post ) {
return 0;
}
$query = <<<SQL
SELECT COUNT( cm.meta_id )
FROM {$wpdb->commentmeta} AS cm
LEFT JOIN {$wpdb->comments} AS c
ON cm.comment_id = c.comment_ID
WHERE cm.meta_key = '_user_upvote'
AND c.comment_post_ID = %d
SQL;
return (int) $wpdb->get_var( $wpdb->prepare( $query, $post->ID ) );
}
/**
* Get upvoted count.
*
* @param null|int|WP_Comment $comment
* @return int
*/
function hamethread_comment_upvoted_count( $comment = null ) {
$comment = get_comment( $comment );
global $wpdb;
$query = <<<SQL
SELECT COUNT( meta_id ) FROM {$wpdb->commentmeta}
WHERE comment_id = %d
AND meta_key = '_user_upvote'
SQL;
return (int) $wpdb->get_var( $wpdb->prepare( $query, $comment->comment_ID ) );
}
/**
* Get login link
*
* @param string $redirect_to
* @return string
*/
function hamethread_login_url( $redirect_to = '' ) {
if ( function_exists( 'WC' ) ) {
// WooCommerce
$url = wc_get_page_permalink( 'myaccount' );
if ( $redirect_to ) {
$url = add_query_arg( [
'redirect_to' => $redirect_to,
], $url );
}
} else {
$url = wp_login_url( $redirect_to );
}
return apply_filters( 'hamethread_login_url', $url, $redirect_to );
}