forked from atwellpub/resend-welcome-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresend-welcome-email.php
195 lines (163 loc) · 4.58 KB
/
resend-welcome-email.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
<?php
/*
Plugin Name: Resend Welcome Email
Plugin URI: http://www.twitter.com/atwellpub
Description: Quickly send a new welcome email and password reset link for a user through the user's profile edit area.
Version: 1.1.9
Author: Hudson Atwell
Author URI: https://codeable.io/developers/hudson-atwell/?ref=99TG1
Text Domain: resend-welcome-email
Domain Path: /languages
*/
/**
* Security check.
* Prevent direct access to the file.
*
* @since 1.0.3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Resend_Welcome_Email
*
* @since 1.0.0
*/
if ( ! class_exists( 'Resend_Welcome_Email' ) ) {
/**
* Class Resend_Welcome_Email
*/
class Resend_Welcome_Email {
/**
* Constructor.
*/
public function __construct() {
/* Check user permission */
if ( ! current_user_can( 'edit_users' ) ) {
return;
}
/* Define constants */
self::define_constants();
add_filter( 'user_row_actions', array( __CLASS__, 'filter_user_row_actions' ), 10, 2 );
add_filter( 'personal_options', array( __CLASS__, 'personal_options' ), 10, 2 );
/* Adds admin listeners for processing actions */
self::add_admin_listeners();
}
/**
* Defines constants.
*/
public static function define_constants() {
define( 'RESEND_WELCOME_EMAIL_CURRENT_VERSION', '1.1.9' );
define( 'RESEND_WELCOME_EMAIL_FILE', __FILE__ );
define( 'RESEND_WELCOME_EMAIL_URLPATH', plugins_url( ' ', __FILE__ ) );
define( 'RESEND_WELCOME_EMAIL_PATH', WP_PLUGIN_DIR . '/' . plugin_basename( dirname( __FILE__ ) ) . '/' );
}
/**
* Discovers which tests to run and runs them.
*
* @param array $actions
* @param \WP_User $user
*
* @return array
*/
public static function filter_user_row_actions( array $actions, WP_User $user ) {
if ( ! ( $link = self::send_welcome_email_url( $user ) ) ) {
return $actions;
}
$actions['send_welcome_email'] = '<a href="' . $link . '">' . esc_html__( 'Resend Welcome Email', 'resend-welcome-email' ) . '</a>';
return $actions;
}
/**
* Add link in user profile.
*
* @param \WP_User $user
*/
public static function personal_options( WP_User $user ) {
if ( ! ( $link = self::send_welcome_email_url( $user ) ) ) {
return;
}
?>
<tr>
<th scope="row"><?php esc_html_e( 'Welcome Email', 'resend-welcome-email' ); ?></th>
<td>
<a href="<?php echo $link; ?>"><?php esc_html_e( 'Send New', 'resend-welcome-email' ); ?></a>
</td>
</tr>
<?php
}
/**
* Listens for email send commands and fires them.
*/
public static function add_admin_listeners() {
if ( ! isset( $_GET['action'] ) ||
( 'resend_welcome_email' !== $_GET['action'] )
) {
return;
}
/* Resend welcome email */
self::resend_welcome_email();
/* Register success notice */
add_action( 'admin_notices', array( __CLASS__, 'define_notice' ) );
add_action( 'network_admin_notices', array( __CLASS__, 'define_notice' ) );
}
/**
* Register admin notice that email has been sent.
*/
public static function define_notice() {
?>
<div class="updated">
<p><?php esc_html_e( 'Welcome email sent!', 'resend-welcome-email' ); ?></p>
</div>
<?php
}
/**
* Helper function. Returns the switch to or switch back URL for a given user.
*
* @param WP_User $user The user to be switched to.
*
* @return string|bool The required URL, or false if there's no old user or the user doesn't have the required capability.
*/
public static function send_welcome_email_url( WP_User $user ) {
return esc_url( wp_nonce_url( add_query_arg( array(
'action' => 'resend_welcome_email',
'user_id' => $user->ID,
), '' ),
"send_welcome_email_{$user->ID}" )
);
}
/**
* Resends the welcome email.
*
* @return bool|WP_User WP_User object on success, false on failure.
*/
public static function resend_welcome_email() {
if ( ! isset( $_GET['user_id'] ) ) {
return false;
}
$user_id = $_GET['user_id'];
if ( ! $user = get_userdata( $user_id ) ) {
return false;
}
wp_new_user_notification( $user_id, null, 'both' );
}
/**
* Load the text domain for translation.
*
* since: 1.0.3
*/
}
/**
* Load Resend_Welcome_Email class in init.
*/
function Load_Resend_Welcome_Email() {
new Resend_Welcome_Email();
}
add_action( 'admin_init', 'Load_Resend_Welcome_Email', 10 );
/**
* Load text domain
*/
add_action( 'plugins_loaded', 'rwe_load_textdomain' );
function rwe_load_textdomain() {
load_plugin_textdomain( 'resend-welcome-email' , FALSE, basename( dirname( __FILE__ ) ) . '/languages/');
}
}