counter = 1; if ( ! is_user_logged_in() && fusion_library()->get_option( 'recaptcha_comment_form' ) ) { add_action( 'comment_form_after_fields', [ $this, 'render_comment_form_recaptcha' ] ); add_action( 'pre_comment_on_post', [ $this, 'check_recaptcha_comment_form' ] ); } } /** * Render reCAPTCHA HTML on comment forms. * * @access public * @since 7.11.6 * @return void */ public function render_comment_form_recaptcha() { $this->render_field( [ 'counter' => $this->counter, 'element' => 'comments', 'wrapper_class' => 'form-creator-recaptcha', ] ); $recaptcha_error = ( isset( $_GET['recaptcha_error'] ) && '' !== $_GET['recaptcha_error'] ) ? sanitize_text_field( wp_unslash( $_GET['recaptcha_error'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $type = ( isset( $_GET['type'] ) && '' !== $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( $recaptcha_error && $type ) { echo do_shortcode( '[fusion_alert margin_top="20px" type="' . esc_attr( strip_shortcodes( $type ) ) . '"]' . esc_html( strip_shortcodes( $recaptcha_error ) ) . '[/fusion_alert]' ); } } /** * Check reCAPTCHA on comment forms. * * @since 7.11.6 * @access private * @param string $post_id current post id. * @return void */ public function check_recaptcha_comment_form( $post_id ) { if ( ! isset( $_POST['g-recaptcha-response'] ) || empty( $_POST['g-recaptcha-response'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification wp_safe_redirect( add_query_arg( [ 'type' => 'error', 'recaptcha_error' => __( 'Sorry, reCAPTCHA could not verify that you are a human. Please try again.', 'fusion-builder' ), ], esc_url( get_permalink( $post_id ) ) ) ); exit; } if ( isset( $_POST['g-recaptcha-response'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification if ( fusion_library()->get_option( 'recaptcha_public' ) && fusion_library()->get_option( 'recaptcha_private' ) ) { $response = $this->verify(); if ( is_array( $response ) && $response['has_error'] && $response['message'] ) { wp_safe_redirect( add_query_arg( [ 'type' => 'error', 'recaptcha_error' => $response['message'], ], esc_url( get_permalink( $post_id ) ) ) ); exit; } } else { wp_safe_redirect( add_query_arg( [ 'type' => 'error', 'recaptcha_error' => esc_html__( 'reCAPTCHA configuration error. Please check the Global Options settings and your reCAPTCHA account settings.', 'fusion-builder' ), ], esc_url( get_permalink( $post_id ) ) ) ); exit; } } } /** * Render reCAPTCHA field HTML. * * @access public * @since 7.11.6 * @param array $args params. * @return void */ public function render_field( $args = [] ) { $defaults = [ 'color_theme' => fusion_library()->get_option( 'recaptcha_color_scheme' ), 'badge_position' => fusion_library()->get_option( 'recaptcha_badge_position' ), 'tab_index' => '', 'counter' => $this->counter, 'element' => 'form', 'wrapper_class' => 'form-creator-recaptcha', ]; $args = wp_parse_args( $args, $defaults ); ?> get_option( 'recaptcha_public' ) && fusion_library()->get_option( 'recaptcha_private' ) ) : ?> get_option( 'recaptcha_version' ) ) : ?>
counter ) { $this->enqueue_scripts(); } $this->counter++; } /** * Sets the necessary scripts. * * @access public * @since 7.11.6 * @return void */ public function enqueue_scripts() { // Add reCAPTCHA script. $fusion_settings = awb_get_fusion_settings(); if ( fusion_library()->get_option( 'recaptcha_public' ) && fusion_library()->get_option( 'recaptcha_private' ) && ! function_exists( 'recaptcha_get_html' ) && ! class_exists( 'ReCaptcha' ) ) { $recaptcha_script_uri = 'https://www.google.com/recaptcha/api.js?render=explicit&hl=' . get_locale() . '&onload=fusionOnloadCallback'; if ( 'v2' === fusion_library()->get_option( 'recaptcha_version' ) ) { $recaptcha_script_uri = 'https://www.google.com/recaptcha/api.js?hl=' . get_locale(); } wp_enqueue_script( 'recaptcha-api', $recaptcha_script_uri, [], Avada::get_theme_version(), false ); // Inline JS to render reCaptcha. add_action( 'wp_footer', [ $this, 'recaptcha_callback' ], 99 ); } } /** * Generate reCAPTCHA callback. * * @access public * @since 7.11.6 * @return void */ public function recaptcha_callback() { ?> false, 'message' => '', ]; require_once FUSION_LIBRARY_PATH . '/inc/recaptcha/src/autoload.php'; // We use a wrapper class to avoid fatal errors due to syntax differences on PHP 5.2. require_once FUSION_LIBRARY_PATH . '/inc/recaptcha/class-fusion-recaptcha.php'; // Instantiate recaptcha. $re_captcha_wrapper = new Fusion_ReCaptcha( fusion_library()->get_option( 'recaptcha_private' ) ); $re_captcha = $re_captcha_wrapper->recaptcha; if ( $re_captcha && isset( $_POST['g-recaptcha-response'] ) && ! empty( $_POST['g-recaptcha-response'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification $re_captcha_response = null; // Was there a reCAPTCHA response. $post_recaptcha_response = ( isset( $_POST['g-recaptcha-response'] ) ) ? trim( wp_unslash( $_POST['g-recaptcha-response'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification $server_remote_addr = ( isset( $_SERVER['REMOTE_ADDR'] ) ) ? trim( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification if ( 'v2' === fusion_library()->get_option( 'recaptcha_version' ) ) { $re_captcha_response = $re_captcha->verify( $post_recaptcha_response, $server_remote_addr ); } else { $site_url = get_option( 'siteurl' ); $url_parts = wp_parse_url( $site_url ); $site_url = isset( $url_parts['host'] ) ? $url_parts['host'] : $site_url; $re_captcha_response = $re_captcha->setExpectedHostname( apply_filters( 'avada_recaptcha_hostname', $site_url ) )->setExpectedAction( 'contact_form' )->setScoreThreshold( fusion_library()->get_option( 'recaptcha_score' ) )->verify( $post_recaptcha_response, $server_remote_addr ); } // Check the reCAPTCHA response. if ( null === $re_captcha_response || ! $re_captcha_response->isSuccess() ) { $response = [ 'has_error' => true, 'message' => __( 'Sorry, reCAPTCHA could not verify that you are a human. Please try again.', 'fusion-builder' ), ]; $error_codes = []; if ( null !== $re_captcha_response ) { $error_codes = $re_captcha_response->getErrorCodes(); } if ( empty( $error_codes ) || in_array( 'score-threshold-not-met', $error_codes, true ) ) { $response = [ 'has_error' => true, 'message' => __( 'Sorry, reCAPTCHA could not verify that you are a human. Please try again.', 'fusion-builder' ), ]; } } } else { $response = [ 'has_error' => true, 'message' => __( 'Sorry, reCAPTCHA could not verify that you are a human. Please try again.', 'fusion-builder' ), ]; } return $response; } /** * Returns a single instance of the object (singleton). * * @since 7.11.6 * @access public * @return object */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new AWB_Google_Recaptcha(); } return self::$instance; } }