3361 | | return array( |
3362 | | 'activity-comment' => __( 'A member has replied to an activity update that the recipient posted.', 'buddypress' ), |
3363 | | 'activity-comment-author' => __( 'A member has replied to a comment on an activity update that the recipient posted.', 'buddypress' ), |
3364 | | 'activity-at-message' => __( 'Recipient was mentioned in an activity update.', 'buddypress' ), |
3365 | | 'groups-at-message' => __( 'Recipient was mentioned in a group activity update.', 'buddypress' ), |
3366 | | 'core-user-registration' => __( 'Recipient has registered for an account.', 'buddypress' ), |
3367 | | 'core-user-registration-with-blog' => __( 'Recipient has registered for an account and site.', 'buddypress' ), |
3368 | | 'friends-request' => __( 'A member has sent a friend request to the recipient.', 'buddypress' ), |
3369 | | 'friends-request-accepted' => __( 'Recipient has had a friend request accepted by a member.', 'buddypress' ), |
3370 | | 'groups-details-updated' => __( "A group's details were updated.", 'buddypress' ), |
3371 | | 'groups-invitation' => __( 'A member has sent a group invitation to the recipient.', 'buddypress' ), |
3372 | | 'groups-member-promoted' => __( "Recipient's status within a group has changed.", 'buddypress' ), |
3373 | | 'groups-membership-request' => __( 'A member has requested permission to join a group.', 'buddypress' ), |
3374 | | 'messages-unread' => __( 'Recipient has received a private message.', 'buddypress' ), |
3375 | | 'settings-verify-email-change' => __( 'Recipient has changed their email address.', 'buddypress' ), |
3376 | | 'groups-membership-request-accepted' => __( 'Recipient had requested to join a group, which was accepted.', 'buddypress' ), |
3377 | | 'groups-membership-request-rejected' => __( 'Recipient had requested to join a group, which was rejected.', 'buddypress' ), |
| 3364 | $type_schema_description = wp_list_pluck( bp_email_types_get_schema(), 'description' ); |
| 3365 | return $type_schema_description; |
| 3366 | } |
| 3367 | |
| 3368 | /** |
| 3369 | * Get a list of emails for populating email type taxonomy terms. |
| 3370 | * |
| 3371 | * @since |
| 3372 | * |
| 3373 | * @return array Array of the email type schema. |
| 3374 | */ |
| 3375 | function bp_email_types_get_schema() { |
| 3376 | |
| 3377 | $activity_comment = array( |
| 3378 | 'description' => __( 'A member has replied to an activity update that the recipient posted.', 'buddypress' ), |
| 3379 | 'unsubscribe' => array( |
| 3380 | 'meta_key' => 'notification_activity_new_reply', |
| 3381 | 'message' => __( 'You will no longer receive emails when someone replies to an update or comment you posted.', 'buddypress' ), |
| 3382 | ), |
| 3384 | |
| 3385 | $activity_comment_author = array( |
| 3386 | 'description' => __( 'A member has replied to a comment on an activity update that the recipient posted.', 'buddypress' ), |
| 3387 | 'unsubscribe' => array( |
| 3388 | 'meta_key' => 'notification_activity_new_reply', |
| 3389 | 'message' => __( 'You will no longer receive emails when someone replies to an update or comment you posted.', 'buddypress' ), |
| 3390 | ), |
| 3391 | ); |
| 3392 | |
| 3393 | $activity_at_message = array( |
| 3394 | 'description' => __( 'Recipient was mentioned in an activity update.', 'buddypress' ), |
| 3395 | 'unsubscribe' => array( |
| 3396 | 'meta_key' => 'notification_activity_new_mention', |
| 3397 | 'message' => __( 'You will no longer receive emails when someone mentions you in an update.', 'buddypress' ), |
| 3398 | ), |
| 3399 | ); |
| 3400 | |
| 3401 | $groups_at_message = array( |
| 3402 | 'description' => __( 'Recipient was mentioned in a group activity update.', 'buddypress' ), |
| 3403 | 'unsubscribe' => array( |
| 3404 | 'meta_key' => 'notification_activity_new_mention', |
| 3405 | 'message' => __( 'You will no longer receive emails when someone mentions you in an update.', 'buddypress' ), |
| 3406 | ), |
| 3407 | ); |
| 3408 | |
| 3409 | $core_user_registration = array( |
| 3410 | 'description' => __( 'Recipient has registered for an account.', 'buddypress' ), |
| 3411 | 'unsubscribe' => false, |
| 3412 | ); |
| 3413 | |
| 3414 | $core_user_registration_with_blog = array( |
| 3415 | 'description' => __( 'Recipient has registered for an account and site.', 'buddypress' ), |
| 3416 | 'unsubscribe' => false, |
| 3417 | ); |
| 3418 | |
| 3419 | $friends_request = array( |
| 3420 | 'description' => __( 'A member has sent a friend request to the recipient.', 'buddypress' ), |
| 3421 | 'unsubscribe' => array( |
| 3422 | 'meta_key' => 'notification_friends_friendship_request', |
| 3423 | 'message' => __( 'You will no longer receive emails when someone sends you a friend request.', 'buddypress' ), |
| 3424 | ), |
| 3425 | ); |
| 3426 | |
| 3427 | $friends_request_accepted = array( |
| 3428 | 'description' => __( 'Recipient has had a friend request accepted by a member.', 'buddypress' ), |
| 3429 | 'unsubscribe' => array( |
| 3430 | 'meta_key' => 'notification_friends_friendship_accepted', |
| 3431 | 'message' => __( 'You will no longer receive emails when someone accepts your friendship request.', 'buddypress' ), |
| 3432 | ), |
| 3433 | ); |
| 3434 | |
| 3435 | $groups_details_updated = array( |
| 3436 | 'description' => __( "A group's details were updated.", 'buddypress' ), |
| 3437 | 'unsubscribe' => array( |
| 3438 | 'meta_key' => 'notification_groups_group_updated', |
| 3439 | 'message' => __( 'You will no longer receive emails when one of your groups is updated.', 'buddypress' ), |
| 3440 | ), |
| 3441 | ); |
| 3442 | |
| 3443 | $groups_details_updated = array( |
| 3444 | 'description' => __( "A group's details were updated.", 'buddypress' ), |
| 3445 | 'unsubscribe' => array( |
| 3446 | 'meta_key' => 'notification_groups_group_updated', |
| 3447 | 'message' => __( 'You will no longer receive emails when one of your groups is updated.', 'buddypress' ), |
| 3448 | ), |
| 3449 | ); |
| 3450 | |
| 3451 | $groups_invitation = array( |
| 3452 | 'description' => __( 'A member has sent a group invitation to the recipient.', 'buddypress' ), |
| 3453 | 'unsubscribe' => array( |
| 3454 | 'meta_key' => 'notification_groups_invite', |
| 3455 | 'message' => __( 'You will no longer receive emails when you are invited to join a group.', 'buddypress' ), |
| 3456 | ), |
| 3457 | ); |
| 3458 | |
| 3459 | $groups_member_promoted = array( |
| 3460 | 'description' => __( "Recipient's status within a group has changed.", 'buddypress' ), |
| 3461 | 'unsubscribe' => array( |
| 3462 | 'meta_key' => 'notification_groups_admin_promotion', |
| 3463 | 'message' => __( 'You will no longer receive emails when you have been promoted in a group.', 'buddypress' ), |
| 3464 | ), |
| 3465 | ); |
| 3466 | |
| 3467 | $groups_member_promoted = array( |
| 3468 | 'description' => __( "Recipient's status within a group has changed.", 'buddypress' ), |
| 3469 | 'unsubscribe' => array( |
| 3470 | 'meta_key' => 'notification_groups_admin_promotion', |
| 3471 | 'message' => __( 'You will no longer receive emails when you have been promoted in a group.', 'buddypress' ), |
| 3472 | ), |
| 3473 | ); |
| 3474 | |
| 3475 | $groups_membership_request = array( |
| 3476 | 'description' => __( 'A member has requested permission to join a group.', 'buddypress' ), |
| 3477 | 'unsubscribe' => array( |
| 3478 | 'meta_key' => 'notification_groups_membership_request', |
| 3479 | 'message' => __( 'You will no longer receive emails when someone requests to be a member of your group.', 'buddypress' ), |
| 3480 | ), |
| 3481 | ); |
| 3482 | |
| 3483 | $messages_unread = array( |
| 3484 | 'description' => __( 'Recipient has received a private message.', 'buddypress' ), |
| 3485 | 'unsubscribe' => array( |
| 3486 | 'meta_key' => 'notification_messages_new_message', |
| 3487 | 'message' => __( 'You will no longer receive emails when someone sends you a message.', 'buddypress' ), |
| 3488 | ), |
| 3489 | ); |
| 3490 | |
| 3491 | $settings_verify_email_change = array( |
| 3492 | 'description' => __( 'Recipient has changed their email address.', 'buddypress' ), |
| 3493 | 'unsubscribe' => false, |
| 3494 | ); |
| 3495 | |
| 3496 | $groups_membership_request_accepted = array( |
| 3497 | 'description' => __( 'Recipient had requested to join a group, which was accepted.', 'buddypress' ), |
| 3498 | 'unsubscribe' => array( |
| 3499 | 'meta_key' => 'notification_membership_request_completed', |
| 3500 | 'message' => __( 'You will no longer receive emails when your request to join a group has been accepted or denied.', 'buddypress' ), |
| 3501 | ), |
| 3502 | ); |
| 3503 | |
| 3504 | $groups_membership_request_rejected = array( |
| 3505 | 'description' => __( 'Recipient had requested to join a group, which was rejected.', 'buddypress' ), |
| 3506 | 'unsubscribe' => array( |
| 3507 | 'meta_key' => 'notification_membership_request_completed', |
| 3508 | 'message' => __( 'You will no longer receive emails when your request to join a group has been accepted or denied.', 'buddypress' ), |
| 3509 | ), |
| 3510 | ); |
| 3511 | |
| 3512 | $email_type_schema_array = array( |
| 3513 | 'activity-comment' => $activity_comment, |
| 3514 | 'activity-comment-author' => $activity_comment_author, |
| 3515 | 'activity-at-message' => $activity_at_message, |
| 3516 | 'groups-at-message' => $groups_at_message, |
| 3517 | 'core-user-registration' => $core_user_registration, |
| 3518 | 'core-user-registration-with-blog' => $core_user_registration_with_blog, |
| 3519 | 'friends-request' => $friends_request, |
| 3520 | 'friends-request-accepted' => $friends_request_accepted, |
| 3521 | 'groups-details-updated' => $groups_details_updated, |
| 3522 | 'groups-invitation' => $groups_invitation, |
| 3523 | 'groups-member-promoted' => $groups_member_promoted, |
| 3524 | 'groups-membership-request' => $groups_membership_request, |
| 3525 | 'messages-unread' => $messages_unread, |
| 3526 | 'settings-verify-email-change' => $settings_verify_email_change, |
| 3527 | 'groups-membership-request-accepted' => $groups_membership_request_accepted, |
| 3528 | 'groups-membership-request-rejected' => $groups_membership_request_rejected, |
| 3529 | ); |
| 3530 | |
| 3531 | /** |
| 3532 | * Email types schema. |
| 3533 | * |
| 3534 | * @since |
| 3535 | * |
| 3536 | * @param array $email_type_schema_array { |
| 3537 | * The array of email types and their schema. |
| 3538 | * |
| 3539 | * @type string $description The description of the action which causes this to trigger. |
| 3540 | * @type array $email_type_schema_array['unsubscribe'] { |
| 3541 | * The unsubscribe array. |
| 3542 | * Setting the value to false indicates that a user cannot unsubscribe from this type. |
| 3543 | * |
| 3544 | * @type string $email_type_schema_array['unsubscribe']['meta_key'] The meta_key used to toggle the email setting for this notification. |
| 3545 | * @type string $email_type_schema_array['unsubscribe']['message'] The message shown when the user has successfully unsubscribed. |
| 3546 | * } |
| 3547 | * } |
| 3548 | */ |
| 3549 | return apply_filters( 'bp_email_types_get_schema', $email_type_schema_array ); |
| 3551 | |
| 3552 | /** |
| 3553 | * Handles unsubscribe action to unsubscribe user from notification emails. |
| 3554 | * |
| 3555 | * @since |
| 3556 | * |
| 3557 | */ |
| 3558 | function bp_emails_unsubscribe_handler() { |
| 3559 | |
| 3560 | $request = $_GET; |
| 3561 | $admin_email = get_bloginfo( 'admin_email' ); |
| 3562 | $bp_email_schema = bp_email_types_get_schema(); |
| 3563 | |
| 3564 | $user_id = ( isset( $request['uid'] ) && ! empty( $request['uid'] ) ) ? (int) $request['uid'] : ''; |
| 3565 | $notification_type = ( isset( $request['nt'] ) && ! empty( $request['nt'] ) ) ? esc_attr( $request['nt'] ) : ''; |
| 3566 | $to_check = ( isset( $request['nn'] ) && ! empty( $request['nn'] ) ) ? $request['nn'] : ''; |
| 3567 | |
| 3568 | $check_args = array( |
| 3569 | 'uid' => $user_id, |
| 3570 | 'nt' => $notification_type, |
| 3571 | ); |
| 3572 | |
| 3573 | $check = bp_hash_array( $check_args ); |
| 3574 | |
| 3575 | $current_user = is_user_logged_in() ? wp_get_current_user() : false; |
| 3576 | |
| 3577 | if ( empty( $user_id ) || empty( $notification_type ) || empty( $to_check ) || ! array_key_exists( $notification_type, $bp_email_schema ) ) { |
| 3578 | |
| 3579 | $settings_link = site_url( 'wp-login.php' ); |
| 3580 | $result_message = __( 'Sorry, something has gone wrong.', 'buddypress' ); |
| 3581 | $how_to_unsubscribe_message = __( 'Please log in and go to your settings to unsubscribe from notifications.', 'buddypress' ); |
| 3582 | } elseif ( ! hash_equals( $check, $to_check ) ) { |
| 3583 | |
| 3584 | $settings_link = site_url( 'wp-login.php' ); |
| 3585 | $result_message = __( 'Sorry the security check failed.', 'buddypress' ); |
| 3586 | $how_to_unsubscribe_message = __( 'Please log in and go to your settings to unsubscribe from notifications.', 'buddypress' ); |
| 3587 | |
| 3588 | } elseif ( is_user_logged_in() && (int) $current_user->ID !== (int) $user_id ) { |
| 3589 | |
| 3590 | $settings_link = esc_url( sprintf( |
| 3591 | '%s%s/notifications/', |
| 3592 | bp_core_get_user_domain( (int) $current_user->ID ), |
| 3593 | function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings' |
| 3594 | ) ); |
| 3595 | |
| 3596 | $result_message = __( 'Something has gone wrong.', 'buddypress' ); |
| 3597 | $how_to_unsubscribe_message = __( 'Please go to your settings to unsubscribe from notifications.', 'buddypress' ); |
| 3598 | } else { |
| 3599 | |
| 3600 | $settings_link = esc_url( sprintf( |
| 3601 | '%s%s/notifications/', |
| 3602 | bp_core_get_user_domain( $user_id ), |
| 3603 | function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings' |
| 3604 | ) ); |
| 3605 | |
| 3606 | // unsubscribe them by setting the usermeta to 'no' |
| 3607 | $notification_meta_key = $bp_email_schema[ $notification_type ]['unsubscribe']['meta_key']; |
| 3608 | $is_unsubscribed = bp_update_user_meta( $user_id, $notification_meta_key, 'no' ); |
| 3609 | |
| 3610 | $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']; |
| 3611 | |
| 3612 | $how_to_unsubscribe_message = __( 'You can change this or any other email notification preferences in your email settings.', 'buddypress' ); |
| 3613 | } |
| 3614 | |
| 3615 | $message = sprintf( |
| 3616 | '%1$s <a href="%2$s">%3$s</a>', |
| 3617 | $result_message, |
| 3618 | $settings_link, |
| 3619 | $how_to_unsubscribe_message |
| 3620 | ); |
| 3621 | |
| 3622 | bp_core_add_message( $message ); |
| 3623 | bp_core_redirect( esc_url( remove_query_arg( array_keys( $request ) ) ) ); |
| 3624 | } |
| 3625 | |
| 3626 | /** |
| 3627 | * Creates unsubscribe link for notification emails. |
| 3628 | * |
| 3629 | * @since |
| 3630 | * |
| 3631 | * @param string $redirect_to The URL to which the unsubscribe query string is appended. |
| 3632 | * @param array $args { |
| 3633 | * |
| 3634 | * Used to build unsubscribe query string. |
| 3635 | * |
| 3636 | * @type string $notification_type Which notification type is being sent. |
| 3637 | * @type string $user_id The ID of the user to whom the notification is sent. |
| 3638 | * @type string $redirect_to Optional. The url to which the user will be redirected. Default is the activity directory. |
| 3639 | * |
| 3640 | * } |
| 3641 | * @return string The unsubscribe link. |
| 3642 | */ |
| 3643 | function bp_email_get_unsubscribe_link( $args = null ) { |
| 3644 | |
| 3645 | $bp_emails_type_schema = bp_email_types_get_schema(); |
| 3646 | |
| 3647 | if ( ! is_array( $args ) || ! isset( $args['notification_type'] ) || ! array_key_exists( $args['notification_type'], $bp_emails_type_schema ) ) { |
| 3648 | return site_url( 'wp-login.php' ); |
| 3649 | } |
| 3650 | |
| 3651 | $notification_type = $unsubscribe_args['nt'] = esc_attr( $args['notification_type'] ); |
| 3652 | $user_id = $unsubscribe_args['uid'] = (int) $args['user_id']; |
| 3653 | |
| 3654 | // If the activity type is not unsubscribable (ununsubscribable), return false. |
| 3655 | if ( empty( $bp_emails_type_schema[ $notification_type ]['unsubscribe'] ) ) { |
| 3656 | return false; |
| 3657 | } |
| 3658 | |
| 3659 | $redirect_to = ( isset( $args['redirect_to'] ) ) ? isset( $args['redirect_to'] ) : bp_get_activity_directory_permalink(); |
| 3660 | |
| 3661 | $check = bp_hash_array( $unsubscribe_args ); |
| 3662 | |
| 3663 | $unsubscribe_args['nn'] = $check; |
| 3664 | $unsubscribe_args['action'] = 'unsubscribe'; |
| 3665 | |
| 3666 | $unsubscribe_link = esc_url_raw( add_query_arg( $unsubscribe_args, $redirect_to ) ); |
| 3667 | |
| 3668 | /** |
| 3669 | * Filters the unsubscribe link. |
| 3670 | * |
| 3671 | * @since |
| 3672 | * |
| 3673 | * @param string $redirect_to URL to which the unsubscribe query string is appended. |
| 3674 | * @param array $args Used to build unsubscribe query string. |
| 3675 | */ |
| 3676 | return apply_filters( 'bp_email_get_unsubscribe_link', $unsubscribe_link, $redirect_to, $args ); |
| 3677 | } |
| 3678 | |