3276 | | return array( |
3277 | | 'activity-comment' => __( 'A member has replied to an activity update that the recipient posted.', 'buddypress' ), |
3278 | | 'activity-comment-author' => __( 'A member has replied to a comment on an activity update that the recipient posted.', 'buddypress' ), |
3279 | | 'activity-at-message' => __( 'Recipient was mentioned in an activity update.', 'buddypress' ), |
3280 | | 'groups-at-message' => __( 'Recipient was mentioned in a group activity update.', 'buddypress' ), |
3281 | | 'core-user-registration' => __( 'Recipient has registered for an account.', 'buddypress' ), |
3282 | | 'core-user-registration-with-blog' => __( 'Recipient has registered for an account and site.', 'buddypress' ), |
3283 | | 'friends-request' => __( 'A member has sent a friend request to the recipient.', 'buddypress' ), |
3284 | | 'friends-request-accepted' => __( 'Recipient has had a friend request accepted by a member.', 'buddypress' ), |
3285 | | 'groups-details-updated' => __( "A group's details were updated.", 'buddypress' ), |
3286 | | 'groups-invitation' => __( 'A member has sent a group invitation to the recipient.', 'buddypress' ), |
3287 | | 'groups-member-promoted' => __( "Recipient's status within a group has changed.", 'buddypress' ), |
3288 | | 'groups-membership-request' => __( 'A member has requested permission to join a group.', 'buddypress' ), |
3289 | | 'messages-unread' => __( 'Recipient has received a private message.', 'buddypress' ), |
3290 | | 'settings-verify-email-change' => __( 'Recipient has changed their email address.', 'buddypress' ), |
3291 | | 'groups-membership-request-accepted' => __( 'Recipient had requested to join a group, which was accepted.', 'buddypress' ), |
3292 | | 'groups-membership-request-rejected' => __( 'Recipient had requested to join a group, which was rejected.', 'buddypress' ), |
| 3280 | $type_schema_description = wp_list_pluck( bp_email_type_schema(), 'description' ); |
| 3281 | return $type_schema_description; |
| 3282 | } |
| 3283 | |
| 3284 | /** |
| 3285 | * Get a list of emails for populating email type taxonomy terms. |
| 3286 | * |
| 3287 | * @since |
| 3288 | * |
| 3289 | * @return array Array of the email type schema. |
| 3290 | */ |
| 3291 | function bp_email_type_schema() { |
| 3292 | |
| 3293 | $activity_comment = array( |
| 3294 | 'description' => __( 'A member has replied to an activity update that the recipient posted.', 'buddypress' ), |
| 3295 | 'unsubscribe' => array( |
| 3296 | 'meta_key' => 'notification_activity_new_reply', |
| 3297 | 'message' => __( 'You will no longer receive emails when someone replies to an update or comment you posted.', 'buddypress' ), |
| 3298 | ), |
| 3300 | |
| 3301 | $activity_comment_author = array( |
| 3302 | 'description' => __( 'A member has replied to a comment on an activity update that the recipient posted.', 'buddypress' ), |
| 3303 | 'unsubscribe' => array( |
| 3304 | 'meta_key' => 'notification_activity_new_reply', |
| 3305 | 'message' => __( 'You will no longer receive emails when someone replies to an update or comment you posted.', 'buddypress' ), |
| 3306 | ), |
| 3307 | ); |
| 3308 | |
| 3309 | $activity_at_message = array( |
| 3310 | 'description' => __( 'Recipient was mentioned in an activity update.', 'buddypress' ), |
| 3311 | 'unsubscribe' => array( |
| 3312 | 'meta_key' => 'notification_activity_new_mention', |
| 3313 | 'message' => __( 'You will no longer receive emails when someone mentions you in an update.', 'buddypress' ), |
| 3314 | ), |
| 3315 | ); |
| 3316 | |
| 3317 | $groups_at_message = array( |
| 3318 | 'description' => __( 'Recipient was mentioned in a group activity update.', 'buddypress' ), |
| 3319 | 'unsubscribe' => array( |
| 3320 | 'meta_key' => 'notification_activity_new_mention', |
| 3321 | 'message' => __( 'You will no longer receive emails when someone mentions you in an update.', 'buddypress' ), |
| 3322 | ), |
| 3323 | ); |
| 3324 | |
| 3325 | $core_user_registration = array( |
| 3326 | 'description' => __( 'Recipient has registered for an account.', 'buddypress' ), |
| 3327 | 'unsubscribe' => false, |
| 3328 | ); |
| 3329 | |
| 3330 | $core_user_registration_with_blog = array( |
| 3331 | 'description' => __( 'Recipient has registered for an account and site.', 'buddypress' ), |
| 3332 | 'unsubscribe' => false, |
| 3333 | ); |
| 3334 | |
| 3335 | $friends_request = array( |
| 3336 | 'description' => __( 'A member has sent a friend request to the recipient.', 'buddypress' ), |
| 3337 | 'unsubscribe' => array( |
| 3338 | 'meta_key' => 'notification_friends_friendship_request', |
| 3339 | 'message' => __( 'You will no longer receive emails when someone sends you a friend request.', 'buddypress' ), |
| 3340 | ), |
| 3341 | ); |
| 3342 | |
| 3343 | $friends_request_accepted = array( |
| 3344 | 'description' => __( 'Recipient has had a friend request accepted by a member.', 'buddypress' ), |
| 3345 | 'unsubscribe' => array( |
| 3346 | 'meta_key' => 'notification_friends_friendship_accepted', |
| 3347 | 'message' => __( 'You will no longer receive emails when someone accepts your friendship request.', 'buddypress' ), |
| 3348 | ), |
| 3349 | ); |
| 3350 | |
| 3351 | $groups_details_updated = array( |
| 3352 | 'description' => __( "A group's details were updated.", 'buddypress' ), |
| 3353 | 'unsubscribe' => array( |
| 3354 | 'meta_key' => 'notification_groups_group_updated', |
| 3355 | 'message' => __( 'You will no longer receive emails when one of your groups is updated.', 'buddypress' ), |
| 3356 | ), |
| 3357 | ); |
| 3358 | |
| 3359 | $groups_details_updated = array( |
| 3360 | 'description' => __( "A group's details were updated.", 'buddypress' ), |
| 3361 | 'unsubscribe' => array( |
| 3362 | 'meta_key' => 'notification_groups_group_updated', |
| 3363 | 'message' => __( 'You will no longer receive emails when one of your groups is updated.', 'buddypress' ), |
| 3364 | ), |
| 3365 | ); |
| 3366 | |
| 3367 | $groups_invitation = array( |
| 3368 | 'description' => __( 'A member has sent a group invitation to the recipient.', 'buddypress' ), |
| 3369 | 'unsubscribe' => array( |
| 3370 | 'meta_key' => 'notification_groups_invite', |
| 3371 | 'message' => __( 'You will no longer receive emails when you are invited to join a group.', 'buddypress' ), |
| 3372 | ), |
| 3373 | ); |
| 3374 | |
| 3375 | $groups_member_promoted = array( |
| 3376 | 'description' => __( "Recipient's status within a group has changed.", 'buddypress' ), |
| 3377 | 'unsubscribe' => array( |
| 3378 | 'meta_key' => 'notification_groups_admin_promotion', |
| 3379 | 'message' => __( 'You will no longer receive emails when you have been promoted in a group.', 'buddypress' ), |
| 3380 | ), |
| 3381 | ); |
| 3382 | |
| 3383 | $groups_member_promoted = array( |
| 3384 | 'description' => __( "Recipient's status within a group has changed.", 'buddypress' ), |
| 3385 | 'unsubscribe' => array( |
| 3386 | 'meta_key' => 'notification_groups_admin_promotion', |
| 3387 | 'message' => __( 'You will no longer receive emails when you have been promoted in a group.', 'buddypress' ), |
| 3388 | ), |
| 3389 | ); |
| 3390 | |
| 3391 | $groups_membership_request = array( |
| 3392 | 'description' => __( 'A member has requested permission to join a group.', 'buddypress' ), |
| 3393 | 'unsubscribe' => array( |
| 3394 | 'meta_key' => 'notification_groups_membership_request', |
| 3395 | 'message' => __( 'You will no longer receive emails when someone requests to be a member of your group.', 'buddypress' ), |
| 3396 | ), |
| 3397 | ); |
| 3398 | |
| 3399 | $messages_unread = array( |
| 3400 | 'description' => __( 'Recipient has received a private message.', 'buddypress' ), |
| 3401 | 'unsubscribe' => array( |
| 3402 | 'meta_key' => 'notification_messages_new_message', |
| 3403 | 'message' => __( 'You will no longer receive emails when someone sends you a message.', 'buddypress' ), |
| 3404 | ), |
| 3405 | ); |
| 3406 | |
| 3407 | $settings_verify_email_change = array( |
| 3408 | 'description' => __( 'Recipient has changed their email address.', 'buddypress' ), |
| 3409 | 'unsubscribe' => false, |
| 3410 | ); |
| 3411 | |
| 3412 | $groups_membership_request_accepted = array( |
| 3413 | 'description' => __( 'Recipient had requested to join a group, which was accepted.', 'buddypress' ), |
| 3414 | 'unsubscribe' => array( |
| 3415 | 'meta_key' => 'notification_membership_request_completed', |
| 3416 | 'message' => __( 'You will no longer receive emails when your request to join a group has been accepted or denied.', 'buddypress' ), |
| 3417 | ), |
| 3418 | ); |
| 3419 | |
| 3420 | $groups_membership_request_rejected = array( |
| 3421 | 'description' => __( 'Recipient had requested to join a group, which was rejected.', 'buddypress' ), |
| 3422 | 'unsubscribe' => array( |
| 3423 | 'meta_key' => 'notification_membership_request_completed', |
| 3424 | 'message' => __( 'You will no longer receive emails when your request to join a group has been accepted or denied.', 'buddypress' ), |
| 3425 | ), |
| 3426 | ); |
| 3427 | |
| 3428 | $email_type_schema_array = array( |
| 3429 | 'activity-comment' => $activity_comment, |
| 3430 | 'activity-comment-author' => $activity_comment_author, |
| 3431 | 'activity-at-message' => $activity_at_message, |
| 3432 | 'groups-at-message' => $groups_at_message, |
| 3433 | 'core-user-registration' => $core_user_registration, |
| 3434 | 'core-user-registration-with-blog' => $core_user_registration_with_blog, |
| 3435 | 'friends-request' => $friends_request, |
| 3436 | 'friends-request-accepted' => $friends_request_accepted, |
| 3437 | 'groups-details-updated' => $groups_details_updated, |
| 3438 | 'groups-invitation' => $groups_invitation, |
| 3439 | 'groups-member-promoted' => $groups_member_promoted, |
| 3440 | 'groups-membership-request' => $groups_membership_request, |
| 3441 | 'messages-unread' => $messages_unread, |
| 3442 | 'settings-verify-email-change' => $settings_verify_email_change, |
| 3443 | 'groups-membership-request-accepted' => $groups_membership_request_accepted, |
| 3444 | 'groups-membership-request-rejected' => $groups_membership_request_rejected, |
| 3445 | ); |
| 3446 | |
| 3447 | /** |
| 3448 | * Email types schema. |
| 3449 | * |
| 3450 | * @since |
| 3451 | * |
| 3452 | * @param array $email_type_schema_array { |
| 3453 | * The array of email types and their schema. |
| 3454 | * |
| 3455 | * @type string $description The description of the action which causes this to trigger. |
| 3456 | * @type array $email_type_schema_array['unsubscribe'] { |
| 3457 | * The unsubscribe array. |
| 3458 | * Setting the value to false indicates that a user cannot unsubscribe from this type. |
| 3459 | * |
| 3460 | * @type string $email_type_schema_array['unsubscribe']['meta_key'] The meta_key used to toggle the email setting for this notification. |
| 3461 | * @type string $email_type_schema_array['unsubscribe']['message'] The message shown when the user has successfully unsubscribed. |
| 3462 | * } |
| 3463 | * } |
| 3464 | */ |
| 3465 | return apply_filters( 'bp_email_type_schema', $email_type_schema_array ); |
| 3467 | |
| 3468 | /** |
| 3469 | * Handles unsubscribe action to unsubscribe user from notification emails. |
| 3470 | * |
| 3471 | * @since |
| 3472 | * |
| 3473 | */ |
| 3474 | function bp_emails_unsubscribe_handler() { |
| 3475 | |
| 3476 | $request = $_GET; |
| 3477 | $admin_email = get_bloginfo( 'admin_email' ); |
| 3478 | $bp_email_schema = bp_email_type_schema(); |
| 3479 | |
| 3480 | $user_id = ( isset( $request['uid'] ) && ! empty( $request['uid'] ) ) ? (int) $request['uid'] : ''; |
| 3481 | $notification_type = ( isset( $request['nt'] ) && ! empty( $request['nt'] ) ) ? esc_attr( $request['nt'] ) : ''; |
| 3482 | $to_check = ( isset( $request['nn'] ) && ! empty( $request['nn'] ) ) ? $request['nn'] : ''; |
| 3483 | |
| 3484 | $check_args = array( |
| 3485 | 'uid' => $user_id, |
| 3486 | 'nt' => $notification_type, |
| 3487 | ); |
| 3488 | |
| 3489 | $check = bp_hash_array( $check_args ); |
| 3490 | |
| 3491 | $current_user = is_user_logged_in() ? wp_get_current_user() : false; |
| 3492 | |
| 3493 | if ( empty( $user_id ) || empty( $notification_type ) || empty( $to_check ) || ! array_key_exists( $notification_type, $bp_email_schema ) ) { |
| 3494 | |
| 3495 | $settings_link = site_url( 'wp-login.php' ); |
| 3496 | $result_message = __( 'Sorry, something has gone wrong.', 'buddypress' ); |
| 3497 | $how_to_unsubscribe_message = __( 'Please log in and go to your settings to unsubscribe from notifications.', 'buddypress' ); |
| 3498 | } elseif ( ! hash_equals( $check, $to_check ) ) { |
| 3499 | |
| 3500 | $settings_link = site_url( 'wp-login.php' ); |
| 3501 | $result_message = __( 'Sorry the security check failed.', 'buddypress' ); |
| 3502 | $how_to_unsubscribe_message = __( 'Please log in and go to your settings to unsubscribe from notifications.', 'buddypress' ); |
| 3503 | |
| 3504 | } elseif ( is_user_logged_in() && (int) $current_user->ID !== (int) $user_id ) { |
| 3505 | |
| 3506 | $settings_link = esc_url( sprintf( |
| 3507 | '%s%s/notifications/', |
| 3508 | bp_core_get_user_domain( (int) $current_user->ID ), |
| 3509 | function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings' |
| 3510 | ) ); |
| 3511 | |
| 3512 | $result_message = __( 'Something has gone wrong.', 'buddypress' ); |
| 3513 | $how_to_unsubscribe_message = __( 'Please go to your settings to unsubscribe from notifications.', 'buddypress' ); |
| 3514 | } else { |
| 3515 | |
| 3516 | $settings_link = esc_url( sprintf( |
| 3517 | '%s%s/notifications/', |
| 3518 | bp_core_get_user_domain( $user_id ), |
| 3519 | function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings' |
| 3520 | ) ); |
| 3521 | |
| 3522 | // unsubscribe them by setting the usermeta to 'no' |
| 3523 | $notification_meta_key = $bp_email_schema[ $notification_type ]['unsubscribe']['meta_key']; |
| 3524 | $is_unsubscribed = bp_update_user_meta( $user_id, $notification_meta_key, 'no' ); |
| 3525 | |
| 3526 | $result_message = ( empty( $is_unsubscribed ) && bp_get_user_meta( $user_id, $notification_meta_key, true ) !== 'no' ) ? __( 'Sorry, something has gone wrong with unsubscribing you from these emails.', 'buddypress' ) : $bp_email_schema[ $notification_type ]['unsubscribe']['message']; |
| 3527 | |
| 3528 | $how_to_unsubscribe_message = __( 'You can change this or any other email notification preferences in your email settings.', 'buddypress' ); |
| 3529 | } |
| 3530 | |
| 3531 | $message = sprintf( |
| 3532 | '%1$s <a href="%2$s">%3$s</a>', |
| 3533 | $result_message, |
| 3534 | $settings_link, |
| 3535 | $how_to_unsubscribe_message |
| 3536 | ); |
| 3537 | |
| 3538 | bp_core_add_message( $message ); |
| 3539 | bp_core_redirect( esc_url( remove_query_arg( array_keys( $request ) ) ) ); |
| 3540 | } |
| 3541 | |
| 3542 | /** |
| 3543 | * Creates unsubscribe link for notification emails. |
| 3544 | * |
| 3545 | * @since |
| 3546 | * |
| 3547 | * @param string $redirect_to The URL to which the unsubscribe query string is appended. |
| 3548 | * @param array $args { |
| 3549 | * |
| 3550 | * Used to build unsubscribe query string. |
| 3551 | * |
| 3552 | * @type string $notification_type Which notification type is being sent. |
| 3553 | * @type string $user_id The ID of the user to whom the notification is sent. |
| 3554 | * @type string $redirect_to Optional. The url to which the user will be redirected. Default is the activity directory. |
| 3555 | * |
| 3556 | * } |
| 3557 | * @return string The unsubscribe link. |
| 3558 | */ |
| 3559 | function bp_email_get_unsubscribe_link( $args = null ) { |
| 3560 | |
| 3561 | $bp_emails_type_schema = bp_email_type_schema(); |
| 3562 | |
| 3563 | if ( ! is_array( $args ) || ! isset( $args['notification_type'] ) || ! array_key_exists( $args['notification_type'], $bp_emails_type_schema ) ) { |
| 3564 | return site_url( 'wp-login.php' ); |
| 3565 | } |
| 3566 | |
| 3567 | $notification_type = $unsubscribe_args['nt'] = esc_attr( $args['notification_type'] ); |
| 3568 | $user_id = $unsubscribe_args['uid'] = (int) $args['user_id']; |
| 3569 | |
| 3570 | // if the activity type is not unsubscribable (ununsubscribable), return false |
| 3571 | if ( empty( $bp_emails_type_schema[ $notification_type ]['unsubscribe'] ) ) { |
| 3572 | return false; |
| 3573 | } |
| 3574 | |
| 3575 | $redirect_to = ( isset( $args['redirect_to'] ) ) ? isset( $args['redirect_to'] ) : bp_get_activity_directory_permalink(); |
| 3576 | |
| 3577 | $check = bp_hash_array( $unsubscribe_args ); |
| 3578 | |
| 3579 | $unsubscribe_args['nn'] = $check; |
| 3580 | $unsubscribe_args['action'] = 'unsubscribe'; |
| 3581 | |
| 3582 | $unsubscribe_link = esc_url( add_query_arg( $unsubscribe_args, $redirect_to ) ); |
| 3583 | |
| 3584 | /** |
| 3585 | * Filters the unsubscribe link. |
| 3586 | * |
| 3587 | * @since |
| 3588 | * |
| 3589 | * @param string $redirect_to URL to which the unsubscribe query string is appended. |
| 3590 | * @param array $args Used to build unsubscribe query string. |
| 3591 | */ |
| 3592 | return apply_filters( 'bp_email_get_unsubscribe_link', $unsubscribe_link, $redirect_to, $args ); |
| 3593 | } |
| 3594 | |