-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
482 lines (409 loc) · 13.9 KB
/
plugin.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
/**
* @package Silverback Email Services
* @version 1.1
*/
/**
* Plugin Name: Silverback Email Services
* Plugin URI: https://github.com/silverbackstudio/wp-email
* Description: Send Wordpress emails through Email Services API with templates
* Author: Silverback Studio
* Version: 2.2.1
* Author URI: http://www.silverbackstudio.it/
* Text Domain: svbk-email
*/
use Svbk\WP\Email;
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
function svbk_email_init() {
load_plugin_textdomain( 'svbk-email', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
Email\Wordpress::trackMessages();
}
add_action( 'muplugins_loaded', 'svbk_email_init' );
$providerInstance = svbk_email_get_provider();
if ( $providerInstance && !function_exists( 'wp_mail' ) ) {
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
$options = get_option( 'svbk_email_options' );
$wp_email = new Email\Wordpress();
$message = $wp_email->message( $to, $subject, $message, $headers = '', $attachments = array() );
$email_id = Email\Wordpress::get_last_email_id();
$providerInstance = svbk_email_get_provider();
$template_key = 'template_' . $providerInstance::SERVICE_NAME . '_' . $email_id;
$template = empty( $options[$template_key] ) ? false : $options[$template_key];
$template = apply_filters( 'svbk_email_template', $template );
$message_id = false;
try {
if ( $template ) {
$message->subject = '';
$message_id = $providerInstance->sendTemplate( $template, $message, Email\Wordpress::$last_email_data ) ;
} else {
$message_id = $providerInstance->send( $message );
}
} catch( Exception $e ) {
do_action(
'log', 'error', 'SVBK wp-email send error',
array(
'error' => $e->getMessage(),
)
);
$message_id = false;
}
return $message_id ? true : false;
}
}
function svbk_email_get_templates(){
$result = wp_cache_get( 'svbk_email_templates' );
if ( false === $result ) {
$provider = svbk_email_get_provider();
try {
$result = $provider->getTemplates();
wp_cache_set( 'svbk_email_templates', $result, null, 5 * MINUTE_IN_SECONDS );
} catch( \Exception $e) {
return false;
}
}
return $result;
}
/**
* top level menu
*/
function svbk_email_options_page() {
// add top level menu page
add_submenu_page(
//parent_slug
'options-general.php',
//page_title
'Email Settings',
//menu title
'Email Settings',
//capability
'manage_options',
//menu page slug
'svbk-email',
//function
'svbk_email_options_page_html'
);
}
/**
* register our wporg_options_page to the admin_menu action hook
*/
add_action( 'admin_menu', 'svbk_email_options_page' );
/**
* @internal never define functions inside callbacks.
* these functions could be run multiple times; this would result in a fatal error.
*/
/**
* Return selected provider
*
* @return Email\Transactional\ServiceInterface|null
*/
function svbk_email_get_provider() {
$options = get_option( 'svbk_email_options' );
$provider = empty($options['provider']) ? '' : $options['provider'];
$providerInstance = null;
try {
switch( $provider ){
default:
$providerClass = null;
break;
case Email\Transactional\SendInBlue::SERVICE_NAME:
$providerInstance = new Email\Transactional\SendInBlue( !empty($options['provider_apikey']) ? $options['provider_apikey'] : null );
break;
case Email\Transactional\Mandrill::SERVICE_NAME:
$providerInstance = new Email\Transactional\Mandrill( $options['provider_apikey'] );
break;
}
} catch ( Exception $e ) {
$providerInstance = null;
add_action( 'admin_notices', 'svbk_email_admin_notice__invalid_apikey' );
}
if ( !$providerInstance ) {
add_action( 'admin_notices', 'svbk_email_admin_notice__core_email' );
}
return $providerInstance;
}
function svbk_email_admin_notice__invalid_apikey() {
?>
<div class="notice notice-warning">
<p>
<?php _e( 'No email keys configured or invalid configuration. No email will be sent.', 'svbk-email' ); ?>
<a href="<?php esc_attr(menu_page_url('svbk-email')); ?>" ><?php _e('Settings', 'svbk-email') ?></a>
</p>
</div>
<?php
}
function svbk_email_admin_notice__core_email() {
?>
<div class="notice notice-warning is-dismissible">
<p>
<?php _e( 'There is no email provider configured, using server "mail()" function. Please check if email are working.', 'svbk-email' ); ?>
<a href="<?php esc_attr(menu_page_url('svbk-email')); ?>" ><?php _e('Settings', 'svbk-email') ?></a>
</p>
</div>
<?php
}
/**
* custom option and settings
*/
function svbk_email_settings_init() {
$options = get_option( 'svbk_email_options' );
// register a new setting for "svbk-email" page
register_setting(
//option group
'svbk-email',
//option name
'svbk_email_options'
);
// register a new section in the "svbk-email" page
add_settings_section(
// id
'svbk_email_section_general',
//title
__( 'General Settings', 'svbk-email' ),
//callback
'svbk_email_section_general_cb',
//page
'svbk-email'
);
add_settings_field(
//id. As of WP 4.6 this value is used only internally, use $args' label_for to populate the id inside the callback
'svbk_email_from',
//title
__( 'From', 'svbk-email' ),
//callback
'svbk_email_field_from_cb',
//page
'svbk-email',
//section
'svbk_email_section_general',
[
'label_for' => 'from',
'class' => 'svbk_email_row',
]
);
add_settings_field(
//id. As of WP 4.6 this value is used only internally, use $args' label_for to populate the id inside the callback
'svbk_email_provider',
//title
__( 'Email Provider', 'svbk-email' ),
//callback
'svbk_email_field_provider_cb',
//page
'svbk-email',
//section
'svbk_email_section_general',
[
'label_for' => 'provider',
'class' => 'svbk_email_row',
]
);
$trackedMessages = Email\Wordpress::trackedMessages();
$provider = svbk_email_get_provider();
add_settings_field(
//id. As of WP 4.6 this value is used only internally, use $args' label_for to populate the id inside the callback
'svbk_email_provider_apikey',
//title
__( 'Provider Api Key', 'svbk-email' ),
//callback
'svbk_email_field_provider_apikey_cb',
//page
'svbk-email',
//section
'svbk_email_section_general',
[
'label_for' => 'provider_apikey',
'class' => 'svbk_email_row',
]
);
if ( $provider ) {
// register a new section in the "svbk-email" page
add_settings_section(
// id
'svbk_email_section_templates',
//title
__( 'Templates', 'svbk-email' ),
//callback
'svbk_email_section_templates_cb',
//page
'svbk-email'
);
add_settings_field(
//id. As of WP 4.6 this value is used only internally, use $args' label_for to populate the id inside the callback
'svbk_email_default_template',
//title
__( 'Default Template', 'svbk-email' ),
//callback
'svbk_email_field_select_template_cb',
//page
'svbk-email',
//section
'svbk_email_section_templates',
[
'label_for' => 'default_' . $provider::SERVICE_NAME . '_template',
'class' => 'svbk_email_row',
]
);
foreach( $trackedMessages as $trackedMessage => $trackedMessageLabel ) {
add_settings_field(
//id. As of WP 4.6 this value is used only internally, use $args' label_for to populate the id inside the callback
'svbk_email_template_' . $trackedMessage,
//title
$trackedMessageLabel,
//callback
'svbk_email_field_select_template_cb',
//page
'svbk-email',
//section
'svbk_email_section_templates',
[
'label_for' => 'template_' . $provider::SERVICE_NAME . '_'. $trackedMessage,
'class' => 'svbk_email_row',
]
);
}
}
}
/**
* register our wporg_settings_init to the admin_init action hook
*/
add_action( 'admin_init', 'svbk_email_settings_init' );
/**
* custom option and settings:
* callback functions
*/
function svbk_email_section_general_cb( $args ) { ?>
<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Choose wich provider you want to use to send emails', 'svbk-email' ); ?></p>
<?php
}
function svbk_email_section_templates_cb( $args ) { ?>
<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Choose which template should be used', 'svbk-email' ); ?></p>
<?php
}
function svbk_email_field_provider_cb( $args ) {
// get the value of the setting we've registered with register_setting()
$options = get_option( 'svbk_email_options' );
// output the field
?>
<select id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="svbk_email_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
>
<option value="" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], '', false ) ) : ( '' ); ?>>
<?php esc_html_e( '-- Core --', 'svbk-email' ); ?>
</option>
<option value="sendinblue" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'sendinblue', false ) ) : ( '' ); ?>>
<?php esc_html_e( 'Sendinblue', 'svbk-email' ); ?>
</option>
<option value="mandrill" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'mandrill', false ) ) : ( '' ); ?>>
<?php esc_html_e( 'Mandrill', 'svbk-email' ); ?>
</option>
</select>
<?php
}
function svbk_email_field_provider_apikey_cb( $args ) {
// get the value of the setting we've registered with register_setting()
$options = get_option( 'svbk_email_options' );
$provider = svbk_email_get_provider();
$value = !empty($options[ $args['label_for'] ]) ? $options[ $args['label_for'] ] : '';
// output the field
?>
<input id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="svbk_email_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
type="password"
value="<?php esc_attr_e($value); ?>"
size="100"
/>
<p><?php _e('Warning: This api key will be used if no system-wide api-key is defined') ?></p>
<?php
}
function svbk_email_field_from_cb( $args ) {
// get the value of the setting we've registered with register_setting()
$options = get_option( 'svbk_email_options' );
$value = !empty($options[ $args['label_for'] ]) ? $options[ $args['label_for'] ] : '';
$value_name = !empty($options[ $args['label_for'] . '_name' ]) ? $options[ $args['label_for'] . '_name' ] : '';
// output the field
?>
<input id="<?php echo esc_attr( $args['label_for'] ); ?>_name"
name="svbk_email_options[<?php echo esc_attr( $args['label_for'] ); ?>_name]"
placeholder="<?php _e('Enter from name..', 'svbk-email') ?>"
type="text"
value="<?php esc_attr_e($value_name); ?>"
/>
<input id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="svbk_email_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
placeholder="<?php _e('Enter from email..', 'svbk-email') ?>"
type="text"
value="<?php esc_attr_e($value); ?>"
/>
<?php
}
function svbk_email_field_select_template_cb( $args ) {
// get the value of the setting we've registered with register_setting()
$options = get_option( 'svbk_email_options' );
// output the field
$templates = svbk_email_get_templates();
if ($templates === false){
echo '<p>Invalid API key</p>';
return;
}
?>
<select id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="svbk_email_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
>
<option value="" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], '', false ) ) : ( '' ); ?>>
<?php esc_html_e( '-- No Template --', 'svbk-email' ); ?>
</option>
<?php foreach( $templates as $template_id => $template_name ) : ?>
<option value="<?php esc_attr_e( $template_id ) ?>" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], $template_id, false ) ) : ( '' ); ?>>
<?php echo esc_html( $template_name ); ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
function svbk_email_options_page_html() {
// check user capabilities
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// add error/update messages
// check if the user have submitted the settings
// wordpress will add the "settings-updated" $_GET parameter to the url
if ( isset( $_GET['settings-updated'] ) ) {
// add settings saved message with the class of "updated"
add_settings_error( 'svbk_email_messages', 'svbk_email_message', __( 'Settings Saved', 'svbk-email' ), 'updated' );
}
// show error/update messages
settings_errors( 'svbk_email_messages' ); ?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
// output security fields for the registered setting "wporg"
settings_fields( 'svbk-email' );
// output setting sections and their fields
// (sections are registered for "wporg", each field is registered to a specific section)
do_settings_sections( 'svbk-email' );
// output save settings button
submit_button( 'Save Settings' );
?>
</form>
</div>
<?php
}
function svbk_email_from_address( $from = '' ){
$options = get_option( 'svbk_email_options' );
if ( empty( $options['from'] ) ) {
return $from;
}
return $options['from'];
}
add_filter('wp_mail_from', 'svbk_email_from_address');
function svbk_email_from_name( $from = '' ){
$options = get_option( 'svbk_email_options' );
if ( empty( $options['from_name'] ) ) {
return $from;
}
return $options['from_name'];
}
add_filter('wp_mail_from_name', 'svbk_email_from_name');