AnsPress_reCcaptcha::ap_ask_form_fields( $args,  $editing )

Description #

Source #

File: addons/free/recaptcha.php

	public static function ap_ask_form_fields( $args, $editing ) {
		global $editing_post;

		if ( ap_show_captcha_to_user() ) {
			// Show recpatcha if key exists and enabled.
			if ( ap_opt( 'recaptcha_site_key' ) == '' ) {
				$html = '<div class="ap-notice red">' . __( 'reCaptach keys missing, please add keys', 'anspress-question-answer' ) . '</div>';
			} else {

				$html = '<div class="g-recaptcha" id="recaptcha" data-sitekey="' . ap_opt( 'recaptcha_site_key' ) . '"></div>';

				$html .= '<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=' . get_locale() . '&onload=onloadCallback&render=explicit" async defer></script>';

				ob_start();
				?>
					<script type="text/javascript">
						var onloadCallback = function() {
						widgetId1 = grecaptcha.render("recaptcha", {
							"sitekey" : "<?php echo ap_opt( 'recaptcha_site_key' ); ?>"
							});
						};

						jQuery(document).ready(function(){
							// Rest widget after answer form get submitted
							if(typeof AnsPress !== 'undefined'){
								AnsPress.on('answerFormPosted', function(){
									if(typeof grecaptcha !== 'undefined')
										grecaptcha.reset(widgetId1);
								});
							}
						});

					</script>
				<?php
				$html .= ob_get_clean();
			}

			$args['fields'][] = array(
				'name'  => 'captcha',
				'type'  => 'custom',
				'order' => 100,
				'html' 	=> $html,
			);
		}

		return $args;
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment