-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveil-mail.php
More file actions
302 lines (254 loc) · 8.04 KB
/
Copy pathveil-mail.php
File metadata and controls
302 lines (254 loc) · 8.04 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
<?php
/**
* Plugin Name: Veil Mail
* Plugin URI: https://veilmail.xyz/integrations/wordpress
* Description: Secure email delivery with automatic PII protection. Replace wp_mail with Veil Mail for better deliverability and compliance.
* Version: 1.0.0
* Author: Resonia Inc
* Author URI: https://resonia.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: veil-mail
* Domain Path: /languages
* Requires at least: 5.8
* Requires PHP: 7.4
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Plugin constants
define('VEIL_MAIL_VERSION', '1.0.0');
define('VEIL_MAIL_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('VEIL_MAIL_PLUGIN_URL', plugin_dir_url(__FILE__));
define('VEIL_MAIL_PLUGIN_BASENAME', plugin_basename(__FILE__));
// Autoload classes
spl_autoload_register(function ($class) {
$prefix = 'VeilMail\\';
$base_dir = VEIL_MAIL_PLUGIN_DIR . 'includes/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . 'class-' . strtolower(str_replace('\\', '-', $relative_class)) . '.php';
if (file_exists($file)) {
require $file;
}
});
/**
* Main Veil Mail plugin class
*/
class VeilMail {
/**
* Single instance of the plugin
*/
private static $instance = null;
/**
* API client instance
*/
public $api;
/**
* Admin instance
*/
public $admin;
/**
* Mail handler instance
*/
public $mail;
/**
* Get single instance
*/
public static function instance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->load_dependencies();
$this->init_hooks();
}
/**
* Load required files
*/
private function load_dependencies() {
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-api.php';
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-admin.php';
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-mail.php';
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-forms.php';
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-sync.php';
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-webhooks.php';
require_once VEIL_MAIL_PLUGIN_DIR . 'includes/class-veil-blocks.php';
}
/**
* Initialize hooks
*/
private function init_hooks() {
// Initialize components
add_action('plugins_loaded', array($this, 'init_components'));
// Activation/deactivation hooks
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
// Load translations
add_action('init', array($this, 'load_textdomain'));
}
/**
* Webhooks handler instance
*/
public $webhooks;
/**
* Blocks handler instance
*/
public $blocks;
/**
* Initialize plugin components
*/
public function init_components() {
$this->api = new VeilMail_API();
$this->admin = new VeilMail_Admin();
$this->mail = new VeilMail_Mail();
$this->webhooks = new VeilMail_Webhooks();
$this->blocks = new VeilMail_Blocks();
// Initialize form integrations if enabled
if ($this->get_option('enable_forms')) {
new VeilMail_Forms();
}
// Initialize user sync if enabled
if ($this->get_option('enable_sync')) {
new VeilMail_Sync();
}
// Run database upgrade if needed
$this->maybe_upgrade_database();
}
/**
* Plugin activation
*/
public function activate() {
// Set default options
$defaults = array(
'api_key' => '',
'api_url' => 'https://api.veilmail.xyz',
'from_email' => get_option('admin_email'),
'from_name' => get_option('blogname'),
'enable_wp_mail' => true,
'enable_forms' => false,
'enable_sync' => false,
'default_audience' => '',
'log_emails' => true,
);
$existing = get_option('veil_mail_settings', array());
update_option('veil_mail_settings', array_merge($defaults, $existing));
// Create email log table
$this->create_log_table();
// Clear any cached data
wp_cache_flush();
}
/**
* Plugin deactivation
*/
public function deactivate() {
// Clear scheduled events
wp_clear_scheduled_hook('veil_mail_cleanup_logs');
}
/**
* Create email log table
*/
private function create_log_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'veil_mail_log';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
email_id varchar(255) DEFAULT NULL,
to_email varchar(255) NOT NULL,
subject varchar(255) NOT NULL,
status varchar(50) NOT NULL DEFAULT 'pending',
response text DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
delivered_at datetime DEFAULT NULL,
opened_at datetime DEFAULT NULL,
clicked_at datetime DEFAULT NULL,
PRIMARY KEY (id),
KEY email_id (email_id),
KEY status (status),
KEY created_at (created_at)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
// Store database version
update_option('veil_mail_db_version', '1.1.0');
}
/**
* Maybe upgrade database schema
*/
private function maybe_upgrade_database() {
$current_version = get_option('veil_mail_db_version', '1.0.0');
if (version_compare($current_version, '1.1.0', '<')) {
$this->upgrade_to_1_1_0();
}
}
/**
* Upgrade database to version 1.1.0
* Adds tracking columns for webhook status updates
*/
private function upgrade_to_1_1_0() {
global $wpdb;
$table_name = $wpdb->prefix . 'veil_mail_log';
// Check if columns exist before adding
$columns = $wpdb->get_col("DESCRIBE $table_name", 0);
if (!in_array('delivered_at', $columns)) {
$wpdb->query("ALTER TABLE $table_name ADD COLUMN delivered_at datetime DEFAULT NULL AFTER created_at");
}
if (!in_array('opened_at', $columns)) {
$wpdb->query("ALTER TABLE $table_name ADD COLUMN opened_at datetime DEFAULT NULL AFTER delivered_at");
}
if (!in_array('clicked_at', $columns)) {
$wpdb->query("ALTER TABLE $table_name ADD COLUMN clicked_at datetime DEFAULT NULL AFTER opened_at");
}
update_option('veil_mail_db_version', '1.1.0');
}
/**
* Load plugin textdomain
*/
public function load_textdomain() {
load_plugin_textdomain(
'veil-mail',
false,
dirname(VEIL_MAIL_PLUGIN_BASENAME) . '/languages'
);
}
/**
* Get plugin option
*/
public function get_option($key, $default = '') {
$options = get_option('veil_mail_settings', array());
return isset($options[$key]) ? $options[$key] : $default;
}
/**
* Update plugin option
*/
public function update_option($key, $value) {
$options = get_option('veil_mail_settings', array());
$options[$key] = $value;
update_option('veil_mail_settings', $options);
}
/**
* Check if plugin is configured
*/
public function is_configured() {
$api_key = $this->get_option('api_key');
return !empty($api_key);
}
}
/**
* Main function to get plugin instance
*/
function veil_mail() {
return VeilMail::instance();
}
// Initialize the plugin
veil_mail();