-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapvalid-email-verification.php
More file actions
196 lines (160 loc) · 4.91 KB
/
snapvalid-email-verification.php
File metadata and controls
196 lines (160 loc) · 4.91 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
<?php
/**
* Plugin Name: SnapValid Email Verification
* Description: Real-time email validation using SnapValid API.
* Version: 1.0.0
* Author: SnapValid
* Requires at least: 5.0
* Requires PHP: 7.4
* License: GPL2+
*/
if (!defined('ABSPATH')) {
exit;
}
/*
|--------------------------------------------------------------------------
| Constants
|--------------------------------------------------------------------------
*/
define('SNAPVALID_PATH', plugin_dir_path(__FILE__));
define('SNAPVALID_URL', plugin_dir_url(__FILE__));
define('SNAPVALID_VERSION', '1.0.0');
/*
|--------------------------------------------------------------------------
| Includes
|--------------------------------------------------------------------------
*/
require_once SNAPVALID_PATH . 'includes/class-api.php';
require_once SNAPVALID_PATH . 'includes/class-admin.php';
require_once SNAPVALID_PATH . 'includes/class-validation.php';
require_once SNAPVALID_PATH . 'includes/class-cf7.php';
/*
|--------------------------------------------------------------------------
| Initialize Plugin
|--------------------------------------------------------------------------
*/
function snapvalid_init() {
new SnapValid_Admin();
new SnapValid_Validation();
new SnapValid_CF7();
}
add_action('plugins_loaded', 'snapvalid_init');
/*
|--------------------------------------------------------------------------
| AJAX Email Validation (UX Improvement Step 1)
|--------------------------------------------------------------------------
*/
add_action('wp_ajax_snapvalid_check_email', 'snapvalid_check_email');
add_action('wp_ajax_nopriv_snapvalid_check_email', 'snapvalid_check_email');
function snapvalid_check_email() {
// -----------------------------
// Security: Verify Nonce
// -----------------------------
if (
!isset($_POST['nonce']) ||
!wp_verify_nonce($_POST['nonce'], 'snapvalid_nonce')
) {
wp_send_json_error([
'message' => 'Security check failed'
]);
}
// -----------------------------
// Validate Email Input
// -----------------------------
if (!isset($_POST['email'])) {
wp_send_json_error();
}
$email = sanitize_email(wp_unslash($_POST['email']));
if (empty($email) || !is_email($email)) {
wp_send_json_success([
'valid' => false,
'message' => 'Invalid email format'
]);
}
// -----------------------------
// If no API key, allow by default
// -----------------------------
if (!get_option('snapvalid_api_key')) {
wp_send_json_success([
'valid' => true
]);
}
// -----------------------------
// Caching (Reduce API Calls)
// -----------------------------
$cache_key = 'snapvalid_' . md5($email);
$cached_result = get_transient($cache_key);
if ($cached_result !== false) {
wp_send_json_success([
'valid' => $cached_result,
'cached' => true
]);
}
// -----------------------------
// Call SnapValid API
// -----------------------------
$api = new SnapValid_API();
$valid = $api->verify_email($email);
// Fail-safe: if API fails, allow checkout
if ($valid === null) {
$valid = true;
}
// Store in cache for 1 hour
set_transient($cache_key, $valid, HOUR_IN_SECONDS);
wp_send_json_success([
'valid' => $valid,
'cached' => false
]);
}
/*
|--------------------------------------------------------------------------
| Enqueue Frontend Script (Checkout Only)
|--------------------------------------------------------------------------
*/
add_action('wp_enqueue_scripts', function () {
if (!function_exists('is_checkout') || !is_checkout()) {
return;
}
wp_enqueue_script(
'snapvalid-js',
SNAPVALID_URL . 'assets/js/snapvalid.js',
['jquery'],
SNAPVALID_VERSION,
true
);
wp_enqueue_style(
'snapvalid-css',
SNAPVALID_URL . 'assets/css/snapvalid.css',
[],
SNAPVALID_VERSION
);
wp_localize_script('snapvalid-js', 'snapvalid_ajax', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('snapvalid_nonce')
]);
});
/*
|--------------------------------------------------------------------------
| Enqueue Frontend Script (bulk)
|--------------------------------------------------------------------------
*/
add_action('admin_enqueue_scripts', function($hook){
if ($hook !== 'snapvalid_page_snapvalid-bulk') {
return;
}
wp_enqueue_script(
'snapvalid-bulk-js',
SNAPVALID_URL . 'assets/js/snapvalid-bulk.js',
['jquery'],
'1.0',
true
);
wp_localize_script(
'snapvalid-bulk-js',
'snapvalid_ajax',
[
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('snapvalid_nonce')
]
);
});