MC4WP_Form_Validator

The MailChimp for WordPress Lite MC4WP Form Validator class.

Defined (1)

The class is defined in the following location(s).

/includes/class-form-validator.php  
  1. class MC4WP_Form_Validator { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. protected $internal_data = array(); 
  7.  
  8. /** 
  9. * @var array 
  10. */ 
  11. protected $user_data = array(); 
  12.  
  13.  
  14. /** 
  15. * @param array $internal_data Array of fields with their values 
  16. * @param array $user_data 
  17. */ 
  18. public function __construct( $internal_data, $user_data = array() ) { 
  19. $this->internal_data = $internal_data; 
  20. $this->user_data = $user_data; 
  21.  
  22. /** 
  23. * @param array $internal_data Array of fields with their values 
  24. * @param array $user_data 
  25. */ 
  26. public function set_data( $internal_data, $user_data = array() ) { 
  27. $this->internal_data = $internal_data; 
  28. $this->user_data = $user_data; 
  29.  
  30. /** 
  31. * Validate form nonce 
  32. * @return bool 
  33. */ 
  34. public function validate_nonce() { 
  35. // detect caching plugin 
  36. $using_caching = ( defined( 'WP_CACHE' ) && WP_CACHE ); 
  37.  
  38. // validate form nonce, but only if not using caching 
  39. if ( ! $using_caching && ( ! isset( $this->internal_data['form_nonce'] ) || ! wp_verify_nonce( $this->internal_data['form_nonce'], '_mc4wp_form_nonce' ) ) ) { 
  40. return false; 
  41.  
  42. return true; 
  43.  
  44. /** 
  45. * Ensure honeypot is given but not filled 
  46. * @return bool 
  47. */ 
  48. public function validate_honeypot() { 
  49. // ensure honeypot was given but not filled 
  50. if ( ! isset( $this->internal_data['required_but_not_really'] ) || '' !== $this->internal_data['required_but_not_really'] ) { 
  51. return false; 
  52.  
  53. return true; 
  54.  
  55. /** 
  56. * Validate form timestamp, should be at least 1.5 seconds in the past 
  57. * @return bool 
  58. */ 
  59. public function validate_timestamp() { 
  60. // check timestamp difference, token should be generated at least 2 seconds before form submit 
  61. if( ! isset( $this->internal_data['timestamp'] ) || time() < ( intval( $this->internal_data['timestamp'] ) + 1.5 ) ) { 
  62. return false; 
  63.  
  64. return true; 
  65.  
  66. /** 
  67. * Validate Captcha, if form had one. 
  68. * @return bool 
  69. */ 
  70. public function validate_captcha() { 
  71. // check if captcha was present and valid 
  72. if( isset( $this->internal_data['has_captcha'] ) && $this->internal_data['has_captcha'] == 1 && function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) { 
  73. return false; 
  74.  
  75. return true; 
  76.  
  77. /** 
  78. * Ensure email address is given and valid 
  79. * @return bool 
  80. */ 
  81. public function validate_email() { 
  82. // validate email 
  83. if( ! isset( $this->user_data['EMAIL'] ) || ! is_string( $this->user_data['EMAIL'] ) || ! is_email( $this->user_data['EMAIL'] ) ) { 
  84. return false; 
  85.  
  86. return true; 
  87.  
  88. /** 
  89. * Ensure a list is selected or submitted 
  90. * @param $lists 
  91. * @return bool 
  92. */ 
  93. public function validate_lists( array $lists ) { 
  94. return ! empty( $lists ); 
  95.  
  96. /** 
  97. * Run custom validation filter, should return true if valid, or an error code string when invalid 
  98. * @return string|bool 
  99. */ 
  100. public function custom_validation() { 
  101. /** 
  102. * @filter mc4wp_valid_form_request 
  103. * Use this to perform custom form validation. 
  104. * Return true if the form is valid or an error string if it isn't. 
  105. * Use the `mc4wp_form_messages` filter to register custom error messages. 
  106. */ 
  107. $valid_form_request = apply_filters( 'mc4wp_valid_form_request', true, $this->user_data ); 
  108.  
  109. return $valid_form_request; 
  110.  
  111.