Captcha()

Description #

The reCaptcha class.

Source #

File: addons/recaptcha/recaptcha.php

class Captcha extends \AnsPress\Singleton {
	/**
	 * Instance of this class.
	 *
	 * @var     object
	 * @since 4.1.8
	 */
	protected static $instance = null;

	/**
	 * Initialize the plugin by setting localization and loading public scripts
	 * and styles.
	 *
	 * @since 2.4.8 Removed `$ap` args.
	 */
	protected function __construct() {
		ap_add_default_options(
			array(
				'recaptcha_method'        => 'post',
				'recaptcha_exclude_roles' => array( 'ap_moderator' => 1 ),
			)
		);

		anspress()->add_filter( 'ap_settings_menu_features_groups', $this, 'add_to_settings_page' );
		anspress()->add_action( 'ap_form_options_features_recaptcha', $this, 'options' );
		anspress()->add_action( 'wp_enqueue_scripts', $this, 'enqueue_scripts' );
		anspress()->add_action( 'ap_question_form_fields', $this, 'ap_question_form_fields', 10, 2 );
		anspress()->add_action( 'ap_answer_form_fields', $this, 'ap_question_form_fields', 10, 2 );
		anspress()->add_action( 'ap_comment_form_fields', $this, 'ap_question_form_fields', 10, 2 );
	}

	/**
	 * Enqueue script.
	 *
	 * @return void
	 * @since 4.1.9
	 */
	public function enqueue_scripts() {
		wp_enqueue_script( 'ap-recaptcha', ANSPRESS_URL . 'addons/recaptcha/script.js', array(), AP_VERSION, true );
	}

	/**
	 * Add tags settings to features settings page.
	 *
	 * @param array $groups Features settings group.
	 * @return array
	 * @since 4.2.0
	 */
	public function add_to_settings_page( $groups ) {
		$groups['recaptcha'] = array(
			'label' => __( 'reCaptcha', 'anspress-question-answer' ),
		);

		return $groups;
	}

	/**
	 * Register Categories options
	 */
	public function options() {
		global $wp_roles;
		$opt = ap_opt();

		$roles = array();
		foreach ( $wp_roles->roles as $key => $role ) {
			$roles[ $key ] = $role['name'];
		}

		$form = array(
			'fields' => array(
				'recaptcha_site_key'      => array(
					'label' => __( 'Recaptcha site key', 'anspress-question-answer' ),
					'desc'  => __( 'Enter your site key, if you dont have it get it from here https://www.google.com/recaptcha/admin', 'anspress-question-answer' ),
					'value' => $opt['recaptcha_site_key'],
				),
				'recaptcha_secret_key'    => array(
					'label' => __( 'Recaptcha secret key', 'anspress-question-answer' ),
					'desc'  => __( 'Enter your secret key', 'anspress-question-answer' ),
					'value' => $opt['recaptcha_secret_key'],
				),
				'recaptcha_method'        => array(
					'label'   => __( 'Recaptcha Method', 'anspress-question-answer' ),
					'desc'    => __( 'Select method to use when verification keeps failing', 'anspress-question-answer' ),
					'type'    => 'select',
					'options' => array(
						'curl' => 'CURL',
						'post' => 'POST',
					),
					'value'   => $opt['recaptcha_method'],
				),
				'recaptcha_exclude_roles' => array(
					'label'   => __( 'Hide reCaptcha for roles', 'anspress-question-answer' ),
					'desc'    => __( 'Select roles for which reCaptcha will be hidden.', 'anspress-question-answer' ),
					'type'    => 'checkbox',
					'options' => $roles,
					'value'   => $opt['recaptcha_exclude_roles'],
				),
			),
		);

		return $form;
	}

	/**
	 * Add captcha field in question and answer form.
	 *
	 * @param array $form Form arguments.
	 * @return array
	 * @since 4.1.0
	 */
	public function ap_question_form_fields( $form ) {
		if ( ap_show_captcha_to_user() ) {
			$form['fields']['captcha'] = array(
				'label' => __( 'Prove that you are a human', 'anspress-question-answer' ),
				'type'  => 'captcha',
				'order' => 100,
			);
		}

		return $form;
	}

}

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