-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail-tag.php
197 lines (150 loc) · 4.1 KB
/
mail-tag.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Class that represents a mail-tag.
*/
class WPCF7_MailTag {
private $tag;
private $tagname = '';
private $name = '';
private $options = array();
private $values = array();
private $form_tag = null;
/**
* The constructor method.
*/
public function __construct( $tag, $tagname, $values ) {
$this->tag = $tag;
$this->name = $this->tagname = $tagname;
$this->options = array(
'do_not_heat' => false,
'format' => '',
);
if ( ! empty( $values ) ) {
preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
$this->values = wpcf7_strip_quote_deep( $matches[0] );
}
if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
$this->name = trim( $matches[1] );
$this->options['do_not_heat'] = true;
}
if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
$this->name = trim( $matches[1] );
$this->options['format'] = $this->values[0];
}
}
/**
* Returns the name part of this mail-tag.
*/
public function tag_name() {
return $this->tagname;
}
/**
* Returns the form field name corresponding to this mail-tag.
*/
public function field_name() {
return strtr( $this->name, '.', '_' );
}
/**
* Returns the value of the specified option.
*/
public function get_option( $option ) {
return $this->options[$option];
}
/**
* Returns the values part of this mail-tag.
*/
public function values() {
return $this->values;
}
/**
* Retrieves the WPCF7_FormTag object that corresponds to this mail-tag.
*/
public function corresponding_form_tag() {
if ( $this->form_tag instanceof WPCF7_FormTag ) {
return $this->form_tag;
}
if ( $submission = WPCF7_Submission::get_instance() ) {
$contact_form = $submission->get_contact_form();
$tags = $contact_form->scan_form_tags( array(
'name' => $this->field_name(),
'feature' => '! zero-controls-container',
) );
if ( $tags ) {
$this->form_tag = $tags[0];
}
}
return $this->form_tag;
}
}
use Contactable\SWV;
/**
* Mail-tag output calculator.
*/
class WPCF7_MailTag_OutputCalculator {
const email = 0b100;
const text = 0b010;
const blank = 0b001;
private $contact_form;
public function __construct( WPCF7_ContactForm $contact_form ) {
$this->contact_form = $contact_form;
}
public function calc_output( WPCF7_MailTag $mail_tag ) {
return $this->calc_swv_result(
$mail_tag,
$this->contact_form->get_schema()
);
}
private function calc_swv_result( WPCF7_MailTag $mail_tag, SWV\Rule $rule ) {
if ( $rule instanceof SWV\AnyRule ) {
$result = 0b000;
foreach ( $rule->rules() as $child_rule ) {
$result |= $this->calc_swv_result( $mail_tag, $child_rule );
}
return $result;
}
if ( $rule instanceof SWV\CompositeRule ) {
$result = 0b111;
foreach ( $rule->rules() as $child_rule ) {
$result &= $this->calc_swv_result( $mail_tag, $child_rule );
}
return $result;
}
$field_prop = $rule->get_property( 'field' );
if ( empty( $field_prop ) or $field_prop !== $mail_tag->field_name() ) {
return self::email | self::text | self::blank;
}
if ( $rule instanceof SWV\RequiredRule ) {
return ~ self::blank;
}
if ( $rule instanceof SWV\EmailRule ) {
return self::email | self::blank;
}
if ( $rule instanceof SWV\EnumRule ) {
$acceptable_values = (array) $rule->get_property( 'accept' );
$acceptable_values = array_map( 'strval', $acceptable_values );
$acceptable_values = array_filter( $acceptable_values );
$acceptable_values = array_unique( $acceptable_values );
if ( ! $mail_tag->get_option( 'do_not_heat' ) ) {
$pipes = $this->contact_form->get_pipes(
$mail_tag->field_name()
);
$acceptable_values = array_map(
static function ( $val ) use ( $pipes ) {
return $pipes->do_pipe( $val );
},
$acceptable_values
);
}
$email_values = array_filter(
$acceptable_values,
'wpcf7_is_mailbox_list'
);
if ( count( $email_values ) === count( $acceptable_values ) ) {
return self::email | self::blank;
} else {
return self::email | self::text | self::blank;
}
}
return self::email | self::text | self::blank;
}
}