Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- <?
- // Hooks only for version 5 only, may be for previous version also works
- if (!defined('WPCF7_VERSION') OR intval(WPCF7_VERSION) !== 5)
- {
- return;
- }
- add_filter( 'wpcf7_validate_text', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_text*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_email', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_email*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_url', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_url*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_tel', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- add_filter( 'wpcf7_validate_tel*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
- /**
- * @param WPCF7_Validation $result
- * @param WPCF7_FormTag $tag
- * @return mixed
- */
- function wpcf7_text_allowed_chars_validation_filter($result, $tag)
- {
- $name = $tag->name;
- $value = isset($_POST[$name])
- ? trim(wp_unslash(strtr((string)$_POST[$name], "\n", " ")))
- : '';
- if ($validation_patterns = $tag->get_option('valid-chars'))
- {
- $validation_patterns = wpcf7_field_validation_patterns_filter($validation_patterns);
- }
- if ($validation_patterns)
- {
- if (!wpcf7_validate_field_with_pattern($validation_patterns, $value))
- {
- $result->invalidate($tag, wpcf7_get_message(wpcf7_get_field_invalidation_pattern_message($validation_patterns)));
- }
- }
- return $result;
- }
- function wpcf7_get_default_validation_patterns()
- {
- return $patterns = [
- 'letters' => 'A-ZА-Я\s_\-',
- 'digits' => '\d'
- ];
- }
- function wpcf7_field_validation_patterns_filter($patterns = [])
- {
- $valid_patterns = wpcf7_get_default_validation_patterns();
- return array_filter($patterns, function ($pattern) use ($valid_patterns)
- {
- return !empty($valid_patterns[$pattern]);
- });
- }
- function wpcf7_get_field_invalidation_pattern_message($patterns = [])
- {
- $is_letters_invalid = in_array('letters', $patterns);
- $is_digits_invalid = in_array('digits', $patterns);
- if ($is_letters_invalid AND $is_digits_invalid)
- {
- return 'invalid_letters_digits';
- }
- elseif ($is_letters_invalid)
- {
- return 'invalid_letters';
- }
- elseif ($is_digits_invalid)
- {
- return 'invalid_digits';
- }
- }
- /**
- * @param string[] $validation_patterns
- * @param string $value
- * @return bool
- */
- function wpcf7_validate_field_with_pattern($validation_patterns = [], $value)
- {
- $validation_patterns = array_filter((array)$validation_patterns);
- if (!$validation_patterns)
- {
- return TRUE;
- }
- $patterns = wpcf7_get_default_validation_patterns();
- $pattern = [];
- foreach ($validation_patterns AS $pattern_name)
- {
- if (isset($patterns[$pattern_name]))
- {
- $pattern[] = $patterns[$pattern_name];
- }
- }
- $pattern = implode('', $pattern);
- return preg_match("/^[$pattern]+$/iu", $value);
- }
- add_filter('wpcf7_messages', function ($messages)
- {
- $messages['invalid_letters_digits'] = [
- 'description' => __('Allowed letters and digits only.', 'THEME_DOMAIN'),
- 'default' => __('Letters and didgits only.', 'THEME_DOMAIN'),
- ];
- $messages['invalid_letters'] = [
- 'description' => __('Allowed letters only.', 'THEME_DOMAIN'),
- 'default' => __('Letters only.', 'THEME_DOMAIN'),
- ];
- $messages['invalid_digits'] = [
- 'description' => __('Allowed digits only.', 'THEME_DOMAIN'),
- 'default' => __('Digits only.', 'THEME_DOMAIN'),
- ];
- return $messages;
- });
Add Comment
Please, Sign In to add comment