SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | /** | |
| 3 | * Plugin Name:BuddyPress Activity Moderator | |
| 4 | * Plugin URI: | |
| 5 | * Author: Dan Brellis | |
| 6 | * Author URI: | |
| 7 | * Description: | |
| 8 | * Version: | |
| 9 | * License: GPL | |
| 10 | * Site Wide Only: true | |
| 11 | * Network: true | |
| 12 | * Date Updated: October 05, 2011 | |
| 13 | * | |
| 14 | */ | |
| 15 | /** | |
| 16 | * Special desclaimer: BuddyPress does not allows multiple items from same component with similar action(It is grouped by buddypress before delivering to the notfication formatting function), so I have hacked the component_action to be unique per item, because we want update for individual action items | |
| 17 | * Most important: It is just for fun :) hope you would like it | |
| 18 | */ | |
| 19 | ||
| 20 | // we are not much concerned with the slug, it is not visible | |
| 21 | define("BP_ACTIVITY_MODERATOR_SLUG","mod");
| |
| 22 | ||
| 23 | //register a dummy notifier component, I don't want to do it, but bp has no other mechanism for passing the notification data to function, so we need the format_notification_function | |
| 24 | function mod_setup_globals() {
| |
| 25 | global $bp, $current_blog; | |
| 26 | ||
| 27 | $bp->mod->id = 'mod';//I asume others are not going to use this is | |
| 28 | $bp->mod->slug = BP_ACTIVITY_MODERATOR_SLUG; | |
| 29 | $bp->mod->format_notification_function = 'mod_format_notifications';//show the notification | |
| 30 | /* Register this in the active components array */ | |
| 31 | $bp->active_components[$bp->mod->slug] = $bp->mod->id; | |
| 32 | ||
| 33 | do_action( 'mod_setup_globals' ); | |
| 34 | } | |
| 35 | add_action( 'bp_setup_globals', 'mod_setup_globals' ); | |
| 36 | ||
| 37 | ||
| 38 | ||
| 39 | ||
| 40 | ||
| 41 | ||
| 42 | ||
| 43 | ||
| 44 | ||
| 45 | //Check if current user is a group moderator or super user | |
| 46 | function mod_user_is_mod ($group_id=false) {
| |
| 47 | global $bp; | |
| 48 | $user_id = $bp->loggedin_user->id; | |
| 49 | ||
| 50 | if ( $bp->loggedin_user->is_super_admin || groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) ) return true; | |
| 51 | else return false; | |
| 52 | } | |
| 53 | ||
| 54 | //Return a string of comma seperate activity ids that are pending for specific group | |
| 55 | function mod_get_pending_ids ($group_id) {
| |
| 56 | global $wpdb; | |
| 57 | ||
| 58 | $activity_table = $wpdb->prefix . 'bp_activity'; | |
| 59 | $activity_meta_table = $wpdb->prefix . 'bp_activity_meta'; | |
| 60 | ||
| 61 | $sql = " | |
| 62 | SELECT id | |
| 63 | FROM $activity_table WHERE"; | |
| 64 | if(isset($group_id)) $sql .= " item_id = $group_id AND"; | |
| 65 | $sql .= " id IN ( | |
| 66 | SELECT activity_id | |
| 67 | FROM $activity_meta_table | |
| 68 | WHERE meta_key = 'status' | |
| 69 | AND meta_value = 'pending') | |
| 70 | ORDER BY date_recorded DESC"; | |
| 71 | ||
| 72 | $activity_ids = $wpdb->get_col($wpdb->prepare($sql)); | |
| 73 | $activity_ids = array_map(create_function('$value', 'return (int)$value;'),$activity_ids);
| |
| 74 | ||
| 75 | $all_activities = $wpdb->get_col($wpdb->prepare( "SELECT id FROM $activity_table WHERE item_id = $group_id" )); | |
| 76 | $all_activities = array_map(create_function('$value', 'return (int)$value;'),$all_activities);
| |
| 77 | ||
| 78 | foreach ($all_activities as $activity_id) {
| |
| 79 | $comment_ids_ar = array(); | |
| 80 | $comment_ids_ar[] = $wpdb->get_col ($wpdb->prepare( " | |
| 81 | SELECT id | |
| 82 | FROM $activity_table | |
| 83 | WHERE item_id = $activity_id | |
| 84 | AND id IN ( | |
| 85 | SELECT activity_id | |
| 86 | FROM $activity_meta_table | |
| 87 | WHERE meta_key = 'status' | |
| 88 | AND meta_value = 'pending') | |
| 89 | ORDER BY date_recorded DESC")); | |
| 90 | foreach($comment_ids_ar as $val) {
| |
| 91 | $comment_ids .= implode(',',$val);
| |
| 92 | } | |
| 93 | unset($comment_ids_ar); | |
| 94 | } | |
| 95 | ||
| 96 | if(isset($comment_ids)) | |
| 97 | return rtrim( (implode(',',$activity_ids) . ',' . $comment_ids) , ',' );
| |
| 98 | else return rtrim( (implode(',',$activity_ids)) , ',' );
| |
| 99 | } | |
| 100 | ||
| 101 | //Add the list of ids to the bp_has_activities query_string | |
| 102 | /* abandoned for mod_exclude_pending_activities */ | |
| 103 | function mod_exclude_pending_activity ( $query_string, $object, $filter, $scope, $page, $search_terms, $extras ) {
| |
| 104 | global $bp, $activities_template; | |
| 105 | ||
| 106 | $group = $bp->groups->current_group->id; | |
| 107 | ||
| 108 | if ( $object != 'activity' ) | |
| 109 | return $query_string; | |
| 110 | ||
| 111 | //For viewing 'Activity' | |
| 112 | //if user is not a moderator, get all id's except for pending | |
| 113 | if (mod_user_is_mod ($group) == false ) | |
| 114 | {
| |
| 115 | $ids = mod_get_published_ids ($group); | |
| 116 | //add the filter | |
| 117 | $query_string .= "&include=" . $ids; | |
| 118 | var_dump($query_string); | |
| 119 | return $query_string; | |
| 120 | } | |
| 121 | else return $query_string; | |
| 122 | } | |
| 123 | //add_filter( 'bp_dtheme_ajax_querystring', 'mod_exclude_pending_activity', 1, 7 ); | |
| 124 | ||
| 125 | ||
| 126 | ||
| 127 | //Add the list of ids to the bp_has_activities query_string | |
| 128 | /* Abandoned the above function for this one, still not working 100% */ | |
| 129 | function mod_exclude_pending_activities ( $has_activities ) {
| |
| 130 | global $bp, $activities_template; | |
| 131 | ||
| 132 | $group = $bp->groups->current_group->id; | |
| 133 | ||
| 134 | //For viewing 'Activity' | |
| 135 | //if user is not a moderator, get all id's except for pending | |
| 136 | if (mod_user_is_mod ($group) == false ) | |
| 137 | {
| |
| 138 | $ids= mod_get_pending_ids ($group); | |
| 139 | $exclude_ids = explode(',', mod_get_pending_ids ($group));
| |
| 140 | //add the filter | |
| 141 | ||
| 142 | foreach( $activities_template->activities as $activity_key => $activity ) {
| |
| 143 | //var_dump( $activity->id ); | |
| 144 | - | if ( in_array($activity->id, $include_ids ) ) {
|
| 144 | + | if ( in_array($activity->id, $exclude_ids ) ) {
|
| 145 | // need comment support here | |
| 146 | unset( $activities_template->activities[$activity_key] ); | |
| 147 | ||
| 148 | $activities_template->activity_count--; | |
| 149 | $activities_template->total_activity_count--; | |
| 150 | } | |
| 151 | } | |
| 152 | ||
| 153 | // Reset indexes | |
| 154 | $activities_template->activities = array_values( $activities_template->activities ); | |
| 155 | ||
| 156 | - | return $activities_template['include=$ids']; |
| 156 | + | return $has_activities; |
| 157 | } | |
| 158 | else return $has_activities; | |
| 159 | ||
| 160 | ||
| 161 | } | |
| 162 | add_filter( 'bp_has_activities', 'mod_exclude_pending_activities'); | |
| 163 | ||
| 164 | ||
| 165 | ||
| 166 | ||
| 167 | //Check if activity ID is one of those in the list of IDs that don't have a meta_value of pending | |
| 168 | function mod_needs_approval($acivity_id=false) {
| |
| 169 | global $bp, $activities_template; | |
| 170 | ||
| 171 | $group = $bp->groups->current_group->id; | |
| 172 | if (!isset($group)) | |
| 173 | {
| |
| 174 | $group = $bp->activity->item_id; | |
| 175 | } | |
| 176 | if(!isset($activity_id)) $activity_id = bp_get_activity_id(); | |
| 177 | ||
| 178 | $published_ids = explode( ",", mod_get_pending_ids($group)); | |
| 179 | ||
| 180 | if ( !in_array($activity_id, $published_ids ) ) return false; | |
| 181 | else return true; | |
| 182 | } | |
| 183 | ||
| 184 | ||
| 185 | ||
| 186 | ||
| 187 | ||
| 188 | //Apply filters for moderators to approve pending activity | |
| 189 | function mod_add_pending_class_to_activity ($class ) | |
| 190 | {
| |
| 191 | global $bp, $activities_template; | |
| 192 | ||
| 193 | $activity_id = bp_get_activity_id(); | |
| 194 | ||
| 195 | if ( mod_needs_approval($activity_id) == false ){
| |
| 196 | return $activities_template->activity->component . ' ' . $activities_template->activity->type . $class; | |
| 197 | } | |
| 198 | else return $activities_template->activity->component . ' ' . $activities_template->activity->type . $class . ' pending'; | |
| 199 | } | |
| 200 | add_filter ('bp_get_activity_css_class', 'mod_add_pending_class_to_activity' );
| |
| 201 | ||
| 202 | //Construct the link for the mod to approve | |
| 203 | function mod_get_activity_approve_link() {
| |
| 204 | global $activities_template; | |
| 205 | return apply_filters( 'mod_get_activity_approve_link', wp_nonce_url( site_url( BP_ACTIVITY_SLUG . '/approve/' . $activities_template->activity->id . '/' ), 'status_publish' ) ); | |
| 206 | } | |
| 207 | ||
| 208 | ||
| 209 | ||
| 210 | ||
| 211 | ||
| 212 | /* AJAX mark an activity as published */ | |
| 213 | function mod_mark_activity_publish() {
| |
| 214 | global $bp; | |
| 215 | ||
| 216 | mod_activity_add_status_publish( $_POST['id'] ); | |
| 217 | } | |
| 218 | add_action( 'wp_ajax_activity_mark_publish', 'mod_mark_activity_publish' ); | |
| 219 | ||
| 220 | ||
| 221 | //Add meta to activity that it is published! | |
| 222 | function mod_activity_add_status_publish( $activity_id ) {
| |
| 223 | global $bp, $wpdb; | |
| 224 | ||
| 225 | $activity_table = $wpdb->prefix . 'bp_ativity'; | |
| 226 | ||
| 227 | //$activity = $wpdb->get_row($wpdb->prepare('SELECT * FROM $activity_table WHERE id = ' . $activity_id), 'OBJECT');
| |
| 228 | $activity = new BP_Activity_Activity( $activity_id ); | |
| 229 | ||
| 230 | if (bp_activity_update_meta( $activity_id, 'status', 'publish' ) ) {
| |
| 231 | do_action( 'custom_after_activity_approved', $activity ); | |
| 232 | return true; | |
| 233 | } | |
| 234 | else return false; | |
| 235 | } | |
| 236 | ||
| 237 | ||
| 238 | ||
| 239 | ||
| 240 | ||
| 241 | ||
| 242 | ||
| 243 | ||
| 244 | ||
| 245 | ||
| 246 | //When the approval action is called, run the function to approve it | |
| 247 | function mod_activity_action_approve() {
| |
| 248 | global $bp, $wpdb; | |
| 249 | ||
| 250 | if ( !is_user_logged_in() || $bp->current_component != $bp->activity->slug || $bp->current_action != 'approve' ) | |
| 251 | return false; | |
| 252 | ||
| 253 | /* Check the nonce */ | |
| 254 | check_admin_referer( 'status_publish' ); | |
| 255 | ||
| 256 | if ( mod_activity_add_status_publish( $bp->action_variables[0] ) ) | |
| 257 | bp_core_add_message( __( 'Activity approved.', 'buddypress' ) ); | |
| 258 | else | |
| 259 | bp_core_add_message( __( 'There was an error approving that message, please try again.', 'buddypress' ), 'error' ); | |
| 260 | ||
| 261 | bp_core_redirect( wp_get_referer() . '#activity-' . $bp->action_variables[0] ); | |
| 262 | } | |
| 263 | add_action( 'wp', 'mod_activity_action_approve', 3 ); | |
| 264 | ||
| 265 | ||
| 266 | //Add a link for the mod to approve an activity | |
| 267 | function mod_add_approval_link() {
| |
| 268 | global $bp, $activities_template; | |
| 269 | if( mod_user_is_mod() !== true ) return; | |
| 270 | if (mod_needs_approval() == true){
| |
| 271 | echo '<div class="moderation"> | |
| 272 | <a href="' . mod_get_activity_approve_link() . '" class="moderate-this">Approve</a> | |
| 273 | </div> | |
| 274 | <div class="clear"></div>'; | |
| 275 | } | |
| 276 | } | |
| 277 | add_action( 'bp_activity_entry_meta', 'mod_add_approval_link'); | |
| 278 | ||
| 279 | //Add some text to let the user know their post is awaiting moderation | |
| 280 | function mod_inform_user_activity_is_pending () {
| |
| 281 | global $bp, $activities_template; | |
| 282 | if( mod_user_is_mod() == true ) return; | |
| 283 | ||
| 284 | if(mod_get_group_activity_moderation() !== 'yes') return; | |
| 285 | if (mod_needs_approval() == true){
| |
| 286 | echo '<div id="message">Your post is awaiting approval.</div>'; | |
| 287 | } | |
| 288 | } | |
| 289 | add_action( 'bp_activity_entry_meta', 'mod_inform_user_activity_is_pending'); | |
| 290 | ||
| 291 | ||
| 292 | // Get the moderation settings for the group | |
| 293 | function mod_get_group_activity_moderation($group_id=false) {
| |
| 294 | global $bp; | |
| 295 | if(!$group_id) | |
| 296 | $group_id = $bp->groups->current_group->id; | |
| 297 | ||
| 298 | $default_subscription = groups_get_groupmeta( $group_id, 'activity_moderation' ); | |
| 299 | return $default_subscription; | |
| 300 | } | |
| 301 | ||
| 302 | ||
| 303 | ||
| 304 | ||
| 305 | ||
| 306 | ||
| 307 | ||
| 308 | ||
| 309 | ||
| 310 | //Add publish or pending option for group activity | |
| 311 | function mod_group_activity_moderation_form() {
| |
| 312 | ?> | |
| 313 | <hr /> | |
| 314 | <h4>Moderate Group Member Posts</h4> | |
| 315 | <p>Should new posts from group members to this group be moderated?</p> | |
| 316 | <div class="radio"> | |
| 317 | <label><input type="radio" name="group_activity_moderation" value="no" <?php mod_group_activity_moderation( 'no' ) ?> /> | |
| 318 | No moderation (any group member can post to the rest of the group freely)</label> | |
| 319 | <label><input type="radio" name="group_activity_moderation" value="yes" <?php mod_group_activity_moderation( 'yes' ) ?> /> | |
| 320 | Yes, moderate group member posts (this will require a group admin to moderate each post)</label> | |
| 321 | </div> | |
| 322 | <hr /> | |
| 323 | <?php | |
| 324 | } | |
| 325 | add_action ( 'bp_after_group_settings_admin' ,'mod_group_activity_moderation_form' ); | |
| 326 | add_action ( 'bp_after_group_settings_creation_step' ,'mod_group_activity_moderation_form' ); | |
| 327 | ||
| 328 | ||
| 329 | // echo subscription default checked setting for the group admin settings - default to 'unsubscribed' in group creation | |
| 330 | function mod_group_activity_moderation( $setting ) {
| |
| 331 | $stored_setting = mod_get_group_activity_moderation(); | |
| 332 | if ( $setting == $stored_setting ) | |
| 333 | echo ' checked="checked"'; | |
| 334 | else if ( $setting == 'no' && !$stored_setting ) | |
| 335 | echo ' checked="checked"'; | |
| 336 | } | |
| 337 | ||
| 338 | ||
| 339 | // Save the group activity moderation setting in the group meta, if no, delete it | |
| 340 | function mod_save_group_activity_moderation_seting( $group ) {
| |
| 341 | global $bp, $_POST; | |
| 342 | ||
| 343 | if ( isset( $_POST['group_activity_moderation'] ) && $postval = $_POST['group_activity_moderation'] ) {
| |
| 344 | if ( $postval && $postval != 'no' ) | |
| 345 | groups_update_groupmeta( $group->id, 'activity_moderation', $postval ); | |
| 346 | elseif ( $postval == 'no' ) | |
| 347 | groups_delete_groupmeta( $group->id, 'activity_moderation' ); | |
| 348 | } | |
| 349 | } | |
| 350 | add_action( 'groups_group_after_save', 'mod_save_group_activity_moderation_seting' ); | |
| 351 | ||
| 352 | ||
| 353 | //If group activity is moderated, set new activity posts to pending | |
| 354 | function mod_add_activity_meta_pending($content, $user_id, $group_id, $activity_id) {
| |
| 355 | global $bp; | |
| 356 | ||
| 357 | if(mod_get_group_activity_moderation($group_id) == 'yes') {
| |
| 358 | if( bp_activity_update_meta( $activity_id, 'status', 'pending' ) ) | |
| 359 | {
| |
| 360 | bp_core_add_message( __( 'Update Was Submitted for Approval!', 'buddypress' ) ); | |
| 361 | ||
| 362 | //Send notification to group admins | |
| 363 | $admins = groups_get_group_admins( $group_id ); | |
| 364 | ||
| 365 | require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' ); | |
| 366 | ||
| 367 | for ( $i = 0; $i < count( $admins ); $i++ ) {
| |
| 368 | mod_groups_notification_new_approval( $user_id, $admins[$i]->user_id, $group_id, $activity_id ); | |
| 369 | } | |
| 370 | return; | |
| 371 | } | |
| 372 | } | |
| 373 | return; | |
| 374 | } | |
| 375 | add_action( 'bp_groups_posted_update', 'mod_add_activity_meta_pending', 10, 4); | |
| 376 | add_action( 'postie_activity_meta_pending', 'mod_add_activity_meta_pending', 9, 4); | |
| 377 | ||
| 378 | function mod_intercept_pending_comment($comment_id, $params=array()) {
| |
| 379 | global $bp; | |
| 380 | ||
| 381 | extract( $params ); | |
| 382 | ||
| 383 | $parent_activity = new BP_Activity_Activity( $activity_id ); | |
| 384 | $group_id = $parent_activity->item_id; | |
| 385 | ||
| 386 | if(mod_get_group_activity_moderation($group_id) == 'yes') {
| |
| 387 | if( bp_activity_update_meta( $comment_id, 'status', 'pending' ) ) | |
| 388 | {
| |
| 389 | bp_core_add_message( __( 'Update Was Submitted for Approval!', 'buddypress' ) ); | |
| 390 | ||
| 391 | //Send notification to group admins | |
| 392 | $admins = groups_get_group_admins( $group_id ); | |
| 393 | ||
| 394 | require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' ); | |
| 395 | ||
| 396 | for ( $i = 0; $i < count( $admins ); $i++ ) {
| |
| 397 | mod_groups_notification_new_approval( $user_id, $admins[$i]->user_id, $group_id, $comment_id ); | |
| 398 | } | |
| 399 | return; | |
| 400 | } | |
| 401 | } | |
| 402 | return; | |
| 403 | } | |
| 404 | add_action( 'bp_activity_comment_posted', 'mod_intercept_pending_comment', 10, 2); | |
| 405 | ||
| 406 | //Post notice in group header that the group is moderated. | |
| 407 | function mod_show_group_moderation_settings() {
| |
| 408 | global $bp, $activities_template; | |
| 409 | if(mod_get_group_activity_moderation() !== 'yes') return; | |
| 410 | ||
| 411 | $notice = '<div><span>'; | |
| 412 | if( mod_user_is_mod() == true ) {
| |
| 413 | $notice .= 'All activity posted in this group must be approved by the group moderator. Please click approve or delete for highlighted posts.'; | |
| 414 | } | |
| 415 | else $notice .= 'All activity posted in this group must be approved. Please allow some time for the moderator to review your post before it is publicly viewable.'; | |
| 416 | ||
| 417 | $notice .= '</span></div>'; | |
| 418 | ||
| 419 | echo $notice; | |
| 420 | ||
| 421 | } | |
| 422 | add_action('bp_group_header_meta', 'mod_show_group_moderation_settings', 100);
| |
| 423 | ||
| 424 | ||
| 425 | ||
| 426 | ||
| 427 | ||
| 428 | ||
| 429 | // Add options for receiving these notifications via email | |
| 430 | function mod_group_approval_screen_notification_settings() {
| |
| 431 | global $current_user; ?> | |
| 432 | <tr> | |
| 433 | <td></td> | |
| 434 | <td><?php _e( 'A message is posted in a moderated group for which you are an admin', 'buddypress' ) ?></td> | |
| 435 | <td class="yes"><input type="radio" id="notification_approval_yes" name="notifications[notification_group_approval]" value="yes" <?php if ( !get_user_meta( $current_user->id, 'notification_group_approval', true ) || 'yes' == get_user_meta( $current_user->id, 'notification_group_approval', true ) ) { ?>checked="checked" <?php } ?> /></td>
| |
| 436 | <td class="no"><input type="radio" id="notification_approval_no" name="notifications[notification_group_approval]" value="no" <?php if ( 'no' == get_user_meta( $current_user->id, 'notification_group_approval', true ) ) { ?>checked="checked" <?php } ?> />
| |
| 437 | </td> | |
| 438 | </tr> | |
| 439 | <?php | |
| 440 | } | |
| 441 | add_action( 'groups_screen_notification_settings', 'mod_group_approval_screen_notification_settings' ); | |
| 442 | ||
| 443 | ||
| 444 | ||
| 445 | ||
| 446 | ||
| 447 | ||
| 448 | ||
| 449 | ||
| 450 | ||
| 451 | ||
| 452 | ||
| 453 | ||
| 454 | ||
| 455 | ||
| 456 | ||
| 457 | ||
| 458 | ||
| 459 | ||
| 460 | ||
| 461 | ||
| 462 | /************************************************************** | |
| 463 | *Add a notification in the admin bar when a user's group has items to be approved | |
| 464 | */ | |
| 465 | ||
| 466 | //Send group admins an email & add notification to database for each admin of group | |
| 467 | function mod_groups_notification_new_approval( $requesting_user_id, $admin_id, $group_id, $activity_id ) {
| |
| 468 | global $bp; | |
| 469 | ||
| 470 | //bp_core_add_notification( $requesting_user_id, $admin_id, 'groups', 'new_activity_needs_approval', $group_id ); | |
| 471 | bp_core_add_notification( $activity_id, $admin_id, $bp->mod->id, 'new_activity_needs_approval_'.$activity_id, $group_id ); | |
| 472 | ||
| 473 | if ( 'no' == get_user_meta( $admin_id, 'notification_group_approval', true ) ) | |
| 474 | return false; | |
| 475 | ||
| 476 | $requesting_user_name = bp_core_get_user_displayname( $requesting_user_id ); | |
| 477 | $group = new BP_Groups_Group( $group_id ); | |
| 478 | ||
| 479 | $ud = bp_core_get_core_userdata($admin_id); | |
| 480 | $requesting_ud = bp_core_get_core_userdata($requesting_user_id); | |
| 481 | ||
| 482 | $group_requests = bp_get_group_permalink( $group ); | |
| 483 | $profile_link = bp_core_get_user_domain( $requesting_user_id ); | |
| 484 | $settings_link = bp_core_get_user_domain( $requesting_user_id ) . BP_SETTINGS_SLUG . '/notifications/'; | |
| 485 | ||
| 486 | $activity = new BP_Activity_Activity( $activity_id ); | |
| 487 | $activity_subject = bp_activity_get_meta($activity_id, 'subject'); | |
| 488 | if (!$activity_subject) $activity_subject = '<em>None</em>'; | |
| 489 | $activity_content = $activity->content; | |
| 490 | ||
| 491 | // Set up and send the message | |
| 492 | $to = $ud->user_email; | |
| 493 | $sitename = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); | |
| 494 | $subject = '[' . $sitename . '] ' . sprintf( __( 'Message Approval for group: %s', 'buddypress' ), $group->name ); | |
| 495 | $headers = 'Content-Type: text/html' . "\r\n"; | |
| 496 | ||
| 497 | $message = sprintf( __( | |
| 498 | '%s has posted a new item in "%s". | |
| 499 | ||
| 500 | Because you are the administrator of this group, you must either approve or delete the message. | |
| 501 | ||
| 502 | To view all pending messages for this group, please visit: | |
| 503 | %s | |
| 504 | ||
| 505 | To view %s\'s profile: %s | |
| 506 | ||
| 507 | --------------------- | |
| 508 | ||
| 509 | The message awaiting approval is below: | |
| 510 | ||
| 511 | Subject: %s | |
| 512 | ||
| 513 | %s | |
| 514 | ||
| 515 | --------------------- | |
| 516 | ||
| 517 | ', 'buddypress' ), $requesting_user_name, $group->name, $group_requests, $requesting_user_name, $profile_link, $activity_subject, $activity_content ); | |
| 518 | ||
| 519 | $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link ); | |
| 520 | ||
| 521 | /* Send the message */ | |
| 522 | $to = apply_filters( 'mod_groups_notification_new_approval_to', $to ); | |
| 523 | $subject = apply_filters( 'mod_groups_notification_new_approval_subject', $subject, &$group ); | |
| 524 | $message = apply_filters( 'mod_groups_notification_new_approval_message', $message, &$group, $requesting_user_name, $profile_link, $group_requests ); | |
| 525 | ||
| 526 | ||
| 527 | wp_mail( $to, $subject, $message ); | |
| 528 | } | |
| 529 | ||
| 530 | ||
| 531 | ||
| 532 | ||
| 533 | ||
| 534 | ||
| 535 | /** our notification format function which shows notification to user | |
| 536 | * | |
| 537 | * @global $bp | |
| 538 | * @param <type> $action | |
| 539 | * @param <type> $activity_id | |
| 540 | * @param <type> $secondary_item_id | |
| 541 | * @param <type> $total_items | |
| 542 | * @return <type> | |
| 543 | * @since 1.0.2 | |
| 544 | * @desc format and show the notification to the user | |
| 545 | */ | |
| 546 | function mod_format_notifications( $action, $activity_id, $secondary_item_id, $total_items ) {
| |
| 547 | ||
| 548 | global $bp; | |
| 549 | $activity = new BP_Activity_Activity( $activity_id ); | |
| 550 | $link = mod_activity_get_permalink( $activity_id ); | |
| 551 | ||
| 552 | ||
| 553 | $ac_action='new_activity_needs_approval_'.$activity_id; | |
| 554 | ||
| 555 | $group_id = $secondary_item_id; | |
| 556 | ||
| 557 | $user_id_to_be_moderated = mod_get_requesting_user_id($activity_id); | |
| 558 | $user_fullname = bp_core_get_user_displayname( $user_id_to_be_moderated ); | |
| 559 | ||
| 560 | $group = new BP_Groups_Group( $group_id ); | |
| 561 | ||
| 562 | $group_link = bp_get_group_permalink( $group ); | |
| 563 | ||
| 564 | if($action==$ac_action){
| |
| 565 | return apply_filters( 'mod_single_new_activity_needs_approval_notification', '<a href="' . $link . '">' . sprintf( __( '%s has a message awaiting moderation in "%s"', 'buddypress' ), $user_fullname, $group->name ) . '</a>' ); | |
| 566 | } | |
| 567 | do_action( 'mod_format_notifications', $action, $activity_id, $secondary_item_id, $total_items ); | |
| 568 | return false; | |
| 569 | } | |
| 570 | ||
| 571 | /** | |
| 572 | * This section deals with removing notification when a notified item is viewed | |
| 573 | */ | |
| 574 | ||
| 575 | /* | |
| 576 | * For single activity view, we will remove the notification associated with current user and current activity item | |
| 577 | */ | |
| 578 | ||
| 579 | ||
| 580 | function mod_remove_notification($activity,$has_access){
| |
| 581 | global $bp; | |
| 582 | if($has_access)//if user can view this activity, remove notification(just a safeguard for hidden activity) | |
| 583 | bp_core_delete_notifications_for_user_by_item_id( $bp->loggedin_user->id, $activity->id, $bp->mod->id, 'new_activity_needs_approval_'.$activity->id); | |
| 584 | ||
| 585 | } | |
| 586 | add_action("bp_activity_screen_single_activity_permalink","mod_remove_notification",10,2);
| |
| 587 | ||
| 588 | ||
| 589 | /** | |
| 590 | * @since v 1.0.2 | |
| 591 | * @desc delete notification when an activity is deleted, thanks to @kat_uk for pointing the issue | |
| 592 | * @param ac_ids:we get an arry of activity ids | |
| 593 | */ | |
| 594 | ||
| 595 | function bp_mod_clear_notification_on_activity_delete($ac_ids){
| |
| 596 | global $bp; | |
| 597 | ||
| 598 | //bp_core_delete_notifications_for_user_by_item_id( $bp->loggedin_user->id, $activity->id, $bp->activity->id, 'new_activity_comment_'.$activity->id); | |
| 599 | foreach((array)$ac_ids as $activity_id) | |
| 600 | bp_core_delete_all_notifications_by_type( $activity_id, $bp->ac_notifier->id, 'new_activity_needs_approval_'.$activity_id, $secondary_item_id = false ); | |
| 601 | } | |
| 602 | add_action("bp_activity_deleted_activities","bp_mod_clear_notification_on_activity_delete");
| |
| 603 | ||
| 604 | ||
| 605 | ||
| 606 | ||
| 607 | ||
| 608 | ||
| 609 | ||
| 610 | ||
| 611 | ||
| 612 | ||
| 613 | ||
| 614 | ||
| 615 | /************************************ HELPER FUNCTIONS ********************************************************/ | |
| 616 | /** | |
| 617 | * @desc: find all the unique user_ids who have commented on this activity | |
| 618 | */ | |
| 619 | ||
| 620 | function mod_find_involved_persons($activity_id){
| |
| 621 | global $bp,$wpdb; | |
| 622 | return $wpdb->get_col($wpdb->prepare("select DISTINCT(user_id) from {$bp->activity->table_name} where item_id=%d and user_id!=%d ",$activity_id,$bp->loggedin_user->id));//it finds all uses who commted on the activity
| |
| 623 | } | |
| 624 | ||
| 625 | /** | |
| 626 | * @desc: find all the unique user_ids who have commented on this activity | |
| 627 | */ | |
| 628 | ||
| 629 | function mod_get_requesting_user_id($activity_id){
| |
| 630 | global $bp,$wpdb; | |
| 631 | return $wpdb->get_var($wpdb->prepare("select DISTINCT(user_id) from {$bp->activity->table_name} where id=%d",$activity_id));//it finds all uses who commted on the activity
| |
| 632 | } | |
| 633 | ||
| 634 | ||
| 635 | ||
| 636 | ||
| 637 | //get the thread permalink for activity | |
| 638 | function mod_activity_get_permalink( $activity_id, $activity_obj = false ) {
| |
| 639 | global $bp; | |
| 640 | ||
| 641 | if ( !$activity_obj ) | |
| 642 | $activity_obj = new BP_Activity_Activity( $activity_id ); | |
| 643 | ||
| 644 | ||
| 645 | if ( 'activity_comment' == $activity_obj->type ) | |
| 646 | $link = $bp->root_domain . '/' . BP_ACTIVITY_SLUG . '/p/' . $activity_obj->item_id . '/'; | |
| 647 | else | |
| 648 | $link = $bp->root_domain . '/' . BP_ACTIVITY_SLUG . '/p/' . $activity_obj->id . '/'; | |
| 649 | ||
| 650 | ||
| 651 | return apply_filters( 'mod_activity_get_permalink', $link ); | |
| 652 | } | |
| 653 | ?> |