Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak - Save the user IP on the user meta. #609

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions includes/class-ur-form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function init() {
add_action( 'wp_loaded', array( __CLASS__, 'process_lost_password' ), 20 );
add_action( 'wp_loaded', array( __CLASS__, 'process_reset_password' ), 20 );
add_action( 'user_registration_before_customer_login_form', array( __CLASS__, 'export_confirmation_request' ) );
add_action( 'user_registration_save_profile_details', array( __CLASS__, 'ur_update_user_ip_after_profile_update' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -711,6 +712,19 @@ public function create( $title = '', $template = 'blank', $args = array(), $data

return $form_id;
}

/**
* Update the user's IP address in form data if not already present.
*
* @since 3.0.4.1
*
* @param int $user_id The ID of the User.
* @param int $form_id The ID of the form.
*/
public static function ur_update_user_ip_after_profile_update( $user_id, $form_id ) {
$user_ip = ur_get_ip_address();
update_user_meta( $user_id, 'ur_user_ip', $user_ip );
}
}

UR_Form_Handler::init();
9 changes: 9 additions & 0 deletions includes/frontend/class-ur-frontend-form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ public static function ur_update_user_meta( $user_id, $valid_form_data, $form_id
}
}
update_user_meta( $user_id, 'ur_form_id', $form_id );

/**
* Saving the user ip in user meta.
*
* @since 3.0.4.1
*/
$user_ip = ur_get_ip_address();
update_user_meta( $user_id, 'ur_user_ip', $user_ip );

$current_language = ur_get_current_language();
update_user_meta( $user_id, 'ur_registered_language', $current_language );
}
Expand Down
40 changes: 30 additions & 10 deletions includes/functions-ur-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1558,9 +1558,9 @@ function ur_get_recaptcha_node( $context, $recaptcha_enabled = false ) {
$recaptcha_site_secret = get_option( 'user_registration_captcha_setting_recaptcha_site_secret_hcaptcha' );
$enqueue_script = 'ur-recaptcha-hcaptcha';
} elseif ( 'cloudflare' === $recaptcha_type ) {
$recaptcha_site_key = get_option( 'user_registration_captcha_setting_recaptcha_site_key_cloudflare' );
$theme_mod = get_option( 'user_registration_captcha_setting_recaptcha_cloudflare_theme' );
$enqueue_script = 'ur-recaptcha-cloudflare';
$recaptcha_site_key = get_option( 'user_registration_captcha_setting_recaptcha_site_key_cloudflare' );
$theme_mod = get_option( 'user_registration_captcha_setting_recaptcha_cloudflare_theme' );
$enqueue_script = 'ur-recaptcha-cloudflare';
}
static $rc_counter = 0;

Expand Down Expand Up @@ -2836,7 +2836,7 @@ function user_registration_install_pages_notice() {
}

if ( ! empty( $myaccount_page ) ) {
$matched = ur_find_my_account_in_page( $myaccount_page->ID );
$matched = ur_find_my_account_in_page( $myaccount_page->ID );
}

if ( 0 === $matched ) {
Expand Down Expand Up @@ -2865,7 +2865,7 @@ function user_registration_install_pages_notice() {
*/
function ur_find_my_account_in_page( $login_page_id ) {
global $wpdb;
$post_table = $wpdb->prefix . 'posts';
$post_table = $wpdb->prefix . 'posts';
$post_meta_table = $wpdb->prefix . 'postmeta';

$matched = $wpdb->get_var(
Expand Down Expand Up @@ -3481,7 +3481,7 @@ function ur_process_login( $nonce_value ) {
}

if ( ur_is_ajax_login_enabled() ) {
$recaptcha_value = $captcha_response;
$recaptcha_value = $captcha_response;
}

if ( $recaptcha_enabled && ! empty( $site_key ) && ! empty( $secret_key ) ) {
Expand All @@ -3494,16 +3494,16 @@ function ur_process_login( $nonce_value ) {
throw new Exception( '<strong>' . esc_html__( 'ERROR:', 'user-registration' ) . '</strong>' . esc_html__( 'Error on hCaptcha. Contact your site administrator.', 'user-registration' ) );
}
} elseif ( 'cloudflare' === $recaptcha_type ) {
$url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$params = array(
$url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$params = array(
'method' => 'POST',
'body' => array(
'secret' => $secret_key,
'response' => $recaptcha_value,
),
);
$data = wp_safe_remote_post( $url, $params );
$data = json_decode( wp_remote_retrieve_body( $data ) );
$data = wp_safe_remote_post( $url, $params );
$data = json_decode( wp_remote_retrieve_body( $data ) );

if ( empty( $data->success ) ) {
throw new Exception( '<strong>' . esc_html__( 'ERROR:', 'user-registration' ) . '</strong>' . esc_html__( 'Error on Cloudflare. Contact your site administrator.', 'user-registration' ) );
Expand Down Expand Up @@ -3924,3 +3924,23 @@ function user_registration_conditional_user_meta_filter( $valid_form_data, $user

add_filter( 'user_registration_before_user_meta_update', 'user_registration_conditional_user_meta_filter', 10, 3 );
add_filter( 'user_registration_before_save_profile_details', 'user_registration_conditional_user_meta_filter', 10, 3 );

if ( ! function_exists( 'ur_get_ip_address' ) ) {
/**
* Get current user IP Address.
*
* @return string
*/
function ur_get_ip_address() {
if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { // WPCS: input var ok, CSRF ok.
return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ); // WPCS: input var ok, CSRF ok.
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { // WPCS: input var ok, CSRF ok.
// Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
// Make sure we always only send through the first IP in the list which should always be the client IP.
return (string) rest_is_ip_address( trim( current( preg_split( '/[,:]/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) ); // WPCS: input var ok, CSRF ok.
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) { // @codingStandardsIgnoreLine
return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); // @codingStandardsIgnoreLine
}
return '';
}
}