Guest User

Untitled

a guest
Mar 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. <?php
  2. <?
  3. // Hooks only for version 5 only, may be for previous version also works
  4. if (!defined('WPCF7_VERSION') OR intval(WPCF7_VERSION) !== 5)
  5. {
  6. return;
  7. }
  8.  
  9. add_filter( 'wpcf7_validate_text', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  10. add_filter( 'wpcf7_validate_text*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  11. add_filter( 'wpcf7_validate_email', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  12. add_filter( 'wpcf7_validate_email*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  13. add_filter( 'wpcf7_validate_url', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  14. add_filter( 'wpcf7_validate_url*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  15. add_filter( 'wpcf7_validate_tel', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  16. add_filter( 'wpcf7_validate_tel*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 );
  17.  
  18. /**
  19. * @param WPCF7_Validation $result
  20. * @param WPCF7_FormTag $tag
  21. * @return mixed
  22. */
  23. function wpcf7_text_allowed_chars_validation_filter($result, $tag)
  24. {
  25. $name = $tag->name;
  26.  
  27. $value = isset($_POST[$name])
  28. ? trim(wp_unslash(strtr((string)$_POST[$name], "\n", " ")))
  29. : '';
  30.  
  31. if ($validation_patterns = $tag->get_option('valid-chars'))
  32. {
  33. $validation_patterns = wpcf7_field_validation_patterns_filter($validation_patterns);
  34. }
  35.  
  36. if ($validation_patterns)
  37. {
  38. if (!wpcf7_validate_field_with_pattern($validation_patterns, $value))
  39. {
  40. $result->invalidate($tag, wpcf7_get_message(wpcf7_get_field_invalidation_pattern_message($validation_patterns)));
  41. }
  42. }
  43.  
  44. return $result;
  45. }
  46.  
  47. function wpcf7_get_default_validation_patterns()
  48. {
  49. return $patterns = [
  50. 'letters' => 'A-ZА-Я\s_\-',
  51. 'digits' => '\d'
  52. ];
  53. }
  54.  
  55. function wpcf7_field_validation_patterns_filter($patterns = [])
  56. {
  57. $valid_patterns = wpcf7_get_default_validation_patterns();
  58.  
  59. return array_filter($patterns, function ($pattern) use ($valid_patterns)
  60. {
  61. return !empty($valid_patterns[$pattern]);
  62. });
  63. }
  64.  
  65. function wpcf7_get_field_invalidation_pattern_message($patterns = [])
  66. {
  67. $is_letters_invalid = in_array('letters', $patterns);
  68. $is_digits_invalid = in_array('digits', $patterns);
  69.  
  70. if ($is_letters_invalid AND $is_digits_invalid)
  71. {
  72. return 'invalid_letters_digits';
  73. }
  74. elseif ($is_letters_invalid)
  75. {
  76. return 'invalid_letters';
  77. }
  78. elseif ($is_digits_invalid)
  79. {
  80. return 'invalid_digits';
  81. }
  82. }
  83.  
  84. /**
  85. * @param string[] $validation_patterns
  86. * @param string $value
  87. * @return bool
  88. */
  89. function wpcf7_validate_field_with_pattern($validation_patterns = [], $value)
  90. {
  91. $validation_patterns = array_filter((array)$validation_patterns);
  92.  
  93. if (!$validation_patterns)
  94. {
  95. return TRUE;
  96. }
  97.  
  98. $patterns = wpcf7_get_default_validation_patterns();
  99.  
  100. $pattern = [];
  101.  
  102. foreach ($validation_patterns AS $pattern_name)
  103. {
  104. if (isset($patterns[$pattern_name]))
  105. {
  106. $pattern[] = $patterns[$pattern_name];
  107. }
  108. }
  109.  
  110. $pattern = implode('', $pattern);
  111.  
  112. return preg_match("/^[$pattern]+$/iu", $value);
  113. }
  114.  
  115. add_filter('wpcf7_messages', function ($messages)
  116. {
  117. $messages['invalid_letters_digits'] = [
  118. 'description' => __('Allowed letters and digits only.', 'THEME_DOMAIN'),
  119. 'default' => __('Letters and didgits only.', 'THEME_DOMAIN'),
  120. ];
  121.  
  122. $messages['invalid_letters'] = [
  123. 'description' => __('Allowed letters only.', 'THEME_DOMAIN'),
  124. 'default' => __('Letters only.', 'THEME_DOMAIN'),
  125. ];
  126.  
  127. $messages['invalid_digits'] = [
  128. 'description' => __('Allowed digits only.', 'THEME_DOMAIN'),
  129. 'default' => __('Digits only.', 'THEME_DOMAIN'),
  130. ];
  131.  
  132. return $messages;
  133. });
Add Comment
Please, Sign In to add comment