sitekeys = WPCF7::get_option( 'recaptcha' ); } public function get_title() { return __( 'reCAPTCHA', 'contact-form-7' ); } public function is_active() { $sitekey = $this->get_sitekey(); $secret = $this->get_secret( $sitekey ); return $sitekey && $secret; } public function get_categories() { return array( 'captcha' ); } public function icon() { } public function link() { echo sprintf( '%2$s', 'https://www.google.com/recaptcha/intro/index.html', 'google.com/recaptcha' ); } public function get_sitekey() { if ( empty( $this->sitekeys ) || ! is_array( $this->sitekeys ) ) { return false; } $sitekeys = array_keys( $this->sitekeys ); return $sitekeys[0]; } public function get_secret( $sitekey ) { $sitekeys = (array) $this->sitekeys; if ( isset( $sitekeys[$sitekey] ) ) { return $sitekeys[$sitekey]; } else { return false; } } public function verify( $response_token ) { $is_human = false; if ( empty( $response_token ) ) { return $is_human; } $url = self::VERIFY_URL; $sitekey = $this->get_sitekey(); $secret = $this->get_secret( $sitekey ); $response = wp_safe_remote_post( $url, array( 'body' => array( 'secret' => $secret, 'response' => $response_token, 'remoteip' => $_SERVER['REMOTE_ADDR'], ), ) ); if ( 200 != wp_remote_retrieve_response_code( $response ) ) { return $is_human; } $response = wp_remote_retrieve_body( $response ); $response = json_decode( $response, true ); $is_human = isset( $response['success'] ) && true == $response['success']; return $is_human; } private function menu_page_url( $args = '' ) { $args = wp_parse_args( $args, array() ); $url = menu_page_url( 'wpcf7-integration', false ); $url = add_query_arg( array( 'service' => 'recaptcha' ), $url ); if ( ! empty( $args) ) { $url = add_query_arg( $args, $url ); } return $url; } public function load( $action = '' ) { if ( 'setup' == $action ) { if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) { check_admin_referer( 'wpcf7-recaptcha-setup' ); $sitekey = isset( $_POST['sitekey'] ) ? trim( $_POST['sitekey'] ) : ''; $secret = isset( $_POST['secret'] ) ? trim( $_POST['secret'] ) : ''; if ( $sitekey && $secret ) { WPCF7::update_option( 'recaptcha', array( $sitekey => $secret ) ); $redirect_to = $this->menu_page_url( array( 'message' => 'success', ) ); } elseif ( '' === $sitekey && '' === $secret ) { WPCF7::update_option( 'recaptcha', null ); $redirect_to = $this->menu_page_url( array( 'message' => 'success', ) ); } else { $redirect_to = $this->menu_page_url( array( 'action' => 'setup', 'message' => 'invalid', ) ); } wp_safe_redirect( $redirect_to ); exit(); } } } public function admin_notice( $message = '' ) { if ( 'invalid' == $message ) { echo sprintf( '

%1$s: %2$s

', esc_html( __( "ERROR", 'contact-form-7' ) ), esc_html( __( "Invalid key values.", 'contact-form-7' ) ) ); } if ( 'success' == $message ) { echo sprintf( '

%s

', esc_html( __( 'Settings saved.', 'contact-form-7' ) ) ); } } public function display( $action = '' ) { ?>

display_setup(); return; } if ( $this->is_active() ) { $sitekey = $this->get_sitekey(); $secret = $this->get_secret( $sitekey ); ?>

__( 'CAPTCHA', 'contact-form-7' ), ); foreach ( $categories as $name => $category ) { $integration->add_category( $name, $category ); } $services = array( 'recaptcha' => WPCF7_RECAPTCHA::get_instance(), ); foreach ( $services as $name => $service ) { $integration->add_service( $name, $service ); } } add_action( 'wpcf7_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts' ); function wpcf7_recaptcha_enqueue_scripts() { $url = 'https://www.google.com/recaptcha/api.js'; $url = add_query_arg( array( 'onload' => 'recaptchaCallback', 'render' => 'explicit', ), $url ); wp_register_script( 'google-recaptcha', $url, array(), '2.0', true ); } add_action( 'wp_footer', 'wpcf7_recaptcha_callback_script' ); function wpcf7_recaptcha_callback_script() { if ( ! wp_script_is( 'google-recaptcha', 'enqueued' ) ) { return; } ?> is_active() ) { wpcf7_add_form_tag( 'recaptcha', 'wpcf7_recaptcha_form_tag_handler', array( 'display-block' => true ) ); } } function wpcf7_recaptcha_form_tag_handler( $tag ) { if ( ! wp_script_is( 'google-recaptcha', 'registered' ) ) { wpcf7_recaptcha_enqueue_scripts(); } wp_enqueue_script( 'google-recaptcha' ); $atts = array(); $recaptcha = WPCF7_RECAPTCHA::get_instance(); $atts['data-sitekey'] = $recaptcha->get_sitekey(); $atts['data-type'] = $tag->get_option( 'type', '(audio|image)', true ); $atts['data-size'] = $tag->get_option( 'size', '(compact|normal|invisible)', true ); $atts['data-theme'] = $tag->get_option( 'theme', '(dark|light)', true ); $atts['data-badge'] = $tag->get_option( 'badge', '(bottomright|bottomleft|inline)', true ); $atts['data-tabindex'] = $tag->get_option( 'tabindex', 'int', true ); $atts['data-callback'] = $tag->get_option( 'callback', '', true ); $atts['data-expired-callback'] = $tag->get_option( 'expired_callback', '', true ); $atts['class'] = $tag->get_class_option( wpcf7_form_controls_class( $tag->type, 'g-recaptcha' ) ); $atts['id'] = $tag->get_id_option(); $html = sprintf( '
', wpcf7_format_atts( $atts ) ); $html .= wpcf7_recaptcha_noscript( array( 'sitekey' => $atts['data-sitekey'] ) ); $html = sprintf( '
%s
', $html ); return $html; } function wpcf7_recaptcha_noscript( $args = '' ) { $args = wp_parse_args( $args, array( 'sitekey' => '', ) ); if ( empty( $args['sitekey'] ) ) { return; } $url = add_query_arg( 'k', $args['sitekey'], 'https://www.google.com/recaptcha/api/fallback' ); ob_start(); ?> scan_form_tags( array( 'type' => 'recaptcha' ) ); if ( empty( $tags ) ) { return $spam; } $recaptcha = WPCF7_RECAPTCHA::get_instance(); if ( ! $recaptcha->is_active() ) { return $spam; } $response_token = wpcf7_recaptcha_response(); $spam = ! $recaptcha->verify( $response_token ); return $spam; } add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_recaptcha', 45 ); function wpcf7_add_tag_generator_recaptcha() { $tag_generator = WPCF7_TagGenerator::get_instance(); $tag_generator->add( 'recaptcha', __( 'reCAPTCHA', 'contact-form-7' ), 'wpcf7_tag_generator_recaptcha', array( 'nameless' => 1 ) ); } function wpcf7_tag_generator_recaptcha( $contact_form, $args = '' ) { $args = wp_parse_args( $args, array() ); $recaptcha = WPCF7_RECAPTCHA::get_instance(); if ( ! $recaptcha->is_active() ) { ?>