LLMS_Google_Recaptcha::render()


Source Source

File: includes/spam/class-llms-recaptcha.php

	public function render() {
		if ( ! $this->is_enabled() ) {
			return;
		}

		if ( is_admin() ) {
			return;
		}

		echo '<input type="hidden" name="g-recaptcha-response" class="llms-google-recaptcha g-recaptcha-response" />';

		wp_enqueue_script(
			'llms-google-recaptcha',
			'https://www.google.com/recaptcha/api.js?render=' . $this->site_key,
			array(),
			null,
			true
		);
		wp_add_inline_script(
			'llms-google-recaptcha',
			'


		document.querySelectorAll( "form" ).forEach( function( form ) {
			if ( form.querySelector( ".llms-google-recaptcha" ) === null ) {
				return;
			}

			function checkout_before_submit( self, callback ) {
				grecaptcha.ready(() => {
					grecaptcha.execute( "' . esc_js( $this->site_key ) . '", { action: "' . esc_js( $this->action ) . '" } ).then( token => {
						self.querySelector("[name=g-recaptcha-response]").value = token;
						callback( true );
					} );
				} );
			}

			if ( window.llms && "llms-product-purchase-form" === form.id ) {
				// If this is the checkout form, use the before_submit method to handle reCAPTCHA.
				if ( window.llms.checkout && window.llms.checkout.add_before_submit_event ) {
					window.llms.checkout.add_before_submit_event( { data: form, handler: checkout_before_submit } );
				} else {
					const interval = setInterval( function() {
						if ( window.llms.checkout && window.llms.checkout.add_before_submit_event ) {
							window.llms.checkout.add_before_submit_event( { data: form, handler: checkout_before_submit } );
							clearInterval( interval );
						}
					}, 100 );
				}

				return;
			}

			form.addEventListener( "submit", function( event ) {
				event.preventDefault();

				if ( form.querySelector( ".llms-password-strength-meter" ) &&
					window.LLMS &&
					window.LLMS.PasswordStrength &&
					window.LLMS.PasswordStrength.get_current_strength_status &&
					! window.LLMS.PasswordStrength.get_current_strength_status() ) {
					console.log( "Password strength validation failed." );
					return false;
				}

				grecaptcha.ready(() => {
					grecaptcha.execute( "' . esc_js( $this->site_key ) . '", { action: "' . esc_js( $this->action ) . '" } ).then( token => {
						form.querySelector( "[name=g-recaptcha-response]" ).value = token;
						form.submit();
					} );
				} );
			} );
		} );
		'
		);
	}

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.