forked from buddydev/bp-notify-post-author-on-blog-comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-blog-comment-notifier.php
333 lines (243 loc) · 8.46 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
<?php
/**
* Plugin Name: BP Notify Post Author on Blog Comment
* Plugin URI: http://buddydev.com/plugins/bp-notify-post-author-on-blog-comment/
* Author: Brajesh Singh
* Author URI: http://buddydev.com/
* Description: Notify the Blog post author of any new comment on their blog post
* Version: 1.0.1
* License: GPL
* Date Updated: January 05, 2015
* Compatible with BuddyPress 2.0+
* Text Domain: bp-notify-post-author-on-blog-comment
* Domain Path: /languages/
*/
class DevB_Blog_Comment_Notifier {
private static $instance;
private $id = 'blog_comment_notifier';
private function __construct() {
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' ) );
// Load plugin text domain
add_action( 'bp_init', array( $this, 'load_textdomain' ) );
}
/**
*
* @return DevB_Blog_Comment_Notifier
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* 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' );
}
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 type $comment_id
* @param type $comment_status
* @return type
*/
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 type $comment_id
* @param type $comment_status
*
*/
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->comment_approved == 'spam' || $comment->comment_approve == '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->comment_approve == 0 && $this->is_notified( $comment_id ) ) {
$this->comment_deleted( $comment_id );
return ;
}
if ( $comment->comment_approve == 1 ) {
$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 type $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
* @param string $comment_id
* @param string $secondary_item_id
* @param string $total_items
* @param string $format
* @return mixed
*/
public function format_notifications( $action, $comment_id, $secondary_item_id, $total_items, $format = 'string' ) {
$bp = buddypress();
$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 ( $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 type $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 type $comment_id
*/
public function mark_notified( $comment_id ) {
update_comment_meta( $comment_id, 'bd_post_author_notified', 1 );
}
/**
* Delete the notification mark meta
*
* @param type $comment_id
*/
public function unmark_notified( $comment_id ) {
delete_comment_meta( $comment_id, 'bd_post_author_notified' );
}
public function notify( $user_id, $comment ) {
$comment_id = $comment->comment_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
));
$this->mark_notified( $comment_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
DevB_Blog_Comment_Notifier::get_instance();