0 ) { $this->setExternalId( get_current_user_id() ); } $this->setClientIpAddress( $this->getIp() ) ->setClientUserAgent( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : null ); if ( ! empty( $this->external_id ) ) { $this->autoFillData( $this->getUserMeta() ); } } private function getUserMeta() { $meta = get_user_meta( $this->getExternalId() ); foreach ( $meta as $k => $v ) { $meta[ $k ] = is_array( $v ) ? $v[0] : $v; } return $meta; } public function getExternalId() { return $this->external_id; } function getIp() { foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) { if ( array_key_exists( $key, $_SERVER ) === true ) { foreach ( explode( ',', sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) ) ) as $ip ) { $ip = trim( $ip ); if ( filter_var( $ip, FILTER_VALIDATE_IP ) !== false ) { return $ip; } } } } return null; } /** * @param string $em */ public function setEmail( $em ) { $this->em = $em; return $this; } /** * @param string $ph */ public function setPhone( $ph ) { $this->ph = $ph; return $this; } /** * @param string $ge */ public function setGender( $ge ) { $this->ge = $ge; return $this; } /** * @param string $db */ public function setDateOfBirth( $db ) { $this->db = $db; return $this; } /** * @param string $ln */ public function setLastName( $ln ) { $this->ln = $ln; return $this; } /** * @param string $fn */ public function setFirstName( $fn ) { $this->fn = $fn; return $this; } /** * @param string $ct */ public function setCity( $ct ) { $this->ct = $ct; return $this; } /** * @param string $st */ public function setState( $st ) { $this->st = $st; return $this; } /** * @param string $zp */ public function setZip( $zp ) { $this->zp = $zp; return $this; } /** * @param string $country */ public function setCountry( $country ) { $this->country = $country; return $this; } /** * @param string $external_id */ public function setExternalId( $external_id ) { $this->external_id = $external_id; return $this; } /** * @param string $fbc */ public function setFbc( $fbc ) { $this->fbc = $fbc; return $this; } /** * @param string $fbp */ public function setFbp( $fbp ) { $this->fbp = $fbp; return $this; } /** * @param string $subscription_id */ public function setSubscriptionId( $subscription_id ) { $this->subscription_id = $subscription_id; return $this; } /** * @param string $lead_id */ public function setLeadId( $lead_id ) { $this->lead_id = $lead_id; return $this; } /** * @param string $fb_login_id */ public function setFbLoginId( $fb_login_id ) { $this->fb_login_id = $fb_login_id; return $this; } /** * @param string $client_ip_address */ public function setClientIpAddress( $client_ip_address ) { $this->client_ip_address = $client_ip_address; return $this; } /** * @param string $client_user_agent */ public function setClientUserAgent( $client_user_agent ) { $this->client_user_agent = $client_user_agent; return $this; } public function autoFillData( $postedData ) { $fields = array( 'billing_first_name' => 'setFirstName', 'billing_last_name' => 'setLastName', 'billing_city' => 'setCity', 'billing_phone' => 'setPhone', 'billing_state' => 'setState', 'billing_postcode' => 'setZip', 'billing_country' => 'setCountry', 'billing_email' => 'setEmail', ); foreach ( $fields as $fieldName => $method ) { if ( ! empty( $postedData[ $fieldName ] ) ) { $this->{$method}( $postedData[ $fieldName ] ); } else { $fieldName = str_replace( 'billing_', '', $fieldName ); if ( ! empty( $postedData[ $fieldName ] ) ) { $this->{$method}( $postedData[ $fieldName ] ); } } } if ( empty( $this->em ) && ! empty( $this->external_id ) ) { $user_info = get_userdata( $this->external_id ); if ( ! empty( $user_info->user_email ) ) { $this->setEmail( $user_info->user_email ); } } return $this; } private function hash( $val ) { return hash( 'sha256', $val ); } public function __toArray() { return array( 'external_id' => $this->external_id, 'em' => empty( $this->em ) ? null : $this->hash( $this->em ), 'ph' => empty( $this->ph ) ? null : $this->hash( $this->ph ), 'ge' => empty( $this->ge ) ? null : $this->hash( $this->ge ), 'db' => empty( $this->db ) ? null : $this->hash( $this->db ), 'ln' => empty( $this->ln ) ? null : $this->hash( $this->ln ), 'fn' => empty( $this->fn ) ? null : $this->hash( $this->fn ), 'ct' => empty( $this->ct ) ? null : $this->hash( $this->ct ), 'st' => empty( $this->st ) ? null : $this->hash( $this->st ), 'zp' => empty( $this->zp ) ? null : $this->hash( $this->zp ), 'country' => empty( $this->country ) ? null : $this->hash( $this->country ), 'fbc' => $this->fbc, 'fbp' => $this->fbp, 'subscription_id' => $this->subscription_id, 'lead_id' => $this->lead_id, 'fb_login_id' => $this->fb_login_id, 'client_ip_address' => $this->client_ip_address, 'client_user_agent' => $this->client_user_agent, ); } } }