<?php



if (!defined( 'ABSPATH' ) ) {

  exit; // Exit if accessed directly.

}



class SensFRX_WP_Login_Failed {

  public function wp_login_failed_track($username, WP_Error $error ) {

		$device_id = '';

		if ( isset($_POST['device_id']) && isset($_POST['woocommerce-login-nonce']) && wp_verify_nonce(sanitize_text_field($_POST['woocommerce-login-nonce']), 'woocommerce-login') ) {

			$device_id = sanitize_text_field($_POST['device_id']);

		}

		if (empty($device_id)) {

			$device_id = null;

			$device_id_1 = null;

			$device_id_2 = null;

			$device_id_3 = null;

			$device_id_4 = null;

			$res_bot = null;

			if (isset($_COOKIE['device0'])) {   

				$device_id_1 = sanitize_text_field($_COOKIE['device0']);

			} 

			if (isset($_COOKIE['device1'])) {   

				$device_id_2 = sanitize_text_field($_COOKIE['device1']);

			}  

			if (isset($_COOKIE['device2'])) {   

				$device_id_3 = sanitize_text_field($_COOKIE['device2']);

			}  

			if (isset($_COOKIE['device3'])) {   

				$device_id_4 = sanitize_text_field($_COOKIE['device3']);

			}   

			$device_id = $device_id_1 . $device_id_2 . $device_id_3 . $device_id_4;

			if ( empty($device_id) ) {

				$device_id = null;

			} 

		}

		$options = get_option('sensfrx_options', SensFRX::default_options());

		if (filter_var($username, FILTER_VALIDATE_EMAIL)) {

			$user = get_user_by('email', $username);

		} else {

			$user = get_user_by('login', $username);

		}

		require_once(SENSFRX_DIR . 'sensfrx-php-sdk/SensFRX/autoload.php');

		$obj = array();

		if (isset($options['sensfrx_property_id']) && isset($options['sensfrx_property_secret'])) {

			$obj = new SensFRX\SensFRX([

				'property_id' => $options['sensfrx_property_id'],

				'property_secret' => $options['sensfrx_property_secret']

			]);

		}

		if (isset($options['sensfrx_property_id']) && isset($options['sensfrx_property_secret'])) {

			$obj = new SensFRX\SensFRX([

				'property_id' => $options['sensfrx_property_id'],

				'property_secret' => $options['sensfrx_property_secret']

			]);

			if ($user) {

				$res = $obj->loginAttempt('login_failed', $user->ID, $device_id, array('email'=>$user->data->user_email,'username'=>$user->data->user_login));

			} else {

				$res = $obj->loginAttempt('login_failed', '', $device_id);

			}

		}

	}

}