1 | <?php |
||
2 | /** |
||
3 | * User_Approval Namespace. |
||
4 | * |
||
5 | * @package user-approval |
||
6 | */ |
||
7 | |||
8 | namespace User_Approval\Registration; |
||
9 | |||
10 | use function User_Approval\filter_input; |
||
11 | use function User_Approval\get_default_user_role; |
||
12 | use function User_Approval\get_role_names; |
||
13 | use function User_Approval\is_default_role_user; |
||
14 | |||
15 | /** |
||
16 | * Hook up all the filters and actions. |
||
17 | */ |
||
18 | function bootstrap() { |
||
19 | |||
20 | // Remove default user emails/notifications on user registration. |
||
21 | 1 | remove_action( 'register_new_user', 'wp_send_new_user_notifications' ); |
|
22 | |||
23 | 1 | add_action( 'register_new_user', __NAMESPACE__ . '\\send_notification_to_allowed_users' ); |
|
24 | |||
25 | 1 | add_filter( 'wp_new_user_notification_email_admin', __NAMESPACE__ . '\\update_new_user_admin_email', 50, 3 ); |
|
26 | |||
27 | // Add a message after registration about account is being sent for verification. |
||
28 | 1 | add_filter( 'wp_login_errors', __NAMESPACE__ . '\\update_registered_user_message' ); |
|
29 | 1 | } |
|
30 | |||
31 | /** |
||
32 | * Send user emails/notification on user registration for pre-approved users. |
||
33 | * |
||
34 | * @param int $user_id User id. |
||
35 | */ |
||
36 | function send_notification_to_allowed_users( $user_id ) { |
||
37 | 1 | $user = get_userdata( $user_id ); |
|
38 | |||
39 | if ( is_default_role_user( $user ) ) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
40 | 1 | wp_send_new_user_notifications( $user_id, 'admin' ); |
|
41 | 1 | return; |
|
42 | } |
||
43 | 1 | ||
44 | 1 | wp_send_new_user_notifications( $user_id ); |
|
45 | } |
||
46 | |||
47 | 1 | /** |
|
48 | 1 | * Send a notification/email to admin when user register. |
|
49 | * |
||
50 | * @param array $email_data { |
||
51 | * Used to build wp_mail(). |
||
52 | * |
||
53 | * @type string $to The intended recipient - New user email address. |
||
54 | * @type string $subject The subject of the email. |
||
55 | * @type string $message The body of the email. |
||
56 | * @type string $headers The headers of the email. |
||
57 | * } |
||
58 | * @param \WP_User $user User object for new user. |
||
59 | * @param string $blogname The site title. |
||
60 | * |
||
61 | * @return array Updated email data for wp_mail. |
||
62 | */ |
||
63 | function update_new_user_admin_email( $email_data, $user, $blogname ) { |
||
64 | |||
65 | if ( ! is_default_role_user( $user ) ) { |
||
66 | return $email_data; |
||
67 | } |
||
68 | 1 | ||
69 | 1 | $user_roles = get_role_names(); |
|
70 | $user_role = $user_roles[ get_default_user_role() ]; |
||
71 | |||
72 | 1 | $message = sprintf( |
|
73 | 1 | /* translators: 1: User role label, 2: Site title. */ |
|
74 | esc_html__( 'New %1$s registration on your site %2$s:', 'user-approval' ), |
||
75 | 1 | $user_role, |
|
76 | $blogname |
||
77 | 1 | ); |
|
78 | 1 | ||
79 | 1 | $message .= "\r\n\r\n"; |
|
80 | |||
81 | $message .= sprintf( |
||
82 | 1 | /* translators: %s: User login. */ |
|
83 | esc_html__( 'Username: %s', 'user-approval' ), |
||
84 | 1 | $user->user_login |
|
85 | ); |
||
86 | 1 | ||
87 | 1 | $message .= "\r\n\r\n"; |
|
88 | |||
89 | $message .= sprintf( |
||
90 | 1 | /* translators: %s: User email address. */ |
|
91 | esc_html__( 'Email: %s', 'user-approval' ), |
||
92 | 1 | $user->user_email |
|
93 | ); |
||
94 | 1 | ||
95 | 1 | $message .= "\r\n"; |
|
96 | |||
97 | /* translators: New user registration notification email subject. %s: Site title. */ |
||
98 | 1 | $email_data['subject'] = esc_html__( '[%s] New Contributor Registration', 'user-approval' ); |
|
99 | $email_data['message'] = $message; |
||
100 | |||
101 | 1 | return apply_filters( 'user_approval_new_user_admin_email_data', $email_data ); |
|
102 | 1 | } |
|
103 | |||
104 | 1 | /** |
|
105 | * Add a message after registration about account is being sent for verification. |
||
106 | * |
||
107 | * @param \WP_Error $errors WP Error object. |
||
108 | * |
||
109 | * @return \WP_Error |
||
110 | */ |
||
111 | function update_registered_user_message( $errors ) { |
||
112 | $checkemail = filter_input( INPUT_GET, 'checkemail', FILTER_SANITIZE_STRING ); |
||
113 | |||
114 | if ( 'registered' !== $checkemail ) { |
||
115 | 1 | return $errors; |
|
116 | } |
||
117 | 1 | ||
118 | 1 | $errors->remove( 'registered' ); |
|
119 | |||
120 | $message = sprintf( |
||
121 | 1 | esc_html__( 'An email has been sent to the site administrator for account verification.', 'user-approval' ) |
|
122 | ); |
||
123 | 1 | $message .= ' '; |
|
124 | 1 | $message .= sprintf( |
|
125 | esc_html__( 'You will receive an email once your account is reviewed. Thanks for your patience.', 'user-approval' ) |
||
126 | 1 | ); |
|
127 | 1 | ||
128 | 1 | $message = apply_filters( 'user_approval_registered_user_message', $message ); |
|
129 | |||
130 | $errors->add( 'registered', $message, 'message' ); |
||
131 | 1 | ||
132 | return $errors; |
||
133 | } |
||
134 |