class-field-guesser.php
3.52 KB
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
<?php
/**
* Class MC4WP_Field_Guesser
*
* @access private
* @ignore
*/
class MC4WP_Field_Guesser
{
/**
* @var array
*/
protected $fields;
/**
* @param array $fields
*/
public function __construct(array $fields)
{
$fields = array_change_key_case($fields, CASE_UPPER);
$this->fields = $fields;
}
/**
* Get all data which is namespaced with a given namespace
*
* @param string $namespace
*
* @return array
*/
public function namespaced($namespace = 'mc4wp-')
{
$prefix = strtoupper($namespace);
$return = array();
$length = strlen($prefix);
foreach ($this->fields as $key => $value) {
if (strpos($key, $prefix) === 0) {
$new_key = substr($key, $length);
$return[ $new_key ] = $value;
}
}
return $return;
}
/**
* Guess values for the following fields
* - EMAIL
* - NAME
* - FNAME
* - LNAME
*
* @return array
*/
public function guessed()
{
$guessed = array();
foreach ($this->fields as $field => $value) {
// transform value into array to support 1-level arrays
$sub_fields = is_array($value) ? $value : array( $value );
foreach ($sub_fields as $sub_field_value) {
// poor man's urldecode, to get Enfold theme's contact element to work.
$sub_field_value = str_replace('%40', '@', $sub_field_value);
// is this an email value? if so, assume it's the EMAIL field
if (empty($guessed['EMAIL']) && is_string($sub_field_value) && is_email($sub_field_value)) {
$guessed['EMAIL'] = $sub_field_value;
continue 2;
}
// remove special characters from field name
$simple_key = str_replace(array( '-', '_', ' ' ), '', $field);
if (empty($guessed['FNAME']) && $this->string_contains($simple_key, array( 'FIRSTNAME', 'FNAME', 'GIVENNAME', 'FORENAME' ))) {
// find first name field
$guessed['FNAME'] = $sub_field_value;
} elseif (empty($guessed['LNAME']) && $this->string_contains($simple_key, array( 'LASTNAME', 'LNAME', 'SURNAME', 'FAMILYNAME' ))) {
// find last name field
$guessed['LNAME'] = $sub_field_value;
} elseif (empty($guessed['NAME']) && $this->string_contains($simple_key, 'NAME')) {
// find name field
$guessed['NAME'] = $sub_field_value;
}
}
}
return $guessed;
}
/**
* @param $methods
*
* @return array
*/
public function combine(array $methods)
{
$combined = array();
foreach ($methods as $method) {
if (method_exists($this, $method)) {
$combined = array_merge($combined, call_user_func(array( $this, $method )));
}
}
return $combined;
}
/**
* @param string $haystack
* @param string|array $needles
*
* @return bool
*/
private function string_contains($haystack, $needles)
{
if (! is_array($needles)) {
$needles = array( $needles );
}
foreach ($needles as $needle) {
if (strpos($haystack, $needle) !== false) {
return true;
}
}
return false;
}
}