recaptcha.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function ninja_forms_register_field_recaptcha() {
  3. $settings = get_option( "ninja_forms_settings" );
  4. $args = array(
  5. 'name' => __( 'reCAPTCHA', 'ninja-forms' ),
  6. 'sidebar' => 'template_fields',
  7. 'edit_function' => '',
  8. 'display_function' => 'ninja_forms_field_recaptcha_display',
  9. 'save_function' => '',
  10. 'group' => 'standard_fields',
  11. 'default_label' => __( 'Confirm that you are not a bot', 'ninja-forms' ),
  12. 'edit_label' => true,
  13. 'req' => true,
  14. 'edit_label_pos' => true,
  15. 'edit_req' => false,
  16. 'edit_custom_class' => false,
  17. 'edit_help' => false,
  18. 'edit_meta' => false,
  19. 'sidebar' => 'template_fields',
  20. 'edit_conditional' => true,
  21. 'conditional' => array(
  22. 'action' => array(
  23. 'show' => array(
  24. 'name' => __( 'Show This', 'ninja-forms' ),
  25. 'js_function' => 'show',
  26. 'output' => 'hide',
  27. ),
  28. 'hide' => array(
  29. 'name' => __( 'Hide This', 'ninja-forms' ),
  30. 'js_function' => 'hide',
  31. 'output' => 'hide',
  32. ),
  33. ),
  34. ),
  35. 'display_label' => true,
  36. 'process_field' => false,
  37. 'pre_process' => 'ninja_forms_field_recaptcha_pre_process',
  38. );
  39. // show recaptcha field in admin only if site and secret key exists.
  40. if ( !empty( $settings['recaptcha_site_key'] ) && !empty( $settings['recaptcha_secret_key'] ) ) {
  41. ninja_forms_register_field( '_recaptcha', $args );
  42. }
  43. }
  44. add_action( 'init', 'ninja_forms_register_field_recaptcha' );
  45. function ninja_forms_field_recaptcha_display( $field_id, $data, $form_id = '' ) {
  46. $settings = get_option( "ninja_forms_settings" );
  47. $lang = $settings['recaptcha_lang'];
  48. $siteKey = $settings['recaptcha_site_key'];
  49. $field_class = ninja_forms_get_field_class( $field_id, $form_id );
  50. if ( !empty( $siteKey ) ) { ?>
  51. <input id="ninja_forms_field_<?php echo $field_id;?>" name="ninja_forms_field_<?php echo $field_id;?>" type="hidden" class="<?php echo $field_class;?>" value="" rel="<?php echo $field_id;?>" />
  52. <div class="g-recaptcha" data-callback="nf_recaptcha_set_field_value" data-sitekey="<?php echo $siteKey; ?>"></div>
  53. <script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>"> </script>
  54. <script type="text/javascript">
  55. function nf_recaptcha_set_field_value(inpval){
  56. jQuery("#ninja_forms_field_<?php echo $field_id;?>").val(inpval)
  57. }
  58. </script>
  59. <?php
  60. }
  61. }
  62. function ninja_forms_field_recaptcha_pre_process( $field_id, $user_value ) {
  63. global $ninja_forms_processing;
  64. // Set our captcha field id for later processing.
  65. $ninja_forms_processing->update_form_setting( 'recaptcha_field', $field_id );
  66. // Add our captcha processing.
  67. add_action( 'ninja_forms_process', 'nf_field_recaptcha_pre_process', -1 );
  68. }
  69. /**
  70. * Function that actually processes our recaptcha. Runs on a later priority than the field pre_process function
  71. * @since 2.9.27
  72. * @param int $form_id
  73. * @return void
  74. */
  75. function nf_field_recaptcha_pre_process( $form_id ) {
  76. global $ninja_forms_processing;
  77. if ( empty( $_POST['g-recaptcha-response'] ) ) {
  78. $ninja_forms_processing->add_error( 'error_recaptcha', __( 'Please complete the captcha field' , 'ninja-forms' ) );
  79. }else {
  80. $settings = get_option( 'ninja_forms_settings' );
  81. $url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$settings['recaptcha_secret_key'].'&response='.sanitize_text_field( $_POST['g-recaptcha-response'] );
  82. $resp = wp_remote_get( esc_url_raw( $url ) );
  83. if ( !is_wp_error( $resp ) ) {
  84. $body = wp_remote_retrieve_body( $resp );
  85. $response = json_decode( $body );
  86. if ( $response->success===false ) {
  87. if ( !empty( $response->{'error-codes'} ) && $response->{'error-codes'} != 'missing-input-response' ) {
  88. $error= __( 'Please make sure you have entered your Site & Secret keys correctly', 'ninja-forms' );
  89. }else {
  90. $error= __( 'Captcha mismatch. Please enter the correct value in captcha field', 'ninja-forms' );
  91. }
  92. $ninja_forms_processing->add_error( 'error_recaptcha', $error );
  93. }
  94. }
  95. }
  96. }