NF_Fields_Recaptcha

Class NF_Fields_CreditCard.

Defined (1)

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

/includes/Fields/Recaptcha.php  
  1. class NF_Fields_Recaptcha extends NF_Abstracts_Field 
  2. protected $_name = 'recaptcha'; 
  3.  
  4. protected $_type = 'recaptcha'; 
  5.  
  6. protected $_section = 'misc'; 
  7.  
  8. protected $_icon = 'filter'; 
  9.  
  10. protected $_templates = 'recaptcha'; 
  11.  
  12. protected $_test_value = ''; 
  13.  
  14. protected $_settings = array( 'label' ); 
  15.  
  16. public function __construct() 
  17. parent::__construct(); 
  18.  
  19. $this->_nicename = __( 'Recaptcha', 'ninja-forms' ); 
  20.  
  21. $this->_settings[ 'wrapper_class '] = array( 
  22. 'name' => 'wrapper_class',  
  23. 'type' => 'textbox',  
  24. 'placeholder' => '',  
  25. 'label' => __( 'Wrapper Class', 'ninja-forms' ),  
  26. 'width' => 'full',  
  27. 'value' => '',  
  28. 'group' => 'primary',  
  29. 'use_merge_tags' => FALSE,  
  30. 'help' => __( 'Adds an extra class to your field wrapper.', 'ninja-forms' ),  
  31. ); 
  32.  
  33. add_filter( 'nf_sub_hidden_field_types', array( $this, 'hide_field_type' ) ); 
  34.  
  35. public function localize_settings( $settings, $form ) { 
  36. $settings['site_key'] = Ninja_Forms()->get_setting( 'recaptcha_site_key' ); 
  37. $settings['theme'] = Ninja_Forms()->get_setting( 'recaptcha_theme' ); 
  38. $settings['theme'] = ( $settings['theme'] ) ? $settings['theme'] : 'light'; 
  39. $settings['lang'] = Ninja_Forms()->get_setting( 'recaptcha_lang' ); 
  40. return $settings; 
  41.  
  42. public function validate( $field, $data ) { 
  43. if ( empty( $field['value'] ) ) { 
  44. return array( __( 'Please complete the recaptcha', 'ninja-forms' ) ); 
  45.  
  46. $secret_key = Ninja_Forms()->get_setting( 'recaptcha_secret_key' ); 
  47. $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret_key . '&response='.sanitize_text_field( $field['value'] ); 
  48. $resp = wp_remote_get( esc_url_raw( $url ) ); 
  49.  
  50. if ( !is_wp_error( $resp ) ) { 
  51. $body = wp_remote_retrieve_body( $resp ); 
  52. $response = json_decode( $body ); 
  53. if ( $response->success === false ) { 
  54. if ( !empty( $response->{'error-codes'} ) && $response->{'error-codes'} != 'missing-input-response' ) { 
  55. return array( __( 'Please make sure you have entered your Site & Secret keys correctly', 'ninja-forms' ) ); 
  56. }else { 
  57. return array( __( 'Captcha mismatch. Please enter the correct value in captcha field', 'ninja-forms' ) ); 
  58.  
  59. function hide_field_type( $field_types ) 
  60. $field_types[] = $this->_name; 
  61. return $field_types;