-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathclass-give-comment.php
More file actions
717 lines (617 loc) · 17 KB
/
Copy pathclass-give-comment.php
File metadata and controls
717 lines (617 loc) · 17 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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
/**
* Class for managing comments
*
* @package Give
* @subpackage Classes/Give_Cache
* @copyright Copyright (c) 2018, GiveWP
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 2.2.0
*/
class Give_Comment {
/**
* Instance.
*
* @since 2.2.0
* @access private
* @var
*/
private static $instance;
/**
* Comment Types.
*
* @since 2.2.0
* @access private
* @var array
*/
private $comment_types;
/**
* Comment Table.
*
* @since 2.3.0
* @access public
* @var Give_DB_Comments
*/
public $db;
/**
* Comment Meta Table.
*
* @since 2.3.0
* @access public
* @var Give_DB_Comment_Meta
*/
public $db_meta;
/**
* Singleton pattern.
*
* @since 2.2.0
* @access private
*/
private function __construct() {
}
/**
* Get instance.
*
* @since 2.2.0
* @access pu
* @return Give_Comment
*/
public static function get_instance() {
if ( null === static::$instance ) {
self::$instance = new static();
self::$instance->init();
}
return self::$instance;
}
/**
* Initialize
*
* @since 2.2.0
* @access private
*/
private function init() {
$this->db = new Give_DB_Comments();
$this->db_meta = new Give_DB_Comment_Meta();
/**
* Filter the comment type
*
* @since 2.2.0
*/
$this->comment_types = apply_filters(
'give_comment_type',
self::get_comment_types( array( 'payment', 'donor' ) )
);
// Backward compatibility.
if ( ! give_has_upgrade_completed( 'v230_move_donation_note' ) ) {
add_action( 'pre_get_comments', array( $this, 'hide_comments' ), 10 );
add_filter( 'comments_clauses', array( $this, 'hide_comments_pre_wp_41' ), 10, 1 );
add_filter( 'comment_feed_where', array( $this, 'hide_comments_from_feeds' ), 10, 1 );
add_filter( 'wp_count_comments', array( $this, 'remove_comments_from_comment_counts' ), 10, 2 );
add_filter( 'get_comment_author', array($this, 'get_comment_author'), 10, 3 );
}
}
/**
* Insert/Update comment
*
* @since 2.2.0
* @access public
*
* @param array $comment_args Comment arguments.
*
* @return int|WP_Error
*/
public static function add( $comment_args = array() ) {
// Backward compatibility.
$func_args = func_get_args();
$comment_id = self::_bc_add( $func_args );
if ( ! is_null( $comment_id ) ) {
return $comment_id;
}
// Backward compatibility.
if ( is_numeric( $comment_args ) ) {
$comment_args = array(
'comment_parent' => $func_args[0],
'comment_content' => $func_args[1],
'comment_type' => 'payment' === $func_args[2] ? 'donation' : $func_args[1],
);
}
$comment_args = wp_parse_args(
$comment_args,
array(
'comment_ID' => 0,
'comment_parent' => 0,
'comment_type' => 'general',
)
);
$is_existing_comment = array_key_exists( 'comment_ID', $comment_args ) && ! empty( $comment_args['comment_ID'] );
$action_type = $is_existing_comment ? 'update' : 'insert';
/**
* Fires before inserting/updating payment|donor comment.
*
* @param int $id Payment|Donor ID.
* @param string $note Comment text.
*
* @since 1.0
*/
do_action(
"give_pre_{$action_type}_{$comment_args['comment_type']}_note",
$comment_args['comment_parent'],
$comment_args['comment_content'],
$comment_args
);
$comment_id = Give()->comment->db->add(
array(
'comment_ID' => absint( $comment_args['comment_ID'] ),
'comment_content' => $comment_args['comment_content'],
'comment_parent' => $comment_args['comment_parent'],
'comment_type' => $comment_args['comment_type'],
)
);
/**
* Fires after payment|donor comment inserted/updated.
*
* @param int $comment_id Comment ID.
* @param int $id Payment|Donor ID.
* @param string $note Comment text.
*
* @since 1.0
*/
do_action(
"give_{$action_type}_{$comment_args['comment_type']}_note",
$comment_args['comment_ID'],
$comment_args['comment_parent'],
$comment_args['comment_content'],
$comment_args
);
return $comment_id;
}
/**
* Delete comment
*
* @since 2.2.0
* @access public
*
* @param int $comment_id The comment ID to delete.
*
* @since 1.0
*
* @return bool True on success, false otherwise.
*/
public static function delete( $comment_id ) {
// Backward compatibility.
$func_args = func_get_args();
$ret = self::_bc_delete( $func_args );
if ( ! is_null( $ret ) ) {
return $ret;
}
$ret = false;
// Bailout
if ( empty( $comment_id ) ) {
return $ret;
}
/* @var stdClass $comment */
$comment = Give()->comment->db->get_by( 'comment_ID', $comment_id );
if ( ! is_object( $comment ) ) {
return $ret;
}
$comment_type = $comment->comment_type;
$comment_parent = $comment->comment_parent;
/**
* Fires before deleting donation note.
*
* @param int $comment_id Comment ID.
* @param int $id Payment|Donor ID.
*
* @since 1.0
*/
do_action( "give_pre_delete_{$comment_type}_note", $comment_id, $comment_parent );
$ret = Give()->comment->db->delete( $comment_id );
// Delete comment meta.
Give()->comment->db_meta->delete_all_meta( $comment_id );
/**
* Fires after donation note deleted.
*
* @param int $comment_id Note ID.
* @param int $id Payment|Donor ID.
* @param bool $ret Flag to check if comment deleted or not.
*
* @since 1.0
*/
do_action( "give_post_delete_{$comment_type}_note", $comment_id, $comment_parent, $ret );
return $ret;
}
/**
* Get comments
*
* @since 2.2.0
* @access public
*
* @param array $comment_args
*
* @return array
*/
public static function get( $comment_args ) {
global $wpdb;
// Backward compatibility.
$func_args = func_get_args();
$comments = self::_bc_get( $func_args );
if ( ! is_null( $comments ) ) {
return $comments;
}
// Backward compatibility.
if ( is_numeric( $comment_args ) ) {
$comment_args = array(
'comment_parent' => $func_args[0],
'comment_type' => 'payment' === $func_args[1] ? 'donation' : $func_args[1],
);
}
$comments = $wpdb->get_results( Give()->comment->db->get_sql( $comment_args ) );
return $comments;
}
/**
* Exclude comments from showing in Recent
* Comments widgets
*
* @since 2.2.0
* @access public
*
* @param object $query WordPress Comment Query Object.
*
* @return void
*/
public function hide_comments( $query ) {
if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.1', '>=' ) ) {
$types = isset( $query->query_vars['type__not_in'] ) ? $query->query_vars['type__not_in'] : array();
if ( ! is_array( $types ) ) {
$types = array( $types );
}
$types = array_filter( array_merge( $types, $this->comment_types ) );
$query->query_vars['type__not_in'] = $types;
}
}
/**
* Exclude notes (comments) from showing in Recent Comments widgets
*
* @since 2.2.0
* @access public
*
* @param array $clauses Comment clauses for comment query.
*
* @return array $clauses Updated comment clauses.
*/
public function hide_comments_pre_wp_41( $clauses ) {
if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.1', '<' ) ) {
foreach ( $this->comment_types as $comment_type ) {
$clauses['where'] .= " AND comment_type != \"{$comment_type}\"";
}
}
return $clauses;
}
/**
* Exclude notes (comments) from showing in comment feeds
*
* @since 2.2.0
* @access public
*
* @param string $where
*
* @return string $where
*/
public function hide_comments_from_feeds( $where ) {
global $wpdb;
foreach ( $this->comment_types as $comment_type ) {
$where .= $wpdb->prepare( ' AND comment_type!=%s', $comment_type );
}
return $where;
}
/**
* Remove Give Comments from the wp_count_comments function
*
* @since 2.2.0
* @access public
*
* @param array $stats (empty from core filter).
* @param int $post_id Post ID.
*
* @return array|object Array of comment counts.
*/
public function remove_comments_from_comment_counts( $stats, $post_id ) {
global $wpdb;
$post_id = (int) $post_id;
if ( apply_filters( 'give_count_payment_notes_in_comments', false ) ) {
return $stats;
}
$stats = Give_Cache::get_group( "comments-{$post_id}", 'counts' );
// Return result from cache.
if ( ! is_null( $stats ) ) {
return $stats;
}
$where = 'WHERE';
foreach ( $this->comment_types as $index => $comment_type ) {
$where .= ( $index ? ' AND ' : ' ' ) . "comment_type != \"{$comment_type}\"";
}
if ( $post_id > 0 ) {
$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
}
$count = $wpdb->get_results(
"
SELECT comment_approved, COUNT( * ) AS num_comments
FROM {$wpdb->comments} {$where}
GROUP BY comment_approved
",
ARRAY_A
);
$total = 0;
$approved = array(
'0' => 'moderated',
'1' => 'approved',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);
foreach ( (array) $count as $row ) {
// Don't count post-trashed toward totals.
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
$total += $row['num_comments'];
}
if ( isset( $approved[ $row['comment_approved'] ] ) ) {
$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
}
}
$stats['total_comments'] = $stats['all'] = $total;
foreach ( $approved as $key ) {
if ( empty( $stats[ $key ] ) ) {
$stats[ $key ] = 0;
}
}
$stats = (object) $stats;
Give_Cache::set_group( "comments-{$post_id}", $stats, 'counts' );
return $stats;
}
/**
* Get donor name
*
* @since 4.9.0 rename function - PHP 8 compatibility
* @since 2.2.0
* @access public
*
* @param string $author
* @param int $comment_id
* @param WP_Comment $comment
*
* @return mixed
*/
public function get_comment_author( $author, $comment_id, $comment ) {
if ( in_array( $comment->comment_type, $this->comment_types ) ) {
switch ( $comment->comment_type ) {
case 'give_payment_note':
if ( get_comment_meta( $comment_id, '_give_donor_id', true ) ) {
$author = give_get_donor_name_by( $comment->comment_post_ID );
}
}
}
return $author;
}
/**
* Get comment types
*
* @since 2.2.0
* @access public
*
* @param array @comment_types
*
* @return array
*/
public static function get_comment_types( $comment_types ) {
$_comment_types = array();
foreach ( $comment_types as $comment_type ) {
$_comment_types[] = "give_{$comment_type}_note";
}
return $_comment_types;
}
/**
* Get comments
* Note: This function add backward compatibility for get function
*
* @since 2.3.0
* @access public
*
* @param array $comment_args
*
* @return array|null
*/
private static function _bc_get( $comment_args ) {
$comments = null;
if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) {
$id = $comment_args[0];
$comment_type = $comment_args[1];
$comment_args = isset( $comment_args[2] ) ? $comment_args[2] : array();
$search = isset( $comment_args[3] ) ? $comment_args[3] : '';
// Set default meta_query value.
if ( ! isset( $comment_args['meta_query'] ) ) {
$comment_args['meta_query'] = array();
}
// Bailout
if ( empty( $id ) || empty( $comment_type ) ) {
return $comments;
}
remove_action( 'pre_get_comments', array( self::$instance, 'hide_comments' ), 10 );
remove_filter( 'comments_clauses', array( self::$instance, 'hide_comments_pre_wp_41' ), 10 );
switch ( $comment_type ) {
case 'payment':
$comment_args['meta_query'] = ! empty( $comment_args['meta_query'] )
? $comment_args['meta_query']
: array(
array(
'key' => '_give_donor_id',
'compare' => 'NOT EXISTS',
),
);
$comments = get_comments(
wp_parse_args(
$comment_args,
array(
'post_id' => $id,
'order' => 'ASC',
'search' => $search,
'type' => 'give_payment_note',
)
)
);
break;
case 'donor':
$comment_args['meta_query'] = ! empty( $comment_args['meta_query'] )
? $comment_args['meta_query']
: array(
array(
'key' => "_give_{$comment_type}_id",
'value' => $id,
),
);
$comments = get_comments(
wp_parse_args(
$comment_args,
array(
'order' => 'ASC',
'search' => $search,
'type' => 'give_donor_note',
)
)
);
break;
}
add_action( 'pre_get_comments', array( self::$instance, 'hide_comments' ), 10, 1 );
add_filter( 'comments_clauses', array( self::$instance, 'hide_comments_pre_wp_41' ), 10, 1 );
}
return $comments;
}
/**
* Insert/Update comment
* Note: This function add backward compatibility for add function
*
* @since 2.3.0
* @access public
*
* @param array $comment_args Comment arguments.
*
* @return int|null|WP_Error
*/
private static function _bc_add( $comment_args = array() ) {
$comment_id = null;
if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) {
$id = $comment_args[0];
$note = $comment_args[1];
$comment_type = $comment_args[2];
$comment_args = isset( $comment_args[3] ) ? $comment_args[3] : array();
// Bailout
if ( empty( $id ) || empty( $note ) || empty( $comment_type ) ) {
return new WP_Error( 'give_invalid_required_param', __( 'This comment has invalid ID or comment text or comment type', 'give' ) );
}
$is_existing_comment = array_key_exists( 'comment_ID', $comment_args ) && ! empty( $comment_args['comment_ID'] );
$action_type = $is_existing_comment ? 'update' : 'insert';
/**
* Fires before inserting/updating payment|donor comment.
*
* @param int $id Payment|Donor ID.
* @param string $note Comment text.
*
* @since 1.0
*/
do_action( "give_pre_{$action_type}_{$comment_type}_note", $id, $note );
$comment_args = wp_parse_args(
$comment_args,
array(
'comment_post_ID' => $id,
'comment_content' => $note,
'user_id' => is_admin() ? get_current_user_id() : 0,
'comment_date' => current_time( 'mysql' ),
'comment_date_gmt' => current_time( 'mysql', 1 ),
'comment_approved' => 1,
'comment_parent' => 0,
'comment_author' => '',
'comment_author_IP' => '',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => "give_{$comment_type}_note",
)
);
// Check comment max length.
$error = wp_check_comment_data_max_lengths( $comment_args );
if ( is_wp_error( $error ) ) {
return $error;
}
// Remove moderation emails when comment posted.
remove_action( 'comment_post', 'wp_new_comment_notify_moderator' );
remove_action( 'comment_post', 'wp_new_comment_notify_postauthor' );
// Remove comment flood check.
remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
$comment_id = $is_existing_comment
? wp_update_comment( $comment_args )
: wp_new_comment( $comment_args, true );
// Add moderation emails when comment posted.
add_action( 'comment_post', 'wp_new_comment_notify_moderator' );
add_action( 'comment_post', 'wp_new_comment_notify_postauthor' );
// Add comment flood check.
add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
update_comment_meta( $comment_id, "_give_{$comment_type}_id", $id );
/**
* Fires after payment|donor comment inserted/updated.
*
* @param int $comment_id Comment ID.
* @param int $id Payment|Donor ID.
* @param string $note Comment text.
*
* @since 1.0
*/
do_action( "give_{$action_type}_{$comment_type}_note", $comment_id, $id, $note );
}
return $comment_id;
}
/**
* Delete comment
* Note: This function add backward compatibility for delete function
*
* @since 2.3.0
* @access public
*
* @param array $comment_args Comment arguments.
*
* @since 1.0
*
* @return bool True on success, false otherwise.
*/
private static function _bc_delete( $comment_args ) {
$ret = null;
if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) {
$comment_id = $comment_args[0];
$id = $comment_args[1];
$comment_type = $comment_args[2];
$ret = false;
// Bailout
if ( empty( $id ) || empty( $comment_id ) || empty( $comment_type ) ) {
return $ret;
}
/**
* Fires before deleting donation note.
*
* @param int $comment_id Comment ID.
* @param int $id Payment|Donor ID.
*
* @since 1.0
*/
do_action( "give_pre_delete_{$comment_type}_note", $comment_id, $id );
$ret = wp_delete_comment( $comment_id, true );
/**
* Fires after donation note deleted.
*
* @param int $comment_id Note ID.
* @param int $id Payment|Donor ID.
* @param bool $ret Flag to check if comment deleted or not.
*
* @since 1.0
*/
do_action( "give_post_delete_{$comment_type}_note", $comment_id, $id, $ret );
}
return $ret;
}
}