-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbp-blog-comment-notifier.php
402 lines (326 loc) · 10.2 KB
/
bp-blog-comment-notifier.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
<?php
/**
* Plugin Name: BuddyPress Notify Post Author on Blog Comment
* Plugin URI: https://buddydev.com/plugins/bp-notify-post-author-on-blog-comment/
* Author: BuddyDev
* Author URI: https://buddydev.com/
* Description: Notify the Blog post author of any new comment on their blog post
* Version: 1.0.7
* License: GPL
* Text Domain: bp-notify-post-author-on-blog-comment
* Domain Path: /languages/
*/
// No direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class BDBP_Blog_Comment_Notifier
*/
class BDBP_Blog_Comment_Notifier {
/**
* Class instance
*
* @var BDBP_Blog_Comment_Notifier
*/
private static $instance;
/**
* Id
*
* @var string
*/
private $id = 'blog_comment_notifier';
/**
* BDBP_Blog_Comment_Notifier constructor.
*/
private function __construct() {
$this->setup();
}
/**
* Class instance
*
* @return BDBP_Blog_Comment_Notifier
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Setup necessary hooks
*/
public function setup() {
add_action( 'bp_setup_globals', array( $this, 'setup_globals' ) );
//On New comment
add_action( 'comment_post', array( $this, 'comment_posted' ), 15, 2 );
//on delete post, we should delete all notifications for the comment on that post
//add_action( 'delete_post', array( $this, 'post_deleted' ), 10, 2 );
// Monitor actions on existing comments
add_action( 'deleted_comment', array( $this, 'comment_deleted' ) );
//add_action( 'trashed_comment', array( $this, 'comment_deleted' ) );
//add_action( 'spam_comment', array( $this, 'comment_deleted' ) );
//should we do something on the action untrash_comment & unspam_comment
add_action( 'wp_set_comment_status', array( $this, 'comment_status_changed' ), 10, 2 );
// Load plugin text domain
add_action( 'bp_init', array( $this, 'load_textdomain' ) );
add_action( 'template_redirect', array( $this, 'mark_read' ) );
}
/**
* Load plugin text domain
*
* @hook action plugins_loaded
*/
public function load_textdomain() {
load_plugin_textdomain( 'bp-notify-post-author-on-blog-comment', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Setup globals
*/
public function setup_globals() {
//BD: BuddyDev
if ( ! defined( 'BD_BLOG_NOTIFIER_SLUG' ) ) {
define( 'BD_BLOG_NOTIFIER_SLUG', 'bd-blog-notifier' );
}
$bp = buddypress();
$bp->blog_comment_notifier = new stdClass();
$bp->blog_comment_notifier->id = $this->id;//I asume others are not going to use this is.
$bp->blog_comment_notifier->slug = BD_BLOG_NOTIFIER_SLUG;
$bp->blog_comment_notifier->notification_callback = array( $this, 'format_notifications' ); //show the notification.
/* Register this in the active components array */
$bp->active_components[ $bp->blog_comment_notifier->id ] = $bp->blog_comment_notifier->id;
do_action( 'blog_comment_notifier_setup_globals' );
}
/**
* Notify when a comment is posted
*
* @param int $comment_id Comment id.
* @param int|string $comment_status 1 if the comment is approved, 0 if not, 'spam' if spam.
*
* @return null
*/
public function comment_posted( $comment_id = 0, $comment_status = 0 ) {
if ( ! $this->is_bp_active() ) {
return ;
}
$comment = get_comment( $comment_id );
//if the comment does not exists(not likely), or if the comment is marked as spam, we don't take any action
if ( empty( $comment ) || $comment->comment_approved == 'spam' ) {
return ;
}
//should we handle trackback? currently we don't
if ( $comment->comment_type == 'trackback' || $comment->comment_type == 'pingback' ) {
return ;
}
$post_id = $comment->comment_post_ID;
$post = get_post( $post_id );
//no need to generate any notification if an author is making comment
if ( $post->post_author == $comment->user_id ) {
return ;
}
//can the post author moderate comment?
if ( ! user_can( $post->post_author, 'moderate_comments' ) && $comment->comment_approved == 0 ) {
return;
}
//if we are here, we must provide a notification to the author of the post
$this->notify( $post->post_author, $comment );
}
/**
* When a comment status changes, we check for the notification and also
* think about changing the read link?
*
* @param int $comment_id Comment id.
* @param int|string $comment_status 1 if the comment is approved, 0 if not, 'spam' if spam.
*
* @return mixed
*/
public function comment_status_changed( $comment_id = 0, $comment_status = 0 ) {
if ( ! $this->is_bp_active() ) {
return;
}
$comment = get_comment( $comment_id );
if ( empty( $comment ) ) {
return;
}
//we are only interested in 2 cases
//1. comment is notified and then it was marked as deleted or spam?
if ( $comment_status == 'spam' || $comment_status == 'trash' ) {
if ( $this->is_notified( $comment_id ) ) {
$this->comment_deleted( $comment_id );
}
return;
}
//if an apprived comment is marked as pending, delete notification
if ( $comment_status == 0 && $this->is_notified( $comment_id ) ) {
$this->comment_deleted( $comment_id );
return;
}
if ( $comment_status == 'approve' ) {
$post = get_post( $comment->comment_post_ID );
if ( get_current_user_id() == $post->post_author ) {
if ( $this->is_notified( $comment_id ) ) {
$this->comment_deleted ( $comment_id );
}
return;
} else {
//if approver is not the author
$this->notify( $post->post_author, $comment );
}
}
}
/**
* On Comment Delete
*
* @param int $comment_id Comment id.
*/
public function comment_deleted( $comment_id ) {
if ( ! $this->is_bp_active() ) {
return;
}
bp_notifications_delete_all_notifications_by_type( $comment_id, $this->id );
$this->unmark_notified( $comment_id );
}
/**
* Generate human readable notification
*
* @param string $action Notification action.
* @param string $comment_id Comment id.
* @param string $secondary_item_id Secondary item id.
* @param string $total_items Total items.
* @param string $format Notification format.
*
* @return mixed
*/
public function format_notifications( $action, $comment_id, $secondary_item_id, $total_items, $format = 'string', $notification_id = 0 ) {
$bp = buddypress();
$switched = false;
$blog_id = bp_notifications_get_meta( $notification_id, '_blog_id' );
if ( $blog_id && get_current_blog_id() != $blog_id ) {
switch_to_blog( $blog_id );
$switched = true;
}
$comment = get_comment( $comment_id );
$post = get_post( $comment->comment_post_ID );
$link = $text = $name = $post_title = $comment_content ='';
if ( $comment->user_id ) {
$name = bp_core_get_user_displayname ( $comment->user_id );
} else {
$name = $comment->comment_author;
}
$post_title = $post->post_title;
$comment_content = wp_trim_words( $comment->comment_content, 12, ' ...' );
$text = sprintf(
__( '%s commented on <strong>%s</strong>: <em>%s</em>', 'bp-notify-post-author-on-blog-comment' ),
$name,
$post_title,
$comment_content
);
if ( $comment->comment_approved == 1 ) {
$link = get_comment_link( $comment );
} else {
$link = admin_url( 'comment.php?action=approve&c=' . $comment_id );
}
if( $switched ) {
restore_current_blog();
}
if ( $format == 'string' ) {
return apply_filters( 'bp_blog_notieifier_new_comment_notification_string', '<a href="' . $link . '">' . $text . '</a>' );
} else {
return array(
'link' => $link,
'text' => $text,
);
}
return false;
}
/**
* Is BuddyPress Active
* We test to avoid any fatal errors when Bp is not active
*
* @return boolean
*/
public function is_bp_active() {
if ( function_exists( 'buddypress' ) ) {
return true;
}
return false;
}
/**
* Was the comment already added to bp notification?
*
* @param int $comment_id Comment id.
*
* @return boolean
*/
public function is_notified( $comment_id ) {
return get_comment_meta( $comment_id, 'bd_post_author_notified', true );
}
/**
* Mark that a comment was notified to the post author
*
* @param int $comment_id Comment id.
*/
public function mark_notified( $comment_id ) {
update_comment_meta( $comment_id, 'bd_post_author_notified', 1 );
}
/**
* Delete the notification mark meta
*
* @param int $comment_id
*/
public function unmark_notified( $comment_id ) {
delete_comment_meta( $comment_id, 'bd_post_author_notified' );
}
/**
* Notify author
*
* @param int $user_id User id.
* @param WP_Comment $comment WP Comment object.
*/
public function notify( $user_id, $comment ) {
$comment_id = $comment->comment_ID;
// If blog forum commenting not disable.
if ( bp_is_active( 'blogs' ) && ! bp_disable_blogforum_comments() ) {
return;
}
$notification_id = bp_notifications_add_notification(
array(
'item_id' => $comment_id,
'user_id' => $user_id,
'component_name' => $this->id,
'component_action' => 'new_blog_comment_' . $comment_id,
'secondary_item_id' => $comment->comment_post_ID,
)
);
if ( $notification_id && is_multisite() ) {
bp_notifications_add_meta( $notification_id, '_blog_id', get_current_blog_id() );
}
$this->mark_notified( $comment_id );
}
/**
* Mark read
*
* @return false|int|void
*/
public function mark_read() {
if ( ! $this->is_bp_active() || ! is_singular() ) {
return ;
}
$post_id = get_queried_object_id();
if ( ! $post_id ) {
return ;
}
return BP_Notifications_Notification::update(
array( 'is_new' => 0 ),
array(
'secondary_item_id' => $post_id,
'component_name' => $this->id,
'user_id' => get_current_user_id(),
)
);
}
//we need to delete all notification for the user when he/she visits the single blog post?
//no, we won't as there is no sure way to know if the user has seen a comment on the front end or not
}
//initialize
BDBP_Blog_Comment_Notifier::get_instance();