-
Notifications
You must be signed in to change notification settings - Fork 18
/
Wpcf7cfMailParser.php
executable file
·72 lines (49 loc) · 2.3 KB
/
Wpcf7cfMailParser.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
<?php
//require_once __DIR__.'/init.php';
class Wpcf7cfMailParser {
private $mail_body;
private $visible_groups;
private $hidden_groups;
private $repeaters;
private $posted_data;
function __construct($mail_body, $visible_groups, $hidden_groups, $repeaters, $posted_data) {
$this->mail_body = $mail_body;
$this->visible_groups = $visible_groups;
$this->hidden_groups = $hidden_groups;
$this->repeaters = $repeaters;
$this->posted_data = $posted_data;
}
public function getParsedMail() {
return preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $this->mail_body );
}
function hide_hidden_mail_fields_regex_callback ( $matches ) {
$name = $matches[1];
$name_parts = explode('__', $name);
$name_root = array_shift($name_parts);
$name_suffix = implode('__',$name_parts);
$content = $matches[2];
if ( in_array( $name, $this->hidden_groups ) ) {
// The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
return '';
} elseif ( in_array( $name, $this->visible_groups ) ) {
// The tag name represents a visible group, so remove the tags themselves, but return everything else
// ( instead of just returning the $content, return the preg_replaced content )
return preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $content );
} elseif ( $this->repeaters !== null && in_array( $name, $this->repeaters ) ) {
$original_name = explode('__',$name)[0];
$inner_template = $content;
ob_start();
$num_subs = $this->posted_data[$name.'_count'];
for ($i=1; $i<=$num_subs; $i++) {
$str = preg_replace(["/\[{$original_name}\:index[^\]]*?\]/"],$i,$inner_template);
//echo str_replace(']','__'.$i.']',$str);
echo preg_replace("/\[([^\s^\]]*?)([\s\]]+)([^\]]*?)/", "[$1__{$i}$2",$str);
}
$underscored_content = ob_get_clean();
return preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $underscored_content );
}else {
// The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
return $matches[0];
}
}
}