Affiliates reCAPTCHA and input the Site Key and the Secret Key. * Version: 2.0.0 * Author: itthinx * Author URI: http://www.itthinx.com */ if ( !defined( 'ABSPATH' ) ) { exit; } /** * Plugin reCAPTCHA handler for the API v2. * Renders admin and handles captcha. */ class Affiliates_Recaptcha { /** * Adds action and filter hooks. */ public static function init() { add_action( 'affiliates_admin_menu', array( __CLASS__, 'affiliates_admin_menu' ) ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( __CLASS__, 'plugin_action_links' ) ); if ( !apply_filters( 'affiliates_recaptcha_legacy', false ) ) { add_filter( 'affiliates_captcha_get', array( __CLASS__, 'affiliates_captcha_get' ), 10, 2 ); add_filter( 'affiliates_captcha_validate', array( __CLASS__, 'affiliates_captcha_validate' ), 10, 2 ); } else { require_once 'includes/class-affiliates-recaptcha-legacy.php'; add_filter( 'affiliates_captcha_get', array( 'Affiliates_Recaptcha_Legacy', 'affiliates_captcha_get' ), 10, 2 ); add_filter( 'affiliates_captcha_validate', array( 'Affiliates_Recaptcha_Legacy', 'affiliates_captcha_validate' ), 10, 2 ); } } /** * Adds a submenu item to the Affiliates menu for integration options. */ public static function affiliates_admin_menu() { $page = add_submenu_page( 'affiliates-admin', __( 'Affiliates reCAPTCHA', 'affiliates-recaptcha' ), __( 'reCAPTCHA', 'affiliates-recaptcha' ), AFFILIATES_ADMINISTER_OPTIONS, 'affiliates-admin-recaptcha', array( __CLASS__, 'settings' ) ); $pages[] = $page; add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' ); add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' ); } /** * Adds a link to our settings on the plugin entry. * * @param array $links * @return array */ public static function plugin_action_links( $links ) { if ( current_user_can( 'manage_options' ) ) { $_links = array(); $_links[] = sprintf( '%s', esc_url( admin_url( 'admin.php?page=affiliates-admin-recaptcha' ) ), esc_html( __( 'Settings', 'affiliates-recaptcha' ) ) ); $links = $_links + $links; } return $links; } /** * Renders the settings page. */ public static function settings() { if ( !current_user_can( 'manage_options' ) ) { wp_die( esc_html__( '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 '
' . esc_html__( 'Settings Saved.', 'affiliates-recaptcha' ) . '
'; if ( empty( $public_key ) ) { echo '
' . esc_html__( 'The public key is empty, you must input a valid public key.', 'affiliates-recaptcha' ) . '
'; } if ( empty( $public_key ) ) { echo '
' . esc_html__( '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 esc_html__( 'Affiliates reCAPTCHA', 'affiliates-recaptcha' ); echo '

'; echo '
'; echo '
'; echo '
'; echo '

'; echo ''; echo '

'; echo '

'; echo ''; echo '

'; wp_nonce_field( 'admin', 'affiliates-recaptcha', true, true ); echo '
'; echo '
'; echo sprintf( '', esc_html__( 'Save', 'affiliates-recaptcha' ) ); echo ''; echo '
'; echo '
'; 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.', 'affiliates-recaptcha' ) . '
'; } wp_register_script( 'affiliates-recaptcha-api', 'https://www.google.com/recaptcha/api.js', array( ), '2.0.0' ); wp_enqueue_script( 'affiliates-recaptcha-api' ); $field .= '
'; $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 ( isset( $_POST['g-recaptcha-response'] ) ) { $captcha = $_POST['g-recaptcha-response']; } if ( !$captcha ) { $affiliates_recaptcha_error = true; return false; } $response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret=' . get_option( 'affiliates-recaptcha-private-key', '' ) . '&response=' . $captcha . '&remoteip=' . $_SERVER['REMOTE_ADDR'] ); $response = json_decode( $response['body'], true ); if ( $response['success'] !== true ) { return false; } return $result; } } add_action( 'init', array( 'Affiliates_Recaptcha', 'init' ) );