|
|
|
|
|
|
|
|
//does not work for too many users or employees |
|
|
//does not work for too many users or employees |
|
|
public function sync_users(){ |
|
|
public function sync_users(){ |
|
|
$this->sync_clients(); |
|
|
$this->sync_clients(); |
|
|
//$this->sync_employees(); |
|
|
|
|
|
|
|
|
$this->sync_employees(); |
|
|
} |
|
|
} |
|
|
private function sync_clients(){ |
|
|
private function sync_clients(){ |
|
|
$contacts = $this->getClients($this->clientgroup); |
|
|
$contacts = $this->getClients($this->clientgroup); |
|
|
|
|
|
|
|
|
private function ensure_staff_exists($employee) |
|
|
private function ensure_staff_exists($employee) |
|
|
{ |
|
|
{ |
|
|
$login = $employee->getEmployeeID(); |
|
|
$login = $employee->getEmployeeID(); |
|
|
$xero_employee = $this->getEmployee($login); |
|
|
|
|
|
$args = $this->xero_employee_profile($xero_employee); |
|
|
|
|
|
$user = get_user_by('login', $login); |
|
|
$user = get_user_by('login', $login); |
|
|
if ($user === false){ |
|
|
if ($user === false){ |
|
|
|
|
|
$xero_employee = $this->getEmployee($login); |
|
|
|
|
|
$args = $this->xero_employee_profile($xero_employee); |
|
|
$id = wp_insert_user($args); |
|
|
$id = wp_insert_user($args); |
|
|
$user = get_user_by('ID', $id); |
|
|
$user = get_user_by('ID', $id); |
|
|
}else{ |
|
|
}else{ |
|
|
if ($this->is_too_close_to_sync($user)){ |
|
|
if ($this->is_too_close_to_sync($user)){ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
$xero_employee = $this->getEmployee($login); |
|
|
|
|
|
$args = $this->xero_employee_profile($xero_employee); |
|
|
$args['ID'] = $user->ID; |
|
|
$args['ID'] = $user->ID; |
|
|
unset($args['user_pass']); |
|
|
unset($args['user_pass']); |
|
|
wp_update_user($args); |
|
|
wp_update_user($args); |
|
|
|
|
|
|
|
|
$lastsync = $this->get_last_sync($user->ID); |
|
|
$lastsync = $this->get_last_sync($user->ID); |
|
|
$now = time(); |
|
|
$now = time(); |
|
|
$diff = $now - (int) $lastsync; |
|
|
$diff = $now - (int) $lastsync; |
|
|
echo "lastsync = $lastsync, now = $now , diff = $diff; \n" ; |
|
|
|
|
|
if ($diff < $this->minimum_sync_interval_in_seconds){ |
|
|
if ($diff < $this->minimum_sync_interval_in_seconds){ |
|
|
$msg = sprintf("SKIP sync user %d, login=%s, display_name=%s\n", $user->ID, $user->user_login, $user->display_name); |
|
|
|
|
|
|
|
|
$msg = sprintf("\tSKIP userid(%d),login=%s,display_name=%s,(lastsync=%d secs ago, mininterval=%d) \n", |
|
|
|
|
|
$user->ID, $user->user_login, $user->display_name, $diff, $this->minimum_sync_interval_in_seconds); |
|
|
$this->logConsole($msg); |
|
|
$this->logConsole($msg); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |