View difference between Paste ID: w3bDTuf9 and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | <?php |
2 | /** | |
3 | * @package Allow_Multiple_Accounts | |
4 | * @author Scott Reilly | |
5 | * @version 2.0.1 | |
6 | */ | |
7 | /* | |
8 | Plugin Name: Allow Multiple Accounts | |
9 | Version: 2.0.1 | |
10 | Plugin URI: http://coffee2code.com/wp-plugins/allow-multiple-accounts/ | |
11 | Author: Scott Reilly | |
12 | Author URI: http://coffee2code.com | |
13 | Text Domain: allow-multiple-accounts | |
14 | Description: Allow multiple user accounts to be created from the same email address. | |
15 | ||
16 | Compatible with WordPress 2.8+, 2.9+, 3.0+. | |
17 | ||
18 | =>> Read the accompanying readme.txt file for instructions and documentation. | |
19 | =>> Also, visit the plugin's homepage for additional information and updates. | |
20 | =>> Or visit: http://wordpress.org/extend/plugins/allow-multiple-accounts/ | |
21 | ||
22 | */ | |
23 | ||
24 | /* | |
25 | Copyright (c) 2008-2010 by Scott Reilly (aka coffee2code) | |
26 | ||
27 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation | |
28 | files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, | |
29 | modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | |
30 | Software is furnished to do so, subject to the following conditions: | |
31 | ||
32 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
33 | ||
34 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
35 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
36 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | |
37 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
38 | */ | |
39 | ||
40 | if ( !class_exists( 'AllowMultipleAccounts' ) ) : | |
41 | ||
42 | require_once( 'c2c-plugin.php' ); | |
43 | ||
44 | class AllowMultipleAccounts extends C2C_Plugin_016 { | |
45 | ||
46 | var $allow_multiple_accounts = false; // Used internally; not a setting! | |
47 | var $exceeded_limit = false; | |
48 | var $retrieve_password_for = ''; | |
49 | var $during_user_creation = false; // part of a hack | |
50 | ||
51 | /** | |
52 | * Constructor | |
53 | */ | |
54 | function AllowMultipleAccounts() { | |
55 | $this->C2C_Plugin_016( '2.0.1', 'allow-multiple-accounts', 'c2c', __FILE__, array( 'settings_page' => 'users' ) ); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Initializes the plugin's config data array. | |
60 | * | |
61 | * @return void | |
62 | */ | |
63 | function load_config() { | |
64 | $this->name = __( 'Allow Multiple Accounts', $this->textdomain ); | |
65 | $this->menu_name = __( 'Multiple Accounts', $this->textdomain ); | |
66 | ||
67 | $this->config = array( | |
68 | 'allow_for_everyone' => array('input' => 'checkbox', 'default' => true, | |
69 | 'label' => __( 'Allow multiple accounts for everyone?', $this->textdomain ), | |
70 | 'help' => __( 'If not checked, only the emails listed below can have multiple accounts.', $this->textdomain ) ), | |
71 | 'account_limit' => array( 'input' => 'text', 'default' => '', | |
72 | 'label' => __( 'Account limit', $this->textdomain ), | |
73 | 'help' => __( 'The maximum number of accounts that can be associated with a single email address. Leave blank to indicate no limit.', $this->textdomain ) ), | |
74 | 'emails' => array( 'input' => 'inline_textarea', 'datatype' => 'array', 'default' => '', | |
75 | 'input_attributes' => 'style="width:98%;" rows="6"', | |
76 | 'label' => __( 'Multi-account emails', $this->textdomain ), | |
77 | 'help' => __( 'If the checkbox above is unchecked, then only the emails listed here will be allowed to have multiple accounts. Define one per line.', $this->textdomain ) ) | |
78 | ); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Override the plugin framework's register_filters() to actually actions against filters. | |
83 | * | |
84 | * @return void | |
85 | */ | |
86 | function register_filters() { | |
87 | add_action( 'check_passwords', array( &$this, 'hack_check_passwords' ) ); | |
88 | add_filter( 'pre_user_display_name', array( &$this, 'hack_pre_user_email' ) ); | |
89 | add_filter( 'pre_user_email', array( &$this, 'hack_pre_user_email' ) ); | |
90 | add_action( 'register_post', array( &$this, 'register_post' ), 1, 3 ); | |
91 | add_filter( 'registration_errors', array( &$this, 'registration_errors' ), 1 ); | |
92 | add_action( 'retrieve_password', array( &$this, 'retrieve_password' ) ); | |
93 | add_filter( 'retrieve_password_message', array( &$this, 'retrieve_password_message' ) ); | |
94 | add_action( 'user_profile_update_errors', array( &$this, 'user_profile_update_errors' ), 1, 3 ); | |
95 | add_action( $this->get_hook( 'after_settings_form' ), array( &$this, 'list_multiple_accounts' ) ); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Outputs the text above the setting form | |
100 | * | |
101 | * @return void (Text will be echoed.) | |
102 | */ | |
103 | function options_page_description() { | |
104 | $options = $this->get_options(); | |
105 | parent::options_page_description( __( 'Allow Multiple Accounts Settings', $this->textdomain ) ); | |
106 | echo '<p>' . __( 'Allow multiple user accounts to be created from the same email address.', $this->textdomain ) . '</p>'; | |
107 | echo '<p>' . __( 'By default, WordPress only allows a single user account to be assigned to a specific email address. This plugin removes that restriction. A setting is also provided to allow only certain email addresses the ability to have multiple accounts. You may also specify a limit to the number of accounts an email address can have.', $this->textdomain ) . '</p>'; | |
108 | echo '<p><a href="#multiaccount_list">' . __( 'View a list of user accounts grouped by email address.', $this->textdomain ) . '</a></p>'; | |
109 | } | |
110 | ||
111 | /** | |
112 | * This is a HACK because WP 3.0 introduced a change that made it impossible to suppress the unique email check when creating a new user. | |
113 | * | |
114 | * For the hack, this filter is invoked just after wp_insert_user() checks for the uniqueness of the email address. What this | |
115 | * is doing is unsetting the flag by the get_user_by_email() overridden by this plugin, so that when called in any other context than | |
116 | * wp_insert_user(), it'll actually get the user by email. | |
117 | * | |
118 | * @since 2.0 | |
119 | * | |
120 | * @param string $display_name Display name for user | |
121 | * @return string The same value as passed to the function | |
122 | */ | |
123 | function hack_pre_user_display_name( $display_name ) { | |
124 | $this->during_user_creation = false; | |
125 | return $display_name; | |
126 | } | |
127 | ||
128 | /** | |
129 | * This is a HACK because WP 3.0 introduced a change that made it impossible to suppress the unique email check when creating a new user. | |
130 | * | |
131 | * For the hack, this filter is invoked just before wp_insert_user() checks for the uniqueness of the email address. What this | |
132 | * is doing is setting a flag so that the get_user_by_email() overridden by this plugin, when called in the wp_insert_user() context, | |
133 | * knows to return false, making WP think the email address isn't in use. | |
134 | * | |
135 | * @since 2.0 | |
136 | * | |
137 | * @param string $email Email for the user | |
138 | * @return string The same value as passed to the function | |
139 | */ | |
140 | function hack_pre_user_email( $email ) { | |
141 | $this->during_user_creation = true; | |
142 | return $email; | |
143 | } | |
144 | ||
145 | /** | |
146 | * This is a HACK because WP 3.0 introduced a change that made it impossible to suppress the unique email check when creating a new user. | |
147 | * | |
148 | * For the hack, this filter is invoked just before edit_user() does a bunch of error checks. What this | |
149 | * is doing is setting a flag so that the get_user_by_email() overridden by this plugin, when called in the edit_user() context, | |
150 | * knows to return false, making WP think the email address isn't in use. | |
151 | * | |
152 | * @since 2.0 | |
153 | * | |
154 | * @param string $user_login User login | |
155 | * @return void | |
156 | */ | |
157 | function hack_check_passwords( $user_login ) { | |
158 | $this->during_user_creation = true; | |
159 | } | |
160 | ||
161 | /** | |
162 | * Outputs a list of all user email addresses and their associated accounts. | |
163 | * | |
164 | * @return void (Text is echoed.) | |
165 | */ | |
166 | function list_multiple_accounts() { | |
167 | global $wpdb; | |
168 | ||
169 | // Get doublons IDs | |
170 | $doublon_emails = $wpdb->get_col("SELECT user_email FROM $wpdb->users GROUP BY user_email HAVING count(user_email) > 1"); | |
171 | ||
172 | // Get datas for doublon ID | |
173 | $users = $wpdb->get_results( "SELECT ID, user_email FROM $wpdb->users WHERE user_email IN ('".implode("', '", $doublon_emails)."') ORDER BY user_login ASC" ); | |
174 | ||
175 | $by_email = array(); | |
176 | foreach ( $users as $user ) | |
177 | $by_email[$user->user_email][] = $user; | |
178 | $emails = array_keys( $by_email ); | |
179 | sort( $emails ); | |
180 | $style = ''; | |
181 | ||
182 | echo <<<END | |
183 | <style type="text/css"> | |
184 | .emailrow { | |
185 | background-color:#ffffef; | |
186 | } | |
187 | .check-column { | |
188 | display:none; | |
189 | } | |
190 | </style> | |
191 | <div class='wrap'><a name='multiaccount_list'></a> | |
192 | <h2> | |
193 | ||
194 | END; | |
195 | echo __( 'E-mail Addresses with Multiple User Accounts', $this->textdomain ); | |
196 | echo <<<END | |
197 | </h2> | |
198 | <table class="widefat"> | |
199 | <thead> | |
200 | <tr class="thead"> | |
201 | ||
202 | END; | |
203 | echo '<th>' . __( 'Username', $this->textdomain ) . '</th>' . | |
204 | '<th>' . __( 'Name', $this->textdomain ) . '</th>' . | |
205 | '<th>' . __( 'E-mail', $this->textdomain ) . '</th>' . | |
206 | '<th>' . __( 'Role', $this->textdomain ) . '</th>' . | |
207 | '<th class="num">' . __( 'Posts', $this->textdomain ) . '</th>'; | |
208 | echo <<<END | |
209 | </tr> | |
210 | </thead> | |
211 | <tbody id="users" class="list:user user-list"> | |
212 | ||
213 | END; | |
214 | ||
215 | foreach ( $emails as $email ) { | |
216 | $email_users = $by_email[$email]; | |
217 | $count = count( $by_email[$email] ); | |
218 | echo '<tr class="emailrow"><td colspan="6">'; | |
219 | printf( _n( '%1$s — %2$d account', '%1$s — %2$d accounts', $count, $this->textdomain ), $email, $count ); | |
220 | echo '</td></tr>'; | |
221 | foreach ( $by_email[$email] as $euser ) { | |
222 | $user_object = new WP_User($euser->ID); | |
223 | $roles = $user_object->roles; | |
224 | $role = array_shift( $roles ); | |
225 | $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"'; | |
226 | echo "\n\t" . $this->user_row( $user_object, $style, $role ); | |
227 | } | |
228 | } | |
229 | ||
230 | echo <<<END | |
231 | </tbody> | |
232 | </table> | |
233 | </div> | |
234 | ||
235 | END; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Generate HTML for a single row on the users.php admin panel. | |
240 | * | |
241 | * @since 2.1.0 | |
242 | * | |
243 | * @param object $user_object | |
244 | * @param string $style Optional. Attributes added to the TR element. Must be sanitized. | |
245 | * @param string $role Key for the $wp_roles array. | |
246 | * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts. | |
247 | * @return string | |
248 | */ | |
249 | function user_row( $user_object, $style = '', $role = '', $numposts = 0 ) { | |
250 | global $wp_roles; | |
251 | ||
252 | if ( !( is_object( $user_object) && is_a( $user_object, 'WP_User' ) ) ) | |
253 | $user_object = new WP_User( (int) $user_object ); | |
254 | $user_object = sanitize_user_object($user_object, 'display'); | |
255 | $email = $user_object->user_email; | |
256 | $url = $user_object->user_url; | |
257 | $short_url = str_replace( 'http://', '', $url ); | |
258 | $short_url = str_replace( 'www.', '', $short_url ); | |
259 | if ('/' == substr( $short_url, -1 )) | |
260 | $short_url = substr( $short_url, 0, -1 ); | |
261 | if ( strlen( $short_url ) > 35 ) | |
262 | $short_url = substr( $short_url, 0, 32 ).'...'; | |
263 | $checkbox = ''; | |
264 | // Check if the user for this row is editable | |
265 | if ( current_user_can( 'list_users' ) ) { | |
266 | // Set up the user editing link | |
267 | // TODO: make profile/user-edit determination a separate function | |
268 | if ( get_current_user_id() == $user_object->ID) { | |
269 | $edit_link = 'profile.php'; | |
270 | } else { | |
271 | $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( esc_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ), "user-edit.php?user_id=$user_object->ID" ) ); | |
272 | } | |
273 | $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />"; | |
274 | ||
275 | // Set up the hover actions for this user | |
276 | $actions = array(); | |
277 | ||
278 | if ( current_user_can('edit_user', $user_object->ID) ) { | |
279 | $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />"; | |
280 | $actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit') . '</a>'; | |
281 | } else { | |
282 | $edit = "<strong>$user_object->user_login</strong><br />"; | |
283 | } | |
284 | ||
285 | if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can('delete_user', $user_object->ID) ) | |
286 | $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("users.php?action=delete&user=$user_object->ID", 'bulk-users') . "'>" . __('Delete') . "</a>"; | |
287 | if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can('remove_user', $user_object->ID) ) | |
288 | $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url("users.php?action=remove&user=$user_object->ID", 'bulk-users') . "'>" . __('Remove') . "</a>"; | |
289 | $actions = apply_filters('user_row_actions', $actions, $user_object); | |
290 | $action_count = count($actions); | |
291 | $i = 0; | |
292 | $edit .= '<div class="row-actions">'; | |
293 | foreach ( $actions as $action => $link ) { | |
294 | ++$i; | |
295 | ( $i == $action_count ) ? $sep = '' : $sep = ' | '; | |
296 | $edit .= "<span class='$action'>$link$sep</span>"; | |
297 | } | |
298 | $edit .= '</div>'; | |
299 | ||
300 | // Set up the checkbox (because the user is editable, otherwise its empty) | |
301 | $checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />"; | |
302 | ||
303 | } else { | |
304 | $edit = '<strong>' . $user_object->user_login . '</strong>'; | |
305 | } | |
306 | $role_name = isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : __('None'); | |
307 | $r = "<tr id='user-$user_object->ID'$style>"; | |
308 | ||
309 | $columns = array( | |
310 | 'username' => __('Username'), | |
311 | 'name' => __('Name'), | |
312 | 'email' => __('E-mail'), | |
313 | 'role' => __('Role'), | |
314 | 'posts' => __('Posts') | |
315 | ); | |
316 | ||
317 | $avatar = get_avatar( $user_object->ID, 32 ); | |
318 | foreach ( $columns as $column_name => $column_display_name ) { | |
319 | $class = "class=\"$column_name column-$column_name\""; | |
320 | $attributes = "$class"; | |
321 | ||
322 | switch ($column_name) { | |
323 | case 'cb': | |
324 | $r .= "<th scope='row' class='check-column'>$checkbox</th>"; | |
325 | break; | |
326 | case 'username': | |
327 | $r .= "<td $attributes>$avatar $edit</td>"; | |
328 | break; | |
329 | case 'name': | |
330 | $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>"; | |
331 | break; | |
332 | case 'email': | |
333 | $r .= "<td $attributes><a href='mailto:$email' title='" . sprintf( __('E-mail: %s' ), $email ) . "'>$email</a></td>"; | |
334 | break; | |
335 | case 'role': | |
336 | $r .= "<td $attributes>$role_name</td>"; | |
337 | break; | |
338 | case 'posts': | |
339 | $attributes = 'class="posts column-posts num"' . $style; | |
340 | $r .= "<td $attributes>"; | |
341 | if ( $numposts > 0 ) { | |
342 | $r .= "<a href='edit.php?author=$user_object->ID' title='" . __( 'View posts by this author' ) . "' class='edit'>"; | |
343 | $r .= $numposts; | |
344 | $r .= '</a>'; | |
345 | } else { | |
346 | $r .= 0; | |
347 | } | |
348 | $r .= "</td>"; | |
349 | break; | |
350 | default: | |
351 | $r .= "<td $attributes>"; | |
352 | $r .= apply_filters('manage_users_custom_column', '', $column_name, $user_object->ID); | |
353 | $r .= "</td>"; | |
354 | } | |
355 | } | |
356 | $r .= '</tr>'; | |
357 | ||
358 | return $r; | |
359 | } | |
360 | ||
361 | /** | |
362 | * Indicates if the specified email address has exceeded its allowable number of accounts. | |
363 | * | |
364 | * @param string $email Email address | |
365 | * @param int $user_id (optional) ID of existing user, if updating a user | |
366 | * @return boolean True if the email address has exceeded its allowable number of accounts; false otherwise | |
367 | */ | |
368 | function has_exceeded_limit( $email, $user_id = null ) { | |
369 | $has = false; | |
370 | $options = $this->get_options(); | |
371 | if ( $options['account_limit'] ) { | |
372 | $limit = (int) $options['account_limit']; | |
373 | $count = $this->count_multiple_accounts( $email, $user_id ); | |
374 | if ( $count >= $limit ) | |
375 | $has = true; | |
376 | } | |
377 | return $has; | |
378 | } | |
379 | ||
380 | /** | |
381 | * Returns a count of the number of users associated with the given email. | |
382 | * | |
383 | * @param string $email The email account | |
384 | * @param int $user_id (optional) ID of existing user, if updating a user | |
385 | * @return int The number of users associated with the given email | |
386 | */ | |
387 | function count_multiple_accounts( $email, $user_id = null ) { | |
388 | global $wpdb; | |
389 | $sql = "SELECT COUNT(*) AS count FROM $wpdb->users WHERE user_email = %s"; | |
390 | if ( $user_id ) | |
391 | $sql .= ' AND ID != %d'; | |
392 | return (int) $wpdb->get_var( $wpdb->prepare( $sql, $email, $user_id ) ); | |
393 | } | |
394 | ||
395 | /** | |
396 | * Returns the users associated with the given email. | |
397 | * | |
398 | * @param string $email The email account | |
399 | * @return array All of the users associated with the given email | |
400 | */ | |
401 | function get_users_by_email( $email ) { | |
402 | global $wpdb; | |
403 | return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_email = %s", $email ) ); | |
404 | } | |
405 | ||
406 | /** | |
407 | * Returns a boolean indicating if the given email is associated with more than one user account. | |
408 | * | |
409 | * @param string $email The email account | |
410 | * @return bool True if the given email is associated with more than one user account; false otherwise | |
411 | */ | |
412 | function has_multiple_accounts( $email ) { | |
413 | return $this->count_multiple_accounts( $email ) > 1 ? true : false; | |
414 | } | |
415 | ||
416 | /** | |
417 | * Handler for 'register_post' action. Intercepts potential 'email_exists' error and sets flags for later use, pertaining to if | |
418 | * multiple accounts are authorized for the email and/or if the email has exceeded its allocated number of accounts. | |
419 | * | |
420 | * @param string $user_login User login | |
421 | * @param string $user_email User email | |
422 | * @param WP_Error $errors Error object | |
423 | * @param int $user_id (optional) ID of existing user, if updating a user | |
424 | * @return void | |
425 | */ | |
426 | function register_post( $user_login, $user_email, $errors, $user_id = null ) { | |
427 | $options = $this->get_options(); | |
428 | if ( $errors->get_error_message( 'email_exists' ) && | |
429 | ( $options['allow_for_everyone'] || in_array( $user_email, $options['emails'] ) ) ) { | |
430 | if ( $this->has_exceeded_limit( $user_email, $user_id ) ) | |
431 | $this->exceeded_limit = true; | |
432 | else | |
433 | $this->allow_multiple_accounts = true; | |
434 | } | |
435 | } | |
436 | ||
437 | /** | |
438 | * Handler for 'registration_errors' action to add and/or remove registration errors as needed. | |
439 | * | |
440 | * @param WP_Error $errors Error object | |
441 | * @return WP_Error The potentially modified error object | |
442 | */ | |
443 | function registration_errors( $errors ) { | |
444 | if ( $this->exceeded_limit ) | |
445 | $errors->add( 'exceeded_limit', __( '<strong>ERROR</strong>: Too many accounts are associated with this email, please choose another one.', $this->textdomain ) ); | |
446 | if ( $this->allow_multiple_accounts || $this->exceeded_limit ) { | |
447 | unset( $errors->errors['email_exists'] ); | |
448 | unset( $errors->error_data['email_exists'] ); | |
449 | } | |
450 | return $errors; | |
451 | } | |
452 | ||
453 | /** | |
454 | * Roundabout way of determining what user account a password retrieval is being requested for since some of the actions/filters don't specify. | |
455 | * | |
456 | * @param string $user_login User login | |
457 | * @return string The same value as passed to the function | |
458 | */ | |
459 | function retrieve_password( $user_login ) { | |
460 | $this->retrieve_password_for = $user_login; | |
461 | return $user_login; | |
462 | } | |
463 | ||
464 | /** | |
465 | * Appends text at the end of a 'retrieve password' email to remind users what accounts they have associated with their email address. | |
466 | * | |
467 | * @param string $message The original email message | |
468 | * @return string Potentially modified email message | |
469 | */ | |
470 | function retrieve_password_message( $message ) { | |
471 | $user = get_user_by( 'login', $this->retrieve_password_for ); | |
472 | if ( $this->has_multiple_accounts( $user->user_email ) ) { | |
473 | $message .= "\r\n\r\n"; | |
474 | $message .= __( 'For your information, your e-mail address is also associated with the following accounts:', $this->textdomain ) . "\r\n\r\n"; | |
475 | foreach ( $this->get_users_by_email( $user->user_email ) as $user ) { | |
476 | $message .= "\t" . $user->user_login . "\r\n"; | |
477 | } | |
478 | $message .= "\r\n"; | |
479 | $message .= __( 'In order to reset the password for any of these (if you aren\'t already successfully in the middle of doing so already), you should specify the login when requesting a password reset rather than using your e-mail.', $this->textdomain ) . "\r\n\r\n"; | |
480 | } | |
481 | return $message; | |
482 | } | |
483 | ||
484 | /** | |
485 | * Intercept possible email_exists errors during user updating, and also possibly add errors. | |
486 | * | |
487 | * @param WP_Error $errors Error object | |
488 | * @param boolean $update Is this being invoked due to a user being updated? | |
489 | * @param WP_User $user User object | |
490 | */ | |
491 | function user_profile_update_errors( $errors, $update, $user ) { | |
492 | $this->during_user_creation = false; // Part of HACK to work around WP3.0.0 bug | |
493 | $user_id = $update ? $user->ID : null; | |
494 | $this->register_post( $user->user_login, $user->user_email, $errors, $user_id ); | |
495 | $errors = $this->registration_errors( $errors ); | |
496 | } | |
497 | } // end AllowMultipleAccounts | |
498 | ||
499 | $GLOBALS['c2c_allow_multiple_accounts'] = new AllowMultipleAccounts(); | |
500 | ||
501 | endif; // end if !class_exists() | |
502 | ||
503 | ||
504 | // | |
505 | /** | |
506 | * ******************* | |
507 | * TEMPLATE FUNCTIONS | |
508 | * | |
509 | * Functions suitable for use in other themes and plugins | |
510 | * ******************* | |
511 | */ | |
512 | ||
513 | /** | |
514 | * Returns a count of the number of users associated with the given email. | |
515 | * | |
516 | * @since 2.0 | |
517 | * | |
518 | * @param string $email The email account | |
519 | * @return int The number of users associated with the given email | |
520 | */ | |
521 | if ( !function_exists( 'c2c_count_multiple_accounts' ) ) { | |
522 | function c2c_count_multiple_accounts( $email ) { return $GLOBALS['c2c_allow_multiple_accounts']->count_multiple_accounts( $email ); } | |
523 | } | |
524 | ||
525 | /** | |
526 | * Returns the users associated with the given email. | |
527 | * | |
528 | * @since 2.0 | |
529 | * | |
530 | * @param string $email The email account | |
531 | * @return array All of the users associated with the given email | |
532 | */ | |
533 | if ( !function_exists( 'c2c_get_users_by_email' ) ) { | |
534 | function c2c_get_users_by_email( $email ) { return $GLOBALS['c2c_allow_multiple_accounts']->get_users_by_email( $email ); } | |
535 | } | |
536 | ||
537 | /** | |
538 | * Returns a boolean indicating if the given email is associated with more than one user account. | |
539 | * | |
540 | * @since 2.0 | |
541 | * | |
542 | * @param string $email The email account | |
543 | * @return bool True if the given email is associated with more than one user account; false otherwise | |
544 | */ | |
545 | if ( !function_exists( 'c2c_has_multiple_accounts' ) ) { | |
546 | function c2c_has_multiple_accounts( $email ) { return $GLOBALS['c2c_allow_multiple_accounts']->has_multiple_accounts( $email ); } | |
547 | } | |
548 | ||
549 | /** | |
550 | * This is only overridden as part of a HACK solution to a bug in WP 3.0 not allowing suppression of the duplicate email check. | |
551 | * | |
552 | * What it does: Replaces WP's get_user_by_email(). If during the user creation process (hackily determined by the plugin's instance) | |
553 | * AND the email has not exceeded the account limit, then return false. wp_insert_user() calls this function simply to check if the | |
554 | * email is already associated with an account. So in that instance, if we know that's where the request is originating and that the | |
555 | * email in question is allowed to have multiple accounts, then trick the check into thinking the email isn't in use so that an error | |
556 | * isn't generated. | |
557 | * | |
558 | * @since 2.0 | |
559 | * | |
560 | * @param string $email User email | |
561 | * @return string User associated with the email | |
562 | */ | |
563 | if ( !function_exists( 'get_user_by_email' ) ) { | |
564 | function get_user_by_email( $email ) { | |
565 | if ( $GLOBALS['c2c_allow_multiple_accounts']->during_user_creation && !$GLOBALS['c2c_allow_multiple_accounts']->has_exceeded_limit( $email ) ) | |
566 | return false; | |
567 | return get_user_by('email', $email); | |
568 | } | |
569 | } | |
570 | ||
571 | /** | |
572 | * ******************* | |
573 | * DEPRECATED FUNCTIONS | |
574 | * ******************* | |
575 | */ | |
576 | if ( !function_exists( 'count_multiple_accounts' ) ) { | |
577 | function count_multiple_accounts( $email ) { return c2c_count_multiple_accounts( $email ); } | |
578 | } | |
579 | if ( !function_exists( 'get_users_by_email' ) ) { | |
580 | function get_users_by_email( $email ) { return c2c_get_users_by_email( $email ); } | |
581 | } | |
582 | if ( !function_exists( 'has_multiple_accounts' ) ) { | |
583 | function has_multiple_accounts( $email ) { return c2c_has_multiple_accounts( $email ); } | |
584 | } | |
585 | ||
586 | ?> |