From ae48bde687f9416494ee2cbd04a103eb21d3eef8 Mon Sep 17 00:00:00 2001 From: patrick Date: Sun, 30 Jun 2019 22:15:03 +1000 Subject: [PATCH] cli syn works now --- Xero.php | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ts.php | 90 ++------------------------------------ 2 files changed, 132 insertions(+), 87 deletions(-) diff --git a/Xero.php b/Xero.php index 86da2e8..b4181e2 100644 --- a/Xero.php +++ b/Xero.php @@ -4,6 +4,8 @@ namespace Biukop; use \XeroPHP\Application\PrivateApplication; class Xero { private $xero; + private $clientgroup="48646f3d-cf5e-4fea-8c8b-5812bd540e1b"; + private $minimum_sync_interval_in_seconds = 600; private function default_config(){ $config = array( @@ -61,4 +63,131 @@ class Xero { $employee = $this->xero->loadByGUID("PayrollAU\\Employee",$id); return $employee; } + + + + // + //sync users to wordpress system + //does not work for too many users or employees + public function sync_users(){ + $this->sync_clients(); + //$this->sync_employees(); + } + private function sync_clients(){ + $contacts = $this->getClients($this->clientgroup); + foreach ($contacts as $c){ + $msg = sprintf("SYNC Client name=[%s] {%s} \n", $c->getName(), $c->getContactID()); + $this->logConsole($msg); + $this->ensure_contact_exists($c); + } + } + + private function sync_employees(){ + $employees = $this->getEmployees(); + foreach ( $employees as $e){ + $msg = sprintf("SYNC employee name=[%s %s] {%s} \n", $e->getFirstName(), $e->getLastName(), $e->getEmployeeID()); + $this->logConsole($msg); + $this->ensure_staff_exists($e); + } + } + + private function ensure_contact_exists($contact){ + $login = $contact->getContactID(); + $user = get_user_by('login', $login); + if ($user === false){ + $xero_contact = $this->getContact($login); + $args = $this->xero_contact_profile($xero_contact); + $id = wp_insert_user($args); + $user = get_user_by('ID', $id); + }else{//update user + if ($this->is_too_close_to_sync($user)){ + return; + } + $xero_contact = $this->getContact($login); + $args = $this->xero_contact_profile($xero_contact); + $args['ID'] = $user->ID; + unset($args['user_pass']); //we don't change password + wp_update_user($args); + } + $this->mark_updated($user->ID); + } + + private function xero_contact_profile($c){ + $args = [ + 'user_login' => $username = $c->getContactID(), + 'user_pass' => md5(uniqid(rand() + time(), true)), + 'display_name' =>$c->getName(), + 'user_email' => $c->getEmailAddress(), + 'first_name' => $c->getFirstName(), + 'last_name' => $c->getLastName(), + 'nickname' => $c->getName(), + 'role' => 'client', + ]; + return $args; + } + + + private function ensure_staff_exists($employee) + { + $login = $employee->getEmployeeID(); + $xero_employee = $this->getEmployee($login); + $args = $this->xero_employee_profile($xero_employee); + $user = get_user_by('login', $login); + if ($user === false){ + $id = wp_insert_user($args); + $user = get_user_by('ID', $id); + }else{ + if ($this->is_too_close_to_sync($user)){ + return; + } + $args['ID'] = $user->ID; + unset($args['user_pass']); + wp_update_user($args); + } + $this->mark_updated($user->ID); + } + + private function xero_employee_profile($e){ + $args = [ + 'user_login' => $username = $e->getEmployeeID(), + 'user_pass' => md5(uniqid(rand() + time(), true)), + 'display_name' =>$e->getFirstName() . " " . $e->getLastName(), + 'user_email' => $e->getEmail(), + 'first_name' => $e->getFirstName(), + 'last_name' => $e->getLastName(), + 'nickname' => $e->getFirstName(), + 'role' => 'staff', + ]; + return $args; + } + + private function mark_updated($userid){ + update_user_meta($userid, 'lastsync', time()); + } + + private function get_last_sync($userid){ + $lastsync = get_user_meta($userid, 'lastsync'); + return (int)($lastsync[0]); + } + + private function is_too_close_to_sync($user){ + $userid = $user->ID; + $lastsync = $this->get_last_sync($user->ID); + $now = time(); + $diff = $now - (int) $lastsync; + echo "lastsync = $lastsync, now = $now , diff = $diff; \n" ; + 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); + $this->logConsole($msg); + return true; + } + return false; + } + + private function logConsole($str){ + //if is commandline + if ( defined( 'WP_CLI' ) && WP_CLI ) { + echo $str; + } + } } \ No newline at end of file diff --git a/ts.php b/ts.php index 8c7c768..8f6c122 100644 --- a/ts.php +++ b/ts.php @@ -18,9 +18,8 @@ class AcareOffice{ private $nonce; //for ajax verification private $pages = array('time-sheets', 'user-list'); private $acaresydney_userid = 0; - private $clientgroup="48646f3d-cf5e-4fea-8c8b-5812bd540e1b"; + private $xero ; - private $cli = false; public function __construct() { add_action('init', array($this, 'class_loader')); @@ -94,93 +93,10 @@ class AcareOffice{ public function sync_user_cli($args = array(), $assoc_args = array()){ $this->cli = true; - echo "running sync user from command line;"; - $this->sync_users(); + echo "running sync user from command line;\n"; + $this->xero->sync_users(); return; } - - //sync users to wordpress system - public function sync_users(){ - set_time_limit(600); // executing time longer than expected 10 minutes - //$this->sync_clients(); - $this->sync_employees(); - } - private function sync_clients(){ - $contacts = $this->xero->getClients($this->clientgroup); - foreach ($contacts as $c){ - $this->ensure_contact_exists($c); - } - } - - private function sync_employees(){ - $employees = $this->xero->getEmployees(); - foreach ( $employees as $e){ - if ($this->cli){ - echo "sync " . $e->getFirstName() ."\n"; - } - $this->ensure_staff_exists($e); - } - } - - private function ensure_contact_exists($contact){ - $login = $contact->getContactID(); - $xero_contact = $this->xero->getContact($login); - $args = $this->xero_contact_profile($xero_contact); - $user = get_user_by('login', $login); - if ($user === false){ - wp_insert_user($args); - }else{//update user - $args['ID'] = $user->ID; - unset($args['user_pass']); //we don't change password - wp_update_user($args); - } - } - - private function xero_contact_profile($c){ - $args = [ - 'user_login' => $username = $c->getContactID(), - 'user_pass' => md5(uniqid(rand() + time(), true)), - 'display_name' =>$c->getName(), - 'user_email' => $c->getEmailAddress(), - 'first_name' => $c->getFirstName(), - 'last_name' => $c->getLastName(), - 'nickname' => $c->getName(), - 'role' => 'client', - ]; - return $args; - } - - - private function ensure_staff_exists($employee) - { - $login = $employee->getEmployeeID(); - $user = get_user_by('login', $login); - if ($user === false){ - $xero_employee = $this->xero->getEmployee($login); - $args = $this->xero_employee_profile($xero_employee); - wp_insert_user($args); - }else{ - return; - $args['ID'] = $user->ID; - unset($args['user_pass']); - wp_update_user($args); - } - } - - private function xero_employee_profile($e){ - $args = [ - 'user_login' => $username = $e->getEmployeeID(), - 'user_pass' => md5(uniqid(rand() + time(), true)), - 'display_name' =>$e->getFirstName() . " " . $e->getLastName(), - 'user_email' => $e->getEmail(), - 'first_name' => $e->getFirstName(), - 'last_name' => $e->getLastName(), - 'nickname' => $e->getFirstName(), - 'role' => 'staff', - ]; - return $args; - } - } $bb = new AcareOffice();