validator.php
8.05 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
require_once path_join( __DIR__, 'form.php' );
require_once path_join( __DIR__, 'mail.php' );
require_once path_join( __DIR__, 'messages.php' );
require_once path_join( __DIR__, 'additional-settings.php' );
require_once path_join( __DIR__, 'actions.php' );
/**
* Configuration validator.
*
* @link https://contactform7.com/configuration-errors/
*/
class WPCF7_ConfigValidator {
/**
* The plugin version in which important updates happened last time.
*/
const last_important_update = '5.8.1';
const error_codes = array(
'maybe_empty',
'invalid_mailbox_syntax',
'email_not_in_site_domain',
'html_in_message',
'multiple_controls_in_label',
'file_not_found',
'unavailable_names',
'invalid_mail_header',
'deprecated_settings',
'file_not_in_content_dir',
'unavailable_html_elements',
'attachments_overweight',
'dots_in_names',
'colons_in_names',
'upload_filesize_overlimit',
'unsafe_email_without_protection',
);
use WPCF7_ConfigValidator_Form;
use WPCF7_ConfigValidator_Mail;
use WPCF7_ConfigValidator_Messages;
use WPCF7_ConfigValidator_AdditionalSettings;
private $contact_form;
private $errors = array();
private $include;
private $exclude;
/**
* Returns a URL linking to the documentation page for the error type.
*/
public static function get_doc_link( $child_page = '' ) {
$url = __( 'https://contactform7.com/configuration-errors/',
'contact-form-7'
);
if ( '' !== $child_page ) {
$child_page = strtr( $child_page, '_', '-' );
$url = sprintf( '%s/%s', untrailingslashit( $url ), $child_page );
}
return esc_url( $url );
}
/**
* Constructor.
*/
public function __construct( WPCF7_ContactForm $contact_form, $args = '' ) {
$args = wp_parse_args( $args, array(
'include' => null,
'exclude' => null,
) );
$this->contact_form = $contact_form;
if ( isset( $args['include'] ) ) {
$this->include = (array) $args['include'];
}
if ( isset( $args['exclude'] ) ) {
$this->exclude = (array) $args['exclude'];
}
}
/**
* Returns the contact form object that is tied to this validator.
*/
public function contact_form() {
return $this->contact_form;
}
/**
* Returns true if no error has been detected.
*/
public function is_valid() {
return ! $this->count_errors();
}
/**
* Returns true if the given error code is supported by this instance.
*/
public function supports( $error_code ) {
if ( isset( $this->include ) ) {
$supported_codes = array_intersect( self::error_codes, $this->include );
} else {
$supported_codes = self::error_codes;
}
if ( isset( $this->exclude ) ) {
$supported_codes = array_diff( $supported_codes, $this->exclude );
}
return in_array( $error_code, $supported_codes, true );
}
/**
* Counts detected errors.
*/
public function count_errors( $args = '' ) {
$args = wp_parse_args( $args, array(
'section' => '',
'code' => '',
) );
$count = 0;
foreach ( $this->errors as $key => $errors ) {
if ( preg_match( '/^mail_[0-9]+\.(.*)$/', $key, $matches ) ) {
$key = sprintf( 'mail.%s', $matches[1] );
}
if ( $args['section']
and $key !== $args['section']
and preg_replace( '/\..*$/', '', $key, 1 ) !== $args['section'] ) {
continue;
}
foreach ( $errors as $error ) {
if ( empty( $error ) ) {
continue;
}
if ( $args['code'] and $error['code'] !== $args['code'] ) {
continue;
}
$count += 1;
}
}
return $count;
}
/**
* Collects messages for detected errors.
*/
public function collect_error_messages() {
$error_messages = array();
foreach ( $this->errors as $section => $errors ) {
$error_messages[$section] = array();
foreach ( $errors as $error ) {
if ( empty( $error['args']['message'] ) ) {
$message = $this->get_default_message( $error['code'] );
} elseif ( empty( $error['args']['params'] ) ) {
$message = $error['args']['message'];
} else {
$message = $this->build_message(
$error['args']['message'],
$error['args']['params']
);
}
$link = '';
if ( ! empty( $error['args']['link'] ) ) {
$link = $error['args']['link'];
}
$error_messages[$section][] = array(
'message' => $message,
'link' => esc_url( $link ),
);
}
}
return $error_messages;
}
/**
* Builds an error message by replacing placeholders.
*/
public function build_message( $message, $params = '' ) {
$params = wp_parse_args( $params, array() );
foreach ( $params as $key => $val ) {
if ( ! preg_match( '/^[0-9A-Za-z_]+$/', $key ) ) { // invalid key
continue;
}
$placeholder = '%' . $key . '%';
if ( false !== stripos( $message, $placeholder ) ) {
$message = str_ireplace( $placeholder, $val, $message );
}
}
return $message;
}
/**
* Returns a default message that is used when the message for the error
* is not specified.
*/
public function get_default_message( $code = '' ) {
return __( "Configuration error is detected.", 'contact-form-7' );
}
/**
* Returns true if the specified section has the specified error.
*
* @param string $section The section where the error detected.
* @param string $code The unique code of the error.
*/
public function has_error( $section, $code ) {
if ( empty( $this->errors[$section] ) ) {
return false;
}
foreach ( (array) $this->errors[$section] as $error ) {
if ( isset( $error['code'] ) and $error['code'] === $code ) {
return true;
}
}
return false;
}
/**
* Adds a validation error.
*
* @param string $section The section where the error detected.
* @param string $code The unique code of the error.
* @param string|array $args Optional options for the error.
*/
public function add_error( $section, $code, $args = '' ) {
$args = wp_parse_args( $args, array(
'message' => '',
'params' => array(),
) );
$available_error_codes = (array) apply_filters(
'wpcf7_config_validator_available_error_codes',
self::error_codes,
$this->contact_form
);
if ( ! in_array( $code, $available_error_codes, true ) ) {
return false;
}
if ( ! isset( $args['link'] ) ) {
$args['link'] = self::get_doc_link( $code );
}
if ( ! isset( $this->errors[$section] ) ) {
$this->errors[$section] = array();
}
$this->errors[$section][] = array(
'code' => $code,
'args' => $args,
);
return true;
}
/**
* Removes an error.
*
* @param string $section The section where the error detected.
* @param string $code The unique code of the error.
*/
public function remove_error( $section, $code ) {
if ( empty( $this->errors[$section] ) ) {
return;
}
foreach ( (array) $this->errors[$section] as $key => $error ) {
if ( isset( $error['code'] ) and $error['code'] === $code ) {
unset( $this->errors[$section][$key] );
}
}
if ( empty( $this->errors[$section] ) ) {
unset( $this->errors[$section] );
}
}
/**
* The main validation runner.
*
* @return bool True if there is no error detected.
*/
public function validate() {
$this->validate_form();
$this->validate_mail( 'mail' );
$this->validate_mail( 'mail_2' );
$this->validate_messages();
$this->validate_additional_settings();
do_action( 'wpcf7_config_validator_validate', $this );
return $this->is_valid();
}
/**
* Saves detected errors as a post meta data.
*/
public function save() {
if ( $this->contact_form->initial() ) {
return;
}
delete_post_meta( $this->contact_form->id(), '_config_validation' );
if ( $this->errors ) {
update_post_meta(
$this->contact_form->id(), '_config_validation', $this->errors
);
}
}
/**
* Restore errors from the database.
*/
public function restore() {
$config_errors = get_post_meta(
$this->contact_form->id(), '_config_validation', true
);
foreach ( (array) $config_errors as $section => $errors ) {
if ( empty( $errors ) ) {
continue;
}
foreach ( (array) $errors as $error ) {
if ( ! empty( $error['code'] ) ) {
$code = $error['code'];
$args = isset( $error['args'] ) ? $error['args'] : '';
$this->add_error( $section, $code, $args );
}
}
}
}
}