forked from convissor/oop-plugin-template-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfs-plugin.php
333 lines (290 loc) · 8.22 KB
/
cfs-plugin.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
<?php
/**
* Plugin Name: CFS Super Plugin
*
* Description: A well engineered template for creating plugins using
* object-oriented programming practices.
*
* Plugin URI: https://github.com/cleanforestco/cfs-plugin
* Props: http://wordpress.org/extend/plugins/oop-plugin-template-solution/
* Version: 1.1.0
* (Remember to change the VERSION constant, below, as well!)
* Author: Daniel Convissor
* Author URI: http://www.analysisandsolutions.com/
* License: GPLv2
* @package cfs-plugin
*
* This plugin used the Object-Oriented Plugin Template Solution as a skeleton
* REPLACE_PLUGIN_URI
*/
/**
* The instantiated version of this plugin's class
*/
$GLOBALS['cfsplugin'] = new cfsplugin;
/**
* CFS Super Plugin
*
* @package cfs-plugin
* @link http://wordpress.org/extend/plugins/oop-plugin-template-solution/
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @author Daniel Convissor <[email protected]>
* @copyright The Analysis and Solutions Company, 2012
*
* This plugin used the Object-Oriented Plugin Template Solution as a skeleton
* REPLACE_PLUGIN_URI
*/
class cfsplugin {
/**
* This plugin's identifier
*/
const ID = 'cfs-plugin';
/**
* This plugin's name
*/
const NAME = 'CFS Super Plugin';
/**
* This plugin's version
*/
const VERSION = '1.1.1';
/**
* This plugin's table name prefix
* @var string
*/
protected $prefix = 'cfsplugin_';
/**
* Has the internationalization text domain been loaded?
* @var bool
*/
protected $loaded_textdomain = false;
/**
* This plugin's options
*
* Options from the database are merged on top of the default options.
*
* @see cfsplugin::set_options() to obtain the saved
* settings
* @var array
*/
protected $options = array();
/**
* This plugin's default options
* @var array
*/
protected $options_default = array(
'deactivate_deletes_data' => 1,
'example_int' => 5,
'example_string' => 'abc',
'another_string' => 'xyz',
'track_logins' => 1,
);
/**
* Our option name for storing the plugin's settings
* @var string
*/
protected $option_name;
/**
* Name, with $table_prefix, of the table tracking login failures
* @var string
*/
protected $table_login;
/**
* Our usermeta key for tracking when a user logged in
* @var string
*/
protected $umk_login_time;
/**
* Declares the WordPress action and filter callbacks
*
* @return void
* @uses cfsplugin::initialize() to set the object's
* properties
*/
public function __construct() {
$this->initialize();
if ($this->options['track_logins']) {
add_action('wp_login', array(&$this, 'wp_login'), 1, 2);
}
if (is_admin()) {
$this->load_plugin_textdomain();
require_once dirname(__FILE__) . '/admin.php';
$admin = new cfsplugin_admin;
if (is_multisite()) {
$admin_menu = 'network_admin_menu';
$admin_notices = 'network_admin_notices';
$plugin_action_links = 'network_admin_plugin_action_links_cfs-plugin/cfs-plugin.php';
} else {
$admin_menu = 'admin_menu';
$admin_notices = 'admin_notices';
$plugin_action_links = 'plugin_action_links_cfs-plugin/cfs-plugin.php';
}
add_action($admin_menu, array(&$admin, 'admin_menu'));
add_action('admin_init', array(&$admin, 'admin_init'));
add_filter($plugin_action_links, array(&$admin, 'plugin_action_links'));
register_activation_hook(__FILE__, array(&$admin, 'activate'));
if ($this->options['deactivate_deletes_data']) {
register_deactivation_hook(__FILE__, array(&$admin, 'deactivate'));
}
}
}
/**
* Sets the object's properties and options
*
* This is separated out from the constructor to avoid undesirable
* recursion. The constructor sometimes instantiates the admin class,
* which is a child of this class. So this method permits both the
* parent and child classes access to the settings and properties.
*
* @return void
*
* @uses cfsplugin::set_options() to replace the default
* options with those stored in the database
*/
protected function initialize() {
global $wpdb;
$this->table_login = $wpdb->get_blog_prefix(0) . $this->prefix . 'login';
$this->option_name = self::ID . '-options';
$this->umk_login_time = self::ID . '-login-time';
$this->set_options();
}
/*
* ===== ACTION & FILTER CALLBACK METHODS =====
*/
/**
* Stores the time a user logs in
*
* NOTE: This method is automatically called by WordPress when users
* successfully log in.
*
* @param string $user_name the user name from the current login form
* @param WP_User $user the current user
* @return void
*/
public function wp_login($user_name, $user) {
if (!$user_name) {
return;
}
$this->insert_login($user_name);
$this->set_metadata_login_time($user->ID);
$this->notify_login($user_name);
}
/*
* ===== INTERNAL METHODS ====
*/
/**
* Log the user out and send them to the lost password page
*
* This is here solely for demonstration of unit testing.
*/
protected function force_retrieve_pw() {
wp_logout();
wp_redirect(wp_login_url() . '?action=retrievepassword');
}
/**
* Obtains the email addresses the notifications should go to
* @return string
*/
protected function get_admin_email() {
return get_site_option('admin_email');
}
/**
* Obtains the timestamp of the given user's last "login time"
*
* @param int $user_ID the current user's ID number
* @return int the Unix timestamp of the user's last login
*/
protected function get_metadata_login_time($user_ID) {
return (int) get_user_meta($user_ID, $this->umk_login_time, true);
}
/**
* Sanitizes output via htmlspecialchars() using UTF-8 encoding
*
* Makes this program's native text and translated/localized strings
* safe for displaying in browsers.
*
* @param string $in the string to sanitize
* @return string the sanitized string
*/
protected function hsc_utf8($in) {
return htmlspecialchars($in, ENT_QUOTES, 'UTF-8');
}
/**
* Saves the login info in the database
*
* @param string $user_login the user name from the current login form
* @return void
*/
protected function insert_login($user_login) {
global $wpdb;
$wpdb->insert(
$this->table_login,
array(
'user_login' => $user_login,
),
array('%s')
);
}
/**
* A centralized way to load the plugin's textdomain for
* internationalization
* @return void
*/
protected function load_plugin_textdomain() {
if (!$this->loaded_textdomain) {
load_plugin_textdomain(self::ID, false, self::ID . '/languages');
$this->loaded_textdomain = true;
}
}
/**
* Sends an email to the blog's administrator telling them of a login
*
* @param string $user_name the user name from the current login form
* @return bool
*
* @uses wp_mail() to send the messages
*/
protected function notify_login($user_name) {
$this->load_plugin_textdomain();
$to = $this->sanitize_whitespace($this->get_admin_email());
$blog = get_option('blogname');
$subject = sprintf(__("LOGIN TO %s", self::ID), $blog);
$subject = $this->sanitize_whitespace($subject);
$message = sprintf(__("%s just logged in to %s.", self::ID),
$user_name, $blog) . "\n";
return wp_mail($to, $subject, $message);
}
/**
* Replaces all whitespace characters with one space
* @param string $in the string to clean
* @return string the cleaned string
*/
protected function sanitize_whitespace($in) {
return preg_replace('/\s+/u', ' ', $in);
}
/**
* Stores the present time in the given user's "login time" metadata
*
* @param int $user_ID the current user's ID number
* @return int|bool the record number if added, TRUE if updated, FALSE
* if error
*/
protected function set_metadata_login_time($user_ID) {
return update_user_meta($user_ID, $this->umk_login_time, time());
}
/**
* Replaces the default option values with those stored in the database
* @uses login_security_solution::$options to hold the data
*/
protected function set_options() {
if (is_multisite()) {
switch_to_blog(1);
$options = get_option($this->option_name);
restore_current_blog();
} else {
$options = get_option($this->option_name);
}
if (!is_array($options)) {
$options = array();
}
$this->options = array_merge($this->options_default, $options);
}
}