', $html );
return $html;
}
// See `iqfix_wpcf7_add_recaptcha_tag` callback above.
/**
* Display reCaptcha noscript tag should javacript be disabled.
*
* The following function was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN360
*
* @param Array $args
*
* @return String
*/
function iqfix_wpcf7_recaptcha_noscript( $args = '' ) {
$args = wp_parse_args( $args, array(
'sitekey' => '',
) );
if ( empty( $args['sitekey'] ) ) {
return;
}
$source = WPCF7::get_option( 'iqfix_recaptcha_source' );
$source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
$url = add_query_arg( 'k', $args['sitekey'],
sprintf( 'https://www.%s/recaptcha/api/fallback', $source )
);
ob_start();
?>
scan_form_tags( array( 'type' => 'recaptcha' ) );
if ( empty( $tags ) ) {
return $spam;
}
$recaptcha = IQFix_ReCaptcha::get_instance();
if( ! $recaptcha->is_active() ) {
return $spam;
}
$response_token = wpcf7_recaptcha_response();
$spam = ! $recaptcha->verify( $response_token );
return $spam;
}
// See `iqfix_wpcf7_manage_hooks` callback above
/**
* Grab and return the posted reCaptcha response
*
* The following function was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN509
*
* @return String|FALSE
*/
function wpcf7_recaptcha_response() {
if ( isset( $_POST['g-recaptcha-response'] ) ) {
return $_POST['g-recaptcha-response'];
}
return false;
}
/**
* Add [recaptcha] to Contact Form 7 field list
*
* The following function was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN426
*
* @return void
*/
function iqfix_wpcf7_add_tag_generator_recaptcha() {
$tag_generator = WPCF7_TagGenerator::get_instance();
$tag_generator->add(
'recaptcha',
esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ),
'iqfix_wpcf7_tag_generator_recaptcha',
array( 'nameless' => 1 )
);
}
add_action( 'wpcf7_admin_init', 'iqfix_wpcf7_add_tag_generator_recaptcha', 45 );
/**
* Show [recaptcha] field settings
*
* The following function was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN432
*
* @param WPCF7_ContactForm $contact_form
* @param Array $args
*
* @return void
*/
function iqfix_wpcf7_tag_generator_recaptcha( $contact_form, $args = '' ) {
$args = wp_parse_args( $args, array() );
$recaptcha = IQFix_ReCaptcha::get_instance();
if ( ! $recaptcha->is_active() ) {
?>
sitekeys = array( WPCF7_RECAPTCHA_SITEKEY => WPCF7_RECAPTCHA_SECRET );
} else {
$this->sitekeys = WPCF7::get_option( 'recaptcha' );
}
}
/**
* Singleton
*
* The following method was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN10
*
* @return IQFix_ReCaptcha
*/
public static function get_instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Check if reCaptcha is active
*
* The following method was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN26
*
* @return Boolean
*/
public function is_active() {
$sitekey = $this->get_sitekey();
$secret = $this->get_secret( $sitekey );
return $sitekey && $secret;
}
/**
* Get set reCaptcha sitekey
*
* The following method was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN45
*
* @return String|FALSE
*/
public function get_sitekey() {
if ( empty( $this->sitekeys )
or ! is_array( $this->sitekeys ) ) {
return false;
}
$sitekeys = array_keys( $this->sitekeys );
return $sitekeys[0];
}
/**
* Get set reCaptcha secret key
*
* The following method was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN55
*
* @return String|FALSE
*/
public function get_secret( $sitekey ) {
$sitekeys = (array) $this->sitekeys;
if ( isset( $sitekeys[$sitekey] ) ) {
return $sitekeys[$sitekey];
} else {
return false;
}
}
/**
* Verify reCaptcha Response
*
* The following method was not written by IQComputing and is included in
* Contact Form 7 v5.0.5 by Takayuki Miyoshi
* contact-form-7\modules\recaptcha.php LN65
*
* @param String $response_token
*
* @return Boolean $is_human
*/
public function verify( $response_token ) {
$is_human = false;
if ( empty( $response_token ) ) {
return $is_human;
}
$source = WPCF7::get_option( 'iqfix_recaptcha_source' );
$source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
$endpoint = sprintf( 'https://www.%s/recaptcha/api/siteverify', $source );
$sitekey = $this->get_sitekey();
$secret = $this->get_secret( $sitekey );
$request = array(
'body' => array(
'secret' => $secret,
'response' => $response_token,
'remoteip' => $_SERVER['REMOTE_ADDR'],
),
);
$response = wp_safe_remote_post( esc_url_raw( $endpoint ), $request );
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
if ( WP_DEBUG ) {
$this->log( $endpoint, $request, $response );
}
return $is_human;
}
$response = wp_remote_retrieve_body( $response );
$response = json_decode( $response, true );
$is_human = isset( $response['success'] ) && true == $response['success'];
return apply_filters( 'wpcf7_recaptcha_verify_response', $is_human, $response );
}
}
}
add_action( 'init', 'iqfix_recaptcha_class_init', 11 );
/**
* Add some inline CSS for the reCaptcha iframe
*
* @return void
*/
function iqfix_recaptcha_inline_css() {
$iqfix_css = '.wpcf7 .wpcf7-recaptcha iframe {margin-bottom: 0;}';
$iqfix_css .= '.wpcf7 .wpcf7-recaptcha[data-align="center"] > div {margin: 0 auto;}';
$iqfix_css .= '.wpcf7 .wpcf7-recaptcha[data-align="right"] > div {margin: 0 0 0 auto;}';
wp_add_inline_style( 'contact-form-7', $iqfix_css );
}
add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' );