WPCF7_RECAPTCHA

The Contact Form 7 WPCF7 RECAPTCHA class.

Defined (1)

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

/modules/recaptcha.php  
  1. class WPCF7_RECAPTCHA extends WPCF7_Service { 
  2.  
  3. const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; 
  4.  
  5. private static $instance; 
  6. private $sitekeys; 
  7.  
  8. public static function get_instance() { 
  9. if ( empty( self::$instance ) ) { 
  10. self::$instance = new self; 
  11.  
  12. return self::$instance; 
  13.  
  14. private function __construct() { 
  15. $this->sitekeys = WPCF7::get_option( 'recaptcha' ); 
  16.  
  17. public function get_title() { 
  18. return __( 'reCAPTCHA', 'contact-form-7' ); 
  19.  
  20. public function is_active() { 
  21. $sitekey = $this->get_sitekey(); 
  22. $secret = $this->get_secret( $sitekey ); 
  23. return $sitekey && $secret; 
  24.  
  25. public function get_categories() { 
  26. return array( 'captcha' ); 
  27.  
  28. public function icon() { 
  29.  
  30. public function link() { 
  31. echo sprintf( '<a href="%1$s">%2$s</a>',  
  32. 'https://www.google.com/recaptcha/intro/index.html',  
  33. 'google.com/recaptcha' ); 
  34.  
  35. public function get_sitekey() { 
  36. if ( empty( $this->sitekeys ) || ! is_array( $this->sitekeys ) ) { 
  37. return false; 
  38.  
  39. $sitekeys = array_keys( $this->sitekeys ); 
  40.  
  41. return $sitekeys[0]; 
  42.  
  43. public function get_secret( $sitekey ) { 
  44. $sitekeys = (array) $this->sitekeys; 
  45.  
  46. if ( isset( $sitekeys[$sitekey] ) ) { 
  47. return $sitekeys[$sitekey]; 
  48. } else { 
  49. return false; 
  50.  
  51. public function verify( $response_token ) { 
  52. $is_human = false; 
  53.  
  54. if ( empty( $response_token ) ) { 
  55. return $is_human; 
  56.  
  57. $url = self::VERIFY_URL; 
  58. $sitekey = $this->get_sitekey(); 
  59. $secret = $this->get_secret( $sitekey ); 
  60.  
  61. $response = wp_safe_remote_post( $url, array( 
  62. 'body' => array( 
  63. 'secret' => $secret,  
  64. 'response' => $response_token,  
  65. 'remoteip' => $_SERVER['REMOTE_ADDR'] ) ) ); 
  66.  
  67. if ( 200 != wp_remote_retrieve_response_code( $response ) ) { 
  68. return $is_human; 
  69.  
  70. $response = wp_remote_retrieve_body( $response ); 
  71. $response = json_decode( $response, true ); 
  72.  
  73. $is_human = isset( $response['success'] ) && true == $response['success']; 
  74. return $is_human; 
  75.  
  76. private function menu_page_url( $args = '' ) { 
  77. $args = wp_parse_args( $args, array() ); 
  78.  
  79. $url = menu_page_url( 'wpcf7-integration', false ); 
  80. $url = add_query_arg( array( 'service' => 'recaptcha' ), $url ); 
  81.  
  82. if ( ! empty( $args) ) { 
  83. $url = add_query_arg( $args, $url ); 
  84.  
  85. return $url; 
  86.  
  87. public function load( $action = '' ) { 
  88. if ( 'setup' == $action ) { 
  89. if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) { 
  90. check_admin_referer( 'wpcf7-recaptcha-setup' ); 
  91.  
  92. $sitekey = isset( $_POST['sitekey'] ) ? trim( $_POST['sitekey'] ) : ''; 
  93. $secret = isset( $_POST['secret'] ) ? trim( $_POST['secret'] ) : ''; 
  94.  
  95. if ( $sitekey && $secret ) { 
  96. WPCF7::update_option( 'recaptcha', array( $sitekey => $secret ) ); 
  97. $redirect_to = $this->menu_page_url( array( 
  98. 'message' => 'success' ) ); 
  99. } elseif ( '' === $sitekey && '' === $secret ) { 
  100. WPCF7::update_option( 'recaptcha', null ); 
  101. $redirect_to = $this->menu_page_url( array( 
  102. 'message' => 'success' ) ); 
  103. } else { 
  104. $redirect_to = $this->menu_page_url( array( 
  105. 'action' => 'setup',  
  106. 'message' => 'invalid' ) ); 
  107.  
  108. wp_safe_redirect( $redirect_to ); 
  109. exit(); 
  110.  
  111. public function admin_notice( $message = '' ) { 
  112. if ( 'invalid' == $message ) { 
  113. echo sprintf( 
  114. '<div class="error notice notice-error is-dismissible"><p><strong>%1$s</strong>: %2$s</p></div>',  
  115. esc_html( __( "ERROR", 'contact-form-7' ) ),  
  116. esc_html( __( "Invalid key values.", 'contact-form-7' ) ) ); 
  117.  
  118. if ( 'success' == $message ) { 
  119. echo sprintf( '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',  
  120. esc_html( __( 'Settings saved.', 'contact-form-7' ) ) ); 
  121.  
  122. public function display( $action = '' ) { 
  123. ?> 
  124. <p><?php echo esc_html( __( "reCAPTCHA is a free service to protect your website from spam and abuse.", 'contact-form-7' ) ); ?></p> 
  125.  
  126. <?php 
  127. if ( 'setup' == $action ) { 
  128. $this->display_setup(); 
  129. return; 
  130.  
  131. if ( $this->is_active() ) { 
  132. $sitekey = $this->get_sitekey(); 
  133. $secret = $this->get_secret( $sitekey ); 
  134. ?> 
  135. <table class="form-table"> 
  136. <tbody> 
  137. <tr> 
  138. <th scope="row"><?php echo esc_html( __( 'Site Key', 'contact-form-7' ) ); ?></th> 
  139. <td class="code"><?php echo esc_html( $sitekey ); ?></td> 
  140. </tr> 
  141. <tr> 
  142. <th scope="row"><?php echo esc_html( __( 'Secret Key', 'contact-form-7' ) ); ?></th> 
  143. <td class="code"><?php echo esc_html( wpcf7_mask_password( $secret ) ); ?></td> 
  144. </tr> 
  145. </tbody> 
  146. </table> 
  147.  
  148. <p><a href="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>" class="button"><?php echo esc_html( __( "Reset Keys", 'contact-form-7' ) ); ?></a></p> 
  149.  
  150. <?php 
  151. } else { 
  152. ?> 
  153. <p><?php echo esc_html( __( "To use reCAPTCHA, you need to install an API key pair.", 'contact-form-7' ) ); ?></p> 
  154.  
  155. <p><a href="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>" class="button"><?php echo esc_html( __( "Configure Keys", 'contact-form-7' ) ); ?></a></p> 
  156.  
  157. <p><?php echo sprintf( esc_html( __( "For more details, see %s.", 'contact-form-7' ) ), wpcf7_link( __( 'https://contactform7.com/recaptcha/', 'contact-form-7' ), __( 'reCAPTCHA', 'contact-form-7' ) ) ); ?></p> 
  158. <?php 
  159.  
  160. public function display_setup() { 
  161. ?> 
  162. <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>"> 
  163. <?php wp_nonce_field( 'wpcf7-recaptcha-setup' ); ?> 
  164. <table class="form-table"> 
  165. <tbody> 
  166. <tr> 
  167. <th scope="row"><label for="sitekey"><?php echo esc_html( __( 'Site Key', 'contact-form-7' ) ); ?></label></th> 
  168. <td><input type="text" aria-required="true" value="" id="sitekey" name="sitekey" class="regular-text code" /></td> 
  169. </tr> 
  170. <tr> 
  171. <th scope="row"><label for="secret"><?php echo esc_html( __( 'Secret Key', 'contact-form-7' ) ); ?></label></th> 
  172. <td><input type="text" aria-required="true" value="" id="secret" name="secret" class="regular-text code" /></td> 
  173. </tr> 
  174. </tbody> 
  175. </table> 
  176.  
  177. <p class="submit"><input type="submit" class="button button-primary" value="<?php echo esc_attr( __( 'Save', 'contact-form-7' ) ); ?>" name="submit" /></p> 
  178. </form> 
  179. <?php