AfterRule.php
3 weeks ago
ArrayRule.php
3 weeks ago
BaseRule.php
3 weeks ago
BooleanRule.php
3 weeks ago
DateFormatRule.php
3 weeks ago
DateRule.php
3 weeks ago
DateTimeRule.php
3 weeks ago
EmailRule.php
3 weeks ago
EmailUniqueRule.php
3 weeks ago
ExistsRule.php
3 weeks ago
FloatRule.php
3 weeks ago
GreaterThanEqualRule.php
3 weeks ago
GreaterThanRule.php
3 weeks ago
InRule.php
3 weeks ago
IntegerRule.php
3 weeks ago
IsValidImageIdRule.php
3 weeks ago
LessThanEqualRule.php
3 weeks ago
LessThanRule.php
3 weeks ago
MaxRule.php
3 weeks ago
MinRule.php
3 weeks ago
NotInRule.php
3 weeks ago
NullableRule.php
3 weeks ago
NumberRule.php
3 weeks ago
ObjectRule.php
3 weeks ago
ProhibitedIfRule.php
3 weeks ago
ProhibitedRule.php
3 weeks ago
RegexRule.php
3 weeks ago
RequiredIfExists.php
3 weeks ago
RequiredIfRule.php
3 weeks ago
RequiredIfSiblingRule.php
3 weeks ago
RequiredRule.php
3 weeks ago
SameAsRule.php
3 weeks ago
Sanitizer.php
3 weeks ago
StringRule.php
3 weeks ago
UniqueRule.php
3 weeks ago
UrlRule.php
3 weeks ago
UserExists.php
3 weeks ago
EmailUniqueRule.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Validates that an email is unique in the WordPress users table. |
| 5 | * Supports excluding current user during updates. |
| 6 | * |
| 7 | * @package Framework |
| 8 | * @subpackage Validation\Rules |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | namespace Kirki\Framework\Validation\Rules; |
| 12 | |
| 13 | \defined('ABSPATH') || exit; |
| 14 | use function Kirki\Framework\message; |
| 15 | class EmailUniqueRule extends BaseRule |
| 16 | { |
| 17 | /** |
| 18 | * The user ID to exclude from uniqueness check (for updates). |
| 19 | * |
| 20 | * @var int|null |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | protected $exclude_user_id; |
| 25 | /** |
| 26 | * Set the user ID to exclude from uniqueness check. |
| 27 | * |
| 28 | * @param mixed $user_id The user id. |
| 29 | * |
| 30 | * @return $this |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | public function exclude_user($user_id) |
| 35 | { |
| 36 | $this->exclude_user_id = $user_id; |
| 37 | return $this; |
| 38 | } |
| 39 | /** |
| 40 | * Determine if the email is unique in the users table. |
| 41 | * |
| 42 | * @return bool |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | public function validate_rule() |
| 47 | { |
| 48 | if (empty($this->value)) { |
| 49 | return \true; |
| 50 | } |
| 51 | $user = get_user_by('email', $this->value); |
| 52 | if ($user === \false) { |
| 53 | return \true; |
| 54 | } |
| 55 | if ($this->exclude_user_id && $user->ID === (int) $this->exclude_user_id) { |
| 56 | return \true; |
| 57 | } |
| 58 | return \false; |
| 59 | } |
| 60 | /** |
| 61 | * Get the error message for a non-unique email. |
| 62 | * |
| 63 | * @return string |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public function get_error_message() |
| 68 | { |
| 69 | return message('validator.email_unique', $this->value); |
| 70 | } |
| 71 | } |
| 72 |