reCAPTCHA::print_script()

Prints the reCAPTCHA script.

Description

This method must only be called once per request.

See also

Source

File: src/modules/protectors/recaptcha.php

	public function print_script() {
		$locale = str_replace( '_', '-', get_locale() );

		// List of reCAPTCHA locales that need to have the format 'xx-XX' (others have format 'xx').
		$special_locales = array(
			'zh-HK',
			'zh-CN',
			'zh-TW',
			'en-GB',
			'fr-CA',
			'de-AT',
			'de-CH',
			'pt-BR',
			'pt-PT',
			'es-419',
		);

		if ( ! in_array( $locale, $special_locales ) ) {
			$locale = substr( $locale, 0, 2 );
		}

		$callback_name = 'torroReCAPTCHAWidgetsInit';

		$recaptcha_script_url = add_query_arg( array(
			'onload'	=> $callback_name,
			'render'	=> 'explicit',
			'hl'		=> $locale,
		), 'https://www.google.com/recaptcha/api.js' );

		?>
		<script type="text/javascript">
			var <?php echo $callback_name; ?> = function() {
				var captchaTags = document.getElementsByClassName( 'torro-recaptcha-placeholder' );
				var captchaTag, captchaWidgetId, captchaForm, i;

				for ( i = 0; i < captchaTags.length; i++ ) {
					captchaTag = captchaTags[ i ];

					captchaWidgetId = window.grecaptcha.render( captchaTag.getAttribute( 'id' ), {
						sitekey: captchaTag.getAttribute( 'data-sitekey' ),
						type: captchaTag.getAttribute( 'data-type' ),
						size: captchaTag.getAttribute( 'data-size' ),
						theme: captchaTag.getAttribute( 'data-theme' )
					});
				}
			};
		</script>
		<script src="<?php echo esc_url( $recaptcha_script_url ); ?>" async defer></script>
		<?php
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.