%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' ) );