Spam.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Fields_Spam
  4. */
  5. class NF_Fields_Spam extends NF_Abstracts_Input
  6. {
  7. protected $_name = 'spam';
  8. protected $_type = 'spam';
  9. protected $_section = 'misc';
  10. protected $_icon = 'ban';
  11. protected $_templates = 'textbox';
  12. protected $_settings = array( 'spam_answer' );
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->_nicename = __( 'Anti-Spam', 'ninja-forms' );
  17. // Rename Label setting to Question
  18. $this->_settings[ 'label' ][ 'label' ] = __( 'Question', 'ninja-forms' );
  19. $this->_settings[ 'label_pos' ][ 'label' ] = __( 'Question Position', 'ninja-forms' );
  20. // Manually set Field Key and stop tracking.
  21. $this->_settings[ 'key' ][ 'value' ] = 'spam';
  22. $this->_settings[ 'manual_key' ][ 'value' ] = TRUE;
  23. // Default Required setting to TRUE and hide setting.
  24. $this->_settings[ 'required' ][ 'value' ] = 1;
  25. $this->_settings[ 'required' ][ 'group' ] = '';
  26. add_filter( 'nf_sub_hidden_field_types', array( $this, 'hide_field_type' ) );
  27. }
  28. /**
  29. * Validate
  30. *
  31. * @param $field
  32. * @param $data
  33. * @return array $errors
  34. */
  35. public function validate( $field, $data )
  36. {
  37. $errors = parent::validate( $field, $data );
  38. if(
  39. ( isset( $field[ 'spam_answer' ] ) && isset( $field[ 'value' ] ) )
  40. && ( $field[ 'spam_answer' ] != $field[ 'value' ] )
  41. ){
  42. $errors = __( 'Incorrect Answer', 'ninja-forms' );
  43. }
  44. return $errors;
  45. }
  46. public function get_parent_type()
  47. {
  48. return 'spam';
  49. }
  50. function hide_field_type( $field_types )
  51. {
  52. $field_types[] = $this->_name;
  53. return $field_types;
  54. }
  55. }