1: <?php
2:
3: 4: 5:
6: class Quform_Validator_Email extends Quform_Validator_Abstract
7: {
8: const INVALID = 'emailAddressInvalid';
9: const INVALID_FORMAT = 'emailAddressInvalidFormat';
10:
11: 12: 13: 14: 15:
16: protected static $mailer = null;
17:
18: 19: 20: 21: 22: 23:
24: public function isValid($value)
25: {
26: global $wp_version;
27:
28: $this->reset();
29:
30: if ( ! is_string($value)) {
31: $this->error(self::INVALID);
32: return false;
33: }
34:
35: if (version_compare($wp_version, '5.5', '<')) {
36: $class = 'PHPMailer';
37:
38: if ( ! class_exists($class)) {
39: require_once ABSPATH . WPINC . '/class-phpmailer.php';
40: }
41: } else {
42: $class = '\\PHPMailer\\PHPMailer\\PHPMailer';
43:
44: if ( ! class_exists($class)) {
45: require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
46: require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
47: }
48: }
49:
50: if (self::$mailer === null) {
51: self::$mailer = new $class;
52: }
53:
54: self::$mailer->CharSet = $this->config('charset');
55:
56: $address = is_callable(array(self::$mailer, 'punyencodeAddress')) ? self::$mailer->punyencodeAddress($value) : $value;
57: $validationMethod = apply_filters('quform_email_validation_method', 'php', $address, self::$mailer, $this, $class);
58:
59: $valid = call_user_func(array($class, 'validateAddress'), $address, $validationMethod);
60:
61: if ( ! $valid) {
62: $this->error(self::INVALID_FORMAT, compact('value'));
63: }
64:
65: return $valid;
66: }
67:
68: 69: 70: 71: 72: 73:
74: public static function getMessageTemplates($key = null)
75: {
76: $messageTemplates = array(
77: self::INVALID => __('Invalid data type, string expected', 'quform'),
78: self::INVALID_FORMAT => __('Invalid email address', 'quform')
79: );
80:
81: if (is_string($key)) {
82: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
83: }
84:
85: return $messageTemplates;
86: }
87:
88: 89: 90: 91: 92: 93:
94: public static function getDefaultConfig($key = null)
95: {
96: $config = apply_filters('quform_default_config_validator_email', array(
97: 'charset' => 'UTF-8',
98: 'messages' => array(
99: self::INVALID => '',
100: self::INVALID_FORMAT => ''
101: )
102: ));
103:
104: $config['type'] = 'email';
105:
106: if (Quform::isNonEmptyString($key)) {
107: return Quform::get($config, $key);
108: }
109:
110: return $config;
111: }
112: }
113: