-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-custom-api-emails.php
549 lines (482 loc) · 15.7 KB
/
wp-custom-api-emails.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
/**
* Plugin Name: Custom API Emails Manager
* Plugin URI: https://yourwebsite.com/plugins/custom-api-emails
* Description: Manage custom email templates for registration, password reset, and OTP verification.
* Version: 1.0.0
* Requires at least: 5.0
* Requires PHP: 7.2
* Author: Your Name
* Author URI: https://yourwebsite.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: custom-api-emails
*
* @package CustomAPIEmails
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main plugin class
*
* @since 1.0.0
*/
class Custom_API_Emails {
/**
* Instance of this class
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* Plugin options
*
* @since 1.0.0
* @var array
*/
private $options;
/**
* Get singleton instance
*
* @since 1.0.0
* @return Custom_API_Emails
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*
* @since 1.0.0
*/
private function __construct() {
$this->options = get_option( 'custom_email_settings', array() );
$this->init_hooks();
}
/**
* Initialize plugin components
*
* @since 1.0.0
* @return void
*/
private function init_components() {
$this->otp_handler = new Custom_API_Emails_OTP( $this );
}
/**
* Get available Mosaico templates
*
* @since 1.0.0
* @return array Array of templates with id => title
*/
private function get_mosaico_templates() {
try {
$templates = \Civi\Api4\MosaicoTemplate::get(TRUE)
->addSelect('id', 'title')
->setLimit(25)
->execute();
$template_options = array();
foreach ($templates as $template) {
$template_options[$template['id']] = $template['title'];
}
return $template_options;
} catch (Exception $e) {
return array();
}
}
/**
* Initialize WordPress hooks
*
* @since 1.0.0
* @return void
*/
private function init_hooks() {
// Admin hooks
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'wp_ajax_get_mosaico_preview', array( $this, 'ajax_get_mosaico_preview' ) );
// Email customization hooks
add_filter( 'bdpwr_code_email_subject', array( $this, 'custom_reset_subject' ), 10, 1 );
add_filter( 'bdpwr_code_email_text', array( $this, 'custom_reset_content' ), 10, 4 );
add_filter( 'custom_otp_email_subject', array( $this, 'custom_otp_subject' ), 10, 1 );
add_filter( 'custom_otp_email_message', array( $this, 'custom_otp_content' ), 10, 2 );
add_action( 'wp_mail_from', array( $this, 'custom_email_from' ) );
add_action( 'wp_mail_from_name', array( $this, 'custom_email_from_name' ) );
add_filter( 'bdpwr_selection_string' , function( $string ) {
return '0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM';
}, 10 , 4 );
}
/**
* Add admin menu
*
* @since 1.0.0
* @return void
*/
public function add_admin_menu() {
add_menu_page(
__( 'Email Templates', 'custom-api-emails' ),
__( 'Email Templates', 'custom-api-emails' ),
'manage_options',
'custom-email-templates',
array( $this, 'render_admin_page' ),
'dashicons-email'
);
}
/**
* Register plugin settings
*
* @since 1.0.0
* @return void
*/
public function register_settings() {
register_setting( 'custom_email_options', 'custom_email_settings' );
// Email From Settings
add_settings_section(
'email_from_section',
__( 'Email From Settings', 'custom-api-emails' ),
null,
'custom-email-templates'
);
add_settings_field(
'from_email',
__( 'From Email', 'custom-api-emails' ),
array( $this, 'render_text_field' ),
'custom-email-templates',
'email_from_section',
array( 'field' => 'from_email' )
);
add_settings_field(
'from_name',
__( 'From Name', 'custom-api-emails' ),
array( $this, 'render_text_field' ),
'custom-email-templates',
'email_from_section',
array( 'field' => 'from_name' )
);
// Password Reset Email Settings
add_settings_section(
'reset_email_section',
__( 'Password Reset Email', 'custom-api-emails' ),
null,
'custom-email-templates'
);
$this->add_email_fields( 'reset_email_section', 'reset' );
// OTP Email Settings
add_settings_section(
'otp_email_section',
__( 'OTP Email', 'custom-api-emails' ),
null,
'custom-email-templates'
);
$this->add_email_fields( 'otp_email_section', 'otp' );
}
/**
* Add email fields for a section
*
* @since 1.0.0
* @param string $section Section ID.
* @param string $prefix Field prefix.
* @return void
*/
private function add_email_fields( $section, $prefix ) {
// Add Mosaico template selector
add_settings_field(
"{$prefix}_mosaico_template",
__('Mosaico Template', 'custom-api-emails'),
array($this, 'render_select_field'),
'custom-email-templates',
$section,
array(
'field' => "{$prefix}_mosaico_template",
'options' => $this->get_mosaico_templates()
)
);
add_settings_field(
"{$prefix}_email_subject",
__('Subject', 'custom-api-emails'),
array($this, 'render_text_field'),
'custom-email-templates',
$section,
array('field' => "{$prefix}_email_subject")
);
add_settings_field(
"{$prefix}_email_content",
__('Content', 'custom-api-emails'),
array($this, 'render_textarea_field'),
'custom-email-templates',
$section,
array('field' => "{$prefix}_email_content")
);
}
/**
* Render admin page
*
* @since 1.0.0
* @return void
*/
public function render_admin_page() {
// Check user capabilities
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include plugin_dir_path( __FILE__ ) . 'templates/admin-page.php';
}
/**
* Custom reset email subject
*
* @since 1.0.0
* @param string $subject Default subject.
* @return string
*/
public function custom_reset_subject( $subject ) {
return ! empty( $this->options['reset_email_subject'] )
? $this->options['reset_email_subject']
: $subject;
}
/**
* Apply Mosaico template to content
*
* @since 1.0.0
* @param string $content The content to wrap in template
* @param int $template_id The Mosaico template ID
* @return string
*/
private function apply_mosaico_template($content, $template_id) {
// Validate inputs
if (empty($content) || !is_string($content)) {
error_log('Invalid content passed to apply_mosaico_template');
return $content;
}
if (empty($template_id) || !is_numeric($template_id)) {
error_log('Invalid template_id passed to apply_mosaico_template');
return $content;
}
// Check if CiviCRM is active and available
if (!class_exists('\Civi\Api4\MosaicoTemplate')) {
error_log('CiviCRM Mosaico not available');
return $content;
}
try {
// Fetch template
$template = \Civi\Api4\MosaicoTemplate::get(FALSE)
->addSelect('html')
->addWhere('id', '=', $template_id)
->setLimit(1)
->execute()
->first();
// Validate template data
if (empty($template)) {
error_log("Mosaico template not found for ID: $template_id");
return $content;
}
if (empty($template['html'])) {
error_log("Mosaico template HTML is empty for ID: $template_id");
return $content;
}
// Check if template contains the placeholder
if (strpos($template['html'], 'app_message') === false) {
error_log("Mosaico template missing 'app_message' placeholder");
return $content;
}
// Apply template
$template_html = str_replace('app_message', $content, $template['html']);
// Validate output
if (empty($template_html)) {
error_log('Template application resulted in empty content');
return $content;
}
return $template_html;
} catch (Exception $e) {
error_log('Exception in apply_mosaico_template: ' . $e->getMessage());
error_log('Exception trace: ' . $e->getTraceAsString());
return $content;
}
}
/**
* Custom reset email content
*
* @since 1.0.0
* @param string $content Default content.
* @param string $code Reset code.
* @param string $expiry Expiry time.
* @param string $email User email.
* @return string
*/
public function custom_reset_content($content, $email, $code, $expiry) {
if (empty($this->options['reset_email_content'])) {
return $content;
}
$message = $this->options['reset_email_content'];
$replacements = array(
'{code}' => $code,
'{expiry}' => $expiry,
'{user_email}'=> $email,
'{site_name}' => get_bloginfo('name'),
);
$message = str_replace(array_keys($replacements), array_values($replacements), $message);
$message = strpos($message, '<') === false ? wpautop($message) : $message;
// Apply Mosaico template if selected
if (!empty($this->options['reset_mosaico_template'])) {
$message = $this->apply_mosaico_template($message, $this->options['reset_mosaico_template']);
}
return $message;
}
/**
* Custom email from address
*
* @since 1.0.0
* @param string $email Default from email.
* @return string
*/
public function custom_email_from( $email ) {
return ! empty( $this->options['from_email'] )
? $this->options['from_email']
: $email;
}
/**
* Custom email from name
*
* @since 1.0.0
* @param string $name Default from name.
* @return string
*/
public function custom_email_from_name( $name ) {
return ! empty( $this->options['from_name'] )
? $this->options['from_name']
: $name;
}
/**
* Custom OTP email subject
*
* @since 1.0.0
* @param string $subject Default subject.
* @return string
*/
public function custom_otp_subject( $subject ) {
return ! empty( $this->options['otp_email_subject'] )
? $this->options['otp_email_subject']
: $subject;
}
/**
* Custom OTP email content
*
* @since 1.0.0
* @param string $content Default content.
* @param string $otp OTP code.
* @return string
*/
public function custom_otp_content($content, $otp) {
if (empty($this->options['otp_email_content'])) {
$content = __('Your one-time password is: {otp}. This code will expire in 10 minutes.', 'custom-api-emails');
} else {
$content = $this->options['otp_email_content'];
}
$replacements = array(
'{otp}' => $otp,
'{site_name}' => get_bloginfo('name'),
);
$content = str_replace(array_keys($replacements), array_values($replacements), $content);
$content = strpos($content, '<') === false ? wpautop($content) : $content;
// Apply Mosaico template if selected
if (!empty($this->options['otp_mosaico_template'])) {
$content = $this->apply_mosaico_template($content, $this->options['otp_mosaico_template']);
}
return $content;
}
/**
* Render text field
*
* @since 1.0.0
* @param array $args Field arguments.
* @return void
*/
public function render_text_field( $args ) {
$field = $args['field'];
$value = isset( $this->options[$field] ) ? $this->options[$field] : '';
printf(
'<input type="text" id="%1$s" name="custom_email_settings[%1$s]" value="%2$s" class="regular-text">',
esc_attr( $field ),
esc_attr( $value )
);
}
/**
* Render textarea field
*
* @since 1.0.0
* @param array $args Field arguments.
* @return void
*/
public function render_textarea_field( $args ) {
$field = $args['field'];
$value = isset( $this->options[$field] ) ? $this->options[$field] : '';
printf(
'<textarea id="%1$s" name="custom_email_settings[%1$s]" rows="10" class="large-text">%2$s</textarea>',
esc_attr( $field ),
esc_textarea( $value )
);
}
/**
* Render select field
*
* @since 1.0.0
* @param array $args Field arguments.
* @return void
*/
public function render_select_field($args) {
$field = $args['field'];
$options = $args['options'];
$value = isset($this->options[$field]) ? $this->options[$field] : '';
printf('<select id="%1$s" name="custom_email_settings[%1$s]" class="regular-text">', esc_attr($field));
printf('<option value="">%s</option>', esc_html__('Select a template', 'custom-api-emails'));
foreach ($options as $option_value => $option_label) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr($option_value),
selected($value, $option_value, false),
esc_html($option_label)
);
}
echo '</select>';
}
/**
* AJAX handler for Mosaico template preview
*
* @since 1.0.0
* @return void
*/
public function ajax_get_mosaico_preview() {
check_ajax_referer('mosaico_preview_nonce', '_ajax_nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error('Unauthorized');
return;
}
$template_id = isset($_POST['template_id']) ? intval($_POST['template_id']) : 0;
$content = isset($_POST['content']) ? wp_kses_post($_POST['content']) : '';
if (empty($template_id) || empty($content)) {
wp_send_json_error('Invalid parameters');
return;
}
$preview = $this->apply_mosaico_template($content, $template_id);
if ($preview === $content) {
wp_send_json_error('Failed to generate preview');
return;
}
wp_send_json_success($preview);
}
}
/**
* Initialize the plugin
*
* @since 1.0.0
* @return Custom_API_Emails
*/
function custom_api_emails() {
return Custom_API_Emails::get_instance();
}
// Initialize the plugin when WordPress loads.
add_action( 'plugins_loaded', 'custom_api_emails' );