email-options.php
16.8 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class ACUI_Email_Options{
function __construct(){
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ), 10, 1 );
add_action( 'wp_ajax_acui_mail_options_remove_attachment', array( $this, 'ajax_remove_attachment' ) );
add_action( 'wp_ajax_acui_send_test_email', array( $this, 'ajax_send_test_email' ) );
add_action( 'acui_homepage_start', array( $this, 'maybe_fill_empty_options' ) );
add_action( 'acui_mail_options_save_settings', array( $this, 'save_mail_template' ), 10, 1 );
}
public static function admin_gui(){
$automatic_created_edited_wordpress_email = get_option( "acui_automatic_created_edited_wordpress_email" );
$automatic_wordpress_email = get_option( "acui_automatic_wordpress_email" );
$subject_mail = get_option( "acui_mail_subject" );
$body_mail = get_option( "acui_mail_body" );
$template_id = get_option( "acui_mail_template_id" );
$attachment_id = get_option( "acui_mail_attachment_id" );
$enable_email_templates = get_option( "acui_enable_email_templates" );
$disable_wp_editor = get_option( "acui_mail_disable_wp_editor" );
?>
<form method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8">
<h3><?php _e('WordPress automatic emails','import-users-from-csv-with-meta'); ?></h3>
<table class="optiontable form-table">
<tbody>
<tr valign="top">
<th scope="row"><?php _e( 'User created or edited', 'import-users-from-csv-with-meta' ); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e( 'User created or edited', 'import-users-from-csv-with-meta' ); ?></span>
</legend>
<label for="automatic_created_edited_wordpress_email">
<?php ACUIHTML()->select( array(
'options' => array( 'false' => __( "Deactivate WordPress automatic email when an user is created or edited", 'import-users-from-csv-with-meta' ), 'true' => __( 'Activate WordPress automatic email when an user is created or edited', 'import-users-from-csv-with-meta' ) ),
'name' => 'automatic_created_edited_wordpress_email',
'selected' => $automatic_created_edited_wordpress_email,
'show_option_all' => false,
'show_option_none' => false,
)); ?>
<span class="description"><? _e( "When you create or update an user, WordPress prepare and send automatic email, you can deactivate it here.", 'import-users-from-csv-with-meta' ); ?></span>
</label>
</fieldset>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Password changed', 'import-users-from-csv-with-meta' ); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e( 'Send automatic change password WordPress emails?', 'import-users-from-csv-with-meta' ); ?></span>
</legend>
<label for="automatic_wordpress_email">
<?php ACUIHTML()->select( array(
'options' => array( 'false' => __( "Deactivate WordPress automatic email when an user is updated or his password is changed", 'import-users-from-csv-with-meta' ), 'true' => __( 'Activate WordPress automatic email when an user is updated or his password is changed', 'import-users-from-csv-with-meta' ) ),
'name' => 'automatic_wordpress_email',
'selected' => $automatic_wordpress_email,
'show_option_all' => false,
'show_option_none' => false,
)); ?>
<span class="description"><? _e( "When you update an user or change his password, WordPress prepare and send automatic email, you can deactivate it here.", 'import-users-from-csv-with-meta' ); ?></span>
</label>
</fieldset>
</td>
</tr>
</tbody>
</table>
<h3><?php _e( 'Email templates from this plugin', 'import-users-from-csv-with-meta' ); ?></h3>
<table class="optiontable form-table">
<tbody>
<tr valign="top">
<th scope="row"><?php _e( 'Enable mail templates:', 'import-users-from-csv-with-meta' ); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e( 'Do you want to enable mail templates?', 'import-users-from-csv-with-meta' ); ?></span>
</legend>
<label for="enable_email_templates">
<?php ACUIHTML()->checkbox( array( 'name' => 'enable_email_templates', 'compare_value' => $enable_email_templates ) ); ?>
<span class="description"><? _e( "If you activate it, a new option in the menu will be created to store and manage mail templates, instead of using only the next one.", 'import-users-from-csv-with-meta' ); ?></span>
</label>
</fieldset>
</td>
</tr>
<tr valign="top">
<th scope="row">
<?php _e( 'Disable WP Editor:', 'import-users-from-csv-with-meta' ); ?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e( 'Do you want to disable WP Editor?', 'import-users-from-csv-with-meta' ); ?></span>
</legend>
<label for="disable_wp_editor">
<?php ACUIHTML()->checkbox( array( 'name' => 'disable_wp_editor', 'compare_value' => $disable_wp_editor ) ); ?>
<span class="description"><?php _e( 'If you want to use email with custom HTML and CSS tags, disable WP Editor', 'import-users-from-csv-with-meta' ); ?></span>
</label>
</fieldset>
</td>
</tr>
</tbody>
</table>
<?php if( $enable_email_templates && wp_count_posts( 'acui_email_template' )->publish > 0 ): ?>
<h3><?php _e( 'Load custom email from email templates', 'import-users-from-csv-with-meta' ); ?></h3>
<?php wp_dropdown_pages( array( 'id' => 'email_template_selected', 'post_type' => 'acui_email_template', 'selected' => $template_id ) ); ?>
<input id="load_email_template" class="button-primary" type="button" value="<?php _e( "Load subject, content and attachment from this email template", 'import-users-from-csv-with-meta' ); ?>"/>
<?php endif; ?>
<h3><?php _e( 'Customize the email that can be sent when importing users', 'import-users-from-csv-with-meta' ); ?></h3>
<p><?php _e( 'Mail subject:', 'import-users-from-csv-with-meta' ); ?><input name="subject_mail" size="100" value="<?php echo $subject_mail; ?>" id="title" autocomplete="off" type="text"></p>
<?php if( $disable_wp_editor ): ?>
<p><textarea name='body_mail' style="width:100%;" rows="20"><?php echo $body_mail; ?></textarea></p>
<?php else: ?>
<?php wp_editor( $body_mail, 'body_mail'); ?>
<?php endif; ?>
<input type="hidden" id="template_id" name="template_id" value="<?php echo $template_id; ?>"/>
<fieldset>
<div>
<label for="email_template_attachment_file"><?php _e( 'Attachment', 'import-users-from-csv-with-meta' )?></label><br>
<?php ACUIHTML()->text( array( 'type' => 'url', 'name' => 'email_template_attachment_file', 'value' => wp_get_attachment_url( $attachment_id ), 'class' => 'large-text', 'readonly' => true ) ); ?>
<input type="hidden" name="email_template_attachment_id" id="email_template_attachment_id" value="<?php echo $attachment_id ?>"/>
<button type="button" class="button" id="acui_email_option_upload_button"><?php _e( 'Upload file', 'import-users-from-csv-with-meta' )?></button>
<button type="button" class="button" id="acui_email_option_remove_upload_button"><?php _e( 'Remove file', 'import-users-from-csv-with-meta' )?></button>
</div>
</fieldset>
<br/>
<input class="button-primary" type="submit" value="<?php _e( 'Save email template and options', 'import-users-from-csv-with-meta'); ?>" id="save_mail_template_options"/>
<input class="button-primary" type="button" value="<?php _e( 'Send test email', 'import-users-from-csv-with-meta'); ?>" id="send_test_email" title="<?php _e( 'This test email will be sent to the current user', 'import-users-from-csv-with-meta'); ?>"/>
<?php _e( 'If you send a test email, no wildcards will be replaced becuase when you test, we have no data to replace.', 'import-users-from-csv-with-meta' ); ?>
<?php wp_nonce_field( 'codection-security', 'security' ); ?>
<?php do_action( 'acui_email_options_after_editor' ); ?>
</form>
<?php
}
function maybe_fill_empty_options(){
if( get_option( "acui_mail_body" ) == "" )
update_option( "acui_mail_body", __( 'Welcome,', 'import-users-from-csv-with-meta' ) . '<br/>' . __( 'Your data to login in this site is:', 'import-users-from-csv-with-meta' ) . '<br/><ul><li>' . __( 'URL to login', 'import-users-from-csv-with-meta' ) . ': **loginurl**</li><li>' . __( 'Username', 'import-users-from-csv-with-meta' ) . ' = **username**</li><li>' . __( 'Password', 'import-users-from-csv-with-meta' ) . ' = **password**</li></ul>' );
if( get_option( "acui_mail_subject" ) == "" )
update_option( "acui_mail_subject", __('Welcome to','import-users-from-csv-with-meta') . ' ' . get_bloginfo("name") );
}
function save_mail_template( $form_data ){
if ( !isset( $form_data['security'] ) || !wp_verify_nonce( $form_data['security'], 'codection-security' ) ) {
wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
}
add_filter( 'wp_kses_allowed_html', array( $this, 'allow_more_post_tags' ), 10, 2 );
$automatic_wordpress_email = sanitize_text_field( $form_data["automatic_wordpress_email"] );
$automatic_created_edited_wordpress_email = sanitize_text_field( $form_data["automatic_created_edited_wordpress_email"] );
$subject_mail = sanitize_text_field( stripslashes_deep( $form_data["subject_mail"] ) );
$body_mail = wp_kses_post( stripslashes( $form_data["body_mail"] ) );
$template_id = intval( $form_data["template_id"] );
$email_template_attachment_id = intval( $form_data["email_template_attachment_id"] );
$disable_wp_editor = isset( $form_data['disable_wp_editor'] ) && $form_data['disable_wp_editor'] == '1';
remove_filter( 'wp_kses_allowed_html', array( $this, 'allow_more_post_tags' ), 10, 2 );
update_option( "acui_automatic_wordpress_email", $automatic_wordpress_email );
update_option( "acui_automatic_created_edited_wordpress_email", $automatic_created_edited_wordpress_email );
update_option( "acui_mail_subject", $subject_mail );
update_option( "acui_mail_body", $body_mail );
update_option( "acui_mail_template_id", $template_id );
update_option( "acui_mail_attachment_id", $email_template_attachment_id );
update_option( "acui_mail_disable_wp_editor", $disable_wp_editor );
$template_id = absint( $form_data["template_id"] );
if( !empty( $template_id ) ){
wp_update_post( array(
'ID' => $template_id,
'post_title' => $subject_mail,
'post_content' => $body_mail,
) );
update_post_meta( $template_id, 'email_template_attachment_id', $email_template_attachment_id );
}
?>
<div class="updated">
<p><?php _e( 'Mail template and options updated correctly', 'import-users-from-csv-with-meta' )?></p>
</div>
<?php
}
static function send_email( $user_object, $positions = array(), $headers = array(), $data = array(), $created = false, $password = '' ){
$user_id = $user_object->ID;
$user_email = $user_object->user_email;
$key = get_password_reset_key( $user_object );
$body = apply_filters( 'acui_import_email_body_source', get_option( "acui_mail_body" ), $headers, $data, $created, $user_id );
$subject = apply_filters( 'acui_import_email_subject_source', get_option( "acui_mail_subject" ), $headers, $data, $created, $user_id );
$body = self::apply_wildcards( $body, $user_object, $created, $positions, $headers, $data, $password, $key );
$subject = self::apply_wildcards( $subject, $user_object, $created, $positions, $headers, $data, $password, $key );
$body = apply_filters( 'acui_import_email_body_before_wpautop', $body, $headers, $data, $created, $user_id );
$attachments = array();
$attachment_id = get_option( 'acui_mail_attachment_id' );
if( !empty( $attachment_id ) )
$attachments[] = get_attached_file( $attachment_id );
$email_to = apply_filters( 'acui_import_email_to', $user_email, $headers, $data, $created, $user_id );
$subject = apply_filters( 'acui_import_email_subject', $subject, $headers, $data, $created, $user_id );
$body = apply_filters( 'acui_import_email_body', wpautop( $body ), $headers, $data, $created, $user_id );
$headers_mail = apply_filters( 'acui_import_email_headers', array( 'Content-Type: text/html; charset=UTF-8' ), $headers, $data, $created, $user_id );
$attachments = apply_filters( 'acui_import_email_attachments', $attachments, $headers, $data, $created, $user_id );
wp_mail( $email_to, $subject, $body, $headers_mail, $attachments );
}
static function apply_wildcards( $string, $user_object, $created, $positions, $headers, $data, $password, $key ){
$acui_helper = new ACUI_Helper();
$wp_users_fields = $acui_helper->get_wp_users_fields();
$user_login = $user_object->user_login;
$user_email = $user_object->user_email;
$string = str_replace( "**username**", $user_login, $string );
$string = str_replace( "**password**", $password, $string );
$string = str_replace( "**email**", $user_email, $string );
$string = str_replace( "**loginurl**", wp_login_url(), $string );
$string = str_replace( "**lostpasswordurl**", wp_lostpassword_url(), $string );
if( !is_wp_error( $key ) ){
if( is_multisite() ){
$sites = get_blogs_of_user( $user_object->ID );
if( count( $sites ) == 1 ){
$passwordreseturl = get_site_url( array_keys( $sites )[0], 'wp-login.php?action=rp&key=' . $key . '&login=' . rawurlencode( $user_login ), 'login' );
}
else{
$passwordreseturl = network_site_url( 'wp-login.php?action=rp&key=' . $key . '&login=' . rawurlencode( $user_login ), 'login' );
}
}
else{
$passwordreseturl = site_url( 'wp-login.php?action=rp&key=' . $key . '&login=' . rawurlencode( $user_login ), 'login' );
}
$passwordreseturl = apply_filters( 'acui_email_passwordreseturl', $passwordreseturl );
$string = str_replace( "**passwordreseturl**", $passwordreseturl, $string );
$passwordreseturllink = wp_sprintf( '<a href="%s">%s</a>', $passwordreseturl, __( 'Password reset link', 'import-users-from-csv-with-meta' ) );
$string = str_replace( "**passwordreseturllink**", $passwordreseturllink, $string );
}
if( empty( $password ) && !$created ){
$password = __( 'Password has not been changed', 'import-users-from-csv-with-meta' );
}
foreach ( $wp_users_fields as $wp_users_field ) {
if( $positions[ $wp_users_field ] != false && $wp_users_field != "password" ){
$string = str_replace( "**" . $wp_users_field . "**", $data[ $positions[ $wp_users_field ] ] , $string );
}
}
for( $i = 0 ; $i < count( $headers ); $i++ ) {
$to_replace = "**" . $headers[ $i ] . "**";
if( strpos( $string, $to_replace ) === false )
continue;
$data[ $i ] = ( is_array( $data[ $i ] ) ) ? implode( "-", $data[ $i ] ) : $data[ $i ];
$string = str_replace( $to_replace, $data[ $i ] , $string );
}
$string = apply_filters( 'acui_email_apply_wildcards', $string, array( 'key' => $key, 'user_login' => $user_login ) );
return $string;
}
function load_scripts( $hook ) {
global $typenow;
if( $typenow == 'acui_email_template' || $hook == 'tools_page_acui' ) {
wp_enqueue_media();
wp_register_script( 'acui-email-template-attachment-admin', esc_url( plugins_url( 'assets/email-template-attachment-admin.js', dirname( __FILE__ ) ) ), array( 'jquery' ) );
wp_localize_script( 'acui-email-template-attachment-admin', 'email_template_attachment_admin',
array(
'title' => __( 'Choose or upload file', 'import-users-from-csv-with-meta' ),
'button' => __( 'Use this file', 'import-users-from-csv-with-meta' ),
'security' => wp_create_nonce( "codection-security" )
)
);
wp_enqueue_script( 'acui-email-template-attachment-admin' );
}
if( $hook == 'tools_page_acui' ){
wp_register_script( 'acui-email-options', esc_url( plugins_url( 'assets/email-options.js', dirname( __FILE__ ) ) ), array( 'jquery' ) );
wp_localize_script( 'acui-email-options', 'email_options',
array(
'security' => wp_create_nonce( "codection-security" ),
'success_message' => __( 'Test email sent', 'import-users-from-csv-with-meta' ),
)
);
wp_enqueue_script( 'acui-email-options' );
}
}
function ajax_remove_attachment(){
check_ajax_referer( 'codection-security', 'security' );
update_option( "acui_mail_attachment_id", "" );
}
function ajax_send_test_email(){
check_ajax_referer( 'codection-security', 'security' );
self::send_email( wp_get_current_user() );
}
function allow_more_post_tags( $tags, $context ) {
if ( 'post' === $context ) {
$tags['style'] = array();
}
return $tags;
}
}
new ACUI_Email_Options();