-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-redirect.php
More file actions
308 lines (263 loc) · 10.5 KB
/
email-redirect.php
File metadata and controls
308 lines (263 loc) · 10.5 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
<?php
/**
* Plugin Name: Email Redirect
* Plugin URI: https://github.com/latinvm/wordpress-email-redirect
* Description: Redirects users to URLs based on their email domain
* Version: 1.0.0
* Author: Roy Boverhof
* Text Domain: email-redirect
* Domain Path: /languages
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('ABSPATH')) {
exit;
}
class Email_Redirect {
private $option_name = 'er_domain_mappings';
public function __construct() {
// Load text domain
add_action('plugins_loaded', array($this, 'load_textdomain'));
// Admin menu
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'register_settings'));
// Admin scripts
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
// Register widget
add_action('widgets_init', array($this, 'register_widget'));
// AJAX handler
add_action('wp_ajax_er_process_email', array($this, 'process_email'));
add_action('wp_ajax_nopriv_er_process_email', array($this, 'process_email'));
// Enqueue scripts
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
// Register shortcode
add_shortcode('email_redirect_form', array($this, 'render_shortcode'));
}
public function load_textdomain() {
load_plugin_textdomain(
'email-redirect',
false,
dirname(plugin_basename(__FILE__)) . '/languages'
);
}
public function add_admin_menu() {
add_options_page(
__('Email Redirect Settings', 'email-redirect'),
__('Email Redirect', 'email-redirect'),
'manage_options',
'email-redirect',
array($this, 'render_settings_page')
);
}
public function register_settings() {
register_setting('er_settings', $this->option_name, array($this, 'sanitize_mappings'));
}
public function sanitize_mappings($input) {
if (!is_array($input)) {
return array();
}
$sanitized = array();
foreach ($input as $mapping) {
if (!empty($mapping['domain']) && !empty($mapping['url'])) {
$sanitized[] = array(
'domain' => sanitize_text_field($mapping['domain']),
'url' => esc_url_raw($mapping['url'])
);
}
}
return $sanitized;
}
public function enqueue_admin_scripts($hook) {
if ($hook !== 'settings_page_email-redirect') {
return;
}
$mappings = get_option($this->option_name, array());
wp_enqueue_script(
'er-admin-script',
plugin_dir_url(__FILE__) . 'admin-script.js',
array('jquery'),
'1.0.0',
true
);
wp_localize_script('er-admin-script', 'erAdmin', array(
'rowCount' => count($mappings),
'optionName' => $this->option_name,
'placeholderDomain' => esc_attr__('e.g., company.com', 'email-redirect'),
'placeholderUrl' => esc_attr__('https://example.com/page', 'email-redirect'),
'removeText' => esc_html__('Remove', 'email-redirect')
));
wp_enqueue_style(
'er-admin-style',
plugin_dir_url(__FILE__) . 'admin-style.css',
array(),
'1.0.0'
);
}
public function render_settings_page() {
if (!current_user_can('manage_options')) {
return;
}
$mappings = get_option($this->option_name, array());
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields('er_settings');
?>
<table class="wp-list-table widefat fixed striped" id="er-mappings-table">
<thead>
<tr>
<th style="width: 40%;"><?php esc_html_e('Domain', 'email-redirect'); ?></th>
<th style="width: 50%;"><?php esc_html_e('Redirect URL', 'email-redirect'); ?></th>
<th style="width: 10%;"><?php esc_html_e('Action', 'email-redirect'); ?></th>
</tr>
</thead>
<tbody id="er-mappings-body">
<?php
if (!empty($mappings)) {
foreach ($mappings as $index => $mapping) {
$this->render_mapping_row($index, $mapping);
}
} else {
$this->render_mapping_row(0, array('domain' => '', 'url' => ''));
}
?>
</tbody>
</table>
<p>
<button type="button" class="button" id="er-add-row"><?php esc_html_e('Add Mapping', 'email-redirect'); ?></button>
</p>
<p class="description">
<?php esc_html_e('Enter domains without protocol (e.g., company.com or mail.company.com). Subdomain configurations take precedence over main domain matches.', 'email-redirect'); ?>
</p>
<?php submit_button(); ?>
</form>
</div>
<?php
}
private function render_mapping_row($index, $mapping) {
?>
<tr>
<td>
<input type="text"
name="<?php echo esc_attr($this->option_name); ?>[<?php echo esc_attr($index); ?>][domain]"
value="<?php echo esc_attr($mapping['domain']); ?>"
class="regular-text"
placeholder="<?php esc_attr_e('e.g., company.com', 'email-redirect'); ?>" />
</td>
<td>
<input type="url"
name="<?php echo esc_attr($this->option_name); ?>[<?php echo esc_attr($index); ?>][url]"
value="<?php echo esc_url($mapping['url']); ?>"
class="regular-text"
placeholder="<?php esc_attr_e('https://example.com/page', 'email-redirect'); ?>" />
</td>
<td>
<button type="button" class="button er-remove-row"><?php esc_html_e('Remove', 'email-redirect'); ?></button>
</td>
</tr>
<?php
}
public function register_widget() {
require_once plugin_dir_path(__FILE__) . 'widget.php';
register_widget('Email_Redirect_Widget');
}
public function render_shortcode($atts) {
$atts = shortcode_atts(array(
'title' => '',
), $atts, 'email_redirect_form');
$unique_id = uniqid('er-form-');
ob_start();
?>
<div class="er-widget-container">
<?php if (!empty($atts['title'])) : ?>
<h3 class="er-form-title"><?php echo esc_html($atts['title']); ?></h3>
<?php endif; ?>
<form class="er-email-form" id="<?php echo esc_attr($unique_id); ?>" data-form-id="<?php echo esc_attr($unique_id); ?>">
<div class="er-form-group">
<label for="<?php echo esc_attr($unique_id); ?>-email"><?php esc_html_e('Email Address:', 'email-redirect'); ?></label>
<input type="email"
id="<?php echo esc_attr($unique_id); ?>-email"
name="email"
required
placeholder="<?php esc_attr_e('your.email@company.com', 'email-redirect'); ?>" />
</div>
<button type="submit" class="er-submit-btn"><?php esc_html_e('Submit', 'email-redirect'); ?></button>
<div class="er-message" style="display:none;"></div>
</form>
</div>
<?php
return ob_get_clean();
}
public function enqueue_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script(
'er-script',
plugin_dir_url(__FILE__) . 'script.js',
array('jquery'),
'1.0.0',
true
);
wp_localize_script('er-script', 'erAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('er_nonce')
));
wp_enqueue_style(
'er-style',
plugin_dir_url(__FILE__) . 'style.css',
array(),
'1.0.0'
);
}
public function process_email() {
check_ajax_referer('er_nonce', 'nonce');
$email = isset($_POST['email']) ? sanitize_email(wp_unslash($_POST['email'])) : '';
if (!is_email($email)) {
wp_send_json_error(array('message' => __('Please enter a valid email address.', 'email-redirect')));
}
$domain = $this->extract_domain($email);
$redirect_url = $this->find_redirect_url($domain);
if ($redirect_url) {
wp_send_json_success(array('url' => $redirect_url));
} else {
wp_send_json_error(array('message' => __('No redirect URL found for your email domain.', 'email-redirect')));
}
}
private function extract_domain($email) {
$parts = explode('@', $email);
return isset($parts[1]) ? strtolower($parts[1]) : '';
}
private function find_redirect_url($domain) {
$mappings = get_option($this->option_name, array());
// First, try exact match (including subdomain)
foreach ($mappings as $mapping) {
if (strtolower($mapping['domain']) === $domain) {
return $mapping['url'];
}
}
// Then, try main domain match
$main_domain = $this->extract_main_domain($domain);
foreach ($mappings as $mapping) {
if (strtolower($mapping['domain']) === $main_domain) {
return $mapping['url'];
}
}
return false;
}
private function extract_main_domain($domain) {
$parts = explode('.', $domain);
$count = count($parts);
// Handle cases like co.uk, com.au, etc.
if ($count >= 3 && strlen($parts[$count - 2]) <= 3 && strlen($parts[$count - 1]) <= 2) {
return implode('.', array_slice($parts, -3));
}
// Standard domain
if ($count >= 2) {
return implode('.', array_slice($parts, -2));
}
return $domain;
}
}
// Initialize plugin
new Email_Redirect();