-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdm-confirm-email.php
187 lines (160 loc) · 6.03 KB
/
dm-confirm-email.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
<?php
/*
Plugin Name: DM Confirm Email
Plugin URI: http://donmhi.co/projects/dm-confirm-email/
Description: Protect your wordpress site with spam registration. DM Confirm Email makes sure that user trying to register is real by requiring them to confirm their email address.
Version: 1.4
Author: donMhico
Author URI: http://donmhi.co
License: GPLv2
*/
require_once('models/login-register.php');
require_once('models/login-confirm.php');
require_once('models/login-resend.php');
require_once('models/plugin-page.php');
require_once('models/welcome_message.php');
require_once('inc/pluggable.php');
class DmConfirmEmail {
/**
* Folder name
*/
const PLUGIN_FOLDER = 'dm-confirm-email';
/**
* Table name
*/
const PLUGIN_ALIAS = 'dmec';
/**
* DB Version of the plugin
*/
const DB_VERSION = '1.0';
function __construct() {
register_activation_hook(__FILE__, array($this, 'activate'));
// Options page
new DmConfirmEmail_Models_PluginPage();
// Wordpress native registration alteration
new DmConfirmEmail_Models_RegisterForm();
new DmConfirmEmail_Models_ConfirmForm();
new DmConfirmEmail_Models_ResendEmailConfirmForm();
// Welcome email
new DmConfirmEmail_Models_WelcomeMessage();
// Enqueue admin script
add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
}
/**
* Enqueue both scripts
*
* @param string $hook
*/
public function enqueueScripts($hook) {
if($hook != 'settings_page_dm_confirm_email_options')
return;
// Register style
wp_register_style('dmec_general_style', plugins_url(self::PLUGIN_FOLDER . '/css/general.css'), array(), '1.4');
// Register script
wp_register_script('dmec_general_script', plugins_url(self::PLUGIN_FOLDER . '/js/general.js'), array('jquery'), '1.4');
// Enqueue
wp_enqueue_script('dmec_general_script');
wp_enqueue_style('dmec_general_style');
}
/**
* Create database upon activation
*/
public function activate() {
global $wpdb;
// Plugin options
update_option(self::PLUGIN_ALIAS . '_dbversion', self::DB_VERSION);
update_option(self::PLUGIN_ALIAS, array(
'email_subject' => 'Email Confirmation',
'email_text' => '<p>Hello {user_login},</p>
<p> Welcome to {site_link}.</p>
<p> To confirm your email, please click the link below</p>
<p> {confirm_link}</p>',
'email_ishtml' => 'true',
'expiry_time' => '30',
'success_message' => 'Check your e-mail for the confirmation link.',
'user_pass_subject' => self::getPluggableUserPassSubject(),
'user_pass_message' => self::getPluggableUserPassMessage(),
'confirmed_message' => 'Your email is now confirmed. Your password was sent to your email',
'failed_message' => 'Key is not valid or already expired.',
'send_welcome' => 'false',
'welcome_email_subject' => 'Welcome to our site',
'welcome_message' => '',
'send_welcome_html' => 'true'
));
// Create new table
$tableName = $wpdb->prefix . self::PLUGIN_ALIAS;
$sql = "CREATE TABLE {$tableName} (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_login varchar(60) NOT NULL,
user_email varchar(100) NOT NULL,
user_key varchar(60) NOT NULL,
expiry_date date DEFAULT '0000-00-00' NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
/**
* Get default pluggable user pass message
*
* @return string
*/
public static function getPluggableUserPassMessage() {
// Default pluggable message
// see wp-includes/pluggable.php
// function wp_new_user_notification()
$message = "<p>" . sprintf(__('Username: %s'), '{user_login}') . "</p>" . "\r\n";
$message .= "<p>" . sprintf(__('Password: %s'), '{password}') . "</p>" . "\r\n";
$message .= "<p>" . '{login_url}' . "</p>" . "\r\n";
return $message;
}
/**
* Get the default subject for the user pass email
*
* @return string
*/
public static function getPluggableUserPassSubject() {
// Default pluggable message
// see wp-includes/pluggable.php
// function wp_new_user_notification()
$title = '{site_title} Your username and password';
return $title;
}
/**
* Text Parser for the DM Email Confirm.
*
* @param string $message
* @param string $userKey
* @param string $userLogin
* @param string $password
* @return mixed
*/
public static function parser($message, $userKey = '', $userLogin = '', $password = '') {
$siteUrl = site_url();
$loginUrl = wp_login_url();
$keyLink = '<a href="' . esc_url("{$loginUrl}/?action=confirm&eckey={$userKey}") .'" target="_blank">' .
esc_url("{$loginUrl}/?action=confirm&eckey={$userKey}") . '</a>';
$keyUrl = esc_url("{$loginUrl}/?action=confirm&eckey={$userKey}");
$siteTitle = get_bloginfo('name');
$siteLink = '<a href="' . esc_url($siteUrl) . '" target="_blank">' . $siteTitle . '</a>';
// Parsable tags
$tags = array(
'{confirm_key}', // Unique key from user_key
'{confirm_link}', // Link with <a> tag
'{confirm_url}', // Key url without the <a> tag
'{site_title}',
'{site_link}',
'{site_url}',
'{user_login}',
'{password}',
'{login_url}'
);
// Corresponding values.
$values = array(
$userKey, $keyLink, $keyUrl, $siteTitle, $siteLink, $siteUrl, $userLogin, $password, $loginUrl
);
// Replace the tags
return str_ireplace($tags, $values, $message);
}
}
new DmConfirmEmail();