Affiliates reCAPTCHA and input the Public Key and the Private Key.
* Version: 1.0.0
* Author: itthinx
* Author URI: http://www.itthinx.com
*/
class Affiliates_Recaptcha {
/**
* Adds action and filter hooks.
*/
public static function init() {
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
add_filter( 'affiliates_captcha_get', array( __CLASS__, 'affiliates_captcha_get' ), 10, 2 );
add_filter( 'affiliates_captcha_validate', array( __CLASS__, 'affiliates_captcha_validate' ), 10, 2 );
}
/**
* Adds a settings page.
*/
public static function admin_menu() {
add_options_page(
'Affiliates reCAPTCHA',
'Affiliates reCAPTCHA',
'manage_options',
'affiliates-recaptcha',
array( __CLASS__, 'settings' )
);
}
/**
* Renders the settings page.
*/
public static function settings() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'Access denied.', 'affiliates-recaptcha' ) );
}
if ( isset( $_POST['action'] ) && ( $_POST['action'] == 'save' ) && wp_verify_nonce( $_POST['affiliates-recaptcha'], 'admin' ) ) {
$public_key = !empty( $_POST['public_key'] ) ? trim( strip_tags( $_POST['public_key'] ) ) : '';
$private_key = !empty( $_POST['private_key'] ) ? trim( strip_tags( $_POST['private_key'] ) ) : '';
delete_option( 'affiliates-recaptcha-public-key' );
delete_option( 'affiliates-recaptcha-private-key' );
add_option( 'affiliates-recaptcha-public-key', $public_key, '', 'no' );
add_option( 'affiliates-recaptcha-private-key', $private_key, '', 'no' );
echo
'
' .
__( 'Settings Saved.', 'affiliates-recaptcha' ) .
'
';
if ( empty( $public_key ) ) {
echo '' .
__( 'The public key is empty, you must input a valid public key.', 'affiliates-recaptcha' ) .
'
';
}
if ( empty( $public_key ) ) {
echo
'' .
__( 'The private key is empty, you must input a valid private key.', 'affiliates-recaptcha' ) .
'
';
}
}
$public_key = get_option( 'affiliates-recaptcha-public-key', '' );
$private_key = get_option( 'affiliates-recaptcha-private-key', '' );
echo '';
echo __( 'Affiliates reCAPTCHA', 'affiliates-recaptcha' );
echo '
';
echo '';
}
/**
* Renders the captcha field.
*
* @param string $field
* @param string $value
* @return string
*/
public static function affiliates_captcha_get( $field, $value ) {
global $affiliates_recaptcha_error;
if ( !isset( $affiliates_recaptcha_error ) ) {
$affiliates_recaptcha_error = null;
}
$field_error = '';
if ( !empty( $affiliates_recaptcha_error ) ) {
$field_error = '' . __( 'Please solve the captcha to proof that you are human.', 'affiliates-recaptcha' ) . '
';
}
if ( !function_exists( 'recaptcha_get_html' ) ) {
require_once 'includes/recaptcha/recaptchalib.php';
}
$field .= recaptcha_get_html( get_option( 'affiliates-recaptcha-public-key', '' ), $affiliates_recaptcha_error );
$field .= apply_filters(
'affiliates_recaptcha_field_css',
''
);
$field .= apply_filters( 'affiliates_recaptcha_field_error', $field_error );
return $field;
}
/**
* Validates the captcha.
*
* @param boolean $result
* @param string $field_value
* @return boolean
*/
public static function affiliates_captcha_validate( $result, $field_value ) {
global $affiliates_recaptcha_error;
if ( !function_exists( 'recaptcha_check_answer' ) ) {
require_once 'includes/recaptcha/recaptchalib.php';
}
$response = recaptcha_check_answer( get_option( 'affiliates-recaptcha-private-key' ), $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] );
if ( !$response->is_valid ) {
$affiliates_recaptcha_error = $response->error;
} else {
$affiliates_recaptcha_error = null;
}
return $result && $response->is_valid;
}
}
add_action( 'init', array( 'Affiliates_Recaptcha', 'init' ) );